fix incorrect LLM message
Browse files- app/llm.py +34 -7
app/llm.py
CHANGED
|
@@ -100,7 +100,7 @@ class LLMClient:
|
|
| 100 |
Returns:
|
| 101 |
str: Text được tạo ra
|
| 102 |
"""
|
| 103 |
-
logger.info(f"[LLM] generate_text - provider: {self.provider}
|
| 104 |
try:
|
| 105 |
result = None
|
| 106 |
if self.provider == "openai":
|
|
@@ -174,6 +174,11 @@ class LLMClient:
|
|
| 174 |
headers["Authorization"] = f"Bearer {self.api_key}"
|
| 175 |
response = await call_endpoint_with_retry(self._client, endpoint, payload, 3, 500, headers=headers)
|
| 176 |
logger.info(f"[LLM] generate_text - provider: {self.provider}\n\t response: {response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
if response is not None:
|
| 178 |
data = response.json()
|
| 179 |
if 'response' in data:
|
|
@@ -347,10 +352,21 @@ class LLMClient:
|
|
| 347 |
|
| 348 |
try:
|
| 349 |
import re
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
return entities
|
|
|
|
|
|
|
|
|
|
| 354 |
else:
|
| 355 |
return []
|
| 356 |
except json.JSONDecodeError:
|
|
@@ -402,10 +418,21 @@ class LLMClient:
|
|
| 402 |
|
| 403 |
try:
|
| 404 |
import re
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
return entities
|
|
|
|
|
|
|
|
|
|
| 409 |
else:
|
| 410 |
return []
|
| 411 |
except json.JSONDecodeError:
|
|
|
|
| 100 |
Returns:
|
| 101 |
str: Text được tạo ra
|
| 102 |
"""
|
| 103 |
+
logger.info(f"[LLM] generate_text - provider: {self.provider} \n\t prompt: {prompt}")
|
| 104 |
try:
|
| 105 |
result = None
|
| 106 |
if self.provider == "openai":
|
|
|
|
| 174 |
headers["Authorization"] = f"Bearer {self.api_key}"
|
| 175 |
response = await call_endpoint_with_retry(self._client, endpoint, payload, 3, 500, headers=headers)
|
| 176 |
logger.info(f"[LLM] generate_text - provider: {self.provider}\n\t response: {response}")
|
| 177 |
+
try:
|
| 178 |
+
import json as _json
|
| 179 |
+
logger.info(f"[LLM][RAW_RESPONSE] { _json.dumps(response, ensure_ascii=False, indent=2) }")
|
| 180 |
+
except Exception:
|
| 181 |
+
logger.info(f"[LLM][RAW_RESPONSE] {str(response)}")
|
| 182 |
if response is not None:
|
| 183 |
data = response.json()
|
| 184 |
if 'response' in data:
|
|
|
|
| 352 |
|
| 353 |
try:
|
| 354 |
import re
|
| 355 |
+
# Log toàn bộ response dưới dạng text dễ đọc
|
| 356 |
+
try:
|
| 357 |
+
import json as _json
|
| 358 |
+
logger.info(f"[LLM][RAW_RESPONSE] { _json.dumps(response, ensure_ascii=False, indent=2) }")
|
| 359 |
+
except Exception:
|
| 360 |
+
logger.info(f"[LLM][RAW_RESPONSE] {str(response)}")
|
| 361 |
+
# Ưu tiên parse object JSON nếu có
|
| 362 |
+
json_match_obj = re.search(r'\{[\s\S]*?\}', response)
|
| 363 |
+
json_match_list = re.search(r'\[[\s\S]*?\]', response)
|
| 364 |
+
if json_match_list:
|
| 365 |
+
entities = json.loads(json_match_list.group())
|
| 366 |
return entities
|
| 367 |
+
elif json_match_obj:
|
| 368 |
+
entity = json.loads(json_match_obj.group())
|
| 369 |
+
return [entity]
|
| 370 |
else:
|
| 371 |
return []
|
| 372 |
except json.JSONDecodeError:
|
|
|
|
| 418 |
|
| 419 |
try:
|
| 420 |
import re
|
| 421 |
+
# Log toàn bộ response dưới dạng text dễ đọc
|
| 422 |
+
try:
|
| 423 |
+
import json as _json
|
| 424 |
+
logger.info(f"[LLM][RAW_RESPONSE] { _json.dumps(response, ensure_ascii=False, indent=2) }")
|
| 425 |
+
except Exception:
|
| 426 |
+
logger.info(f"[LLM][RAW_RESPONSE] {str(response)}")
|
| 427 |
+
# Ưu tiên parse object JSON nếu có
|
| 428 |
+
json_match_obj = re.search(r'\{[\s\S]*?\}', response)
|
| 429 |
+
json_match_list = re.search(r'\[[\s\S]*?\]', response)
|
| 430 |
+
if json_match_list:
|
| 431 |
+
entities = json.loads(json_match_list.group())
|
| 432 |
return entities
|
| 433 |
+
elif json_match_obj:
|
| 434 |
+
entity = json.loads(json_match_obj.group())
|
| 435 |
+
return [entity]
|
| 436 |
else:
|
| 437 |
return []
|
| 438 |
except json.JSONDecodeError:
|