C.W.K.
Stream
Lesson 04 of 04 · published

Auto Calling 과 Special Tool

~12 min · auto-calling, code-execution, google-search, grounding

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

Automatic function calling — Python 만

Python SDK 가 전체 tool loop 자동 실행 가능. tools=[...] 에 plain callable pass; SDK 가 type hint + docstring introspect, declaration 만들고 call/response loop 내부 처리. Python 만 가능. TypeScript 는 manual loop 필요.

모델한테 직접 줄 수 있는 built-in tool 두 개

Google 이 그쪽이 실행하는 tool 두 개 ship:

  • Code execution (ToolCodeExecution) — 모델이 Python 작성, Google 이 server-side 실행, 출력 반환. 수학, parsing, plotting 에 유용.
  • Google Search (GoogleSearch) — 모델이 검색 발행하고 결과 읽음. 현재 사건의 grounding.

이거 dispatcher 작성 X — Google 인프라 안에서 실행.

Tool 결합

같은 호출에 custom function declaration 과 built-in tool 섞을 수 있어. 모델이 user 의도에 맞는 거 선택.

Code

Auto-calling — SDK 가 loop 처리·python
from google import genai
from google.genai import types

client = genai.Client()

def set_light_values(brightness: int, color_temp: str) -> dict:
    """Sets brightness (0-100) and color_temp (daylight/cool/warm)."""
    return {'brightness': brightness, 'colorTemperature': color_temp}

def get_weather(location: str) -> dict:
    """Get current weather for a location."""
    return {'location': location, 'temp_c': 22, 'conditions': 'clear'}

# SDK introspects type hints + docstring, runs the entire tool loop.
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Set the lights warm and 30%, then tell me the weather in Seoul.',
    config=types.GenerateContentConfig(
        tools=[set_light_values, get_weather],
    ),
)
print(response.text)
# Both tools were called by the SDK; you got the final natural-language reply.

# Disable auto-calling and go back to manual loop:
config = types.GenerateContentConfig(
    tools=[set_light_values],
    automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True),
)
Code execution·python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='What is the sum of the first 50 prime numbers?',
    config=types.GenerateContentConfig(
        tools=[types.Tool(code_execution=types.ToolCodeExecution())],
    ),
)
print(response.text)
# The model wrote and ran Python server-side, then explained the answer.
Google Search grounding·python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Who won the 2024 European Championship in football, and what was the final score?',
    config=types.GenerateContentConfig(
        tools=[types.Tool(google_search=types.GoogleSearch())],
    ),
)
print(response.text)
# Grounded answer with citations available in response.candidates[0].grounding_metadata.
Custom + built-in tool 섞기·python
config = types.GenerateContentConfig(
    tools=[
        set_light_values,                                           # custom
        get_weather,                                                # custom
        types.Tool(code_execution=types.ToolCodeExecution()),       # built-in
        types.Tool(google_search=types.GoogleSearch()),             # built-in
    ],
)
# The model picks whichever tool fits each step of the conversation.

External links

Exercise

2 줄 프로그램 작성: get_weather(location) + convert_celsius_to_fahrenheit(c). 둘 다 auto-calling 활성화로 generate_content 에 pass. 물어: "서울 날씨 어때 + 온도 화씨로 변환?" SDK 가 tool 호출 자동 chain 했는지 확인. 그 다음 auto-calling 끄고 이전 lesson loop 로 manual 로 같은 거 — 차이 느낌.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.