Update chatbot/plugins/chat.py
Browse files- chatbot/plugins/chat.py +54 -0
chatbot/plugins/chat.py
CHANGED
@@ -117,6 +117,60 @@ async def rmchatbot_user(client: Client, message: Message):
|
|
117 |
await db.remove_chatbot(message.chat.id)
|
118 |
await message.reply_text("ok stopped GPT")
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
@Client.on_message(
|
121 |
filters.incoming
|
122 |
& (
|
|
|
117 |
await db.remove_chatbot(message.chat.id)
|
118 |
await message.reply_text("ok stopped GPT")
|
119 |
|
120 |
+
@Client.on_message(
|
121 |
+
~filters.scheduled
|
122 |
+
& filters.command(["ask"])
|
123 |
+
& ~filters.forwarded
|
124 |
+
)
|
125 |
+
async def rmchatbot_user(client: Client, message: Message):
|
126 |
+
chat_user = await db.get_chatbot(message.chat.id)
|
127 |
+
if not chat_user:
|
128 |
+
return
|
129 |
+
query = message.text.split(" ", 1)[1] if len(message.command) > 1 else None
|
130 |
+
|
131 |
+
pro = await message.reply("Processing your request...", quote=True)
|
132 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
|
133 |
+
await asyncio.sleep(1.5)
|
134 |
+
try:
|
135 |
+
backup_chat = await db._get_openai_chat_from_db(message.from_user.id)
|
136 |
+
backup_chat.append({"role": "system", "content": BASE_PROMPT})
|
137 |
+
backup_chat.append({"role": "user", "content": query_base})
|
138 |
+
response = await akeno.OpenAI.run(
|
139 |
+
...,
|
140 |
+
openai_meta=openai,
|
141 |
+
model="gpt-4o-mini-2024-07-18",
|
142 |
+
messages=backup_chat
|
143 |
+
)
|
144 |
+
output = response
|
145 |
+
if len(output) > 4096:
|
146 |
+
with open("chat.txt", "w+", encoding="utf8") as out_file:
|
147 |
+
out_file.write(output)
|
148 |
+
await message.reply_document(
|
149 |
+
document="chat.txt",
|
150 |
+
disable_notification=True
|
151 |
+
)
|
152 |
+
await pro.delete()
|
153 |
+
os.remove("chat.txt")
|
154 |
+
else:
|
155 |
+
await pro.edit_text(output, disable_web_page_preview=True)
|
156 |
+
backup_chat.append({"role": "assistant", "content": output})
|
157 |
+
user_detail = (
|
158 |
+
f"**Akeno GPT Bot**\n"
|
159 |
+
f"**User Username**: @{message.from_user.username if message.from_user else None}\n"
|
160 |
+
f"**User ID**: `{message.from_user.id}`\n"
|
161 |
+
f"**Chat Title**: `{message.chat.title if message.chat else None}`\n"
|
162 |
+
f"**Chat ID**: `{message.chat.id if message.chat else None}`\n"
|
163 |
+
)
|
164 |
+
response_log = await send_log(user_detail)
|
165 |
+
if response_log is None:
|
166 |
+
LOGS.warning("Error response")
|
167 |
+
LOGS.info(response_log)
|
168 |
+
await db._update_openai_chat_in_db(message.from_user.id, backup_chat)
|
169 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
|
170 |
+
return
|
171 |
+
except Exception as e:
|
172 |
+
return await pro.edit_text(f"Error: {e}")
|
173 |
+
|
174 |
@Client.on_message(
|
175 |
filters.incoming
|
176 |
& (
|