From 6e92d169ecebe6b29fd4e3f543a95851ad25d74a Mon Sep 17 00:00:00 2001 From: wengzh <1337326764@qq.com> Date: Mon, 20 Jul 2026 15:51:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(get=5Fnews=5Ffrom=5Fnewsnow):=20?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E6=B5=81=E5=BC=8F=E5=A4=84=E7=90=86=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E7=BD=91=E9=A1=B5=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原有的直接转换响应内容改为流式读取字节流并传入参数,适配MarkItDown的convert_stream接口,优化大内容处理时的内存占用 --- .../plugins_func/functions/get_news_from_newsnow.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/plugins_func/functions/get_news_from_newsnow.py b/main/xiaozhi-server/plugins_func/functions/get_news_from_newsnow.py index 8f0df016..631b3027 100644 --- a/main/xiaozhi-server/plugins_func/functions/get_news_from_newsnow.py +++ b/main/xiaozhi-server/plugins_func/functions/get_news_from_newsnow.py @@ -1,6 +1,7 @@ import random import httpx -from markitdown import MarkItDown +from io import BytesIO +from markitdown import MarkItDown, StreamInfo from config.logger import setup_logging from plugins_func.register import register_function, ToolType, ActionResponse, Action from typing import TYPE_CHECKING @@ -144,7 +145,14 @@ async def fetch_news_detail(url): # 使用MarkItDown清理HTML内容 md = MarkItDown(enable_plugins=False) - result = md.convert(response) + result = md.convert_stream( + BytesIO(response.content), + stream_info=StreamInfo( + mimetype="text/html", + extension=".html", + charset=response.encoding or "utf-8", + ), + ) # 获取清理后的文本内容 clean_text = result.text_content