Function Calling в Gemini API
Передаём функции как tools, включаем automatic_function_calling в ChatSession, разбираем историю с FunctionCall/FunctionResponse и реализуем ручной цикл вызовов.
Создайте чат-бота с тремя инструментами: get_weather(city), convert_currency(amount, from_currency, to_currency) и calculate_tax(income, rate). Протестируйте multi-tool сценарий с одним сообщением пользователя.
Скопируйте и адаптируйте под свой контекст. Текст в треугольных скобках — то, что нужно заменить.
from google import genai
client = genai.Client(api_key=GEMINI_API_KEY)
def get_stock_price(ticker: str) -> dict:
"""Get the current stock price for a given ticker symbol."""
# Mock implementation
prices = {"GOOGL": 175.0, "AAPL": 190.5, "MSFT": 415.2}
return {"ticker": ticker, "price": prices.get(ticker, 0), "currency": "USD"}
chat = client.chats.create(
model="gemini-2.5-flash",
config={"tools": [get_stock_price]}
)
response = chat.send_message("What is the current price of Apple and Google stocks?")
print(response.text)Использование неподдерживаемых типов параметров (только int, float, bool, str, list, dict); отсутствие docstring у функции (модель не поймёт её назначение); забывать про FunctionResponse при ручном режиме.