openai-1.60.2调用deepseek-reasoner并获取思考内容的方法

news/2025/1/29 20:40:00/文章来源:https://www.cnblogs.com/fane-owl/p/18694810

本文写作时间:2025-01-29

过年假期,deepseek爆发式地火了。在尝试了网页端的“深度思考”模式后,我尝试在api端调用“深度思考”模型,然而,官方给出的代码并不行。多次尝试后,我获得了成功,希望对读者有所帮助。

环境

openai-1.60.2

deepseek官方代码

路径:https://api-docs.deepseek.com/zh-cn/guides/reasoning_model

这里我参考了非流式代码,复制如下:

from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")# Round 1
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(model="deepseek-reasoner",messages=messages
)reasoning_content = response.choices[0].message.reasoning_content
content = response.choices[0].message.content

这里有两点注意:

  1. 深度思考,即deepseek-R1,在对话的模型部分,需要指定:model="deepseek-reasoner"
  2. 深度思考状态下,对话的相应除了一般LLM输出的回答(即content),还有思考过程(即reasoning_content)。

然而,上述代码调用后失败,调试时发现,response中没有response.choices[0].message.reasoning_content内容。失败源于API上的微小差别。

修正后的代码

from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]response = client.chat.completions.create(model="deepseek-reasoner",messages=messages,# 尝试添加以下参数extra_body={  # 某些平台允许扩展参数"return_reasoning": True  }
)reasoning = response.choices[0].message.model_extra['reasoning_content']
print("---------------------------思考过程---------------------------")
print(reasoning)
print("---------------------------最终答案:---------------------------")
print(response.choices[0].message.content)

主要有两处修正:

  1. 在client.chat中添加了extra_body,指定"return_reasoning": True
  2. 思考过程的内容在response.choices[0].message.model_extra['reasoning_content']

输出结果如下:

---------------------------思考过程---------------------------
Okay, so I need to figure out whether 9.11 is greater than 9.8 or if 9.8 is greater than 9.11. Let me start by writing both numbers down to compare them properly. First, I know that when comparing decimals, it's important to look at each place value starting from the left. Both numbers have a 9 in the ones place, so that doesn't help me decide. Next, I should look at the tenths place. For 9.11, the tenths place is 1, and for 9.8, the tenths place is 8. Wait, but hold on, 9.8 is the same as 9.80, right? Because adding a zero at the end of a decimal doesn't change its value. So maybe if I write both numbers with the same number of decimal places, it'll be easier to compare. Let me try that.So, 9.11 can be written as 9.11, and 9.8 can be written as 9.80. Now, comparing them digit by digit. The ones place is 9 in both, so still equal. Moving to the tenths place: 1 vs. 8. Here, 8 is greater than 1. So does that mean 9.80 is greater than 9.11? Hmm, wait, but 9.11 is 9 and 11 hundredths, while 9.8 is 9 and 8 tenths. Since tenths are larger than hundredths, 8 tenths should be more than 11 hundredths. Let me verify that.Let me convert both numbers to fractions to see which is larger. For 9.11, that's 9 + 0.11, and 0.11 is 11/100. For 9.8, that's 9 + 0.8, and 0.8 is 8/10. To compare 8/10 and 11/100, I can convert them to have the same denominator. The least common denominator of 10 and 100 is 100. So, 8/10 is equivalent to 80/100. Now, 80/100 compared to 11/100. Clearly, 80/100 is larger. Therefore, 9.8 is greater than 9.11.Wait, but another way to think about it is to consider 9.8 as 9.80 and then compare 9.80 and 9.11. Starting from the left, the ones digit is 9 in both. Then the tenths digit: 8 vs. 1. Since 8 is greater than 1, 9.80 is greater than 9.11. Therefore, 9.8 is greater than 9.11. But I want to make sure I'm not making a mistake here. Sometimes, when numbers have different numbers of decimal places, it's easy to get confused. Let me check by subtracting them. If I subtract 9.11 from 9.8, what do I get? Let's see. 9.8 minus 9.11. To subtract these, I can write 9.8 as 9.80. Then:9.80
-9.11
------
0.69So the result is 0.69, which is a positive number. That means 9.8 is indeed larger than 9.11 by 0.69. Alternatively, if I convert both numbers to hundredths to make them have the same denominator, 9.11 is 911 hundredths, and 9.8 is 980 hundredths. Comparing 911 and 980, 980 is definitely larger. Therefore, 9.8 is greater. Wait, another approach: understanding decimal place values. The first decimal place is tenths (1/10), the second is hundredths (1/100), the third is thousandths (1/1000), etc. So in 9.11, after the decimal, we have 1 tenth and 1 hundredth. In 9.8, which is 9.80, we have 8 tenths and 0 hundredths. Since tenths are 10 times bigger than hundredths, 8 tenths is equal to 80 hundredths. Therefore, 80 hundredths (from 9.8) is way more than 11 hundredths (from 9.11). So 9.8 is greater.Is there any way that 9.11 could be larger? Let's see. If someone mistakenly compares the decimal parts as whole numbers, they might think 11 is greater than 8, but that's incorrect because the 11 is in the hundredths place, whereas the 8 is in the tenths place. So you can't compare them directly without considering their place values. To visualize, if I have 9.11 meters and 9.8 meters, which is longer? 9.8 meters is the same as 9 meters and 80 centimeters, while 9.11 meters is 9 meters, 1 decimeter, and 1 centimeter. Since 80 centimeters is more than 1 decimeter (which is 10 centimeters) and 1 centimeter, clearly 9.8 meters is longer.Another example: money. If I have $9.11 and $9.80, which is more money? $9.80 is $9 and 80 cents, whereas $9.11 is $9 and 11 cents. 80 cents is more than 11 cents, so $9.80 is more.Therefore, after considering different methods—converting to fractions, aligning decimal places, subtracting, using real-life examples—it's clear that 9.8 is greater than 9.11. I think that's thorough enough. I can't think of any perspective where 9.11 would be larger. So, yeah, 9.8 is the greater number.**Final Answer**
The greater number is \boxed{9.8}.
---------------------------最终答案:---------------------------
To determine whether 9.11 or 9.8 is greater, we start by comparing their place values. 1. **Align the decimal places**:- 9.11 can be written as 9.11- 9.8 can be written as 9.802. **Compare digit by digit**:- The ones place is 9 in both numbers.- The tenths place: 1 (in 9.11) vs. 8 (in 9.80). Since 8 is greater than 1, 9.80 is greater than 9.11.3. **Verification by converting to fractions**:- 9.11 is 911 hundredths (911/100).- 9.8 is 980 hundredths (980/100).- Comparing 911 and 980, 980 is larger.4. **Subtraction method**:- Subtracting 9.11 from 9.80 gives 0.69, a positive result, confirming 9.80 is greater.5. **Real-life examples**:- In money, $9.80 (980 cents) is more than $9.11 (911 cents).After considering various methods, it is clear that 9.8 is greater than 9.11.\[
\boxed{9.8}
\]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/876892.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qcom usb PD tcpc overview

该软件层将PMxxxxB硬件连接到LPM模块,因为上述模块使用Type-C端口控制器接口(TCPCI)进行通信。 软件层,使PMIC Type-C PD硬件适用于基于TCPCI的软件架构 基本状态机:进入、离线、待机状态 PMIC Type-C和PD PHY中断的消费者 PMIC硬件专用排序和定时器

男生如何自己简单理发

快过年了,给自己理个发。 从24年10月份开始,目前已经给自己理发两次,都是短发寸头,给我爸和我三叔各理发一次,算是有点经验了,我准备过年前给自己再稍微修理头发一下。自己动手实践,且效果还不错的情况下,真的非常有成就感,如果有人指导情况下,其实自己理发难度不高,…

测序中的GC偏好

001、 测序中的GC偏好指的是基因组上GC含量在50%左右的区域更容易被测到,产生的reads更多,这些区域的覆盖度更高,在高GC或者低GC区域,不容易被测到,产生较少的reads,这些区域的覆盖度更少。用基因组单位长度的bin中的GC含量作为横坐标,覆盖度作为纵坐标作图,可以明显的…

人工智能(AI)简史:推动新时代的科技力量

人工智能(AI,Artificial Intelligence)是计算机科学的一个分支,旨在研究和开发可以模拟、扩展或增强人类智能的**系统**。它涉及多种技术和方法,包括机器学习、深度学习、自然语言处理(NLP)、计算机视觉、专家系统等。一、人工智能简介 人工智能(AI,Artificial Intell…

03. vim编辑器的使用

一、vim编辑器的使用vim 是 Unix 和 类 Unix 操作系统中常用的文本编辑器。如果 Ubuntu 系统默认没有安装 vim,我们可以使用 apt 工具安装 vim 编辑器。sudo apt install vim安装好 vim 之后,我们可以如下命令编辑一个文件。 vim 文件用 vim 打开一个文件就直接进入了 一般模…

“简单”学英语

本文总结了本人作为英语学渣的较为无痛的学习英语方法,无痛当然学习时间长一些,但相对简单一些,实操性强。 前言 最近和人聊天时对方说,感觉没什么可以学,但又想学点什么,我回答:”学英语啊“。有人迷茫,不知道怎么发展时,我回答:“先学点英语啊”。有人在犹豫要不要…

路飞学城5-celery

Celery Celery是一个功能完备即插即用的异步任务队列系统。它适用于异步处理问题,当发送邮件、或者文件上传, 图像处理等等一些比较耗时的操作,我们可将其异步执行,这样用户不需要等待很久,提高用户体验。 文档:http://docs.jinkan.org/docs/celery/getting-started/index…

Linux 安装并使用 EasyConnect

x86_64-linux-gnu下载: 如果你下载之后要登入 https://yzftxdy.com 的话,你就先在浏览器里面输入这个网址,网站会弹出来给你下载的机会如果你的浏览器无法下载,你可以让别人帮你下载 .deb 文件。糖下载完了就 dpkg -i balabala.deb,于是你打开 "show apps"会找到…

[Jest] 测试快照

在对组件进行测试的时候,往往需要从两个方面进行测试:交互:确保组件在进行交互时功能正常 渲染:确保组件渲染输出正确(比如不会多一个或者少一个 DOM 元素)针对渲染方面的测试,我们就可以使用快照来进行测试。 所谓快照,就是给渲染出来的 DOM 元素拍一张“照片”(将最…

[React Jest] 测试 Hook

在进行 React 开发的时候,还有一个非常重要的功能模块,那就是 Hook,自定义 Hook 作为一块公共逻辑的抽离,也会像组件一样被用到多个地方,因此对 Hook 的测试也是非常有必要的。 Hook 没有办法像普通函数一样直接进行测试,因为在 React 中规中,Hook 必须要在组件里面使用…

07_LaTeX之绘图功能

除了排版文字,$\LaTeX{}$ 也支持用代码表示图形。不同的扩展已极大丰富了 $\LaTeX{}$ 的图形功能,`TikZ` 就是其中之一。本章将带你了解一些基本的绘图功能。07_\(\LaTeX{}\) 之绘图功能 目录07_\(\LaTeX{}\) 之绘图功能绘图语言简介\(\textrm{TikZ}\) 绘图语言\(\textrm{Tik…

《计算机网络》笔记——第四章 网络层

目录第4章 网络层网络层提供的两种服务网际协议IP虚拟互连网络分类的IP地址IP地址与硬件地址地址解析协议ARPIP数据报的格式IP层转发分组的流程划分子网和构造超网划分子网无分类编址CIDR(构造超网)最长前缀匹配网际控制报文协议ICMPICMP的应用互联网的路由选择协议 第4章 网络…