Убран ChatGPT, добавлен RWKV, изменение сообщений вместо их удаления.

main
none 7 months ago
parent 0f9829d718
commit 93e39bdb53

@ -1,8 +1,7 @@
from gradio_client import Client from gradio_client import Client
from chat_api import *
from deep_translator import GoogleTranslator from deep_translator import GoogleTranslator
# Обработка запроса генерации
def predict(prompt, client, model = "0.1"): def predict(prompt, client, model = "0.1"):
global iddb global iddb
if model == "0.1": if model == "0.1":
@ -14,23 +13,38 @@ def predict(prompt, client, model = "0.1"):
elif model == "0.2": elif model == "0.2":
result = client.predict( result = client.predict(
prompt, prompt,
0.5, # 'Temperature' 0.5, # 'Температура'
128, # 'Max new tokens' 128, # 'Длина'
0.8, # 'Top-p (nucleus sampling)' 0.8, # 'Top-p (nucleus sampling)'
1.8, # 'Repetition penalty' 1.8, # 'Repetition penalty'
api_name="/chat" api_name="/chat"
) )
elif model == "RWKV":
result = client.predict(
prompt,
333,
0.6,
0, # int | float representing numeric value between 0.0 and 1.0 in 'Top P' Slider component
0, # int | float representing numeric value between 0.0 and 1.0 in 'Presence Penalty' Slider component
0, # int | float representing numeric value between 0.0 and 1.0 in 'Count Penalty' Slider component
fn_index=0
)
else:
print("INCORRECT MODEL: ", model)
print(type(model))
return result return result
# Detect code # Определяем что это код
def iscode(text): def iscode(text):
# Теги
langs = ['sql','php','js','java','c','cpp','python','go'] langs = ['sql','php','js','java','c','cpp','python','go']
is_code = False is_code = False
for i in langs: for i in langs:
if i + r'\n' in text: if i + r'\n' in text:
is_code = True is_code = True
break break
# Тег ассемблера
spec = ['section .'] spec = ['section .']
if not is_code: if not is_code:
for i in spec: for i in spec:
@ -46,7 +60,7 @@ def translate(text, source):
elif source == "en": elif source == "en":
target = "ru" target = "ru"
# Fix code translate # Исправление перевода кода
if '```' in text: if '```' in text:
out = '' out = ''
for i in text.split('```'): for i in text.split('```'):
@ -59,39 +73,35 @@ def translate(text, source):
return out return out
# Словарь пользователь - сессия
iddb = {} iddb = {}
def gen(text, id, model): def gen(prompt, id, model):
global iddb global iddb
# Если нету сессии
if str(id) not in iddb: if str(id) not in iddb:
if model == "0.1": if model == "0.1":
client = Client("https://afischer1985-ai-interface.hf.space/") client = Client("https://afischer1985-ai-interface.hf.space/")
elif model == "0.2": elif model == "0.2":
# client = Client("https://skier8402-mistral-super-fast.hf.space/")
client = Client("https://nonamed33-minigpt-api.hf.space/") client = Client("https://nonamed33-minigpt-api.hf.space/")
# Changed to private space elif model == "RWKV":
if not model == "3.5": client = Client("https://blinkdl-rwkv-gradio-1.hf.space/")
iddb[str(id)] = client iddb[str(id)] = client
else: else:
if not model == "3.5":
client = iddb[str(id)] client = iddb[str(id)]
try: try:
# if True:
if model == "0.1" or model == "0.2": if model == "0.1" or model == "0.2":
prompt = translate(text, "ru") prompt = translate(prompt, "ru")
predicted = predict(prompt, client, model).replace("</s>", "") predicted = predict(prompt, client, model).replace("</s>", "")
predicted = translate(predicted, "en") predicted = translate(predicted, "en")
elif model == "3.5": elif model == "RWKV":
# ChatGPT predicted = predict(prompt, client, model)
global setted_models else:
prompt = text print("INCORRECT MODEL: ", model)
try: print(type(model))
inst = setted_models[id]
except:
inst = None
predicted = chat_predict(prompt, id, inst)
except: except:
pass pass

@ -27,7 +27,6 @@ def send_welcome(message):
__ Есть 3 версии: __ Есть 3 версии:
0.1 - Простейшая, быстрейшая, краткая, не помнит что вы говорили. 0.1 - Простейшая, быстрейшая, краткая, не помнит что вы говорили.
0.2 - Умнее, относительно быстрая, помнит что вы говорили. 0.2 - Умнее, относительно быстрая, помнит что вы говорили.
3.5 - Самая умная, есть цензура, помнит что вы говорили.
/m - Выбор модели /m - Выбор модели
__ Список команд: __ Список команд:
@ -77,33 +76,35 @@ System-prompt: {telebot.formatting.hcode(prompt)}
@bot.message_handler(commands=['m']) @bot.message_handler(commands=['m'])
def set_model(message): def set_model(message):
bot.send_message(message.chat.id, "Выберите новую модель:", reply_markup=gen_markup(str(message.chat.id))) mm = bot.send_message(message.chat.id, "Выберите новую модель:")
m_id = mm.id
bot.edit_message_text("Выберите новую модель:", chat_id = message.chat.id, message_id = m_id
, reply_markup=gen_markup(str(message.chat.id), m_id, message.chat.id))
def gen_markup(id): def gen_markup(id, m_id, c_id):
markup = InlineKeyboardMarkup() markup = InlineKeyboardMarkup()
markup.row_width = 3 markup.row_width = 3
markup.add(InlineKeyboardButton("0.1", callback_data=id+"_0.1"), markup.add(InlineKeyboardButton("0.1", callback_data=id+"_0.1_"+str(m_id)+"_"+str(c_id)),
InlineKeyboardButton("0.2", callback_data=id+"_0.2"), InlineKeyboardButton("0.2", callback_data=id+"_0.2_"+str(m_id)+"_"+str(c_id)),
InlineKeyboardButton("3.5", callback_data=id+"_3.5") ) InlineKeyboardButton("RWKV", callback_data=id+"_RWKV_"+str(m_id)+"_"+str(c_id)),
)
# InlineKeyboardButton("VER", callback_data=id+"_VER_"+str(m_id)+"_"+str(c_id)),
return markup return markup
@bot.callback_query_handler(func=lambda call: True) @bot.callback_query_handler(func=lambda call: True)
def callback_query(call): def callback_query(call):
global setted_models, iddb global setted_models, iddb
id = call.data.split("_")[0] ; m = call.data.split("_")[1] id, m, m_id, c_id = call.data.split("_")
m_id = int(m_id)
c_id = int(c_id)
try: try:
iddb.pop(id) iddb.pop(id)
except: except:
pass pass
if m == "0.1": setted_models[id] = m
setted_models[id] = "0.1" bot.edit_message_text(f"Успешно установлена модель {m} 🤖", chat_id = c_id, message_id = m_id)
elif m == "0.2":
setted_models[id] = "0.2"
elif m == "3.5":
setted_models[id] = "3.5"
bot.send_message(int(id), "Успешно установлена новая модель 🤖")
########################################## ##########################################
@ -138,9 +139,11 @@ def clear_context(message):
@bot.message_handler(func=lambda message: True) @bot.message_handler(func=lambda message: True)
def echo_message(message): def echo_message(message):
# Отвечаем в ЛС, либо по команде
if bot.get_chat(message.chat.id).type == "private" or message.text[:2] == "/a": if bot.get_chat(message.chat.id).type == "private" or message.text[:2] == "/a":
global setted_models, system_prompts global setted_models, system_prompts
# Текст генерации при команде
if message.text[:2] == "/a": if message.text[:2] == "/a":
text = message.text[3:] text = message.text[3:]
else: else:
@ -150,27 +153,42 @@ def echo_message(message):
if id not in setted_models: if id not in setted_models:
setted_models[id] = "0.1" setted_models[id] = "0.1"
mm = bot.send_message(message.chat.id, "Печатает...")
m_id = mm.id
# Если задана инструкция
if id in system_prompts: if id in system_prompts:
if setted_models[id] != 3.5: if setted_models[id] == "0.1" or setted_models[id] == "0.2":
prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + text prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + text
elif setted_models[id] == "RWKV":
prompt = f'''
Instruction: {system_prompts[id]}
\nInput:{text}
\nResponse:\n'''
# Если инструкция не задана
else: else:
if setted_models[id] == "0.1" or setted_models[id] == "0.2":
prompt = text prompt = text
elif setted_models[id] == "RWKV":
prompt = f'''
Input: {text}
\nResponse:\n'''
st = bot.send_message(message.chat.id, "Печатает...")
try: try:
#if 1:
predicted = gen(prompt, message.chat.id, setted_models[id]) predicted = gen(prompt, message.chat.id, setted_models[id])
except: except:
bot.send_message(message.chat.id, "Извините, возникла непредвиденная ошибка") bot.send_message(message.chat.id, "Извините, возникла непредвиденная ошибка")
try: try:
bot.reply_to(message, predicted, parse_mode="Markdown") bot.edit_message_text(predicted, chat_id=message.chat.id, message_id=m_id, parse_mode="Markdown")
except Exception as e: except Exception as e:
bot.reply_to(message, predicted, parse_mode="HTML") bot.edit_message_text(predicted, chat_id=message.chat.id, message_id=m_id, parse_mode="HTML")
logging.error(traceback.format_exc()) logging.error(traceback.format_exc())
print(predicted)
bot.delete_message(message.chat.id, st.id)
#bot.delete_message(message.chat.id, st.id)
############ ############

Loading…
Cancel
Save