修正了 {fixed_count} 条函数调用消息(缺失 content 已补空字符串)

This commit is contained in:
lsp199308 2025-10-25 18:45:10 +08:00 committed by GitHub
parent 4d14caf0d4
commit de53fcc4c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,7 +17,6 @@ class LLMProvider(LLMProviderBase):
self.base_url = config.get("base_url") self.base_url = config.get("base_url")
else: else:
self.base_url = config.get("url") self.base_url = config.get("url")
# 增加timeout的配置项,单位为秒
timeout = config.get("timeout", 300) timeout = config.get("timeout", 300)
self.timeout = int(timeout) if timeout else 300 self.timeout = int(timeout) if timeout else 300
@ -46,13 +45,34 @@ class LLMProvider(LLMProviderBase):
model_key_msg = check_model_key("LLM", self.api_key) model_key_msg = check_model_key("LLM", self.api_key)
if model_key_msg: if model_key_msg:
logger.bind(tag=TAG).error(model_key_msg) logger.bind(tag=TAG).error(model_key_msg)
self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url, timeout=httpx.Timeout(self.timeout))
self.client = openai.OpenAI(
api_key=self.api_key,
base_url=self.base_url,
timeout=httpx.Timeout(self.timeout),
)
# =============== 修正版 response() =================
def response(self, session_id, dialogue, **kwargs): def response(self, session_id, dialogue, **kwargs):
try: try:
# ✅ 清理 messages 中缺失 content 的项,防止 400 错误
clean_dialogue = []
fixed_count = 0
for msg in dialogue:
if "role" not in msg:
continue
if "content" not in msg or msg["content"] is None:
msg["content"] = ""
fixed_count += 1
clean_dialogue.append(msg)
if fixed_count > 0:
logger.bind(tag=TAG).warning(
f"修正了 {fixed_count} 条对话消息(缺失 content 已补空字符串)"
)
responses = self.client.chat.completions.create( responses = self.client.chat.completions.create(
model=self.model_name, model=self.model_name,
messages=dialogue, messages=clean_dialogue,
stream=True, stream=True,
max_tokens=kwargs.get("max_tokens", self.max_tokens), max_tokens=kwargs.get("max_tokens", self.max_tokens),
temperature=kwargs.get("temperature", self.temperature), temperature=kwargs.get("temperature", self.temperature),
@ -65,7 +85,6 @@ class LLMProvider(LLMProviderBase):
is_active = True is_active = True
for chunk in responses: for chunk in responses:
try: try:
# 检查是否存在有效的choice且content不为空
delta = ( delta = (
chunk.choices[0].delta chunk.choices[0].delta
if getattr(chunk, "choices", None) if getattr(chunk, "choices", None)
@ -75,7 +94,6 @@ class LLMProvider(LLMProviderBase):
except IndexError: except IndexError:
content = "" content = ""
if content: if content:
# 处理标签跨多个chunk的情况
if "<think>" in content: if "<think>" in content:
is_active = False is_active = False
content = content.split("<think>")[0] content = content.split("<think>")[0]
@ -88,19 +106,36 @@ class LLMProvider(LLMProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error in response generation: {e}") logger.bind(tag=TAG).error(f"Error in response generation: {e}")
# =============== 修正版 response_with_functions() =================
def response_with_functions(self, session_id, dialogue, functions=None): def response_with_functions(self, session_id, dialogue, functions=None):
try: try:
# ✅ 清理消息列表
clean_dialogue = []
fixed_count = 0
for msg in dialogue:
if "role" not in msg:
continue
if "content" not in msg or msg["content"] is None:
msg["content"] = ""
fixed_count += 1
clean_dialogue.append(msg)
if fixed_count > 0:
logger.bind(tag=TAG).warning(
# f"修正了 {fixed_count} 条函数调用消息(缺失 content 已补空字符串)"
)
stream = self.client.chat.completions.create( stream = self.client.chat.completions.create(
model=self.model_name, messages=dialogue, stream=True, tools=functions model=self.model_name,
messages=clean_dialogue,
stream=True,
tools=functions,
) )
for chunk in stream: for chunk in stream:
# 检查是否存在有效的choice且content不为空
if getattr(chunk, "choices", None): if getattr(chunk, "choices", None):
yield chunk.choices[0].delta.content, chunk.choices[ yield chunk.choices[0].delta.content, chunk.choices[
0 0
].delta.tool_calls ].delta.tool_calls
# 存在 CompletionUsage 消息时,生成 Token 消耗 log
elif isinstance(getattr(chunk, "usage", None), CompletionUsage): elif isinstance(getattr(chunk, "usage", None), CompletionUsage):
usage_info = getattr(chunk, "usage", None) usage_info = getattr(chunk, "usage", None)
logger.bind(tag=TAG).info( logger.bind(tag=TAG).info(