外观
Gemini深度思考
快速开始
使用思考模型发起请求与任何其他内容生成请求类似。主要区别在于,您需要在 model 字段中指定某个支持思考的模型,如以下文本生成示例所示:
js
curl "https://mishu.one/google/v1beta/models/gemini-2.5-pro-preview-06-05:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain the concept of Occam\''s Razor and provide a simple, everyday example."
}
]
}
]
}'thinkingBudget
借助 thinkingBudget 参数,您可以为模型提供指导,帮助其了解在生成回答时可使用的思考 token 数量。令牌数量越多,推理过程通常越详细,这对处理更复杂的任务很有帮助。如果您未设置 thinkingBudget,模型将根据请求的复杂性动态调整预算。
只有 Gemini 2.5 Flash 和 2.5 Pro 支持 thinkingBudget。具体取决于问题,模型可能会超出或不足令牌预算。
以下是每种模型类型的配置要求。
Gemini 2.5 Pro
thinkingBudget 必须是 128 到 32768 之间的整数。
使用 Gemini 2.5 Pro 时,您无法关闭思考功能,最低预算为 128。
如果未设置 thinkingBudget,模型将自动决定要使用多少思考预算。
Gemini 2.5 Flash
thinkingBudget 必须是 0 到 24576 之间的整数。
将思考预算设置为 0 会停用思考功能。
如下代码:
js
curl "https://mishu.one/google/v1beta/models/gemini-2.5-pro-preview-06-05:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Provide a list of 3 famous physicists and their key contributions"
}
]
}
],
"generationConfig": {
"thinkingConfig": {
"thinkingBudget": 1024
}
}
}'返回:
js
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Of course! Here are three of the most famous physicists in history and their key contributions.\n\n### 1. Isaac Newton (1643-1727)\n\nOften considered one of the most influential scientists of all time, Newton's work laid the foundation for classical mechanics.\n\n* **Key Contributions:**\n * **Three Laws of Motion:** These laws describe the relationship between an object and the forces acting upon it. They are the bedrock of classical mechanics (1. Inertia, 2. F=ma, 3. Action-Reaction).\n * **Law of Universal Gravitation:** Newton formulated the law describing the gravitational attraction between two bodies, explaining everything from why an apple falls from a tree to how planets orbit the sun.\n * **Invention of Calculus:** He developed infinitesimal calculus, a powerful mathematical tool essential for modern science and engineering.\n * **Optics:** He demonstrated that white light is composed of a spectrum of colors by splitting it with a prism and built the first practical reflecting telescope.\n\n### 2. Albert Einstein (1879-1955)\n\nEinstein is the most famous scientist of the 20th century, and his name is synonymous with genius. He revolutionized our understanding of space, time, gravity, and energy.\n\n* **Key Contributions:**\n * **Theory of Special Relativity:** Published in 1905, this theory proposed that the laws of physics are the same for all non-accelerating observers and that the speed of light in a vacuum is constant, leading to new concepts of space and time.\n * **Mass-Energy Equivalence (E=mc²):** This iconic equation, a consequence of Special Relativity, reveals the relationship between mass and energy, forming the basis for nuclear power and atomic weapons.\n * **Theory of General Relativity:** Published in 1915, this is a theory of gravitation that describes gravity not as a force, but as a curvature of spacetime caused by mass and energy.\n * **The Photoelectric Effect:** His explanation of this effect (where light can eject electrons from a material) provided crucial evidence for quantum theory and earned him the 1921 Nobel Prize in Physics.\n\n### 3. Marie Curie (1867-1934)\n\nA pioneering physicist and chemist, Marie Curie conducted groundbreaking research on radioactivity and was the first woman to win a Nobel Prize.\n\n* **Key Contributions:**\n * **Theory of Radioactivity:** She coined the term \"radioactivity\" and developed methods to isolate radioactive isotopes. Her work showed that radioactivity was an atomic property.\n * **Discovery of New Elements:** Working with her husband, Pierre Curie, she discovered two new elements: **polonium** (named after her native Poland) and **radium**.\n * **First Woman to Win a Nobel Prize:** She won the 1903 Nobel Prize in Physics (along with her husband and Henri Becquerel) for their joint research on radiation.\n * **Only Person to Win a Nobel Prize in Two Different Scientific Fields:** She won the 1911 Nobel Prize in Chemistry for her discovery of radium and polonium. Her work paved the way for nuclear physics and cancer treatments."
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"usageMetadata": {
"promptTokenCount": 12,
"candidatesTokenCount": 692,
"totalTokenCount": 1464,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 12
}
],
"thoughtsTokenCount": 760
},
"modelVersion": "gemini-2.5-pro-preview-06-05",
"responseId": "zoBRaJDAGKazjrEP7o79yAg"
}输出思维链
思维链内容是模型原始想法的综合版本,可提供有关模型内部推理过程的深入分析。请注意,thinkingBudget适用于模型的原始思考,而不适用于思维链内容。
您可以在请求配置中将 includeThoughts 设置为 true,以返回思维链内容。然后,您可以通过迭代 response 参数的 parts 并检查 thought 布尔值来访问摘要。
js
"generationConfig": {
"thinkingConfig": {
"thinkingBudget": 1024,
"includeThoughts":true
}
}以下示例演示了如何在不流式传输的情况下启用和检索思维链内容,这会在响应中返回单个最终思维链内容: 如下代码:
js
from google import genai
from google.genai import types
client = genai.Client(api_key="GOOGLE_API_KEY")
prompt = "What is the sum of the first 50 prime numbers?"
response = client.models.generate_content(
model="gemini-2.5-pro",
contents=prompt,
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
include_thoughts=True
)
)
)
for part in response.candidates[0].content.parts:
if not part.text:
continue
if part.thought:
print("Thought summary:")
print(part.text)
print()
else:
print("Answer:")
print(part.text)
print()返回示例:
js
{
"candidates": [
{
"content": {
"parts": [
{
"text": "**Deconstructing and Solving for the Sum**\n\nOkay, so the user wants the sum of the first 50 prime numbers. Straightforward enough, but I want to make sure I'm giving a *good* answer. Let's see... they definitely need the final answer, that's a given. I bet they'd also appreciate seeing the actual list of the primes, just to make it transparent, right? And maybe they're curious *how* to find them, not just the answer. So, a clear, direct answer is important, but a little context and explanation makes it more helpful.\n\nSo, first things first, I need to get those primes. That's the crux of it. I can't just throw out a number; I need a *method*. Alright, I'll start listing them out systematically, testing for primality as I go. Start with 2, of course. Then 3, 5, 7. The key is the primality test: a number `n` is prime if it's only divisible by 1 and itself. I should check divisibility by prime numbers less than or equal to the square root of `n`. Gotta jot these down in my \"scratchpad\" (mental or virtual).\n\nHere we go... 2, 3, 5, 7, 11, 13, 17, 19, 23, 29... Okay, first ten done. Let's keep going... 31, 37, 41, 43, 47, 53, 59, 61, 67, 71. Twenty down... need to stay sharp, make sure I'm not missing any composites or including anything that isn't prime. Now the 70s... 73 is good, 77 (7*11, no), 79 is prime. Great, on to the next one: 73, 79, 83, 89, 97... Halfway there! Okay, let's keep it steady. 101, 103, 107, 109, 113... Now, 30 down, 20 more to go. Hmmm, what about the 120's? 121 is out (11*11), 127, is it prime? This is a bit tedious, but it's the most reliable way. I think I'll use the aid of a calculator to help. I have my list of first 50 prime numbers and I'm ready to sum them up.\n",
"thought": true
},
{
"text": "Of course. To find the sum of the first 50 prime numbers, we first need to identify them.\n\n### Step 1: List the first 50 prime numbers\n\nA prime number is a whole number greater than 1 that has only two divisors: 1 and itself. Here is the list of the first 50 prime numbers:\n\n1. 2\n2. 3\n3. 5\n4. 7\n5. 11\n6. 13\n7. 17\n8. 19\n9. 23\n10. 29\n11. 31\n12. 37\n13. 41\n14. 43\n15. 47\n16. 53\n17. 59\n18. 61\n19. 67\n20. 71\n21. 73\n22. 79\n23. 83\n24. 89\n25. 97\n26. 101\n27. 103\n28. 107\n29. 109\n30. 113\n31. 127\n32. 131\n33. 137\n34. 139\n35. 149\n36. 151\n37. 157\n38. 163\n39. 167\n40. 173\n41. 179\n42. 181\n43. 191\n44. 193\n45. 197\n46. 199\n47. 211\n48. 223\n49. 227\n50. 229\n\nThe 50th prime number is **229**.\n\n### Step 2: Calculate the sum\n\nNow, we add all these numbers together. While there's no simple formula for the sum of the first *n* primes (like there is for the sum of the first *n* integers), we can add them up directly.\n\nAdding all the numbers in the list above gives the total.\n\nThe sum of the first 50 prime numbers is **5,133**."
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"usageMetadata": {
"promptTokenCount": 13,
"candidatesTokenCount": 539,
"totalTokenCount": 1365,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 13
}
],
"thoughtsTokenCount": 813
},
"modelVersion": "gemini-2.5-pro-preview-06-05",
"responseId": "qlJSaM6DEfCbjrEP9qLe6As"
}