Commit 925ab2e

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-05-06 07:07:06
feat(gpt): use HTML format if user message has entity urls
1 parent c080c15
Changed files (1)
src
src/llm/contexts.py
@@ -70,7 +70,6 @@ async def single_gpt_context(client: Client, message: Message) -> dict:
     contexts = []
     for msg in messages:
         info = parse_msg(msg, silent=True)
-        msg_text = clean_response(info["text"])
         try:
             if info["mtype"] == "photo":
                 res = await base64_media(client, msg)
@@ -94,11 +93,12 @@ async def single_gpt_context(client: Client, message: Message) -> dict:
                             "text": f"[fileowner]: {info['full_name']}\n[filename]: {info['file_name']}\n[file content]:\n{text.strip()}",
                         }
                     )
-            if msg_text:
-                if role == "user":
-                    contexts.append({"type": "text", "text": f"[username]: {info['full_name']}\n[message]:\n{msg_text}"})
-                else:
-                    contexts.append({"type": "text", "text": msg_text})
+            # user message has entity urls, use full html
+            clean_texts = clean_response(info["html"]) if role == "user" and info["entity_urls"] else clean_response(info["text"])
+            if not clean_texts:
+                continue
+            texts = f"[username]: {info['full_name']}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
+            contexts.append({"type": "text", "text": texts})
         except Exception as e:
             logger.warning(f"Download media from message failed: {e}")
             continue
@@ -129,7 +129,6 @@ async def single_gemini_context(client: Client, message: Message) -> dict:
     parts = []
     for msg in messages:
         info = parse_msg(msg, silent=True)
-        msg_text = clean_response(info["text"])
         try:
             if info["mtype"] == "photo":
                 res = await base64_media(client, msg)
@@ -146,11 +145,12 @@ async def single_gemini_context(client: Client, message: Message) -> dict:
                     text = convert_md(fpath)
                     Path(fpath).unlink(missing_ok=True)
                     parts.append(Part.from_text(text=f"[fileowner]: {info['full_name']}\n[filename]: {info['file_name']}\n[file content]:\n{text.strip()}"))
-            if msg_text:
-                if role == "user":
-                    parts.append(Part.from_text(text=f"[username]: {info['full_name']}\n[message]:\n{msg_text}"))
-                else:
-                    parts.append(Part.from_text(text=msg_text))
+            # user message has entity urls, use full html
+            clean_texts = clean_response(info["html"]) if role == "user" and info["entity_urls"] else clean_response(info["text"])
+            if not clean_texts:
+                continue
+            texts = f"[username]: {info['full_name']}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
+            parts.append(Part.from_text(text=texts))
         except Exception as e:
             logger.warning(f"Download media from message failed: {e}")
             continue