diff --git a/api.py b/api.py index bfb28fd..201b724 100644 --- a/api.py +++ b/api.py @@ -1,4 +1,5 @@ from gradio_client import Client +from chat_api import * from deep_translator import GoogleTranslator @@ -67,19 +68,31 @@ def gen(text, id, model): client = Client("https://afischer1985-ai-interface.hf.space/") elif model == "0.2": client = Client("https://skier8402-mistral-super-fast.hf.space/") - iddb[str(id)] = client + if not model == "3.5": + iddb[str(id)] = client else: - client = iddb[str(id)] - - prompt = translate(text, "ru") + if not model == "3.5": + client = iddb[str(id)] success = False - while not success: - try: + try: + if model == "0.1" or model == "0.2": + prompt = translate(text, "ru") predicted = predict(prompt, client, model).replace("", "") + predicted = translate(predicted, "en") + success = True + elif model == "3.5": + # ChatGPT + global setted_models + prompt = text + try: + inst = setted_models[id] + except: + inst = None + predicted = chat_predict(prompt, id, inst) success = True - except: - pass + except: + pass fixed = predicted.replace(r'\n', '\n').replace('\\ n', '\n') - return translate(fixed, "en") + return fixed diff --git a/chat_api.py b/chat_api.py new file mode 100644 index 0000000..c69c35e --- /dev/null +++ b/chat_api.py @@ -0,0 +1,18 @@ +import g4f + +global history +history = {} + +def chat_predict(prompt, id, sys_inst = None): + global history + if id not in history: + history[id] = [{"role": "user", "content": prompt}] + else: + history[id].append({"role": "user", "content": prompt}) + if sys_inst: + history[id] = [{"role": "system", "content": sys_inst}] + + predicted = g4f.ChatCompletion.create(model="gpt-3.5-turbo", messages=history[id]) + history[id].append({"role": "assistant", "content": predicted}) + + return predicted diff --git a/minigpt.py b/minigpt.py index 640ff16..5ea6886 100755 --- a/minigpt.py +++ b/minigpt.py @@ -23,9 +23,11 @@ bot = telebot.TeleBot(API_TOKEN) def send_welcome(message): bot.reply_to(message, f"""Это подобие ChatGPT на минималках. -__ Есть 2 версии: -{telebot.formatting.hcode("/m 0.1")} - Простейшая, быстрая, краткая, не помнит что вы говорили. +__ Есть 3 версии: +{telebot.formatting.hcode("/m 0.1")} - Простейшая, быстрейшая, краткая, не помнит что вы говорили. {telebot.formatting.hcode("/m 0.2")} - Умнее, относительно быстрая, помнит что вы говорили. +{telebot.formatting.hcode("/m 3.5")} - Самая умная, есть цензура, помнит что вы говорили. +(1,2 - Mistral, 3 - ChatGPT) __ Список кратких команд: /info - /i @@ -77,7 +79,7 @@ def set_model(message): pass model = message.text.split()[1] - if model == "0.1" or model == "0.2": + if model == "0.1" or model == "0.2" or model == "3.5": setted_models[str(message.chat.id)] = model bot.reply_to(message, "Установлена новая модель 🤖") else: @@ -101,17 +103,17 @@ def clear_prompt(message): @bot.message_handler(commands=['ccontext','cc']) def clear_context(message): - global iddb + global iddb, history try: iddb.pop(str(message.chat.id)) except: pass + try: + history[id] = [] + except: + pass bot.reply_to(message, "Контекст очищен 🤖") -#@bot.message_handler(commands=['onoff']) -#def set_onoff(message): -# - @bot.message_handler(func=lambda message: True) def echo_message(message): @@ -122,12 +124,16 @@ def echo_message(message): setted_models[id] = "0.1" if id in system_prompts: - prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + message.text + if setted_models[id] != 3.5: + prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + message.text else: prompt = message.text st = bot.send_message(message.chat.id, "Печатает...") - predicted = gen(prompt, message.chat.id, setted_models[id]) + try: + predicted = gen(prompt, message.chat.id, setted_models[id]) + except: + bot.send_message(message.chat.id, "Извините, возникла непредвиденная ошибка") try: bot.reply_to(message, predicted, parse_mode="Markdown") except Exception as e: