feat: extracting gemini's reply text from the exception if available.

This commit is contained in:
F4ria 2023-12-25 15:32:56 +08:00
parent 0cf511b319
commit 0ddc3af8bf

View File

@ -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,18 @@ 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:
raise e
try: try:
bot.reply_to( bot.reply_to(
message, message,