-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathgemini.py
146 lines (127 loc) · 5.23 KB
/
gemini.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import io
import time
import traceback
import sys
from PIL import Image
from telebot.types import Message
from md2tgmd import escape
from telebot import TeleBot
from config import conf, generation_config
from google import genai
gemini_draw_dict = {}
gemini_chat_dict = {}
gemini_pro_chat_dict = {}
default_model_dict = {}
model_1 = conf["model_1"]
model_2 = conf["model_2"]
error_info = conf["error_info"]
before_generate_info = conf["before_generate_info"]
download_pic_notify = conf["download_pic_notify"]
search_tool = {'google_search': {}}
client = genai.Client(api_key=sys.argv[2])
async def gemini_stream(bot:TeleBot, message:Message, m:str, model_type:str):
sent_message = None
try:
sent_message = await bot.reply_to(message, "🤖 Generating answers...")
chat = None
if model_type == model_1:
chat_dict = gemini_chat_dict
else:
chat_dict = gemini_pro_chat_dict
if str(message.from_user.id) not in chat_dict:
chat = client.aio.chats.create(model=model_type, config={'tools': [search_tool]})
chat_dict[str(message.from_user.id)] = chat
else:
chat = chat_dict[str(message.from_user.id)]
response = await chat.send_message_stream(m)
full_response = ""
last_update = time.time()
update_interval = conf["streaming_update_interval"]
async for chunk in response:
if hasattr(chunk, 'text') and chunk.text:
full_response += chunk.text
current_time = time.time()
if current_time - last_update >= update_interval:
try:
await bot.edit_message_text(
escape(full_response),
chat_id=sent_message.chat.id,
message_id=sent_message.message_id,
parse_mode="MarkdownV2"
)
except Exception as e:
if "parse markdown" in str(e).lower():
await bot.edit_message_text(
full_response,
chat_id=sent_message.chat.id,
message_id=sent_message.message_id
)
else:
if "message is not modified" not in str(e).lower():
print(f"Error updating message: {e}")
last_update = current_time
try:
await bot.edit_message_text(
escape(full_response),
chat_id=sent_message.chat.id,
message_id=sent_message.message_id,
parse_mode="MarkdownV2"
)
except Exception as e:
try:
if "parse markdown" in str(e).lower():
await bot.edit_message_text(
full_response,
chat_id=sent_message.chat.id,
message_id=sent_message.message_id
)
except Exception:
traceback.print_exc()
except Exception as e:
traceback.print_exc()
if sent_message:
await bot.edit_message_text(
f"{error_info}\nError details: {str(e)}",
chat_id=sent_message.chat.id,
message_id=sent_message.message_id
)
else:
await bot.reply_to(message, f"{error_info}\nError details: {str(e)}")
async def gemini_edit(bot: TeleBot, message: Message, m: str, photo_file: bytes):
image = Image.open(io.BytesIO(photo_file))
try:
response = await client.aio.models.generate_content(
model=model_1,
contents=[m, image],
config=generation_config
)
except Exception as e:
await bot.send_message(message.chat.id, e.str())
for part in response.candidates[0].content.parts:
if part.text is not None:
await bot.send_message(message.chat.id, escape(part.text), parse_mode="MarkdownV2")
elif part.inline_data is not None:
photo = part.inline_data.data
await bot.send_photo(message.chat.id, photo)
async def gemini_draw(bot:TeleBot, message:Message, m:str):
chat_dict = gemini_draw_dict
if str(message.from_user.id) not in chat_dict:
chat = client.aio.chats.create(
model=model_1,
config=generation_config,
)
chat_dict[str(message.from_user.id)] = chat
else:
chat = chat_dict[str(message.from_user.id)]
response = await chat.send_message(m)
for part in response.candidates[0].content.parts:
if part.text is not None:
text = part.text
while len(text) > 4000:
await bot.send_message(message.chat.id, escape(text[:4000]), parse_mode="MarkdownV2")
text = text[4000:]
if text:
await bot.send_message(message.chat.id, escape(text), parse_mode="MarkdownV2")
elif part.inline_data is not None:
photo = part.inline_data.data
await bot.send_photo(message.chat.id, photo)