VietCat commited on
Commit
eaee65f
·
1 Parent(s): 0922931

fix bug LLM

Browse files
Files changed (2) hide show
  1. app/main.py +17 -10
  2. app/sheets.py +12 -8
app/main.py CHANGED
@@ -150,7 +150,7 @@ async def process_message(message_data: Dict[str, Any]):
150
  if not message_data or not isinstance(message_data, dict):
151
  logger.error(f"[ERROR] Invalid message_data: {message_data}")
152
  return
153
- required_fields = ["sender_id", "page_id", "text"]
154
  for field in required_fields:
155
  if field not in message_data:
156
  logger.error(f"[ERROR] Missing field {field} in message_data: {message_data}")
@@ -158,6 +158,7 @@ async def process_message(message_data: Dict[str, Any]):
158
  sender_id = message_data["sender_id"]
159
  page_id = message_data["page_id"]
160
  message_text = message_data["text"]
 
161
  attachments = message_data.get('attachments', [])
162
  logger.bind(user_id=sender_id, page_id=page_id, message=message_text).info("Processing message")
163
 
@@ -166,7 +167,19 @@ async def process_message(message_data: Dict[str, Any]):
166
  logger.info(f"[DEBUG] Không có message_text và attachments, không xử lý...")
167
  return
168
 
169
- # Get page access token (sync)
 
 
 
 
 
 
 
 
 
 
 
 
170
  page_token = supabase_client.get_page_token(page_id)
171
  if page_token:
172
  logger.info(f"[DEBUG] page_token: {page_token[:10]} ... {page_token[-10:]}")
@@ -178,7 +191,7 @@ async def process_message(message_data: Dict[str, Any]):
178
  logger.error(f"No access token found for page {page_id}")
179
  return
180
 
181
- # Extract command and keywords
182
  command, remaining_text = extract_command(message_text)
183
  # Sử dụng LLM để phân tích message_text và extract keywords, mục đích, hành vi vi phạm
184
  llm_analysis = await llm_client.analyze(message_text)
@@ -198,13 +211,6 @@ async def process_message(message_data: Dict[str, Any]):
198
 
199
  logger.info(f"[DEBUG] Phương tiện: {keywords} - Hành vi: {hanh_vi_vi_pham} - Mục đích: {muc_dich}")
200
 
201
- # Get conversation history (run in thread pool)
202
- loop = asyncio.get_event_loop()
203
- history = await loop.run_in_executor(
204
- executor, lambda: sheets_client.get_conversation_history(sender_id, page_id)
205
- )
206
- logger.info(f"[DEBUG] history: {history}")
207
-
208
  response = ""
209
  log_needed = True
210
  log_kwargs = {
@@ -218,6 +224,7 @@ async def process_message(message_data: Dict[str, Any]):
218
  'vehicle': ','.join(keywords),
219
  'action': hanh_vi_vi_pham,
220
  'purpose': muc_dich,
 
221
  'is_done': False
222
  }
223
 
 
150
  if not message_data or not isinstance(message_data, dict):
151
  logger.error(f"[ERROR] Invalid message_data: {message_data}")
152
  return
153
+ required_fields = ["sender_id", "page_id", "text", "timestamp"]
154
  for field in required_fields:
155
  if field not in message_data:
156
  logger.error(f"[ERROR] Missing field {field} in message_data: {message_data}")
 
158
  sender_id = message_data["sender_id"]
159
  page_id = message_data["page_id"]
160
  message_text = message_data["text"]
161
+ timestamp = message_data["timestamp"]
162
  attachments = message_data.get('attachments', [])
163
  logger.bind(user_id=sender_id, page_id=page_id, message=message_text).info("Processing message")
164
 
 
167
  logger.info(f"[DEBUG] Không có message_text và attachments, không xử lý...")
168
  return
169
 
170
+ # Get conversation history (run in thread pool)
171
+ loop = asyncio.get_event_loop()
172
+ history = await loop.run_in_executor(
173
+ executor, lambda: sheets_client.get_conversation_history(sender_id, page_id)
174
+ )
175
+ logger.info(f"[DEBUG] history: {history}")
176
+
177
+ if history:
178
+ if history["timestamp"] == timestamp and history["recipient_id"] == sender_id and history["page_id"] == page_id:
179
+ logger.info(f"[DEBUG] Message bị duplicate (trùng sender, page, timestamp), không xử lý...")
180
+ return
181
+
182
+ # Get page access token (sync)
183
  page_token = supabase_client.get_page_token(page_id)
184
  if page_token:
185
  logger.info(f"[DEBUG] page_token: {page_token[:10]} ... {page_token[-10:]}")
 
191
  logger.error(f"No access token found for page {page_id}")
192
  return
193
 
194
+ # Extract command and keywords
195
  command, remaining_text = extract_command(message_text)
196
  # Sử dụng LLM để phân tích message_text và extract keywords, mục đích, hành vi vi phạm
197
  llm_analysis = await llm_client.analyze(message_text)
 
211
 
212
  logger.info(f"[DEBUG] Phương tiện: {keywords} - Hành vi: {hanh_vi_vi_pham} - Mục đích: {muc_dich}")
213
 
 
 
 
 
 
 
 
214
  response = ""
215
  log_needed = True
216
  log_kwargs = {
 
224
  'vehicle': ','.join(keywords),
225
  'action': hanh_vi_vi_pham,
226
  'purpose': muc_dich,
227
+ 'timestamp': timestamp,
228
  'is_done': False
229
  }
230
 
app/sheets.py CHANGED
@@ -90,8 +90,8 @@ class SheetsClient:
90
  history = []
91
 
92
  for row in values:
93
- # Bổ sung cột rỗng cho đủ 11 cột
94
- row = row + [""] * (11 - len(row))
95
  if row[4] == user_id and row[5] == page_id and row[8].lower() == 'false':
96
  history.append({
97
  'conversation_id': row[0],
@@ -104,7 +104,8 @@ class SheetsClient:
104
  'originalvehicle': row[7],
105
  'originalaction': row[8],
106
  'originalpurpose': row[9],
107
- 'isdone': row[10].lower() == 'true'
 
108
  })
109
 
110
  return history
@@ -125,6 +126,7 @@ class SheetsClient:
125
  vehicle: str = "",
126
  action: str = "",
127
  purpose: str = "",
 
128
  is_done: bool = False
129
  ) -> bool:
130
  """
@@ -138,11 +140,11 @@ class SheetsClient:
138
  if not self.service:
139
  raise RuntimeError("Google Sheets service not initialized")
140
 
141
- timestamp = datetime.now().isoformat()
142
 
143
  if not conversation_id:
144
  # Create new conversation
145
- conversation_id = generate_conversation_id(user_id, page_id, timestamp)
146
  values = [[
147
  conversation_id,
148
  command,
@@ -154,6 +156,7 @@ class SheetsClient:
154
  vehicle,
155
  action,
156
  purpose,
 
157
  str(is_done).lower()
158
  ]]
159
 
@@ -188,8 +191,8 @@ class SheetsClient:
188
  if row_index is not None:
189
  # Lấy dữ liệu dòng hiện tại
190
  current_row = values[row_index]
191
- # Đảm bảo đủ 11 cột
192
- while len(current_row) < 11:
193
  current_row.append("")
194
  # Tạo dòng mới với giá trị mới nếu có, giữ nguyên nếu không
195
  new_row = [
@@ -203,7 +206,8 @@ class SheetsClient:
203
  vehicle if vehicle else current_row[7],
204
  action if action else current_row[8],
205
  purpose if purpose else current_row[9],
206
- str(is_done).lower() if is_done is not None else current_row[10]
 
207
  ]
208
  update_range = f"{SHEET_RANGE.split('!')[0]}!A{row_index + 1}"
209
  body = {
 
90
  history = []
91
 
92
  for row in values:
93
+ # Bổ sung cột rỗng cho đủ 12 cột
94
+ row = row + [""] * (12 - len(row))
95
  if row[4] == user_id and row[5] == page_id and row[8].lower() == 'false':
96
  history.append({
97
  'conversation_id': row[0],
 
104
  'originalvehicle': row[7],
105
  'originalaction': row[8],
106
  'originalpurpose': row[9],
107
+ 'timestamp': row[10],
108
+ 'isdone': row[11].lower() == 'true'
109
  })
110
 
111
  return history
 
126
  vehicle: str = "",
127
  action: str = "",
128
  purpose: str = "",
129
+ timestamp: str = "",
130
  is_done: bool = False
131
  ) -> bool:
132
  """
 
140
  if not self.service:
141
  raise RuntimeError("Google Sheets service not initialized")
142
 
143
+ ts = datetime.now().isoformat()
144
 
145
  if not conversation_id:
146
  # Create new conversation
147
+ conversation_id = generate_conversation_id(user_id, page_id, ts)
148
  values = [[
149
  conversation_id,
150
  command,
 
156
  vehicle,
157
  action,
158
  purpose,
159
+ timestamp,
160
  str(is_done).lower()
161
  ]]
162
 
 
191
  if row_index is not None:
192
  # Lấy dữ liệu dòng hiện tại
193
  current_row = values[row_index]
194
+ # Đảm bảo đủ 12 cột
195
+ while len(current_row) < 12:
196
  current_row.append("")
197
  # Tạo dòng mới với giá trị mới nếu có, giữ nguyên nếu không
198
  new_row = [
 
206
  vehicle if vehicle else current_row[7],
207
  action if action else current_row[8],
208
  purpose if purpose else current_row[9],
209
+ timestamp if timestamp else current_row[10],
210
+ str(is_done).lower() if is_done is not None else current_row[11]
211
  ]
212
  update_range = f"{SHEET_RANGE.split('!')[0]}!A{row_index + 1}"
213
  body = {