fix: 修复多个问题
- JSON解析器字符串状态追踪修复 - AI客户端流式响应异常处理 - 写作风格MultipleResultsFound错误 - 职业stages字段类型处理 - 章节分析任务状态同步 - 后台任务返回值修复
This commit is contained in:
@@ -81,6 +81,21 @@ class AnthropicClient:
|
||||
if system_prompt:
|
||||
kwargs["system"] = system_prompt
|
||||
|
||||
async with self.client.messages.stream(**kwargs) as stream:
|
||||
async for text in stream.text_stream:
|
||||
yield text
|
||||
try:
|
||||
async with self.client.messages.stream(**kwargs) as stream:
|
||||
try:
|
||||
async for text in stream.text_stream:
|
||||
yield text
|
||||
except GeneratorExit:
|
||||
# 生成器被关闭,这是正常的清理过程
|
||||
logger.debug("Anthropic 流式响应生成器被关闭(GeneratorExit)")
|
||||
raise
|
||||
except Exception as iter_error:
|
||||
logger.error(f"Anthropic 流式响应迭代出错: {str(iter_error)}")
|
||||
raise
|
||||
except GeneratorExit:
|
||||
# 重新抛出GeneratorExit,让调用方处理
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Anthropic 流式请求出错: {str(e)}")
|
||||
raise
|
||||
@@ -2,6 +2,9 @@
|
||||
from typing import Any, AsyncGenerator, Dict, List, Optional
|
||||
import httpx
|
||||
from app.services.ai_config import AIClientConfig, default_config
|
||||
from app.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class GeminiClient:
|
||||
@@ -123,19 +126,34 @@ class GeminiClient:
|
||||
if system_prompt:
|
||||
payload["systemInstruction"] = {"parts": [{"text": system_prompt}]}
|
||||
|
||||
async with self.client.stream("POST", url, json=payload) as response:
|
||||
response.raise_for_status()
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
import json
|
||||
try:
|
||||
data = json.loads(line[6:])
|
||||
candidates = data.get("candidates", [])
|
||||
if candidates and len(candidates) > 0:
|
||||
parts = candidates[0].get("content", {}).get("parts", [])
|
||||
if parts and len(parts) > 0:
|
||||
text = parts[0].get("text", "")
|
||||
if text:
|
||||
yield text
|
||||
except:
|
||||
continue
|
||||
try:
|
||||
async with self.client.stream("POST", url, json=payload) as response:
|
||||
response.raise_for_status()
|
||||
try:
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
import json
|
||||
try:
|
||||
data = json.loads(line[6:])
|
||||
candidates = data.get("candidates", [])
|
||||
if candidates and len(candidates) > 0:
|
||||
parts = candidates[0].get("content", {}).get("parts", [])
|
||||
if parts and len(parts) > 0:
|
||||
text = parts[0].get("text", "")
|
||||
if text:
|
||||
yield text
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
except GeneratorExit:
|
||||
# 生成器被关闭,这是正常的清理过程
|
||||
logger.debug("Gemini 流式响应生成器被关闭(GeneratorExit)")
|
||||
raise
|
||||
except Exception as iter_error:
|
||||
logger.error(f"Gemini 流式响应迭代出错: {str(iter_error)}")
|
||||
raise
|
||||
except GeneratorExit:
|
||||
# 重新抛出GeneratorExit,让调用方处理
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Gemini 流式请求出错: {str(e)}")
|
||||
raise
|
||||
@@ -60,7 +60,13 @@ class OpenAIClient(BaseAIClient):
|
||||
tool_choice: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
payload = self._build_payload(messages, model, temperature, max_tokens, tools, tool_choice)
|
||||
|
||||
logger.debug(f"📤 OpenAI 请求 payload: {json.dumps(payload, ensure_ascii=False, indent=2)}")
|
||||
|
||||
data = await self._request_with_retry("POST", "/chat/completions", payload)
|
||||
|
||||
# 调试日志:输出原始响应
|
||||
logger.debug(f"📥 OpenAI 原始响应: {json.dumps(data, ensure_ascii=False, indent=2)}")
|
||||
|
||||
choices = data.get("choices", [])
|
||||
if not choices or len(choices) == 0:
|
||||
@@ -83,19 +89,34 @@ class OpenAIClient(BaseAIClient):
|
||||
) -> AsyncGenerator[str, None]:
|
||||
payload = self._build_payload(messages, model, temperature, max_tokens, stream=True)
|
||||
|
||||
async with await self._request_with_retry("POST", "/chat/completions", payload, stream=True) as response:
|
||||
response.raise_for_status()
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
data_str = line[6:]
|
||||
if data_str.strip() == "[DONE]":
|
||||
break
|
||||
try:
|
||||
data = json.loads(data_str)
|
||||
choices = data.get("choices", [])
|
||||
if choices and len(choices) > 0:
|
||||
content = choices[0].get("delta", {}).get("content", "")
|
||||
if content:
|
||||
yield content
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
try:
|
||||
async with await self._request_with_retry("POST", "/chat/completions", payload, stream=True) as response:
|
||||
response.raise_for_status()
|
||||
try:
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
data_str = line[6:]
|
||||
if data_str.strip() == "[DONE]":
|
||||
break
|
||||
try:
|
||||
data = json.loads(data_str)
|
||||
choices = data.get("choices", [])
|
||||
if choices and len(choices) > 0:
|
||||
content = choices[0].get("delta", {}).get("content", "")
|
||||
if content:
|
||||
yield content
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
except GeneratorExit:
|
||||
# 生成器被关闭,这是正常的清理过程
|
||||
logger.debug("流式响应生成器被关闭(GeneratorExit)")
|
||||
raise
|
||||
except Exception as iter_error:
|
||||
logger.error(f"流式响应迭代出错: {str(iter_error)}")
|
||||
raise
|
||||
except GeneratorExit:
|
||||
# 重新抛出GeneratorExit,让调用方处理
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"流式请求出错: {str(e)}")
|
||||
raise
|
||||
@@ -54,37 +54,35 @@ def clean_json_response(text: str) -> str:
|
||||
stack = []
|
||||
i = 0
|
||||
end = -1
|
||||
in_string = False
|
||||
|
||||
while i < len(text):
|
||||
c = text[i]
|
||||
|
||||
# 处理字符串(关键:正确处理转义)
|
||||
# 处理字符串状态
|
||||
if c == '"':
|
||||
# 计算前面有多少个连续的反斜杠
|
||||
num_backslashes = 0
|
||||
j = i - 1
|
||||
while j >= 0 and text[j] == '\\':
|
||||
num_backslashes += 1
|
||||
j -= 1
|
||||
if not in_string:
|
||||
# 进入字符串
|
||||
in_string = True
|
||||
else:
|
||||
# 检查是否是转义的引号
|
||||
num_backslashes = 0
|
||||
j = i - 1
|
||||
while j >= 0 and text[j] == '\\':
|
||||
num_backslashes += 1
|
||||
j -= 1
|
||||
|
||||
# 偶数个反斜杠表示引号未被转义,字符串结束
|
||||
if num_backslashes % 2 == 0:
|
||||
in_string = False
|
||||
|
||||
# 偶数个反斜杠(包括0)表示引号未被转义
|
||||
if num_backslashes % 2 == 0:
|
||||
# 这是字符串边界,跳过整个字符串
|
||||
i += 1
|
||||
while i < len(text):
|
||||
if text[i] == '"':
|
||||
# 再次检查转义
|
||||
num_backslashes = 0
|
||||
j = i - 1
|
||||
while j >= 0 and text[j] == '\\':
|
||||
num_backslashes += 1
|
||||
j -= 1
|
||||
if num_backslashes % 2 == 0:
|
||||
# 字符串结束
|
||||
break
|
||||
i += 1
|
||||
i += 1
|
||||
continue
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 在字符串内部,跳过所有字符
|
||||
if in_string:
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理括号(只有在字符串外部才有效)
|
||||
if c == '{' or c == '[':
|
||||
@@ -96,8 +94,12 @@ def clean_json_response(text: str) -> str:
|
||||
end = i + 1
|
||||
logger.debug(f"✅ 找到JSON结束位置: {end}")
|
||||
break
|
||||
elif len(stack) > 0:
|
||||
# 括号不匹配,可能是损坏的JSON,尝试继续
|
||||
logger.warning(f"⚠️ 括号不匹配:遇到 }} 但栈顶是 {stack[-1]}")
|
||||
else:
|
||||
logger.warning(f"⚠️ 括号不匹配:遇到 }} 但栈顶是 {stack[-1] if stack else 'empty'}")
|
||||
# 栈为空遇到 },忽略多余的闭合括号
|
||||
logger.warning(f"⚠️ 遇到多余的 }},忽略")
|
||||
elif c == ']':
|
||||
if len(stack) > 0 and stack[-1] == '[':
|
||||
stack.pop()
|
||||
@@ -105,11 +107,19 @@ def clean_json_response(text: str) -> str:
|
||||
end = i + 1
|
||||
logger.debug(f"✅ 找到JSON结束位置: {end}")
|
||||
break
|
||||
elif len(stack) > 0:
|
||||
# 括号不匹配,可能是损坏的JSON,尝试继续
|
||||
logger.warning(f"⚠️ 括号不匹配:遇到 ] 但栈顶是 {stack[-1]}")
|
||||
else:
|
||||
logger.warning(f"⚠️ 括号不匹配:遇到 ] 但栈顶是 {stack[-1] if stack else 'empty'}")
|
||||
# 栈为空遇到 ],忽略多余的闭合括号
|
||||
logger.warning(f"⚠️ 遇到多余的 ],忽略")
|
||||
|
||||
i += 1
|
||||
|
||||
# 检查未闭合的字符串
|
||||
if in_string:
|
||||
logger.warning(f"⚠️ 字符串未闭合,JSON可能不完整")
|
||||
|
||||
# 提取结果
|
||||
if end > 0:
|
||||
result = text[:end]
|
||||
|
||||
Reference in New Issue
Block a user