Botik / src /botik /gemini_client.py
AnataBak's picture
Upload folder using huggingface_hub
a571bd0 verified
Raw History Blame Contribute Delete
4.57 kB
from __future__ import annotations
import asyncio
from collections.abc import Sequence
from google import genai
from google.genai.types import GenerateContentConfig
from botik.config import FALLBACK_MODELS
from botik.storage import MessageRecord
class GeminiError(RuntimeError):
pass
class GeminiService:
def __init__(self, api_key: str) -> None:
self._client = genai.Client(api_key=api_key)
async def list_text_models(self) -> list[str]:
try:
return await asyncio.to_thread(self._list_text_models_sync)
except Exception:
return list(FALLBACK_MODELS)
async def generate_reply(
self,
model: str,
default_system_prompt: str,
custom_soul: str | None,
history: Sequence[MessageRecord],
current_author: str,
current_text: str,
reply_context: str | None,
) -> str:
system_instruction = build_system_instruction(default_system_prompt, custom_soul)
prompt = build_chat_prompt(history, current_author, current_text, reply_context)
try:
return await asyncio.to_thread(
self._generate_reply_sync,
model,
system_instruction,
prompt,
True,
)
except Exception as first_error:
try:
return await asyncio.to_thread(
self._generate_reply_sync,
model,
system_instruction,
prompt,
False,
)
except Exception as second_error:
raise GeminiError(str(second_error)) from first_error
def _list_text_models_sync(self) -> list[str]:
names: list[str] = []
for model in self._client.models.list():
name = model.name.removeprefix("models/")
if _is_text_model_name(name):
names.append(name)
return sorted(set(names)) or list(FALLBACK_MODELS)
def _generate_reply_sync(
self,
model: str,
system_instruction: str,
prompt: str,
use_tools: bool,
) -> str:
tools = [{"google_search": {}}, {"url_context": {}}] if use_tools else None
response = self._client.models.generate_content(
model=model,
contents=prompt,
config=GenerateContentConfig(
system_instruction=system_instruction,
tools=tools,
),
)
if response.text:
return response.text.strip()
return "Не смог сформулировать ответ. Попробуй перефразировать запрос."
def build_system_instruction(default_system_prompt: str, custom_soul: str | None) -> str:
if not custom_soul:
return default_system_prompt
return (
f"{default_system_prompt}\n\n"
"Дополнительная временная душа текущего чата. Она не отменяет базовые правила выше:\n"
f"{custom_soul}"
)
def build_chat_prompt(
history: Sequence[MessageRecord],
current_author: str,
current_text: str,
reply_context: str | None,
) -> str:
lines = [
"Ниже история текущего Telegram-чата. Учитывай имена и user_id участников.",
"Не путай разных людей в групповом чате.",
]
if history:
lines.append("\nИстория:")
for item in history:
if item.role == "assistant":
lines.append(f"Бот: {item.text}")
else:
author = item.user_name or f"user_id={item.user_id}"
lines.append(f"{author}: {item.text}")
if reply_context:
lines.append("\nКонтекст сообщения, на которое сейчас ответили свайпом:")
lines.append(reply_context)
lines.append("\nНовое сообщение:")
lines.append(f"{current_author}: {current_text}")
return "\n".join(lines)
def _is_text_model_name(name: str) -> bool:
lowered = name.lower()
if not lowered.startswith("gemini"):
return False
blocked = ("embedding", "imagen", "veo", "banana", "tts", "live", "robotics")
return not any(word in lowered for word in blocked)