mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-07-03 20:26:44 +08:00
feat: Cohere telegra.ph
- Enable Cohere web search - Output the entire LLM message with its source link on Telegraph
This commit is contained in:
@ -18,6 +18,14 @@ from . import *
|
||||
|
||||
from telegramify_markdown.customize import markdown_symbol
|
||||
|
||||
import cohere
|
||||
|
||||
COHERE_API_KEY = environ.get("COHERE_API_KEY")
|
||||
TELEGRA_PH_TOKEN = environ.get("TELEGRA_PH_TOKEN")
|
||||
co = cohere.Client(api_key=COHERE_API_KEY)
|
||||
ph = TelegraphAPI(TELEGRA_PH_TOKEN)
|
||||
COHERE_MODEL = "command-r-plus"
|
||||
|
||||
chat_message_dict = ExpiringDict(max_len=100, max_age_seconds=120)
|
||||
chat_user_dict = ExpiringDict(max_len=100, max_age_seconds=20)
|
||||
|
||||
@ -91,6 +99,7 @@ def latest_handle_messages(message: Message, bot: TeleBot):
|
||||
"sd",
|
||||
"map",
|
||||
"yi",
|
||||
"cohere",
|
||||
)
|
||||
):
|
||||
return
|
||||
@ -120,6 +129,7 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
||||
latest_message = chat_message_dict.get(chat_id)
|
||||
m = latest_message.text.strip()
|
||||
m = enrich_text_with_urls(m)
|
||||
full = ""
|
||||
##### Gemini #####
|
||||
who = "Gemini Pro"
|
||||
# show something, make it more responsible
|
||||
@ -141,6 +151,8 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
||||
convo.history.clear()
|
||||
bot_reply_markdown(reply_id, who, "Error", bot)
|
||||
|
||||
full += f"{who}:\n{s}"
|
||||
chat_id_list = [reply_id.message_id]
|
||||
##### ChatGPT #####
|
||||
who = "ChatGPT Pro"
|
||||
reply_id = bot_reply_first(latest_message, who, bot)
|
||||
@ -173,6 +185,83 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
||||
print(e)
|
||||
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||
|
||||
full += f"\n---\n{who}:\n{s}"
|
||||
chat_id_list.append(reply_id.message_id)
|
||||
|
||||
##### Cohere #####
|
||||
if COHERE_API_KEY:
|
||||
full, chat_id = cohere_answer(latest_message, bot, full, m)
|
||||
chat_id_list.append(chat_id)
|
||||
else:
|
||||
pass
|
||||
|
||||
##### Answer #####
|
||||
if TELEGRA_PH_TOKEN:
|
||||
final_answer(latest_message, bot, full, chat_id_list)
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
def cohere_answer(latest_message: Message, bot: TeleBot, full, m):
|
||||
"""cohere answer"""
|
||||
who = "Command R Plus"
|
||||
reply_id = bot_reply_first(latest_message, who, bot)
|
||||
|
||||
player_message = [{"role": "User", "message": m}]
|
||||
|
||||
try:
|
||||
r = co.chat_stream(
|
||||
model=COHERE_MODEL,
|
||||
message=m,
|
||||
temperature=0.4,
|
||||
chat_history=player_message,
|
||||
prompt_truncation="AUTO",
|
||||
connectors=[{"id": "web-search"}],
|
||||
citation_quality="fast",
|
||||
)
|
||||
s = ""
|
||||
source = ""
|
||||
start = time.time()
|
||||
for event in r:
|
||||
if event.event_type == "text-generation":
|
||||
s += event.text.encode("utf-8").decode("utf-8")
|
||||
if time.time() - start > 1.2:
|
||||
start = time.time()
|
||||
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
||||
elif event.event_type == "search-results":
|
||||
for doc in event.documents:
|
||||
source += f"\n[{doc['title']}]({doc['url']})"
|
||||
elif event.event_type == "stream-end":
|
||||
break
|
||||
|
||||
# maybe not complete
|
||||
# maybe the same message
|
||||
try:
|
||||
bot_reply_markdown(reply_id, who, s, bot)
|
||||
except:
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
bot_reply_markdown(reply_id, who, "Answer wrong", bot)
|
||||
|
||||
content = s + "\n------\n" + source
|
||||
full += f"\n---\n{who}:\n{content}"
|
||||
chat_id = reply_id.chat.id
|
||||
return full, chat_id
|
||||
|
||||
|
||||
def final_answer(latest_message: Message, bot: TeleBot, full, list):
|
||||
"""final answer"""
|
||||
who = "Answer"
|
||||
reply_id = bot_reply_first(latest_message, who, bot)
|
||||
ph_s = ph.create_page_md(title="Answer it", markdown_text=full)
|
||||
bot_reply_markdown(reply_id, who, f"[View]({ph_s})", bot)
|
||||
# delete the chat message, only leave a telegra.ph link
|
||||
|
||||
# for i in list:
|
||||
# bot.delete_message(chat_id=chat_id, message_id=i)
|
||||
|
||||
|
||||
if GOOGLE_GEMINI_KEY and CHATGPT_API_KEY:
|
||||
|
||||
|
Reference in New Issue
Block a user