Increase max_tokens for all models in generation settings to enhance response capacity
Browse files- config/environments/default.yaml +4 -4
- main.py +26 -22
config/environments/default.yaml
CHANGED
|
@@ -23,10 +23,10 @@ llama_index:
|
|
| 23 |
# Generation Settings
|
| 24 |
generation:
|
| 25 |
max_tokens:
|
| 26 |
-
openai:
|
| 27 |
-
anthropic:
|
| 28 |
-
gemini:
|
| 29 |
-
deepseek:
|
| 30 |
max_tokens_analysis: 4000
|
| 31 |
temperature: 0.5
|
| 32 |
|
|
|
|
| 23 |
# Generation Settings
|
| 24 |
generation:
|
| 25 |
max_tokens:
|
| 26 |
+
openai: 4096
|
| 27 |
+
anthropic: 4096
|
| 28 |
+
gemini: 4096
|
| 29 |
+
deepseek: 4096
|
| 30 |
max_tokens_analysis: 4000
|
| 31 |
temperature: 0.5
|
| 32 |
|
main.py
CHANGED
|
@@ -601,30 +601,32 @@ def generate_legal_position(
|
|
| 601 |
raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
|
| 602 |
|
| 603 |
if provider == ModelProvider.OPENAI.value:
|
| 604 |
-
|
| 605 |
-
model=model_name,
|
| 606 |
-
temperature=GENERATION_TEMPERATURE,
|
| 607 |
-
max_tokens=MAX_TOKENS_CONFIG["openai"]
|
| 608 |
-
)
|
| 609 |
-
messages = [
|
| 610 |
-
ChatMessage(role="system", content=system_prompt),
|
| 611 |
-
ChatMessage(role="user", content=content),
|
| 612 |
-
]
|
| 613 |
-
|
| 614 |
try:
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
json_response = extract_json_from_text(response_text)
|
| 619 |
if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):
|
| 620 |
return json_response
|
| 621 |
else:
|
| 622 |
-
|
|
|
|
| 623 |
except Exception as e:
|
| 624 |
print(f"[ERROR] OpenAI generation/parsing failed: {e}")
|
| 625 |
return {
|
| 626 |
"title": "Автоматично сформований заголовок (OpenAI)",
|
| 627 |
-
"text":
|
| 628 |
"proceeding": "Не визначено",
|
| 629 |
"category": "Помилка парсингу"
|
| 630 |
}
|
|
@@ -632,6 +634,7 @@ def generate_legal_position(
|
|
| 632 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 633 |
client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")
|
| 634 |
try:
|
|
|
|
| 635 |
response = client.chat.completions.create(
|
| 636 |
model=model_name,
|
| 637 |
messages=[
|
|
@@ -640,23 +643,21 @@ def generate_legal_position(
|
|
| 640 |
],
|
| 641 |
temperature=GENERATION_TEMPERATURE,
|
| 642 |
max_tokens=MAX_TOKENS_CONFIG["deepseek"],
|
| 643 |
-
response_format={
|
| 644 |
-
'type': 'json_object'
|
| 645 |
-
},
|
| 646 |
-
stream=False
|
| 647 |
)
|
| 648 |
response_text = response.choices[0].message.content
|
|
|
|
| 649 |
|
| 650 |
json_response = extract_json_from_text(response_text)
|
| 651 |
if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):
|
| 652 |
return json_response
|
| 653 |
else:
|
| 654 |
-
|
|
|
|
| 655 |
except Exception as e:
|
| 656 |
print(f"[ERROR] DeepSeek generation/parsing failed: {e}")
|
| 657 |
return {
|
| 658 |
"title": "Автоматично сформований заголовок (DeepSeek)",
|
| 659 |
-
"text": "Помилка при отриманні відповіді від DeepSeek",
|
| 660 |
"proceeding": "Не визначено",
|
| 661 |
"category": "Помилка API/Парсингу"
|
| 662 |
}
|
|
@@ -799,8 +800,8 @@ def generate_legal_position(
|
|
| 799 |
)
|
| 800 |
|
| 801 |
# Only add response_mime_type for models that support it
|
| 802 |
-
if not model_name.startswith("gemini-3"):
|
| 803 |
-
|
| 804 |
|
| 805 |
generate_content_config = types.GenerateContentConfig(**config_params)
|
| 806 |
|
|
@@ -815,6 +816,9 @@ def generate_legal_position(
|
|
| 815 |
if not response_text:
|
| 816 |
raise Exception("Пуста відповідь від моделі Gemini")
|
| 817 |
|
|
|
|
|
|
|
|
|
|
| 818 |
# Спробуємо розпарсити JSON за допомогою універсальної функції
|
| 819 |
json_response = extract_json_from_text(response_text)
|
| 820 |
|
|
|
|
| 601 |
raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
|
| 602 |
|
| 603 |
if provider == ModelProvider.OPENAI.value:
|
| 604 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
try:
|
| 606 |
+
print(f"[DEBUG] OpenAI Generation - Model: {model_name}")
|
| 607 |
+
response = client.chat.completions.create(
|
| 608 |
+
model=model_name,
|
| 609 |
+
messages=[
|
| 610 |
+
{"role": "system", "content": system_prompt},
|
| 611 |
+
{"role": "user", "content": content},
|
| 612 |
+
],
|
| 613 |
+
temperature=GENERATION_TEMPERATURE,
|
| 614 |
+
max_tokens=MAX_TOKENS_CONFIG["openai"],
|
| 615 |
+
)
|
| 616 |
+
response_text = response.choices[0].message.content
|
| 617 |
+
print(f"[DEBUG] OpenAI response length: {len(response_text)}")
|
| 618 |
|
| 619 |
json_response = extract_json_from_text(response_text)
|
| 620 |
if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):
|
| 621 |
return json_response
|
| 622 |
else:
|
| 623 |
+
print(f"[WARNING] Invalid JSON structure from OpenAI. Text: {response_text[:300]}...")
|
| 624 |
+
raise ValueError("Invalid JSON structure")
|
| 625 |
except Exception as e:
|
| 626 |
print(f"[ERROR] OpenAI generation/parsing failed: {e}")
|
| 627 |
return {
|
| 628 |
"title": "Автоматично сформований заголовок (OpenAI)",
|
| 629 |
+
"text": response_text.strip() if 'response_text' in locals() else "Помилка при отриманні відповіді",
|
| 630 |
"proceeding": "Не визначено",
|
| 631 |
"category": "Помилка парсингу"
|
| 632 |
}
|
|
|
|
| 634 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 635 |
client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")
|
| 636 |
try:
|
| 637 |
+
print(f"[DEBUG] DeepSeek Generation - Model: {model_name}")
|
| 638 |
response = client.chat.completions.create(
|
| 639 |
model=model_name,
|
| 640 |
messages=[
|
|
|
|
| 643 |
],
|
| 644 |
temperature=GENERATION_TEMPERATURE,
|
| 645 |
max_tokens=MAX_TOKENS_CONFIG["deepseek"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
)
|
| 647 |
response_text = response.choices[0].message.content
|
| 648 |
+
print(f"[DEBUG] DeepSeek response length: {len(response_text)}")
|
| 649 |
|
| 650 |
json_response = extract_json_from_text(response_text)
|
| 651 |
if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):
|
| 652 |
return json_response
|
| 653 |
else:
|
| 654 |
+
print(f"[WARNING] Invalid JSON structure from DeepSeek. Text: {response_text[:300]}...")
|
| 655 |
+
raise ValueError("Invalid JSON structure")
|
| 656 |
except Exception as e:
|
| 657 |
print(f"[ERROR] DeepSeek generation/parsing failed: {e}")
|
| 658 |
return {
|
| 659 |
"title": "Автоматично сформований заголовок (DeepSeek)",
|
| 660 |
+
"text": response_text.strip() if 'response_text' in locals() else "Помилка при отриманні відповіді від DeepSeek",
|
| 661 |
"proceeding": "Не визначено",
|
| 662 |
"category": "Помилка API/Парсингу"
|
| 663 |
}
|
|
|
|
| 800 |
)
|
| 801 |
|
| 802 |
# Only add response_mime_type for models that support it
|
| 803 |
+
# if not model_name.startswith("gemini-3"):
|
| 804 |
+
# config_params["response_mime_type"] = "application/json"
|
| 805 |
|
| 806 |
generate_content_config = types.GenerateContentConfig(**config_params)
|
| 807 |
|
|
|
|
| 816 |
if not response_text:
|
| 817 |
raise Exception("Пуста відповідь від моделі Gemini")
|
| 818 |
|
| 819 |
+
print(f"[DEBUG] Gemini response length: {len(response_text)}")
|
| 820 |
+
print(f"[DEBUG] Gemini response preview: {response_text[:300]}...")
|
| 821 |
+
|
| 822 |
# Спробуємо розпарсити JSON за допомогою універсальної функції
|
| 823 |
json_response = extract_json_from_text(response_text)
|
| 824 |
|