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.
}
}
جاوا
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(""));
رفتن
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 این مراحل را بهطور خودکار مدیریت میکند و بافت را درطول نوبتها حفظ میکند.
- مراحل فراخوانی تابع: «میانای برنامهسازی کاربردی»
function_callمرحله برای توابع سفارشی شما برمیگرداند. شما تابع را اجرا میکنید و نتیجه را برمیگردانید.
فیلدهای مهم در مراحل برگرداندهشده
برخی فیلدها در مراحل برگرداندهشده برای حفظ بافت ابزار و فعال کردن ترکیبهای ابزار حیاتی هستند:
id: درfunction_callوfunction_responseمرحله پیدا شد. شناسه یکتایی که تماس را به پاسخ آن نگاشت میکند.signature: درthoughtمرحله، و همچنین در همه مراحل فراخوانی ابزار (برای نمونه،function_call) و نتیجه (برای نمونه،function_response) برای مدلهای Gemini 3+ پیدا شد. این بافتار رمزگذاریشده گردش بافتار ابزار را در سراسر تعاملات امکانپذیر میکند.
مدیریت این فیلدها:
- حالت با وضعیت (توصیهشده): وقتی از
previous_interaction_idاستفاده میکنید، سرور بهطور خودکار هر دو فیلدidوsignatureرا مدیریت میکند. - حالت بدون وضعیت: هنگام مدیریت دستی سابقه مکالمه، باید مطمئن شوید که هر دو فیلد
idوsignatureرا در درخواستهای بعدی به مدل برمیگردانید تا اصالت را تأیید کنید و بافت را حفظ کنید. اگر شیء پاسخ کامل را به سابقه برگردانید، کیتهای توسعه نرمافزار رسمی این کار را بهطور خودکار انجام میدهند.
دادههای مختص ابزار
برخیاز ابزارهای داخلی آرگومانهای داده قابلمشاهده برای کاربر را که مختص نوع ابزار است برمیگردانند.
| ابزار | آرگومانهای فراخوانی ابزار قابلمشاهده کاربر (درصورت وجود) | پاسخ ابزار قابلمشاهده برای کاربر (درصورت وجود) |
|---|---|---|
| google_search | queries |
search_suggestions |
| google_maps | queries |
placesgoogle_maps_widget_context_token |
| url_context | urlsنشانی وب برای مرور شدن |
status: وضعیت مرورretrieved_url: نشانیهای وب مرورشده |
| file_search | هیچکدام | هیچکدام |
کدها و قیمتگذاری
توجه داشته باشید که بخشهای فراخوانی ابزار داخلی در درخواستها برای
prompt_token_count محاسبه میشوند. ازآنجاییکه مراحل ابزار واسطه اکنون قابلمشاهده است و
به شما برگردانده میشود، این مراحل بخشی از سابقه مکالمه است. این فقط برای درخواستها صدق میکند، نه پاسخها.
ابزار «جستجوی Google» از این قانون مستثنی است. «جستجوی Google» ازقبل مدل قیمتگذاری خود را در سطح پُرسمان اعمال میکند، بنابراین هزینه توکنها دوبرابر نمیشود (صفحه قیمتگذاری را ببینید).
برای اطلاعات بیشتر، صفحه نشانها را بخوانید.
محدودیتها
- وقتی گردش زمینه ابزار فعال باشد، روی حالت
validated(حالتautoپشتیبانی نمیشود) تنظیم میشود. - ابزارهای داخلی مانند
google_searchبه اطلاعات مکان و زمان فعلی متکی هستند، بنابراین اگرsystem_instructionیاfunction_declaration.descriptionشما اطلاعات مکان و زمان متناقضی داشته باشد، ممکن است ویژگی ترکیب ابزار بهخوبی کار نکند.
ابزارهای پشتیبانیشده
گردش بافتار ابزار استاندارد برای ابزارهای سمت سرور (داخلی) اعمال میشود. «اجرای کد» نیز ابزاری در سمت سرور است، اما راهکار داخلی خودش را برای گردش بافت دارد. «استفاده از رایانه» و فراخوانی تابع ابزارهای سمت کارخواه هستند، و همچنین راهکارهای داخلی برای گردش بافت دارند.
| ابزار | سمت اجرا | پشتیبانی گردش زمینهای |
|---|---|---|
| جستجوی Google | سمت سرور | پشتیبانی میشود |
| Google Maps | سمت سرور | پشتیبانی میشود |
| بافت نشانی وب | سمت سرور | پشتیبانی میشود |
| جستجوی فایل | سمت سرور | پشتیبانی میشود |
| اجرای کد | سمت سرور | پشتیبانی میشود (توکار، از مراحل code_execution و code_execution_result استفاده میکند) |
| استفاده از رایانه | سمت کارخواه | پشتیبانی میشود (توکار، از مراحل function_call و function_response استفاده میکند) |
| توابع سفارشی | سمت کارخواه | پشتیبانی میشود (درونساخت، از function_call و function_response مرحله استفاده میکند) |
قدم بعدی چیست
- درباره فراخوانی تابع در Gemini API بیشتر بدانید.
- ابزارهای پشتیبانیشده را کاوش کنید: