รวมเครื่องมือในตัวและการเรียกใช้ฟังก์ชัน

Gemini อนุญาตให้ใช้ร่วมกันระหว่างเครื่องมือในตัว เช่น google_search และการเรียกใช้ฟังก์ชัน (หรือที่เรียกว่าเครื่องมือที่กำหนดเอง) ในการโต้ตอบครั้งเดียวโดยการเก็บรักษาและแสดงประวัติบริบทของการเรียกใช้เครื่องมือ การผสมผสานเครื่องมือในตัวและเครื่องมือที่กำหนดเองช่วยให้มีเวิร์กโฟลว์ที่ซับซ้อนและเป็นแบบเอเจนต์ ซึ่งโมเดลสามารถอ้างอิงข้อมูลเว็บแบบเรียลไทม์ก่อนที่จะเรียกตรรกะทางธุรกิจที่เฉพาะเจาะจงของคุณได้

ตัวอย่างที่เปิดใช้การผสมผสานเครื่องมือในตัวและเครื่องมือที่กำหนดเองด้วย google_search และฟังก์ชันที่กำหนดเอง getWeather มีดังนี้

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

getWeather = {
    "type": "function",
    "name": "getWeather",
    "description": "Gets the weather for a requested city.",
    "parameters": {
        "type": "object",
        "properties": {
            "city": {
                "type": "string",
                "description": "The city and state, e.g. Utqiaġvik, Alaska",
            },
        },
        "required": ["city"],
    },
}

# The Interactions API manages context automatically across tool calls.
# The model will first use Google Search, then call getWeather.
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input="What is the northernmost city in the United States? What's the weather like there today?",
    tools=[
        {"type": "google_search"},
        getWeather,
    ],
)

# Process steps: the interaction contains search results and a function call
for step in interaction.steps:
    if step.type == "function_call":
        print(f"Function call: {step.name} with args: {step.arguments}")
        # In a real application, you would execute the function here
        # and provide the result back to the model.

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from '@google/genai';

const client = new GoogleGenAI({});

const getWeather = {
    type: "function",
    name: "getWeather",
    description: "Get the weather in a given location",
    parameters: {
        type: "object",
        properties: {
            location: {
                type: "string",
                description: "The city and state, e.g. San Francisco, CA"
            }
        },
        required: ["location"]
    }
};

// The Interactions API manages context automatically across tool calls.
// The model will first use Google Search, then call getWeather.
const interaction = await client.interactions.create({
    model: "gemini-3.8-flash",
    input: "What is the northernmost city in the United States? What's the weather like there today?",
    tools: [
        { type: "google_search" },
        getWeather,
    ],
});

// Process steps: the interaction contains search results and a function call
for (const step of interaction.steps) {
    if (step.type === "function_call") {
        console.log(`Function call: ${step.name} with args: ${JSON.stringify(step.arguments)}`);
        // In a real application, you would execute the function here
        // and provide the result back to the model.
    }
}

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Function;
import com.google.genai.gaos.models.interactions.GoogleSearch;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Client client = new Client();

Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");

Function customFunc =
    Function.builder()
        .name("get_user_location")
        .description("Retrieves user current location.")
        .parameters(parameters)
        .build();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.of("What is the weather like where I am right now?"))
        .tools(Arrays.asList(customFunc, new GoogleSearch()))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

Go

package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    customFunc := interactions.NewTool(interactions.Function{
        Name:        genai.Ptr("get_user_location"),
        Description: genai.Ptr("Retrieves user current location."),
        Parameters: map[string]any{
            "type": "object",
        },
    })

    res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model: interactions.Model("gemini-3.8-flash"),
            Input: interactions.NewInteractionsInput("What is the weather like where I am right now?"),
            Tools: []interactions.Tool{
                customFunc,
                interactions.NewTool(interactions.GoogleSearch{}),
            },
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    if res.Interaction.OutputText != nil {
        fmt.Println(*res.Interaction.OutputText)
    }
}

REST

# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
  "model": "gemini-3.8-flash",
  "input": "What is the northernmost city in the United States? What'\''s the weather like there today?",
  "tools": [
    { "type": "google_search" },
    {
      "type": "function",
      "name": "getWeather",
      "description": "Get the weather in a given location",
      "parameters": {
          "type": "object",
          "properties": {
              "location": {
                  "type": "string",
                  "description": "The city and state, e.g. San Francisco, CA"
              }
          },
          "required": ["location"]
      }
    }
  ]
}'

วิธีการทำงาน

โมเดล Gemini 3 ใช้การหมุนเวียนบริบทของเครื่องมือเพื่อเปิดใช้การผสมผสานเครื่องมือในตัวและเครื่องมือที่กำหนดเอง การหมุนเวียนบริบทของเครื่องมือช่วยให้สามารถรักษาและ แสดงบริบทของเครื่องมือในตัว รวมถึงแชร์กับเครื่องมือที่กำหนดเองในการโต้ตอบเดียวกันได้

เปิดใช้การรวมเครื่องมือ

  • ใส่ function_declarations พร้อมกับเครื่องมือในตัวที่ต้องการใช้เพื่อทริกเกอร์ลักษณะการทำงานร่วมกัน

ขั้นตอนการคืนสินค้าผ่าน API

ในการตอบกลับการโต้ตอบ API จะแสดงขั้นตอนแยกต่างหากสำหรับการเรียกเครื่องมือในตัว และการเรียกฟังก์ชัน (เครื่องมือที่กำหนดเอง)

  • ขั้นตอนเครื่องมือในตัว: API จะจัดการขั้นตอนเหล่านี้โดยอัตโนมัติเพื่อรักษาบริบทในแต่ละรอบ
  • ขั้นตอนการเรียกฟังก์ชัน: API จะแสดงผลขั้นตอน function_call สำหรับฟังก์ชันที่กำหนดเอง คุณเรียกใช้ฟังก์ชันและส่งผลลัพธ์กลับ

ฟิลด์ที่สำคัญในขั้นตอนที่แสดงผล

ฟิลด์บางรายการในขั้นตอนที่ส่งคืนมีความสำคัญต่อการรักษาบริบทของเครื่องมือและเปิดใช้การรวมเครื่องมือ

  • id: พบในขั้นตอนที่ function_call และ function_response ตัวระบุที่ไม่ซ้ำกันซึ่งแมปการเรียกไปยังการตอบกลับ
  • signature: พบในขั้นตอนที่ thought รวมถึงขั้นตอนการเรียกใช้เครื่องมือทั้งหมด (เช่น function_call) และขั้นตอนผลลัพธ์ (เช่น function_response) สำหรับโมเดล Gemini 3 ขึ้นไป บริบทที่เข้ารหัสนี้ช่วยให้การหมุนเวียนบริบทของเครื่องมือเกิดขึ้นได้ในการโต้ตอบ

การจัดการฟิลด์เหล่านี้

  • โหมด Stateful (แนะนำ): เมื่อใช้ previous_interaction_id เซิร์ฟเวอร์จะจัดการทั้งฟิลด์ id และ signature โดยอัตโนมัติ
  • โหมดไม่เก็บสถานะ: เมื่อจัดการประวัติการสนทนาด้วยตนเอง คุณต้องตรวจสอบว่าได้ส่งทั้งฟิลด์ id และ signature กลับไปยังโมเดลในคำขอที่ตามมาเพื่อตรวจสอบความถูกต้องและรักษาบริบท SDK อย่างเป็นทางการจะจัดการเรื่องนี้โดยอัตโนมัติหากคุณส่งออบเจ็กต์การตอบกลับแบบเต็มกลับไปยังประวัติ

ข้อมูลเฉพาะเครื่องมือ

เครื่องมือในตัวบางอย่างจะแสดงอาร์กิวเมนต์ข้อมูลที่ผู้ใช้มองเห็นซึ่งเฉพาะเจาะจงกับประเภทเครื่องมือ

เครื่องมือ อาร์กิวเมนต์การเรียกใช้เครื่องมือที่ผู้ใช้มองเห็น (หากมี) การตอบกลับของเครื่องมือที่ผู้ใช้มองเห็น (หากมี)
google_search queries search_suggestions
google_maps queries places
google_maps_widget_context_token
url_context urls
URL ที่จะเรียกดู
status: สถานะการเรียกดู
retrieved_url: URL ที่เรียกดู
file_search ไม่มี ไม่มี

โทเค็นและการกำหนดราคา

โปรดทราบว่าระบบจะนับรวมชิ้นส่วนการเรียกใช้เครื่องมือในตัวในคำขอเป็นส่วนหนึ่งของ prompt_token_count เนื่องจากตอนนี้คุณสามารถดูขั้นตอนเครื่องมือระดับกลางเหล่านี้และรับข้อมูลดังกล่าวได้แล้ว ขั้นตอนเหล่านี้จึงเป็นส่วนหนึ่งของประวัติการสนทนา กรณีนี้ใช้ได้เฉพาะกับคำขอเท่านั้น ไม่ใช่คำตอบ

เครื่องมือ Google Search เป็นข้อยกเว้นของกฎนี้ Google Search ใช้โมเดลราคาของตัวเองที่ระดับคำค้นหาอยู่แล้ว จึงไม่มีการเรียกเก็บค่าโทเค็นซ้ำ (ดูหน้าราคา)

ดูข้อมูลเพิ่มเติมได้ที่หน้าโทเค็น

ข้อจำกัด

  • ค่าเริ่มต้นคือโหมด validated (ไม่รองรับโหมด auto) เมื่อเปิดใช้การหมุนเวียนบริบทของเครื่องมือ
  • เครื่องมือในตัว เช่น google_search จะอิงตามข้อมูลตำแหน่งและเวลาปัจจุบัน ดังนั้นหาก system_instruction หรือ function_declaration.description มีข้อมูลตำแหน่งและเวลา ที่ขัดแย้งกัน ฟีเจอร์การรวมเครื่องมืออาจทำงานได้ไม่ดี

เครื่องมือที่รองรับ

การหมุนเวียนบริบทของเครื่องมือมาตรฐานใช้กับเครื่องมือฝั่งเซิร์ฟเวอร์ (ในตัว) การดำเนินการโค้ดเป็นเครื่องมือฝั่งเซิร์ฟเวอร์เช่นกัน แต่มีโซลูชันในตัวของตัวเองสำหรับ การหมุนเวียนบริบท การใช้คอมพิวเตอร์และการเรียกใช้ฟังก์ชันเป็นเครื่องมือฝั่งไคลเอ็นต์ และยังมีโซลูชันในตัวสำหรับการหมุนเวียนบริบทด้วย

เครื่องมือ ฝั่งที่ดำเนินการ การสนับสนุนการหมุนเวียนบริบท
Google Search ฝั่งเซิร์ฟเวอร์ รองรับ
Google Maps ฝั่งเซิร์ฟเวอร์ รองรับ
บริบท URL ฝั่งเซิร์ฟเวอร์ รองรับ
การค้นหาไฟล์ ฝั่งเซิร์ฟเวอร์ รองรับ
การรันโค้ด ฝั่งเซิร์ฟเวอร์ รองรับ (ในตัว ใช้ขั้นตอน code_execution และ code_execution_result)
การใช้คอมพิวเตอร์ ฝั่งไคลเอ็นต์ รองรับ (ในตัว ใช้ขั้นตอน function_call และ function_response)
ฟังก์ชันที่กำหนดเอง ฝั่งไคลเอ็นต์ รองรับ (ในตัว ใช้ขั้นตอน function_call และ function_response)

ขั้นตอนถัดไป