mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
Merge pull request #18 from F4ria/gemini-error-content-extraction
feat: extracting gemini's reply text from the exception if available
This commit is contained in:
commit
e753b6ea26
@ -3,6 +3,7 @@ from pathlib import Path
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
import google.generativeai as genai
|
import google.generativeai as genai
|
||||||
|
from google.generativeai.types.generation_types import StopCandidateException
|
||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
@ -161,8 +162,23 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
if len(player.history) > 10:
|
if len(player.history) > 10:
|
||||||
player.history = player.history[2:]
|
player.history = player.history[2:]
|
||||||
player.send_message(m)
|
|
||||||
gemini_reply_text = player.last.text.strip()
|
try:
|
||||||
|
player.send_message(m)
|
||||||
|
gemini_reply_text = player.last.text.strip()
|
||||||
|
except StopCandidateException as e:
|
||||||
|
match = re.search(r'content\s*{\s*parts\s*{\s*text:\s*"([^"]+)"', str(e))
|
||||||
|
if match:
|
||||||
|
gemini_reply_text = match.group(1)
|
||||||
|
gemini_reply_text = re.sub(r"\\n", "\n", gemini_reply_text)
|
||||||
|
else:
|
||||||
|
print("No meaningful text was extracted from the exception.")
|
||||||
|
bot.reply_to(
|
||||||
|
message,
|
||||||
|
"Google gemini encountered an error while generating an answer. Please check the log.",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user