Інтеграція 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])
Потокова передача та асинхронність
# Потокова передача
for chunk in model.generate_content("Довгий текст...", stream=True):
print(chunk.text, end="", flush=True)
# Асинхронність
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 = 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)
| Модель | Вхід (1M) | Вихід (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 тиждень







