Commit 937ca52

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-04-29 07:06:21
style(gpt): blockquote the gemini response
1 parent d294f98
Changed files (3)
src
src/asr/gemini_asr.py
@@ -10,14 +10,13 @@ from google.genai.types import GenerateContentConfig, HttpOptions, UploadFileCon
 from loguru import logger
 from pydantic import BaseModel
 from pyrogram.client import Client
-from pyrogram.parser.markdown import BLOCKQUOTE_EXPANDABLE_DELIM, BLOCKQUOTE_EXPANDABLE_END_DELIM
 from pyrogram.types import Message, ReplyParameters
 
 from config import ASR, TEXT_LENGTH
 from llm.gemini import parse_response
 from llm.utils import beautify_llm_response
 from messages.progress import modify_progress
-from messages.utils import count_without_entities, smart_split
+from messages.utils import blockquote, count_without_entities, smart_split
 
 
 class Transcription(BaseModel):
@@ -92,9 +91,6 @@ async def gemini_stream_asr(client: Client, message: Message, path: str | Path,
     [00:08] 今天要和大家聊一个一直以来都很有争议的话题。
     """
 
-    def warp(s: str) -> str:
-        return BLOCKQUOTE_EXPANDABLE_DELIM + s + BLOCKQUOTE_EXPANDABLE_END_DELIM
-
     path = Path(path)
     api_keys = [x.strip() for x in ASR.GEMINI_API_KEY.split(",") if x.strip()]
     transcriptions = ""
@@ -123,14 +119,14 @@ async def gemini_stream_asr(client: Client, message: Message, path: str | Path,
                     await modify_progress(message=status, text=runtime_texts, detail_progress=True)
             else:  # transcriptions is too long, split it into multiple messages
                 parts = await smart_split(runtime_texts)
-                await modify_progress(message=status, text=warp(parts[0]), force_update=True)  # force send the first part
+                await modify_progress(message=status, text=blockquote(parts[0]), force_update=True)  # force send the first part
                 runtime_texts = parts[-1]  # keep the last part
                 if not slient:
                     status = await client.send_message(message.chat.id, runtime_texts)  # the new message
                     sent_messages.append(status)
 
         # all chunks are processed
-        await modify_progress(message=status, text=warp(beautify_llm_response(runtime_texts)), force_update=True)
+        await modify_progress(message=status, text=blockquote(beautify_llm_response(runtime_texts)), force_update=True)
         if len(sent_messages) > 1:
             with io.BytesIO(transcriptions.encode("utf-8")) as f:
                 await client.send_document(message.chat.id, f, file_name="语音识别结果.txt", reply_parameters=ReplyParameters(message_id=message.id))
src/llm/gemini.py
@@ -18,7 +18,7 @@ from llm.utils import BOT_TIPS, beautify_llm_response, clean_prefix, clean_sourc
 from messages.parser import parse_msg
 from messages.progress import modify_progress
 from messages.sender import send2tg
-from messages.utils import count_without_entities, smart_split
+from messages.utils import blockquote, count_without_entities, smart_split
 from utils import number_to_emoji, rand_string
 
 HELP = f"""🌠**AI生图**
@@ -205,7 +205,7 @@ async def gemini_stream(
     **kwargs,
 ):
     prefix = f"🤖**{model_name}**: ({BOT_TIPS})\n"
-    answers = prefix
+    answers = ""
     try:
         status = kwargs.get("progress")
         api_keys = [x.strip() for x in GEMINI.API_KEYS.split(",") if x.strip()]
@@ -217,17 +217,18 @@ async def gemini_stream(
             answer = resp.get("texts", "")
             answers += answer
             answers = beautify_llm_response(answers)
-            if await count_without_entities(answers) <= TEXT_LENGTH:
-                if len(answers.removeprefix(prefix)) > 3:  # start response if answer is not empty
-                    await modify_progress(message=status, text=answers, detail_progress=True)
+            if await count_without_entities(prefix + answers) <= TEXT_LENGTH:
+                if len(answers.removeprefix(prefix)) > 10:  # start response if answer is not empty
+                    await modify_progress(message=status, text=prefix + answers, detail_progress=True)
             else:  # answers is too long, split it into multiple messages
-                parts = await smart_split(answers)
-                await modify_progress(message=status, text=parts[0], force_update=True)  # force send the first part
+                parts = await smart_split(prefix + answers)
+                await modify_progress(message=status, text=blockquote(parts[0]), force_update=True)  # force send the first part
                 answers = parts[-1]  # keep the last part
                 status = await client.send_message(message.chat.id, answers)  # the new message
 
         # all chunks are processed
-        await modify_progress(message=status, text=beautify_llm_response(answers), force_update=True)
+        answers = blockquote(beautify_llm_response(answers))  # blockquote AI response
+        await modify_progress(message=status, text=prefix + answers, force_update=True)
 
     except Exception as e:
         logger.error(e)
src/messages/utils.py
@@ -128,6 +128,11 @@ async def smart_split(text: str, chars_per_string: int = TEXT_LENGTH, mode: Pars
     return parts
 
 
+def blockquote(s: str) -> str:
+    """Block quote texts."""
+    return BLOCKQUOTE_EXPANDABLE_DELIM + s + BLOCKQUOTE_EXPANDABLE_END_DELIM
+
+
 def warp_comments(texts: str) -> str:
     texts = texts or ""
 
@@ -139,7 +144,7 @@ def warp_comments(texts: str) -> str:
     # <comments><end_delim> ... <content> ... <start_delim><comments>
     if start_idx > -1 and end_idx > -1 and end_idx < start_idx:
         texts = texts.removeprefix(BLOCKQUOTE_EXPANDABLE_DELIM).removesuffix(BLOCKQUOTE_EXPANDABLE_END_DELIM)
-        return BLOCKQUOTE_EXPANDABLE_DELIM + texts + BLOCKQUOTE_EXPANDABLE_END_DELIM
+        return blockquote(texts)
 
     # <content> ... <start_delim><comments>
     if start_idx > -1 and end_idx == -1: