기본 제공 도구와 함수 호출 결합
Gemini를 사용하면 도구 호출의 컨텍스트 기록을 보존하고 노출하여 단일 상호작용에서 기본 제공 도구(예: google_search)와 함수 호출(커스텀 도구라고도 함)을 결합할 수 있습니다. 기본 제공 도구와 커스텀 도구 조합을 사용하면 복잡한 에이전트형 워크플로가 가능합니다. 예를 들어 모델은 특정 비즈니스 로직을 호출하기 전에 실시간 웹 데이터를 기반으로 자체적으로 접지할 수 있습니다.
google_search 및 커스텀 함수 getWeather를 사용하여 기본 제공 도구와 커스텀 도구 조합을 사용 설정하는 예는 다음과 같습니다.
Python
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-flash-preview",
input="What is the northernmost city in the United States? What's the weather like there today?",
tools=[
{"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
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-flash-preview",
input: "What is the northernmost city in the United States? What's the weather like there today?",
tools: [
{ googleSearch: {} },
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.
}
}
REST
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-flash-preview",
"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: Gemini 3+ 모델의 모든 도구 호출 (예:function_call) 및 결과 (예:function_response) 단계뿐만 아니라thought단계에서도 찾을 수 있습니다. 이 암호화된 컨텍스트를 사용하면 상호작용 전반에서 도구 컨텍스트 순환 이 가능합니다.
이러한 필드 관리:
- 상태 저장 모드(권장):
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 컨텍스트 | 서버 측 | 지원됨 |
| 파일 검색 | 서버 측 | 지원됨 |
| 코드 실행 | 서버 측 | 지원됨 (기본 제공, code_execution 및 code_execution_result 단계 사용) |
| 컴퓨터 사용 | 클라이언트 측 | 지원됨 (기본 제공, function_call 및 function_response 단계 사용) |
| 커스텀 함수 | 클라이언트 측 | 지원됨 (기본 제공, function_call 및 function_response 단계 사용) |