Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/app/services/ontology_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def generate(
result = self.llm_client.chat_json(
messages=messages,
temperature=0.3,
max_tokens=4096
max_tokens=32768
)

# 验证和后处理
Expand Down
31 changes: 26 additions & 5 deletions backend/app/utils/llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def chat(
kwargs = {
"model": self.model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
"max_completion_tokens": max_tokens,
}
kwargs["temperature"] = temperature

if response_format:
kwargs["response_format"] = response_format
Expand All @@ -71,7 +71,7 @@ def chat_json(
self,
messages: List[Dict[str, str]],
temperature: float = 0.3,
max_tokens: int = 4096
max_tokens: int = 16384
) -> Dict[str, Any]:
"""
发送聊天请求并返回JSON
Expand All @@ -88,7 +88,6 @@ def chat_json(
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
response_format={"type": "json_object"}
)
# 清理markdown代码块标记
cleaned_response = response.strip()
Expand All @@ -99,5 +98,27 @@ def chat_json(
try:
return json.loads(cleaned_response)
except json.JSONDecodeError:
raise ValueError(f"LLM返回的JSON格式无效: {cleaned_response}")
# Aggressive JSON repair for truncated responses
repaired = cleaned_response
# Try progressively trimming from the end until valid JSON
for trim in range(min(len(repaired), 2000)):
candidate = repaired[:len(repaired) - trim].rstrip(', \n\t\r')
# Remove incomplete string at end
if candidate and candidate[-1] not in ']}}"':
continue
open_braces = candidate.count('{') - candidate.count('}')
open_brackets = candidate.count('[') - candidate.count(']')
fixed = candidate
for _ in range(open_brackets):
fixed += ']'
for _ in range(open_braces):
fixed += '}'
try:
result = json.loads(fixed)
import logging
logging.getLogger(__name__).warning(f"JSON repaired by trimming {trim} chars")
return result
except json.JSONDecodeError:
continue
raise ValueError(f"LLM返回的JSON格式无效: {cleaned_response[:500]}")

4 changes: 0 additions & 4 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.