From 86865a1a28c206d944e771f738db9c3edfec539e Mon Sep 17 00:00:00 2001 From: Yasir Aris M Date: Sun, 1 Sep 2024 12:59:21 +0700 Subject: [PATCH] try fix msg too long --- misskaty/plugins/chatbot_ai.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/misskaty/plugins/chatbot_ai.py b/misskaty/plugins/chatbot_ai.py index cc6d12ab..368ddd58 100644 --- a/misskaty/plugins/chatbot_ai.py +++ b/misskaty/plugins/chatbot_ai.py @@ -38,25 +38,33 @@ async def get_openai_stream_response(is_stream, key, base_url, model, messages, stream=is_stream, ) if not is_stream: - await bmsg.edit_msg(f"{html.escape(response.choices[0].message.content)}\nPowered by: Gemini 1.5 Flash") answer += response.choices[0].message.content + if len(answer) > 4000: + answerlink = await privatebinapi.send_async("https://bin.yasirweb.eu.org", text=answer, expiration="1week", formatting="markdown") + await bmsg.edit_msg( + strings("answers_too_long").format(answerlink=answerlink.get("full_url")), + disable_web_page_preview=True, + ) + else: + await bmsg.edit_msg(f"{html.escape(answer)}\nPowered by: Gemini 1.5 Flash") else: async for chunk in response: if not chunk.choices or not chunk.choices[0].delta.content: continue num += 1 answer += chunk.choices[0].delta.content - if num == 30: + if num == 30 and len(answer) < 4000: await bmsg.edit_msg(html.escape(answer)) await asyncio.sleep(1.5) num = 0 - await bmsg.edit_msg(f"{html.escape(answer)}\n\nPowered by: GPT 4o") - except MessageTooLong: - answerlink = await privatebinapi.send_async("https://bin.yasirweb.eu.org", text=answer, expiration="1week", formatting="markdown") - await bmsg.edit_msg( - strings("answers_too_long").format(answerlink=answerlink.get("full_url")), - disable_web_page_preview=True, - ) + if len(answer) > 4000: + answerlink = await privatebinapi.send_async("https://bin.yasirweb.eu.org", text=answer, expiration="1week", formatting="markdown") + await bmsg.edit_msg( + strings("answers_too_long").format(answerlink=answerlink.get("full_url")), + disable_web_page_preview=True, + ) + else: + await bmsg.edit_msg(f"{html.escape(answer)}\n\nPowered by: GPT 4o") except APIConnectionError as e: await bmsg.edit_msg(f"The server could not be reached because {e.__cause__}") return None