File size: 4,568 Bytes
a571bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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)