Gemini מאפשר לשלב כלים מובנים, כמו google_search, וקריאה להפעלת פונקציות (שנקראות גם כלים בהתאמה אישית) באינטראקציה אחת, על ידי שמירה של היסטוריית ההקשר של בקשות להפעלת פונקציות וחשיפה שלה. שילובים של כלים מובנים ומותאמים אישית מאפשרים Agentic Workflows מורכבים. לדוגמה, המודל יכול לעגן את עצמו בנתונים מהאינטרנט בזמן אמת לפני שהוא קורא ללוגיקה העסקית הספציפית שלכם.
דוגמה להפעלת שילובים של כלים מובנים וכלים בהתאמה אישית עם 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 |
placesgoogle_maps_widget_context_token |
| url_context | urlsכתובות URL שצריך לעיין בהן |
status: סטטוס הגלישהretrieved_url: כתובות ה-URL שנסרקו |
| file_search | ללא | ללא |
טוקנים ותמחור
חשוב לזכור שחלקים של קריאות לפונקציות מובנות בבקשות נספרים כחלק מ-prompt_token_count. השלבים האלה של כלי הביניים גלויים עכשיו ומוחזרים אליכם, ולכן הם חלק מהיסטוריית השיחות. זה נכון רק לגבי בקשות, ולא לגבי תגובות.
הכלי של חיפוש Google הוא חריג לכלל הזה. בחיפוש Google כבר מוחל מודל תמחור משלו ברמת השאילתה, כך שלא מתבצע חיוב כפול על טוקנים (ראו את הדף תמחור).
מידע נוסף זמין בדף אסימונים.
מגבלות
- ברירת המחדל היא מצב
validated(אין תמיכה במצבauto) כשמחזור ההקשר של הכלי מופעל. - כלים מובנים כמו
google_searchמסתמכים על מידע על המיקום והשעה הנוכחית, כך שאם ב-system_instructionאו ב-function_declaration.descriptionיש מידע סותר על המיקום והשעה, יכול להיות שתכונת שילוב הכלים לא תפעל כמו שצריך.
כלים נתמכים
העברת ההקשר הרגילה של הכלים חלה על כלים בצד השרת (מובנים). גם הפעלת קוד היא כלי בצד השרת, אבל יש לה פתרון מובנה משלה להעברת הקשר. השימוש במחשב והפעלת הפונקציות הם כלים בצד הלקוח, ויש להם גם פתרונות מובנים להעברת הקשר.
| כלי | צד הביצוע | תמיכה בהפצת ההקשר |
|---|---|---|
| חיפוש Google | בצד השרת | כן |
| מפות Google | בצד השרת | כן |
| URL Context | בצד השרת | כן |
| חיפוש קבצים | בצד השרת | כן |
| Code Execution | בצד השרת | נתמך (מוטמע, משתמש בשלבים code_execution ו-code_execution_result) |
| שימוש במחשב | בצד הלקוח | נתמך (מובנה, משתמש בשלבים של function_call ו-function_response) |
| פונקציות מותאמות אישית | צד הלקוח | נתמך (מוטמע, משתמש בשלבים function_call ו-function_response) |
המאמרים הבאים
- מידע נוסף על בקשות להפעלת פונקציות ב-Gemini API
- הכלים הנתמכים: