本文写作时间: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
这里有两点注意:
深度思考
,即deepseek-R1
,在对话的模型部分,需要指定:model="deepseek-reasoner"
。- 在
深度思考
状态下,对话的相应除了一般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)
主要有两处修正:
- 在client.chat中添加了extra_body,指定
"return_reasoning": True
。 思考过程
的内容在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}
\]