fix:chatcmpl.py del content <think>,in the ppiochatcmpl.py and modelsopechatcmpl.py fun _closure_stream stream logic

This commit is contained in:
Dong_master
2025-08-09 02:46:13 +08:00
parent 261f50b8ec
commit eede354d3b
3 changed files with 159 additions and 90 deletions
@@ -112,24 +112,32 @@ class PPIOChatCompletions(chatcmpl.OpenAIChatCompletions):
del me['image_base64']
args['messages'] = messages
current_content = ''
args['stream'] = True
chunk_idx = 0
is_think = False
tool_calls_map: dict[str, llm_entities.ToolCall] = {}
chunk_idx = 0
thinking_started = False
thinking_ended = False
role = 'assistant' # 默认角色
accumulated_reasoning = '' # 仅用于判断何时结束思维链
async for chunk in self._req_stream(args, extra_body=extra_args):
# 处理流式消息
if hasattr(chunk, 'choices'):
# 完整响应模式
if chunk.choices:
choice = chunk.choices[0]
delta = choice.delta.model_dump() if hasattr(choice, 'delta') else choice.message.model_dump()
else:
continue
# 解析 chunk 数据
if hasattr(chunk, 'choices') and chunk.choices:
choice = chunk.choices[0]
delta = choice.delta.model_dump() if hasattr(choice, 'delta') else {}
finish_reason = getattr(choice, 'finish_reason', None)
else:
# 流式chunk模式
delta = chunk.delta.model_dump() if hasattr(chunk, 'delta') else {}
delta = {}
finish_reason = None
# 从第一个 chunk 获取 role,后续使用这个 role
if 'role' in delta and delta['role']:
role = delta['role']
# 获取增量内容
delta_content = delta.get('content', '')
# reasoning_content = delta.get('reasoning_content', '')
if remove_think:
if delta['content'] is not None:
if '<think>' in delta['content']:
@@ -141,30 +149,46 @@ class PPIOChatCompletions(chatcmpl.OpenAIChatCompletions):
elif is_think or delta['content'] == '\n\n':
continue
delta_message = await self._make_msg_chunk(delta, chunk_idx)
# 处理流式消息
if delta_message.content:
current_content += delta_message.content
delta_message.content = current_content
# delta_message.all_content = current_content
if delta_message.tool_calls:
for tool_call in delta_message.tool_calls:
if tool_call.id not in tool_calls_map:
tool_calls_map[tool_call.id] = llm_entities.ToolCall(
id=tool_call.id,
type=tool_call.type,
function=llm_entities.FunctionCall(
name=tool_call.function.name if tool_call.function else '', arguments=''
),
)
if tool_call.function and tool_call.function.arguments:
# 流式处理中,工具调用参数可能分多个chunk返回,需要追加而不是覆盖
tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
delta_tool_calls = None
if delta.get('tool_calls'):
delta_tool_calls = []
for tool_call in delta['tool_calls']:
tc_id = tool_call.get('id')
if tc_id:
if tc_id not in tool_calls_map:
# 新的工具调用
tool_calls_map[tc_id] = llm_entities.ToolCall(
id=tc_id,
type=tool_call.get('type', 'function'),
function=llm_entities.FunctionCall(
name=tool_call.get('function', {}).get('name', ''),
arguments=tool_call.get('function', {}).get('arguments', ''),
),
)
delta_tool_calls.append(tool_calls_map[tc_id])
else:
# 追加函数参数
func_args = tool_call.get('function', {}).get('arguments', '')
if func_args:
tool_calls_map[tc_id].function.arguments += func_args
# 返回更新后的完整工具调用
delta_tool_calls.append(tool_calls_map[tc_id])
# 跳过空的第一个 chunk(只有 role 没有内容)
if chunk_idx == 0 and not delta_content and not delta.get('tool_calls'):
chunk_idx += 1
continue
# 构建 MessageChunk - 只包含增量内容
chunk_data = {
'role': role,
'content': delta_content if delta_content else None,
'tool_calls': delta_tool_calls if delta_tool_calls else None,
'is_final': bool(finish_reason),
}
# 移除 None 值
chunk_data = {k: v for k, v in chunk_data.items() if v is not None}
yield llm_entities.MessageChunk(**chunk_data)
chunk_idx += 1
chunk_choices = getattr(chunk, 'choices', None)
if chunk_choices and getattr(chunk_choices[0], 'finish_reason', None):
delta_message.is_final = True
delta_message.content = current_content
yield delta_message