[GH-ISSUE #360] 无法强制指定模型进入思维模式 <think> #280

Closed
opened 2026-10-26 21:18:43 +00:00 by bsakaguchi · 9 comments
Owner

Originally created by @jiumingzhijium on GitHub (Feb 10, 2025).
Original GitHub issue: https://github.com/deepseek-ai/DeepSeek-R1/issues/360

在调用 deepseek-r1 的 api 时,模型总是会绕过思维模式,将 temperature 设置为0.6,在 prompt 最加入 也无法让模型在时回答带有 think

Originally created by @jiumingzhijium on GitHub (Feb 10, 2025). Original GitHub issue: https://github.com/deepseek-ai/DeepSeek-R1/issues/360 在调用 deepseek-r1 的 api 时,模型总是会绕过思维模式,将 temperature 设置为0.6,在 prompt 最加入 <think> 也无法让模型在时回答带有 think
bsakaguchi 2026-10-26 21:18:43 +00:00
Author
Owner

@Alphayellowcat commented on GitHub (Feb 10, 2025):

访问样例
r1的temperature其实并没有启用。我怀疑你没有修改代码把返回数据中的reasoning_content取出来。我写了一个简单样例,你可以参考一下:

from openai import OpenAI
import os
from dotenv import load_dotenv

load_dotenv()

class DeepseekReasoner:
    def __init__(self):
        self.client = OpenAI(
            base_url="https://api.deepseek.com/v1",
            api_key=os.environ["DEEPSEEK_API_KEY"],
        )
        
    def chat(self, messages, stream_output=False):
        response = self.client.chat.completions.create(
            model="deepseek-reasoner",
            messages=messages,
            max_tokens=4096,
            stream=True
        )
        
        if stream_output:
            return response
            
        reasoning_content = ""
        content = ""
        
        for chunk in response:
            if chunk.choices[0].delta.reasoning_content:
                reasoning_content += chunk.choices[0].delta.reasoning_content
            elif chunk.choices[0].delta.content is not None:
                content += chunk.choices[0].delta.content
                
        return {
            "reasoning_content": reasoning_content,
            "content": content
        }

    def chat_with_ai(self):
        """
        启动交互式对话
        """
        messages = [
            {"role": "system", "content": "你是一个强大的大模型助手,你会先进行深入思考,然后再给出答案。"}
        ]
        
        print("开始与 deepseek-reasoner 对话(输入 'quit' 或 'exit' 结束对话)")
        print("-" * 50)
        
        while True:
            user_input = input("\n你: ").strip()
            
            if user_input.lower() in ['quit', 'exit']:
                print("\n对话结束!")
                break
            
            show_reasoning = input("是否显示思考过程?(y/n): ").strip().lower() == 'y'
                
            try:
                messages.append({"role": "user", "content": user_input})
                
                if not show_reasoning:
                    print("\n思考中...\n", end="", flush=True)
                else:
                    print("\n思考过程:\n", end="", flush=True)
                    
                stream = self.chat(messages, stream_output=True)
                
                reasoning_content = ""
                final_content = ""
                is_reasoning = show_reasoning
                
                for chunk in stream:
                    if chunk.choices[0].delta.reasoning_content:
                        chunk_text = chunk.choices[0].delta.reasoning_content
                        if show_reasoning:
                            print(chunk_text, end="", flush=True)
                        reasoning_content += chunk_text
                    elif chunk.choices[0].delta.content is not None:
                        if is_reasoning and reasoning_content:
                            print("\n\n最终答案:\n\n", end="", flush=True)
                            is_reasoning = False
                        chunk_text = chunk.choices[0].delta.content
                        print(chunk_text, end="", flush=True)
                        final_content += chunk_text
                
                # 只保存最终答案到对话历史
                messages.append({"role": "assistant", "content": final_content})
                print("\n" + "-" * 50)
                
            except Exception as e:
                print(f"\n发生错误:\n{str(e)}")
                print("\n" + "-" * 50)

if __name__ == "__main__":
    reasoner = DeepseekReasoner()
    reasoner.chat_with_ai() 
<!-- gh-comment-id:2648108348 --> @Alphayellowcat commented on GitHub (Feb 10, 2025): [访问样例](https://api-docs.deepseek.com/zh-cn/guides/reasoning_model#%E8%AE%BF%E9%97%AE%E6%A0%B7%E4%BE%8B) r1的temperature其实并没有启用。我怀疑你没有修改代码把返回数据中的reasoning_content取出来。我写了一个简单样例,你可以参考一下: ```python from openai import OpenAI import os from dotenv import load_dotenv load_dotenv() class DeepseekReasoner: def __init__(self): self.client = OpenAI( base_url="https://api.deepseek.com/v1", api_key=os.environ["DEEPSEEK_API_KEY"], ) def chat(self, messages, stream_output=False): response = self.client.chat.completions.create( model="deepseek-reasoner", messages=messages, max_tokens=4096, stream=True ) if stream_output: return response reasoning_content = "" content = "" for chunk in response: if chunk.choices[0].delta.reasoning_content: reasoning_content += chunk.choices[0].delta.reasoning_content elif chunk.choices[0].delta.content is not None: content += chunk.choices[0].delta.content return { "reasoning_content": reasoning_content, "content": content } def chat_with_ai(self): """ 启动交互式对话 """ messages = [ {"role": "system", "content": "你是一个强大的大模型助手,你会先进行深入思考,然后再给出答案。"} ] print("开始与 deepseek-reasoner 对话(输入 'quit' 或 'exit' 结束对话)") print("-" * 50) while True: user_input = input("\n你: ").strip() if user_input.lower() in ['quit', 'exit']: print("\n对话结束!") break show_reasoning = input("是否显示思考过程?(y/n): ").strip().lower() == 'y' try: messages.append({"role": "user", "content": user_input}) if not show_reasoning: print("\n思考中...\n", end="", flush=True) else: print("\n思考过程:\n", end="", flush=True) stream = self.chat(messages, stream_output=True) reasoning_content = "" final_content = "" is_reasoning = show_reasoning for chunk in stream: if chunk.choices[0].delta.reasoning_content: chunk_text = chunk.choices[0].delta.reasoning_content if show_reasoning: print(chunk_text, end="", flush=True) reasoning_content += chunk_text elif chunk.choices[0].delta.content is not None: if is_reasoning and reasoning_content: print("\n\n最终答案:\n\n", end="", flush=True) is_reasoning = False chunk_text = chunk.choices[0].delta.content print(chunk_text, end="", flush=True) final_content += chunk_text # 只保存最终答案到对话历史 messages.append({"role": "assistant", "content": final_content}) print("\n" + "-" * 50) except Exception as e: print(f"\n发生错误:\n{str(e)}") print("\n" + "-" * 50) if __name__ == "__main__": reasoner = DeepseekReasoner() reasoner.chat_with_ai() ```
Author
Owner

@ariesfish commented on GitHub (Feb 11, 2025):

提示词加上输出格式,可以大幅提高思考概率

think first and then answer user's question as below format every time:
<think>reasoning content</think>
content
<!-- gh-comment-id:2649701464 --> @ariesfish commented on GitHub (Feb 11, 2025): 提示词加上输出格式,可以大幅提高思考概率 ``` think first and then answer user's question as below format every time: <think>reasoning content</think> content ```
Author
Owner

@jiumingzhijium commented on GitHub (Feb 11, 2025):

在使用代码时,我遇到了数据格式错误的提示,'ChoiceDelta' object has no attribute 'reasoning_content'

你: 非 飞机为什么会飞
是否显示思考过程?(y/n): y

思考过程:

发生错误:
'ChoiceDelta' object has no attribute 'reasoning_content'


我尝试打印 delta 得到的结果是这样的:Delta dict: {'content': '', 'function_call': None, 'refusal': None, 'role': 'assistant', 'tool_calls': None},没有 reasoning_content

<!-- gh-comment-id:2649907912 --> @jiumingzhijium commented on GitHub (Feb 11, 2025): 在使用代码时,我遇到了数据格式错误的提示,'ChoiceDelta' object has no attribute 'reasoning_content' -------------------------------------------------- 你: 非 飞机为什么会飞 是否显示思考过程?(y/n): y 思考过程: 发生错误: 'ChoiceDelta' object has no attribute 'reasoning_content' -------------------------------------------------- 我尝试打印 delta 得到的结果是这样的:Delta dict: {'content': '', 'function_call': None, 'refusal': None, 'role': 'assistant', 'tool_calls': None},没有 reasoning_content
Author
Owner

@Alphayellowcat commented on GitHub (Feb 11, 2025):

在使用代码时,我遇到了数据格式错误的提示,'ChoiceDelta' object has no attribute 'reasoning_content'

你: 非 飞机为什么会飞 是否显示思考过程?(y/n): y

思考过程:

发生错误: 'ChoiceDelta' object has no attribute 'reasoning_content'

我复现的时候时正常输出的,先显示“思考过程”再显示“最终答案”。
我想了一下可能是你环境里openai的包版本不对,导致找不到 reasoning_content 属性?
先试试

pip show openai

我这显示的version是1.60.0,如果不一样你可以试试重装

pip uninstall openai
pip install openai
<!-- gh-comment-id:2649951353 --> @Alphayellowcat commented on GitHub (Feb 11, 2025): > ## 在使用代码时,我遇到了数据格式错误的提示,'ChoiceDelta' object has no attribute 'reasoning_content' > 你: 非 飞机为什么会飞 是否显示思考过程?(y/n): y > > 思考过程: > > 发生错误: 'ChoiceDelta' object has no attribute 'reasoning_content' 我复现的时候时正常输出的,先显示“思考过程”再显示“最终答案”。 我想了一下可能是你环境里openai的包版本不对,导致找不到 reasoning_content 属性? 先试试 ``` pip show openai ``` 我这显示的version是1.60.0,如果不一样你可以试试重装 ``` pip uninstall openai ``` ``` pip install openai ```
Author
Owner

@jiumingzhijium commented on GitHub (Feb 11, 2025):

我发现这个错误和调用模型的方式有关,我升级了openai到1.60.0,还是会出现问题
由于官方的 api 调用目前不可用,我使用了 openrouter 里面的 R1 模型,这是会出现'ChoiceDelta' object has no attribute 'reasoning_content',当使用阿里的 R1 模型时,就没有问题
我不理解相同的模型为什么会返回格式不一致
请问你这里使用的是哪里调用的 R1 模型

<!-- gh-comment-id:2650117889 --> @jiumingzhijium commented on GitHub (Feb 11, 2025): 我发现这个错误和调用模型的方式有关,我升级了openai到1.60.0,还是会出现问题 由于官方的 api 调用目前不可用,我使用了 openrouter 里面的 R1 模型,这是会出现'ChoiceDelta' object has no attribute 'reasoning_content',当使用阿里的 R1 模型时,就没有问题 我不理解相同的模型为什么会返回格式不一致 请问你这里使用的是哪里调用的 R1 模型
Author
Owner

@jiumingzhijium commented on GitHub (Feb 11, 2025):

我找到原因了,是 openrouter 调用 R1 推理模型的坑:openrouter 返回数据不会默认包含 reasoning 字段(这里面是 R1 的推理过程),在调用 api 需要增加"include_reasoning": True,才能返回 reasoning 字段

from openai import OpenAI
import os
from dotenv import load_dotenv

load_dotenv()

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.getenv("OPENROUTER_API_KEY"),
)

response = client.chat.completions.create(
    model="deepseek/deepseek-r1",
    messages=[
        {"role": "user", "content": """飞机为什么会飞\nthink first and then answer user's question as below format every time:
<think>reasoning content</think>
content"""}
    ],
    extra_body={"include_reasoning": True}  # OpenRouter特有参数
)

# 输出完整响应
print(response)
# 提取推理内容
print(response.choices[0].message.reasoning)
<!-- gh-comment-id:2650594097 --> @jiumingzhijium commented on GitHub (Feb 11, 2025): 我找到原因了,是 openrouter 调用 R1 推理模型的坑:openrouter 返回数据不会默认包含 reasoning 字段(这里面是 R1 的推理过程),在调用 api 需要增加"include_reasoning": True,才能返回 reasoning 字段 ``` from openai import OpenAI import os from dotenv import load_dotenv load_dotenv() client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.getenv("OPENROUTER_API_KEY"), ) response = client.chat.completions.create( model="deepseek/deepseek-r1", messages=[ {"role": "user", "content": """飞机为什么会飞\nthink first and then answer user's question as below format every time: <think>reasoning content</think> content"""} ], extra_body={"include_reasoning": True} # OpenRouter特有参数 ) # 输出完整响应 print(response) # 提取推理内容 print(response.choices[0].message.reasoning) ```
Author
Owner

@Alphayellowcat commented on GitHub (Feb 11, 2025):

我找到原因了,是 openrouter 调用 R1 推理模型的坑:openrouter 返回数据不会默认包含 reasoning 字段(这里面是 R1 的推理过程),在调用 api 需要增加"include_reasoning": True,才能返回 reasoning 字段

from openai import OpenAI
import os
from dotenv import load_dotenv

load_dotenv()

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.getenv("OPENROUTER_API_KEY"),
)

response = client.chat.completions.create(
    model="deepseek/deepseek-r1",
    messages=[
        {"role": "user", "content": """飞机为什么会飞\nthink first and then answer user's question as below format every time:
<think>reasoning content</think>
content"""}
    ],
    extra_body={"include_reasoning": True}  # OpenRouter特有参数
)

# 输出完整响应
print(response)
# 提取推理内容
print(response.choices[0].message.reasoning)

我用的deepseek官方的api,按照官方文档改的。
找到问题就行。

<!-- gh-comment-id:2650817605 --> @Alphayellowcat commented on GitHub (Feb 11, 2025): > 我找到原因了,是 openrouter 调用 R1 推理模型的坑:openrouter 返回数据不会默认包含 reasoning 字段(这里面是 R1 的推理过程),在调用 api 需要增加"include_reasoning": True,才能返回 reasoning 字段 > > ``` > from openai import OpenAI > import os > from dotenv import load_dotenv > > load_dotenv() > > client = OpenAI( > base_url="https://openrouter.ai/api/v1", > api_key=os.getenv("OPENROUTER_API_KEY"), > ) > > response = client.chat.completions.create( > model="deepseek/deepseek-r1", > messages=[ > {"role": "user", "content": """飞机为什么会飞\nthink first and then answer user's question as below format every time: > <think>reasoning content</think> > content"""} > ], > extra_body={"include_reasoning": True} # OpenRouter特有参数 > ) > > # 输出完整响应 > print(response) > # 提取推理内容 > print(response.choices[0].message.reasoning) > ``` 我用的deepseek官方的api,按照官方文档改的。 找到问题就行。
Author
Owner

@github-actions[bot] commented on GitHub (Mar 24, 2025):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this issue is still relevant, please leave a comment to keep it open. Thank you for your contributions!

<!-- gh-comment-id:2746569887 --> @github-actions[bot] commented on GitHub (Mar 24, 2025): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this issue is still relevant, please leave a comment to keep it open. Thank you for your contributions!
Author
Owner

@github-actions[bot] commented on GitHub (Apr 29, 2025):

false

<!-- gh-comment-id:2837063782 --> @github-actions[bot] commented on GitHub (Apr 29, 2025): false
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepSeek-R1#280
No description provided.