forked from yym68686/ChatGPT-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
425 lines (374 loc) · 16.9 KB
/
config.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import os
import subprocess
from dotenv import load_dotenv
load_dotenv()
from utils.i18n import strings
from datetime import datetime
from ModelMerge.src.ModelMerge.utils import prompt
from ModelMerge.src.ModelMerge.models import chatgpt, claude, groq, claude3, gemini, PLUGINS, whisper
from ModelMerge.src.ModelMerge.models.base import BaseAPI
from telegram import InlineKeyboardButton
NICK = os.environ.get('NICK', None)
PORT = int(os.environ.get('PORT', '8080'))
BOT_TOKEN = os.environ.get('BOT_TOKEN', None)
GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
API_URL = os.environ.get('API_URL', 'https://api.openai.com/v1/chat/completions')
API = os.environ.get('API', None)
WEB_HOOK = os.environ.get('WEB_HOOK', None)
CHAT_MODE = os.environ.get('CHAT_MODE', "global")
GET_MODELS = (os.environ.get('GET_MODELS', "False") == "False") == False
GROQ_API_KEY = os.environ.get('GROQ_API_KEY', None)
GOOGLE_AI_API_KEY = os.environ.get('GOOGLE_AI_API_KEY', None)
PREFERENCES = {
"PASS_HISTORY" : (os.environ.get('PASS_HISTORY', "True") == "False") == False,
"IMAGEQA" : (os.environ.get('IMAGEQA', "False") == "True") == False,
"LONG_TEXT" : (os.environ.get('LONG_TEXT', "True") == "False") == False,
"LONG_TEXT_SPLIT" : (os.environ.get('LONG_TEXT_SPLIT', "True") == "False") == False,
"FILE_UPLOAD_MESS" : (os.environ.get('FILE_UPLOAD_MESS', "True") == "False") == False,
"FOLLOW_UP" : (os.environ.get('FOLLOW_UP', "False") == "False") == False,
"TITLE" : (os.environ.get('TITLE', "False") == "False") == False,
"TYPING" : (os.environ.get('TYPING', "False") == "False") == False,
"REPLY" : (os.environ.get('REPLY', "False") == "False") == False,
}
LANGUAGE = os.environ.get('LANGUAGE', 'English')
LANGUAGES = {
"English": False,
}
LANGUAGES_TO_CODE = {
"English": "en",
}
current_date = datetime.now()
Current_Date = current_date.strftime("%Y-%m-%d")
systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
claude_systemprompt = os.environ.get('SYSTEMPROMPT', prompt.claude_system_prompt.format(LANGUAGE))
class UserConfig:
def __init__(self,
user_id: str = None,
language="English",
api_url="https://api.openai.com/v1/chat/completions",
api_key=None,
engine="gpt-4o",
mode="global",
preferences=None,
plugins=None,
languages=None,
systemprompt=None,
claude_systemprompt=None
):
self.user_id = user_id
self.language = language
self.languages = languages
self.languages[self.language] = True
self.api_url = api_url
self.api_key = api_key
self.engine = engine
self.preferences = preferences
self.plugins = plugins
self.systemprompt = systemprompt
self.claude_systemprompt = claude_systemprompt
self.users = {
"global": self.get_init_preferences()
}
self.users["global"].update(self.preferences)
self.users["global"].update(self.plugins)
self.users["global"].update(self.languages)
self.mode = mode
self.parameter_name_list = list(self.users["global"].keys())
def get_init_preferences(self):
return {
"language": self.language,
"engine": self.engine,
"systemprompt": self.systemprompt,
"claude_systemprompt": self.claude_systemprompt,
"api_key": self.api_key,
"api_url": self.api_url,
}
def user_init(self, user_id = None):
if user_id == None or self.mode == "global":
user_id = "global"
self.user_id = user_id
if self.user_id not in self.users.keys():
self.users[self.user_id] = self.get_init_preferences()
self.users[self.user_id].update(self.preferences)
self.users[self.user_id].update(self.plugins)
self.users[self.user_id].update(self.languages)
def get_config(self, user_id = None, parameter_name = None):
if parameter_name not in self.parameter_name_list:
raise ValueError("parameter_name is not in the parameter_name_list")
if self.mode == "global":
return self.users["global"][parameter_name]
if self.mode == "multiusers":
self.user_init(user_id)
return self.users[self.user_id][parameter_name]
def set_config(self, user_id = None, parameter_name = None, value = None):
if parameter_name not in self.parameter_name_list:
raise ValueError("parameter_name is not in the parameter_name_list")
if self.mode == "global":
self.users["global"][parameter_name] = value
if self.mode == "multiusers":
self.user_init(user_id)
self.users[self.user_id][parameter_name] = value
def extract_plugins_config(self, user_id = None):
self.user_init(user_id)
user_data = self.users[self.user_id]
plugins_config = {key: value for key, value in user_data.items() if key in self.plugins}
return plugins_config
Users = UserConfig(mode=CHAT_MODE, api_key=API, api_url=API_URL, engine=GPT_ENGINE, preferences=PREFERENCES, plugins=PLUGINS, language=LANGUAGE, languages=LANGUAGES, systemprompt=systemprompt, claude_systemprompt=claude_systemprompt)
def get_ENGINE(user_id = None):
return Users.get_config(user_id, "engine")
temperature = float(os.environ.get('temperature', '0.5'))
CLAUDE_API = os.environ.get('claude_api_key', None)
ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot, whisperBot = None, None, None, None, None, None, None
def update_ENGINE(data = None, chat_id=None):
global Users, ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot, whisperBot
if data:
Users.set_config(chat_id, "engine", data)
engine = Users.get_config(chat_id, "engine")
systemprompt = Users.get_config(chat_id, "systemprompt")
claude_systemprompt = Users.get_config(chat_id, "claude_systemprompt")
api_key = Users.get_config(chat_id, "api_key")
api_url = Users.get_config(chat_id, "api_url")
if api_key:
if "claude" in engine:
ChatGPTbot = chatgpt(api_key=f"{api_key}", api_url=api_url, engine=engine, system_prompt=claude_systemprompt, temperature=temperature)
else:
ChatGPTbot = chatgpt(api_key=f"{api_key}", api_url=api_url, engine=engine, system_prompt=systemprompt, temperature=temperature)
SummaryBot = chatgpt(api_key=f"{api_key}", api_url=api_url, engine="gpt-3.5-turbo", system_prompt=systemprompt, temperature=temperature, use_plugins=False)
whisperBot = whisper(api_key=f"{api_key}", api_url=api_url)
if CLAUDE_API and "claude-2.1" in engine:
claudeBot = claude(api_key=f"{CLAUDE_API}", engine=engine, system_prompt=claude_systemprompt, temperature=temperature)
if CLAUDE_API and "claude-3" in engine:
claude3Bot = claude3(api_key=f"{CLAUDE_API}", engine=engine, system_prompt=claude_systemprompt, temperature=temperature)
if GROQ_API_KEY and ("mixtral" in engine or "llama" in engine):
groqBot = groq(api_key=f"{GROQ_API_KEY}", engine=engine, system_prompt=systemprompt, temperature=temperature)
if GOOGLE_AI_API_KEY and "gemini" in engine:
gemini_Bot = gemini(api_key=f"{GOOGLE_AI_API_KEY}", engine=engine, system_prompt=systemprompt, temperature=temperature)
def update_language_status(language, chat_id=None):
global Users
systemprompt = Users.get_config(chat_id, "systemprompt")
claude_systemprompt = Users.get_config(chat_id, "claude_systemprompt")
LAST_LANGUAGE = Users.get_config(chat_id, "language")
Users.set_config(chat_id, "language", language)
for lang in LANGUAGES:
Users.set_config(chat_id, lang, False)
Users.set_config(chat_id, language, True)
try:
systemprompt = systemprompt.replace(LAST_LANGUAGE, Users.get_config(chat_id, "language"))
claude_systemprompt = claude_systemprompt.replace(LAST_LANGUAGE, Users.get_config(chat_id, "language"))
Users.set_config(chat_id, "systemprompt", systemprompt)
Users.set_config(chat_id, "claude_systemprompt", claude_systemprompt)
except Exception as e:
print("error:", e)
pass
update_ENGINE(chat_id=chat_id)
update_language_status(LANGUAGE)
def get_local_version_info():
current_directory = os.path.dirname(os.path.abspath(__file__))
result = subprocess.run(['git', '-C', current_directory, 'log', '-1'], stdout=subprocess.PIPE)
output = result.stdout.decode()
return output.split('\n')[0].split(' ')[1] # 获取本地最新提交的哈希值
def get_remote_version_info():
current_directory = os.path.dirname(os.path.abspath(__file__))
result = subprocess.run(['git', '-C', current_directory, 'ls-remote', 'origin', 'HEAD'], stdout=subprocess.PIPE)
output = result.stdout.decode()
return output.split('\t')[0] # 获取远程最新提交的哈希值
def check_for_updates():
local_version = get_local_version_info()
remote_version = get_remote_version_info()
if local_version == remote_version:
return "Up to date."
else:
return "A new version is available! Please redeploy."
def replace_with_asterisk(string, start=10, end=45):
if string:
return string[:start] + '*' * (end - start - 8) + string[end:]
else:
return None
def update_info_message(user_id = None):
api_key = Users.get_config(user_id, "api_key")
api_url = Users.get_config(user_id, "api_url")
return "".join([
f"**🤖 Model:** `{get_ENGINE(user_id)}`\n\n",
f"**🔑 API:** `{replace_with_asterisk(api_key)}`\n\n" if api_key else "",
f"**🔗 API URL:** `{api_url}`\n\n" if api_url else "",
f"**🛜 WEB HOOK:** `{WEB_HOOK}`\n\n" if WEB_HOOK else "",
f"**🚰 Tokens gebrikt:** `{get_robot(user_id)[0].tokens_usage[str(user_id)]}`\n\n" if get_robot(user_id)[0] else "",
f"**🃏 Oproepnaam:** `{NICK}`\n\n" if NICK else "",
f"**📖 Version:** `{check_for_updates()}`\n\n",
])
def reset_ENGINE(chat_id, message=None):
global ChatGPTbot, claudeBot, claude3Bot, groqBot, gemini_Bot
api_key = Users.get_config(chat_id, "api_key")
api_url = Users.get_config(chat_id, "api_url")
engine = Users.get_config(chat_id, "engine")
if message:
if "claude" in engine:
Users.set_config(chat_id, "claude_systemprompt", message)
else:
Users.set_config(chat_id, "systemprompt", message)
systemprompt = Users.get_config(chat_id, "systemprompt")
claude_systemprompt = Users.get_config(chat_id, "claude_systemprompt")
if api_key and ChatGPTbot:
if "claude" in engine:
ChatGPTbot.reset(convo_id=str(chat_id), system_prompt=claude_systemprompt)
else:
ChatGPTbot.reset(convo_id=str(chat_id), system_prompt=systemprompt)
if CLAUDE_API and claudeBot:
claudeBot.reset(convo_id=str(chat_id), system_prompt=claude_systemprompt)
if CLAUDE_API and claude3Bot:
claude3Bot.reset(convo_id=str(chat_id), system_prompt=claude_systemprompt)
if GROQ_API_KEY and groqBot:
groqBot.reset(convo_id=str(chat_id), system_prompt=systemprompt)
if GOOGLE_AI_API_KEY and gemini_Bot:
gemini_Bot.reset(convo_id=str(chat_id), system_prompt=systemprompt)
def get_robot(chat_id = None):
global ChatGPTbot, claudeBot, claude3Bot, groqBot, gemini_Bot
engine = Users.get_config(chat_id, "engine")
if CLAUDE_API and "claude-2.1" in engine:
robot = claudeBot
role = "Human"
elif CLAUDE_API and "claude-3" in engine:
robot = claude3Bot
role = "user"
elif ("mixtral" in engine or "llama" in engine) and GROQ_API_KEY:
robot = groqBot
elif GOOGLE_AI_API_KEY and "gemini" in engine:
robot = gemini_Bot
role = "user"
else:
robot = ChatGPTbot
role = "user"
return robot, role
whitelist = os.environ.get('whitelist', None)
if whitelist:
whitelist = [int(id) for id in whitelist.split(",")]
ADMIN_LIST = os.environ.get('ADMIN_LIST', None)
if ADMIN_LIST:
ADMIN_LIST = [int(id) for id in ADMIN_LIST.split(",")]
GROUP_LIST = os.environ.get('GROUP_LIST', None)
if GROUP_LIST:
GROUP_LIST = [id for id in GROUP_LIST.split(",")]
def delete_model_digit_tail(lst):
if len(lst) == 2:
return "-".join(lst)
for i in range(len(lst) - 1, -1, -1):
if not lst[i].isdigit():
if i == len(lst) - 1:
return "-".join(lst)
else:
return "-".join(lst[:i + 1])
def get_status(chatid = None, item = None):
return "✅ " if Users.get_config(chatid, item) else "☑️ "
def create_buttons(strings, plugins_status=False, lang="English", button_text=None, Suffix="", chatid=None):
# 过滤出长度小于15的字符串
filtered_strings1 = [s for s in strings if len(delete_model_digit_tail(s.split("-"))) <= 14]
filtered_strings2 = [s for s in strings if len(delete_model_digit_tail(s.split("-"))) > 14]
buttons = []
temp = []
for string in filtered_strings1:
if plugins_status:
button = InlineKeyboardButton(f"{get_status(chatid, string)}{button_text[string][lang]}", callback_data=string + Suffix)
else:
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string + Suffix)
temp.append(button)
# 每两个按钮一组
if len(temp) == 2:
buttons.append(temp)
temp = []
# 如果最后一组不足两个,也添加进去
if temp:
buttons.append(temp)
for string in filtered_strings2:
if plugins_status:
button = InlineKeyboardButton(f"{get_status(chatid, string)}{button_text[string][lang]}", callback_data=string + Suffix)
else:
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string + Suffix)
buttons.append([button])
return buttons
initial_model = [
"gpt-4o",
"gpt-4o-mini-2024-07-18",
"gpt-3.5-turbo",
"claude-3-opus-20240229",
"claude-3-5-sonnet-20240620",
"claude-3-haiku-20240307",
]
if GROQ_API_KEY:
initial_model.extend([
"mixtral-8x7b-32768",
"llama3-70b-8192",
])
if GOOGLE_AI_API_KEY:
initial_model.extend([
"gemini-1.5-pro",
"gemini-1.5-flash",
])
if GET_MODELS:
try:
endpoint = BaseAPI(api_url=API_URL)
endpoint_models_url = endpoint.v1_models
import requests
response = requests.post(
endpoint_models_url,
headers={"Authorization": f"Bearer {API}"},
)
# response = requests.get(endpoint_models_url)
models = response.json()
models_list = models["data"]
models_id = [model["id"] for model in models_list]
set_models = set()
for model_item in models_id:
set_models.add(delete_model_digit_tail(model_item.split("-")))
models_id = list(set_models)
# print(models_id)
initial_model = models_id
except Exception as e:
print("error:", e)
pass
CUSTOM_MODELS = os.environ.get('CUSTOM_MODELS', None)
if CUSTOM_MODELS:
CUSTOM_MODELS_LIST = [id for id in CUSTOM_MODELS.split(",")]
else:
CUSTOM_MODELS_LIST = None
if CUSTOM_MODELS_LIST:
delete_models = [model[1:] for model in CUSTOM_MODELS_LIST if model[0] == "-"]
for target in delete_models:
for model in initial_model:
if target in model:
initial_model.remove(model)
initial_model.extend([model for model in CUSTOM_MODELS_LIST if model not in initial_model and model[0] != "-"])
def get_current_lang(chatid=None):
current_lang = Users.get_config(chatid, "language")
return LANGUAGES_TO_CODE[current_lang]
def update_models_buttons(chatid=None):
lang = get_current_lang(chatid)
buttons = create_buttons(initial_model, Suffix="_MODELS")
buttons.append(
[
InlineKeyboardButton(strings['button_back'][lang], callback_data="BACK"),
],
)
return buttons
def update_first_buttons_message(chatid=None):
lang = get_current_lang(chatid)
first_buttons = [
[
InlineKeyboardButton(strings["button_change_model"][lang], callback_data="MODELS"),
InlineKeyboardButton(strings['button_preferences'][lang], callback_data="PREFERENCES"),
],
[
InlineKeyboardButton(strings['button_language'][lang], callback_data="LANGUAGE"),
InlineKeyboardButton(strings['button_plugins'][lang], callback_data="PLUGINS"),
],
]
return first_buttons
def update_menu_buttons(setting, _strings, chatid):
lang = get_current_lang(chatid)
setting_list = list(setting.keys())
buttons = create_buttons(setting_list, plugins_status=True, lang=lang, button_text=strings, chatid=chatid, Suffix=_strings)
buttons.append(
[
InlineKeyboardButton(strings['button_back'][lang], callback_data="BACK"),
],
)
return buttons