Gemini 2.5 Pro 实验版和 Gemini 2.0 Flash Thinking 实验版模型在生成回答时会使用内部“思考过程”。此过程有助于提高推理能力,让机器人能够解决复杂任务。本指南介绍了如何使用具有思考能力的 Gemini 模型。
使用思维模型
具有思考能力的模型可在 Google AI Studio 中使用,也可通过 Gemini API 使用。请注意,思考过程会显示在 Google AI Studio 中,但不会作为 API 输出的一部分提供。
发送基本请求
Python
from google import genai
client = genai.Client(api_key="GEMINI_API_KEY")
prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example."
response = client.models.generate_content(
model="gemini-2.5-pro-exp-03-25", # or gemini-2.0-flash-thinking-exp
contents=prompt
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example.";
const response = await ai.models.generateContent({
model: "gemini-2.5-pro-exp-03-25", // or gemini-2.0-flash-thinking-exp
contents: prompt,
});
console.log(response.text);
}
main();
Go
// import packages here
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-2.5-pro-exp-03-25") // or gemini-2.0-flash-thinking-exp
resp, err := model.GenerateContent(ctx, genai.Text("Explain the concept of Occam's Razor and provide a simple, everyday example."))
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Text())
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro-exp-03-25:generateContent?key=$YOUR_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."
}
]
}
]
}'
```
多轮思考对话
如需考虑之前的聊天记录,您可以使用多轮对话。
借助这些 SDK,您可以创建聊天会话来管理对话状态。
Python
from google import genai
client = genai.Client(api_key='GEMINI_API_KEY')
chat = client.aio.chats.create(
model='gemini-2.5-pro-exp-03-25', # or gemini-2.0-flash-thinking-exp
)
response = await chat.send_message('What is your name?')
print(response.text)
response = await chat.send_message('What did you just say before this?')
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const chat = ai.chats.create({
model: 'gemini-2.5-pro-exp-03-25' // or gemini-2.0-flash-thinking-exp
});
const response = await chat.sendMessage({
message: 'What is your name?'
});
console.log(response.text);
response = await chat.sendMessage({
message: 'What did you just say before this?'
});
console.log(response.text);
}
main();
后续操作
- 在 Google AI Studio 中试用 Gemini 2.5 Pro 实验版。
- 详细了解如何提示思考模型。
- 如需详细了解 Gemini 2.5 Pro 实验版和 Gemini Flash 2.0 Thinking,请参阅模型页面。
- 如需尝试更多示例,请参阅思考实战宝典。