mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 08:37:09 +08:00
simplify chatgpt handler functions
This commit is contained in:
parent
394dc8df99
commit
2248d649fc
@ -49,8 +49,9 @@ def chatgpt_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
who = "ChatGPT"
|
||||||
# show something, make it more responsible
|
# show something, make it more responsible
|
||||||
reply_id = bot_reply_first(message, "ChatGPT", bot)
|
reply_id = bot_reply_first(message, who, bot)
|
||||||
|
|
||||||
player_message.append({"role": "user", "content": m})
|
player_message.append({"role": "user", "content": m})
|
||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
@ -64,7 +65,7 @@ def chatgpt_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
content = r.choices[0].message.content.encode("utf8").decode()
|
content = r.choices[0].message.content.encode("utf8").decode()
|
||||||
if not content:
|
if not content:
|
||||||
chatgpt_reply_text = "chatgpt did not answer."
|
chatgpt_reply_text = f"{who} did not answer."
|
||||||
player_message.pop()
|
player_message.pop()
|
||||||
else:
|
else:
|
||||||
chatgpt_reply_text = content
|
chatgpt_reply_text = content
|
||||||
@ -77,17 +78,13 @@ def chatgpt_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
bot.reply_to(
|
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||||
message,
|
|
||||||
"ChatGPT answer:\n" + "chatgpt answer timeout",
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
# pop my user
|
# pop my user
|
||||||
player_message.pop()
|
player_message.pop()
|
||||||
return
|
return
|
||||||
|
|
||||||
# reply back as Markdown and fallback to plain text if failed.
|
# reply back as Markdown and fallback to plain text if failed.
|
||||||
bot_reply_markdown(reply_id, "ChatGPT", chatgpt_reply_text, bot)
|
bot_reply_markdown(reply_id, who, chatgpt_reply_text, bot)
|
||||||
|
|
||||||
|
|
||||||
def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
||||||
@ -110,7 +107,8 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
reply_id = bot_reply_first(message, "ChatGPT", bot)
|
who = "ChatGPT Pro"
|
||||||
|
reply_id = bot_reply_first(message, who, bot)
|
||||||
|
|
||||||
player_message.append({"role": "user", "content": m})
|
player_message.append({"role": "user", "content": m})
|
||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
@ -130,30 +128,14 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
if chunk.choices[0].delta.content is None:
|
if chunk.choices[0].delta.content is None:
|
||||||
break
|
break
|
||||||
s += chunk.choices[0].delta.content
|
s += chunk.choices[0].delta.content
|
||||||
|
|
||||||
if time.time() - start > 1.7:
|
if time.time() - start > 1.7:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
try:
|
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
||||||
bot.edit_message_text(
|
|
||||||
message_id=reply_id.message_id,
|
if not bot_reply_markdown(reply_id, who, s, bot):
|
||||||
chat_id=reply_id.chat.id,
|
|
||||||
text=convert(s),
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print(str(e))
|
|
||||||
try:
|
|
||||||
# maybe not complete
|
# maybe not complete
|
||||||
# maybe the same message
|
# maybe the same message
|
||||||
bot.edit_message_text(
|
|
||||||
message_id=reply_id.message_id,
|
|
||||||
chat_id=reply_id.chat.id,
|
|
||||||
text=convert(s),
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
player_message.clear()
|
player_message.clear()
|
||||||
print(str(e))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
player_message.append(
|
player_message.append(
|
||||||
@ -164,12 +146,8 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
bot.reply_to(
|
print(e)
|
||||||
message,
|
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||||
"chatgpt answer:\n" + "chatgpt answer timeout",
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
# pop my user
|
|
||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -177,7 +155,9 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
def chatgpt_photo_handler(message: Message, bot: TeleBot) -> None:
|
def chatgpt_photo_handler(message: Message, bot: TeleBot) -> None:
|
||||||
s = message.caption
|
s = message.caption
|
||||||
prompt = s.strip()
|
prompt = s.strip()
|
||||||
reply_id = bot_reply_first(message, "ChatGPT Vision", bot)
|
who = "ChatGPT Vision"
|
||||||
|
# show something, make it more responsible
|
||||||
|
reply_id = bot_reply_first(message, who, bot)
|
||||||
# get the high quaility picture.
|
# get the high quaility picture.
|
||||||
max_size_photo = max(message.photo, key=lambda p: p.file_size)
|
max_size_photo = max(message.photo, key=lambda p: p.file_size)
|
||||||
file_path = bot.get_file(max_size_photo.file_id).file_path
|
file_path = bot.get_file(max_size_photo.file_id).file_path
|
||||||
@ -207,45 +187,20 @@ def chatgpt_photo_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
model=CHATGPT_PRO_MODEL,
|
model=CHATGPT_PRO_MODEL,
|
||||||
stream=True,
|
stream=True,
|
||||||
)
|
)
|
||||||
s = "ChatGPT Vision answer:\n"
|
s = ""
|
||||||
start = time.time()
|
start = time.time()
|
||||||
for chunk in r:
|
for chunk in r:
|
||||||
if chunk.choices[0].delta.content is None:
|
if chunk.choices[0].delta.content is None:
|
||||||
break
|
break
|
||||||
s += chunk.choices[0].delta.content
|
s += chunk.choices[0].delta.content
|
||||||
|
|
||||||
if time.time() - start > 1.7:
|
if time.time() - start > 1.7:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
try:
|
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
||||||
bot.edit_message_text(
|
|
||||||
message_id=reply_id.message_id,
|
|
||||||
chat_id=reply_id.chat.id,
|
|
||||||
text=convert(s),
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print(str(e))
|
|
||||||
try:
|
|
||||||
# maybe not complete
|
|
||||||
# maybe the same message
|
|
||||||
bot.edit_message_text(
|
|
||||||
message_id=reply_id.message_id,
|
|
||||||
chat_id=reply_id.chat.id,
|
|
||||||
text=convert(s),
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print(str(e))
|
|
||||||
return
|
|
||||||
|
|
||||||
|
bot_reply_markdown(reply_id, who, s, bot)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(e)
|
||||||
bot.reply_to(
|
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||||
message,
|
|
||||||
"ChatGPT Vision answer:\n" + "ChatGPT Vision answer timeout",
|
|
||||||
parse_mode="MarkdownV2",
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def register(bot: TeleBot) -> None:
|
def register(bot: TeleBot) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user