fix: 修复多个问题

- JSON解析器字符串状态追踪修复
- AI客户端流式响应异常处理
- 写作风格MultipleResultsFound错误
- 职业stages字段类型处理
- 章节分析任务状态同步
- 后台任务返回值修复
This commit is contained in:
xiamuceer
2025-12-31 12:02:36 +08:00
parent 30c044394f
commit fba6922a5c
10 changed files with 155 additions and 82 deletions
@@ -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