Refactor chatbot ai plugins

Signed-off-by: Yasir Aris M <git@yasirdev.my.id>
This commit is contained in:
Yasir Aris M 2024-08-20 22:31:39 +07:00
parent a4146c1157
commit 8be4aa8f14

View file

@ -24,19 +24,32 @@ __HELP__ = """
/ask - Generate text response from AI using OpenAI. /ask - Generate text response from AI using OpenAI.
""" """
user_conversations = TTLCache(maxsize=4000, ttl=24*60*60) duckai_conversations = TTLCache(maxsize=4000, ttl=24*60*60)
gemini_conversations = TTLCache(maxsize=4000, ttl=24*60*60)
async def get_openai_stream_response(messages, bmsg): async def get_openai_stream_response(is_stream, key, base_url, model, messages, bmsg, strings):
ai = AsyncOpenAI(api_key=OPENAI_KEY, base_url="https://duckai.yasirapi.eu.org/v1") ai = AsyncOpenAI(api_key=key, base_url=base_url)
if is_stream:
response = await ai.chat.completions.create( response = await ai.chat.completions.create(
model="gpt-4o-mini", model=model,
messages=messages, messages=messages,
temperature=0.7, temperature=0.7,
stream=True, stream=True,
) )
else:
response = await ai.chat.completions.create(
extra_body={"model":model},
model=model,
messages=messages,
temperature=0.7,
)
answer = "" answer = ""
num = 0 num = 0
try: try:
if is_stream:
await bmsg.edit_msg(f"{response.choices[0].message.content}\n\n<b>Powered by:</b> <code>Gemini 1.5 Flash</code>")
answer += response.choices[0].message.content
else:
async for chunk in response: async for chunk in response:
if not chunk.choices or not chunk.choices[0].delta.content: if not chunk.choices or not chunk.choices[0].delta.content:
continue continue
@ -88,27 +101,19 @@ async def gemini_chatbot(_, ctx: Message, strings):
) )
if not GOOGLEAI_KEY: if not GOOGLEAI_KEY:
return await ctx.reply_msg("GOOGLEAI_KEY env is missing!!!") return await ctx.reply_msg("GOOGLEAI_KEY env is missing!!!")
uid = ctx.from_user.id if ctx.from_user else ctx.sender_chat.id
msg = await ctx.reply_msg(strings("find_answers_str"), quote=True) msg = await ctx.reply_msg(strings("find_answers_str"), quote=True)
try: if uid not in gemini_conversations:
data = {"query": ctx.text.split(maxsplit=1)[1], "key": GOOGLEAI_KEY, "system_instructions": "Kamu adalah AI dengan karakter mirip kucing bernama MissKaty AI yang diciptakan oleh Yasir untuk membantu manusia mencari informasi."} gemini_conversations[uid] = [{"role": "system", "content": "Kamu adalah AI dengan karakter mirip kucing bernama MissKaty AI yang diciptakan oleh Yasir untuk membantu manusia mencari informasi."}, {"role": "user", "content": ctx.input}]
# Fetch from API beacuse my VPS is not supported
response = await fetch.post("https://yasirapi.eu.org/gemini", data=data)
if not response.json().get("candidates"):
await ctx.reply_msg(
"⚠️ Sorry, the prompt you sent maybe contains a forbidden word that is not permitted by AI."
)
else: else:
await ctx.reply_msg( gemini_conversations[uid].append({"role": "user", "content": ctx.input})
html.escape( ai_response = await get_openai_stream_response(False, GOOGLEAI_KEY, "https://gemini.yasirapi.eu.org/v1", "gemini-1.5-flash", gemini_conversations[uid], msg, strings)
response.json()["candidates"][0]["content"]["parts"][0]["text"] if not ai_response:
) gemini_conversations[uid].pop()
+ "\n<b>Powered by:</b> <code>Gemini Flash 1.5</code>" if len(gemini_conversations[uid]) == 1:
) gemini_conversations.pop(uid)
await msg.delete() return
except Exception as e: gemini_conversations[uid].append({"role": "assistant", "content": ai_response})
await ctx.reply_msg(str(e))
await msg.delete()
@app.on_message(filters.command("ask", COMMAND_HANDLER) & pyro_cooldown.wait(10)) @app.on_message(filters.command("ask", COMMAND_HANDLER) & pyro_cooldown.wait(10))
@use_chat_lang() @use_chat_lang()
@ -125,14 +130,14 @@ async def openai_chatbot(self, ctx: Message, strings):
return await ctx.reply_msg(strings("dont_spam"), del_in=5) return await ctx.reply_msg(strings("dont_spam"), del_in=5)
pertanyaan = ctx.input pertanyaan = ctx.input
msg = await ctx.reply_msg(strings("find_answers_str"), quote=True) msg = await ctx.reply_msg(strings("find_answers_str"), quote=True)
if uid not in user_conversations: if uid not in duckai_conversations:
user_conversations[uid] = [{"role": "user", "content": pertanyaan}] duckai_conversations[uid] = [{"role": "system", "content": "Kamu adalah AI dengan karakter mirip kucing bernama MissKaty AI yang diciptakan oleh Yasir untuk membantu manusia mencari informasi."}, {"role": "user", "content": pertanyaan}]
else: else:
user_conversations[uid].append({"role": "user", "content": pertanyaan}) duckai_conversations[uid].append({"role": "user", "content": pertanyaan})
ai_response = await get_openai_stream_response(user_conversations[uid], msg) ai_response = await get_openai_stream_response(True, OPENAI_KEY, "https://duckai.yasirapi.eu.org/v1", "gpt-4o-mini", duckai_conversations[uid], msg, strings)
if not ai_response: if not ai_response:
user_conversations[user_id].pop() duckai_conversations[uid].pop()
if len(user_conversations[user_id]) == 1: if len(duckai_conversations[uid]) == 1:
user_conversations.pop(user_id) duckai_conversations.pop(uid)
return return
user_conversations[uid].append({"role": "assistant", "content": ai_response}) duckai_conversations[uid].append({"role": "assistant", "content": ai_response})