Commit bf3dace
Changed files (2)
src
src/llm/contexts.py
@@ -89,18 +89,17 @@ async def single_context(client: Client, message: Message) -> dict:
media = []
for msg in messages:
info = parse_msg(msg, silent=True)
+ msg_text = clean_text(info["text"])
try:
if GPT.MEDIA_FORMAT == "base64":
- res: BytesIO = await client.download_media(msg, in_memory=True) # type: ignore
- logger.debug(f"Downloaded GPT media: {res.name}")
- ext = Path(res.name).suffix.removeprefix(".").replace("jpg", "jpeg")
- b64 = base64.b64encode(res.getvalue()).decode("utf-8")
if info["mtype"] == "photo":
- media.append({"type": "image_url", "image_url": {"url": f"data:image/{ext};base64,{b64}"}})
+ res = await base64_media(client, msg)
+ media.append({"type": "image_url", "image_url": {"url": f"data:image/{res['ext']};base64,{res['base64']}"}})
# elif info["mtype"] == "video":
# media.append({"type": "video_url", "video_url": {"url": b64}})
elif info["mtype"] == "document" and info["mime_type"] == "text/plain" and not info["file_name"].startswith("GPT-Reasoning"): # skip GPT reasoning
- media.append({"type": "text", "text": res.getvalue().decode("utf-8")})
+ res = await base64_media(client, msg)
+ media.append({"type": "text", "text": res["value"]})
else:
logger.warning(f"Skip message type: {info['mtype']}")
else:
@@ -115,9 +114,19 @@ async def single_context(client: Client, message: Message) -> dict:
Path(path).unlink(missing_ok=True)
else:
logger.warning(f"Unsupported message type: {info['mtype']}")
- if texts:
- media.append({"type": "text", "text": texts})
+ if msg_text:
+ media.append({"type": "text", "text": msg_text})
except Exception as e:
logger.warning(f"Download media from message failed: {e}")
continue
return {"role": role, "content": media}
+
+
+async def base64_media(client: Client, message: Message) -> dict:
+ data: BytesIO = await client.download_media(message, in_memory=True) # type: ignore
+ logger.debug(f"Downloaded message media: {data.name}")
+ return {
+ "ext": Path(data.name).suffix.removeprefix(".").replace("jpg", "jpeg"),
+ "value": data.getvalue().decode("utf-8"),
+ "base64": base64.b64encode(data.getvalue()).decode("utf-8"),
+ }
src/llm/response.py
@@ -126,7 +126,7 @@ async def parse_response(config: dict, response: dict) -> dict[str, str]:
reasoning, content = extract_reasoning(content) # extract reasoning from content (<think>...</think>)
if not reasoning:
reasoning = glom(choice, Coalesce("message.reasoning_content", "message.reasoning"), default="") or ""
- if reasoning: # add expandable block quotation mark for reasoning
+ if reasoning and str(reasoning) != "None": # add expandable block quotation mark for reasoning
reasoning = reasoning.strip().replace("\n", "\n> ")
reasoning = f"**> 🤔{reasoning}"