Интеграция Google Gemini API: Gemini Pro, Ultra, Flash
Google Gemini — нативно мультимодальная модель: обрабатывает текст, изображения, аудио, видео и код в едином контексте. Gemini 1.5 Pro имеет контекстное окно 1 миллион токенов — уникальная характеристика для работы с большими документами. Gemini Flash — быстрый и дешёвый для высоконагруженных задач.
Базовая интеграция через Google AI SDK
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
genai.configure(api_key="GOOGLE_API_KEY")
model = genai.GenerativeModel("gemini-1.5-pro")
# Простой вызов
response = model.generate_content("Объясни квантовые вычисления")
print(response.text)
# Конфигурация генерации
response = model.generate_content(
"Анализ данных",
generation_config=genai.GenerationConfig(
temperature=0.1,
max_output_tokens=2048,
response_mime_type="application/json", # Принудительный JSON
),
)
# Мультимодальность: текст + изображение
import PIL.Image
image = PIL.Image.open("diagram.png")
response = model.generate_content(["Опиши архитектуру на схеме:", image])
# Видео анализ (уникально для Gemini)
video_file = genai.upload_file("presentation.mp4")
response = model.generate_content(["Сделай конспект видео:", video_file])
Streaming и async
# Streaming
for chunk in model.generate_content("Длинный текст...", stream=True):
print(chunk.text, end="", flush=True)
# Async
import asyncio
async def async_generate(prompt: str) -> str:
async_model = genai.GenerativeModel("gemini-1.5-flash")
response = await async_model.generate_content_async(prompt)
return response.text
Chat с историей
chat = model.start_chat(history=[])
response = chat.send_message("Привет! Меня зовут Иван.")
response = chat.send_message("Как меня зовут?")
# Модель помнит контекст из истории
Function Calling (Tool Use)
def get_stock_price(ticker: str) -> dict:
"""Возвращает цену акции"""
return {"ticker": ticker, "price": 150.0, "currency": "USD"}
tools = [get_stock_price] # Gemini принимает Python функции напрямую!
model_with_tools = genai.GenerativeModel("gemini-1.5-pro", tools=tools)
response = model_with_tools.generate_content("Какова цена Apple (AAPL)?")
Vertex AI (enterprise)
import vertexai
from vertexai.generative_models import GenerativeModel
vertexai.init(project="my-project", location="us-central1")
model = GenerativeModel("gemini-1.5-pro-preview-0514")
response = model.generate_content("Запрос")
Стоимость Gemini (2025)
| Модель | Input (1M) | Output (1M) |
|---|---|---|
| Gemini 1.5 Pro | $3.50 | $10.50 |
| Gemini 1.5 Flash | $0.075 | $0.30 |
| Gemini 1.5 Flash-8B | $0.0375 | $0.15 |
Сроки
- Базовая интеграция: 0.5–1 день
- Мультимодальные сценарии: 2–3 дня
- Vertex AI production: 1 неделя







