VietCat commited on
Commit
9d94525
·
1 Parent(s): 440a20d

fix max token error

Browse files
Files changed (2) hide show
  1. app/gemini_client.py +11 -6
  2. app/message_processor.py +8 -2
app/gemini_client.py CHANGED
@@ -76,17 +76,22 @@ class GeminiClient:
76
  # Trường hợp bất thường, response không có candidate. Coi là lỗi tạm thời.
77
  raise ValueError("Gemini response is missing 'candidates' field.")
78
 
79
- candidate = response.candidates[0]
80
- # getattr để truy cập an toàn, tránh AttributeError
81
- finish_reason_name = getattr(getattr(candidate, 'finish_reason', None), 'name', 'UNKNOWN')
 
82
 
83
  # 2. Phân loại lỗi: Lỗi logic (cần xử lý ở tầng nghiệp vụ)
84
- if finish_reason_name not in ["STOP", "FINISH_REASON_UNSPECIFIED"]:
 
85
  usage_metadata = response.usage_metadata if hasattr(response, 'usage_metadata') else None
86
- error_message = f"Gemini response finished with non-OK reason: {finish_reason_name}."
 
 
 
87
  raise GeminiResponseError(
88
  error_message,
89
- finish_reason=finish_reason_name,
90
  usage_metadata=usage_metadata
91
  )
92
 
 
76
  # Trường hợp bất thường, response không có candidate. Coi là lỗi tạm thời.
77
  raise ValueError("Gemini response is missing 'candidates' field.")
78
 
79
+ candidate = response.candidates[0]
80
+ finish_reason_name = getattr(getattr(candidate, 'finish_reason', None), 'name', 'UNKNOWN')
81
+ # Kiểm tra xem nội dung thực sự không
82
+ has_content = bool(candidate.content and candidate.content.parts)
83
 
84
  # 2. Phân loại lỗi: Lỗi logic (cần xử lý ở tầng nghiệp vụ)
85
+ # Lỗi xảy ra nếu: (A) lý do kết thúc không phải là STOP, HOẶC (B) lý do là STOP nhưng lại không có nội dung.
86
+ if finish_reason_name != "STOP" or not has_content:
87
  usage_metadata = response.usage_metadata if hasattr(response, 'usage_metadata') else None
88
+ if finish_reason_name == "STOP" and not has_content:
89
+ error_message = "Gemini response finished with STOP but has no content parts."
90
+ else:
91
+ error_message = f"Gemini response finished with non-OK reason: {finish_reason_name}."
92
  raise GeminiResponseError(
93
  error_message,
94
+ finish_reason=finish_reason_name if finish_reason_name != "STOP" else "STOP_NO_CONTENT",
95
  usage_metadata=usage_metadata
96
  )
97
 
app/message_processor.py CHANGED
@@ -100,8 +100,14 @@ class MessageProcessor:
100
  from app.constants import VEHICLE_KEYWORDS
101
  command, remaining_text = extract_command(message_text)
102
 
103
- llm_analysis = await self.channel.llm.analyze(message_text, self.get_llm_history(history))
104
- logger.info(f"[LLM][RAW] Kết quả trả về từ analyze: {llm_analysis}")
 
 
 
 
 
 
105
 
106
  muc_dich = None
107
  tu_khoa_list = [] # Sửa: đổi tên thành tu_khoa_list và khởi tạo là list rỗng
 
100
  from app.constants import VEHICLE_KEYWORDS
101
  command, remaining_text = extract_command(message_text)
102
 
103
+ llm_analysis = None # Khởi tạo là None
104
+ try:
105
+ llm_analysis = await self.channel.llm.analyze(message_text, self.get_llm_history(history))
106
+ logger.info(f"[LLM][RAW] Kết quả trả về từ analyze: {llm_analysis}")
107
+ except GeminiResponseError as e:
108
+ logger.error(f"[LLM][ANALYZE] Lỗi nội dung (MAX_TOKENS/SAFETY) khi phân tích câu hỏi: {e}. Sẽ fallback về phương pháp cũ.")
109
+ except Exception as e:
110
+ logger.error(f"[LLM][ANALYZE] Lỗi không xác định khi phân tích câu hỏi: {e}. Sẽ fallback về phương pháp cũ.")
111
 
112
  muc_dich = None
113
  tu_khoa_list = [] # Sửa: đổi tên thành tu_khoa_list và khởi tạo là list rỗng