Gemini の考え方を使用する

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();

次のステップ