fix: Long message handle and decorate waiting message

This commit is contained in:
Alter-xyz 2024-06-23 02:20:53 -04:00
parent 3913c5e7a8
commit d104201b00

View File

@ -1,5 +1,6 @@
from os import environ from os import environ
import time import time
import datetime
from telebot import TeleBot from telebot import TeleBot
from telebot.types import Message from telebot.types import Message
@ -69,30 +70,34 @@ def cohere_handler_direct(message: Message, bot: TeleBot) -> None:
prompt_truncation="AUTO", prompt_truncation="AUTO",
connectors=[{"id": "web-search"}], connectors=[{"id": "web-search"}],
citation_quality="accurate", citation_quality="accurate",
preamble=f"You are Command R+, a large language model trained to have polite, helpful, inclusive conversations with people. The current time in Tornoto is {datetime.datetime.now(datetime.timezone.utc).astimezone().strftime('%Y-%m-%d %H:%M:%S')}, in Los Angeles is {datetime.datetime.now(datetime.timezone.utc).astimezone().astimezone(datetime.timezone(datetime.timedelta(hours=-7))).strftime('%Y-%m-%d %H:%M:%S')}, and in China is {datetime.datetime.now(datetime.timezone.utc).astimezone(datetime.timezone(datetime.timedelta(hours=8))).strftime('%Y-%m-%d %H:%M:%S')}.",
) )
s = "" s = ""
source = "" source = ""
start = time.time() start = time.time()
for event in stream: for event in stream:
if event.event_type == "text-generation": if event.event_type == "stream-start":
s += event.text.encode("utf-8").decode("utf-8") bot_reply_markdown(reply_id, who, "Thinking...", bot)
if time.time() - start > 1.2: elif event.event_type == "search-queries-generation":
start = time.time() bot_reply_markdown(reply_id, who, "Searching online...", bot)
bot_reply_markdown(reply_id, who, s, bot, split_text=True)
elif event.event_type == "search-results": elif event.event_type == "search-results":
bot_reply_markdown(reply_id, who, "Reading...", bot)
for doc in event.documents: for doc in event.documents:
source += f"\n[{doc['title']}]({doc['url']})" source += f"\n[{doc['title']}]({doc['url']})"
elif event.event_type == "text-generation":
s += event.text.encode("utf-8").decode("utf-8")
if time.time() - start > 0.4:
start = time.time()
bot_reply_markdown(reply_id, who, f"\nStill thinking{len(s)}...", bot, split_text=True)
elif event.event_type == "stream-end": elif event.event_type == "stream-end":
break break
s += "\n" + source + "\n" s += "\n" + source + "\n"
if not bot_reply_markdown(reply_id, who, s, bot): try:
# maybe not complete bot_reply_markdown(reply_id, who, s, bot, split_text=True)
# maybe the same message except:
player_message.clear() pass
return
player_message.append( player_message.append(
{ {
@ -109,7 +114,7 @@ def cohere_handler_direct(message: Message, bot: TeleBot) -> None:
def cohere_handler(message: Message, bot: TeleBot) -> None: def cohere_handler(message: Message, bot: TeleBot) -> None:
"""cohere : /cohere <question>""" """cohere : /cohere <question> This will return a telegraph link"""
m = message.text.strip() m = message.text.strip()
player_message = [] player_message = []
@ -149,31 +154,36 @@ def cohere_handler(message: Message, bot: TeleBot) -> None:
prompt_truncation="AUTO", prompt_truncation="AUTO",
connectors=[{"id": "web-search"}], connectors=[{"id": "web-search"}],
citation_quality="accurate", citation_quality="accurate",
preamble=f"You are Command R+, a large language model trained to have polite, helpful, inclusive conversations with people. The current time in Tornoto is {datetime.datetime.now(datetime.timezone.utc).astimezone().strftime('%Y-%m-%d %H:%M:%S')}, in Los Angeles is {datetime.datetime.now(datetime.timezone.utc).astimezone().astimezone(datetime.timezone(datetime.timedelta(hours=-7))).strftime('%Y-%m-%d %H:%M:%S')}, and in China is {datetime.datetime.now(datetime.timezone.utc).astimezone(datetime.timezone(datetime.timedelta(hours=8))).strftime('%Y-%m-%d %H:%M:%S')}.",
) )
s = "" s = ""
source = "" source = ""
start = time.time() start = time.time()
for event in stream: for event in stream:
if event.event_type == "text-generation": if event.event_type == "stream-start":
s += event.text.encode("utf-8").decode("utf-8") bot_reply_markdown(reply_id, who, "Thinking...", bot)
if time.time() - start > 1.2: elif event.event_type == "search-queries-generation":
start = time.time() bot_reply_markdown(reply_id, who, "Searching online...", bot)
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
elif event.event_type == "search-results": elif event.event_type == "search-results":
bot_reply_markdown(reply_id, who, "Reading...", bot)
for doc in event.documents: for doc in event.documents:
source += f"\n{doc['title']}\n{doc['url']}\n" source += f"\n{doc['title']}\n{doc['url']}\n"
elif event.event_type == "text-generation":
s += event.text.encode("utf-8").decode("utf-8")
if time.time() - start > 0.4:
start = time.time()
bot_reply_markdown(reply_id, who, f"\nStill thinking{len(s)}...", bot, split_text=True)
elif event.event_type == "stream-end": elif event.event_type == "stream-end":
break break
content = s + "\n------\n------\n" + source content = s + "\n------\n------\n" + source + f"\n------\n------\nLast Update{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
ph_s = ph.create_page_md(title="Cohere", markdown_text=content) ph_s = ph.create_page_md(title="Cohere", markdown_text=content) # or edit_page with get_page so not producing massive pages
s += f"\n\n[View]({ph_s})" s += f"\n\n[View]({ph_s})"
if not bot_reply_markdown(reply_id, who, s, bot): try:
# maybe not complete bot_reply_markdown(reply_id, who, s, bot, split_text=True)
# maybe the same message except:
player_message.clear() pass
return
player_message.append( player_message.append(
{ {