Gemini با حفظ و افشای تاریخچهی زمینهی فراخوانیهای ابزار، امکان ترکیب ابزارهای داخلی مانند google_search و فراخوانی تابع (که به عنوان ابزارهای سفارشی نیز شناخته میشوند) را در یک تعامل واحد فراهم میکند. ترکیب ابزارهای داخلی و سفارشی، گردشهای کاری پیچیده و عاملمحور را امکانپذیر میسازد، به عنوان مثال، مدل میتواند قبل از فراخوانی منطق تجاری خاص شما، خود را در دادههای وب در لحظه مستقر کند.
در اینجا مثالی آورده شده است که ترکیب ابزارهای داخلی و سفارشی را با google_search و یک تابع سفارشی getWeather فعال میکند:
پایتون
# 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.7-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.
جاوا اسکریپت
// 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.7-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.
}
}
استراحت
# 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.7-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",