部分模型调用官方代码时只能输出思考无法输出回复
官方调用模式如下:
from openai import OpenAI
import os
# 初始化OpenAI客户端
client = OpenAI(
# 如果没有配置环境变量,请用百炼API Key替换:api_key="sk-xxx"
api_key = os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
reasoning_content = "" # 定义完整思考过程
answer_content = "" # 定义完整回复
is_answering = False # 判断是否结束思考过程并开始回复
# 创建聊天完成请求
completion = client.chat.completions.create(
model="deepseek-r1", # 此处以 deepseek-r1 为例,可按需更换模型名称
messages=[
{"role": "user", "content": "9.9和9.11谁大"}
],
stream=True,
# 解除以下注释会在最后一个chunk返回Token使用量
# stream_options={
# "include_usage": True
# }
)
print("\n" + "=" * 20 + "思考过程" + "=" * 20 + "\n")
for chunk in completion:
# 如果chunk.choices为空,则打印usage
if not chunk.choices:
print("\nUsage:")
print(chunk.usage)
else:
delta = chunk.choices[0].delta
# 打印思考过程
if hasattr(delta, 'reasoning_content') and delta.reasoning_content != None:
print(delta.reasoning_content, end='', flush=True)
reasoning_content += delta.reasoning_content
else:
# 开始回复
if delta.content != "" and is_answering == False:
print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n")
is_answering = True
# 打印回复过程
print(delta.content, end='', flush=True)
answer_content += delta.content
问题原因是判断思考数据时没有判断思考数据为空字符串
#delta.reasoning_content增加delta.reasoning_content不等于空值的判断
if hasattr(delta, 'reasoning_content') and delta.reasoning_content != None and delta.reasoning_content != '':
#同理delta.content增加不等于None的判断
if delta.content != "" and delta.content != None and is_answering == False:
以下是模型回复的几个例子
模型 | 思考过程 | 最终回复 |
---|---|---|
deepseek-r1-distill-llama-70b | chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='', function_call=None, role=None, tool_calls=None, reasoning_content='愉快!\n'), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-r1-distill-llama-70b', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) | chunk == ', choices=[Choice(delta=ChoiceDelta(content='!欢迎来', function_call=None, role=None, tool_calls=None, reasoning_content=''), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-r1-distill-llama-70b', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) |
deepseek-r1 | chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, role=None, tool_calls=None, reasoning_content='。'), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-r1', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) | chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='', function_call=None, role=None, tool_calls=None, reasoning_content=None), finish_reason='stop', index=0, logprobs=None)], created=, model='deepseek-r1', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) |
三、deepseek-v3输出思考过程是在content中,输出模式跟其他模型完全不同,这个可能得特殊处理,但API示例用的同一套代码误导严重
建议不使用deepseek-v3模型,输出毫无规律,随性至极。
目前看出的问题:
1、content中<\think><\/think>标签无闭合,markdown无法识别
2、chunk中会包含content='思考过程===================='等意义不明的信息。
模型 | 思考过程 | 最终回复 |
---|---|---|
deepseek-v3 | chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='<think', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) | chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, role=None, tool_calls=None, reasoning_content='对话。\n</think'), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None) |
2025-02-26 13:00:55,044 - INFO - chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='', function_call=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None)
2025-02-26 13:00:55,044 - INFO - chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='================', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None)
2025-02-26 13:00:55,124 - INFO - chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='====', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None)
2025-02-26 13:00:55,297 - INFO - chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='完整', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None)
2025-02-26 13:00:55,789 - INFO - chunk == ChatCompletionChunk(id='', choices=[Choice(delta=ChoiceDelta(content='思考过程====================', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=, model='deepseek-v3', object='chat.completion.chunk', service_tier=None, system_fingerprint=None, usage=None)