Update Akeno/plugins/cohere.py
Browse files- Akeno/plugins/cohere.py +7 -53
Akeno/plugins/cohere.py
CHANGED
@@ -5,13 +5,13 @@ from Akeno.utils.database import db
|
|
5 |
|
6 |
from pyrogram import Client, filters, enums
|
7 |
from pyrogram.types import Message
|
8 |
-
from cohere import Client as Client_cohere
|
9 |
from config import cohere_key, CMD_HANDLER
|
|
|
10 |
|
11 |
-
|
12 |
|
13 |
@Akeno(filters.command("cohere", CMD_HANDLER) & filters.me)
|
14 |
-
async def
|
15 |
try:
|
16 |
user_id = message.from_user.id
|
17 |
chat_history = await db._get_cohere_chat_from_db(user_id)
|
@@ -26,60 +26,14 @@ async def cohere(c: Client, message: Message):
|
|
26 |
return
|
27 |
chat_history.append({"role": "USER", "message": prompt})
|
28 |
await message.edit_text("<code>Umm, lemme think...</code>")
|
29 |
-
response =
|
30 |
chat_history=chat_history,
|
31 |
model="command-r-plus",
|
32 |
-
message=prompt
|
33 |
-
temperature=0.3,
|
34 |
-
tools=[{"name": "internet_search"}],
|
35 |
-
connectors=[],
|
36 |
-
prompt_truncation="OFF",
|
37 |
)
|
38 |
-
output =
|
39 |
-
tool_message = ""
|
40 |
-
data = []
|
41 |
-
for event in response:
|
42 |
-
if event.event_type == "tool-calls-chunk":
|
43 |
-
if event.tool_call_delta and event.tool_call_delta.text is None:
|
44 |
-
tool_message += ""
|
45 |
-
else:
|
46 |
-
tool_message += event.text
|
47 |
-
if event.event_type == "search-results":
|
48 |
-
data.append(event.documents)
|
49 |
-
if event.event_type == "text-generation":
|
50 |
-
output += event.text
|
51 |
-
if output == "":
|
52 |
-
output = "I can't seem to find an answer to that"
|
53 |
chat_history.append({"role": "CHATBOT", "message": output})
|
54 |
await db._update_cohere_chat_in_db(user_id, chat_history)
|
55 |
-
await message.edit_text(
|
56 |
-
await asyncio.sleep(5)
|
57 |
-
try:
|
58 |
-
data = data[0]
|
59 |
-
references = ""
|
60 |
-
reference_dict = {}
|
61 |
-
for item in data:
|
62 |
-
title = item["title"]
|
63 |
-
url = item["url"]
|
64 |
-
if title not in reference_dict:
|
65 |
-
reference_dict[title] = url
|
66 |
-
|
67 |
-
i = 1
|
68 |
-
for title, url in reference_dict.items():
|
69 |
-
references += f"**{i}.** [{title}]({url})\n"
|
70 |
-
i += 1
|
71 |
-
|
72 |
-
await message.edit_text(
|
73 |
-
f"**Question:**`{prompt}`\n**Answer:** {output}\n\n**References:**\n{references}",
|
74 |
-
disable_web_page_preview=True
|
75 |
-
)
|
76 |
-
|
77 |
-
except IndexError:
|
78 |
-
references = ""
|
79 |
-
await message.edit_text(
|
80 |
-
f"**Question:**`{prompt}`\n**Answer:** {output}\n",
|
81 |
-
disable_web_page_preview=True
|
82 |
-
)
|
83 |
-
|
84 |
except Exception as e:
|
85 |
await message.edit_text(f"An error occurred: {e}")
|
|
|
5 |
|
6 |
from pyrogram import Client, filters, enums
|
7 |
from pyrogram.types import Message
|
|
|
8 |
from config import cohere_key, CMD_HANDLER
|
9 |
+
import cohere
|
10 |
|
11 |
+
co = cohere.Client(api_key=cohere_key)
|
12 |
|
13 |
@Akeno(filters.command("cohere", CMD_HANDLER) & filters.me)
|
14 |
+
async def coheres_(c: Client, message: Message):
|
15 |
try:
|
16 |
user_id = message.from_user.id
|
17 |
chat_history = await db._get_cohere_chat_from_db(user_id)
|
|
|
26 |
return
|
27 |
chat_history.append({"role": "USER", "message": prompt})
|
28 |
await message.edit_text("<code>Umm, lemme think...</code>")
|
29 |
+
response = co.chat(
|
30 |
chat_history=chat_history,
|
31 |
model="command-r-plus",
|
32 |
+
message=prompt
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
+
output = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
chat_history.append({"role": "CHATBOT", "message": output})
|
36 |
await db._update_cohere_chat_in_db(user_id, chat_history)
|
37 |
+
await message.edit_text(output, disable_web_page_preview=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
except Exception as e:
|
39 |
await message.edit_text(f"An error occurred: {e}")
|