问题

星河社区大模型API调用DeepSeek模型,打印思维链(DeepSeek-R1) 普通代码没问题,流式代码会报错。

报错信息:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[13], line 21
     19 if (len(chunk.choices) > 0):
     20     if hasattr(chunk.choices[0].delta, 'reasoning_content') and chunk.choices[0].delta.reasoning_content:
---> 21         reasoning_content += chunk.choices[0].delta.reasoning_content
     22         print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
     23     else:

NameError: name 'reasoning_content' is not defined

 

源代码

首先需要定义个环境变量,以获得调用权限。AI Studio 访问令牌在“个人中心”-“访问令牌”页面获取。

import os
os.environ["AI_STUDIO_API_KEY"] = " AI Studio 访问令牌的环境变量"

打印思维链普通代码


import os
from openai import OpenAI

client = OpenAI(
     api_key=os.environ.get("AI_STUDIO_API_KEY"),  # 含有 AI Studio 访问令牌的环境变量,https://aistudio.baidu.com/account/accessToken,
     base_url="https://aistudio.baidu.com/llm/lmapi/v3",  # aistudio 大模型 api 服务域名
)

chat_completion = client.chat.completions.create(
    messages=[
        {'role': 'system', 'content': '你是 AI Studio 实训AI开发平台的开发者助理,你精通开发相关的知识,负责给开发者提供搜索帮助建议。'},
        {'role': 'user', 'content': '你好,请介绍一下AI Studio'}
    ],
    model="deepseek-r1",
)

print(chat_completion.choices[0].message.reasoning_content)
print(chat_completion.choices[0].message.content)

 打印思维链流式代码


import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("AI_STUDIO_API_KEY"),  # 含有 AI Studio 访问令牌的环境变量,https://aistudio.baidu.com/account/accessToken,
    base_url="https://aistudio.baidu.com/llm/lmapi/v3",  # aistudio 大模型 api 服务域名
)

completion = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {'role': 'system', 'content': '你是 AI Studio 实训AI开发平台的开发者助理,你精通开发相关的知识,负责给开发者提供搜索帮助建议。'},
        {'role': 'user', 'content': '你好,请介绍一下AI Studio'}
    ],
    stream=True,
)

for chunk in completion:
    if (len(chunk.choices) > 0):
        if hasattr(chunk.choices[0].delta, 'reasoning_content') and chunk.choices[0].delta.reasoning_content:
            reasoning_content += chunk.choices[0].delta.reasoning_content
            print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
        else:
            content += chunk.choices[0].delta.content
            print(chunk.choices[0].delta.content, end="", flush=True)

问题解决

真的很奇怪,官网给的例子怎么会有错呢

看看另一边飞桨星河社区使用Ollama部署deepseek-ai/DeepSeek-R1-Distill-Llama-70Bb大模型给的调用例子代码:

# pip install openai
from openai import OpenAI

client = OpenAI(
    api_key="xxxxx",
    base_url="https://api-4cnac8h6y3w0uehd.aistudio-app.com/v1"
)

completion = client.chat.completions.create(
    model="deepseek-r1:70b",
    temperature=0.6,
    messages=[
        {"role": "user", "content": "你好,请介绍一下你自己"}
    ],
    stream=True
)

for chunk in completion:
    if hasattr(chunk.choices[0].delta, "reasoning_content") and chunk.choices[0].delta.reasoning_content:
        print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
    else:
        print(chunk.choices[0].delta.content, end="", flush=True)

发现问题所在,怎么多出来这些代码了呢?

reasoning_content += reasoning_content += chunk.choices[0].delta.reasoning_content

而且这段代码,reasoning_content += chunk.choices[0].delta.reasoning_content 重复调用了 chunk.choices[0].delta.reasoning_content ,这明显违背了流式的基本性质啊!你都流了还流两次?

同样后面content += chunk.choices[0].delta.content多了一次流调用。把两次重复调用流去掉,问题解决。

修改后的代码

​

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("AI_STUDIO_API_KEY"),  # 含有 AI Studio 访问令牌的环境变量,https://aistudio.baidu.com/account/accessToken,
    base_url="https://aistudio.baidu.com/llm/lmapi/v3",  # aistudio 大模型 api 服务域名
)

completion = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {'role': 'system', 'content': '你是 AI Studio 实训AI开发平台的开发者助理,你精通开发相关的知识,负责给开发者提供搜索帮助建议。'},
        {'role': 'user', 'content': '你好,请介绍一下AI Studio'}
    ],
    stream=True,
)

for chunk in completion:
    if (len(chunk.choices) > 0):
        if hasattr(chunk.choices[0].delta, 'reasoning_content') and chunk.choices[0].delta.reasoning_content:
            # reasoning_content += chunk.choices[0].delta.reasoning_content
            print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
        else:
            # content += chunk.choices[0].delta.content
            print(chunk.choices[0].delta.content, end="", flush=True)

​

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐