From ce17c4802235a2bc9de543a3ae751e8c72655748 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 17 Jul 2026 18:12:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20build=5Fkimi=5Fcontent=20=E8=BF=81?= =?UTF-8?q?=E5=85=A5=20llimage/utils.py=EF=BC=8C=E8=87=AA=E5=90=AB=20webpa?= =?UTF-8?q?th=E2=86=92base64=20=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- llmage/utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/llmage/utils.py b/llmage/utils.py index 41f80be..5222c5f 100644 --- a/llmage/utils.py +++ b/llmage/utils.py @@ -502,3 +502,34 @@ async def llm_query_price(llmid, config_data): prices = await env.buffered_charging(llm.ppid, config_data) return prices +def build_kimi_content(prompt='', image_files=None, video_files=None): + """构造 kimi 多模态 content 数组 JSON,注册到 ServerEnv 供 uapi data 模板调用""" + import base64, json, mimetypes, os + fs = FileStorage() + parts = [] + if prompt: + parts.append({"type": "text", "text": prompt}) + for f in (image_files or []): + fp = fs.realPath(f) + if not os.path.isfile(fp): + continue + mime, _ = mimetypes.guess_type(fp) + if not mime: + mime = 'application/octet-stream' + with open(fp, 'rb') as fh: + b64 = base64.b64encode(fh.read()).decode('utf-8') + parts.append({"type": "image_url", "image_url": f'data:{mime};base64,{b64}'}) + for f in (video_files or []): + fp = fs.realPath(f) + if not os.path.isfile(fp): + continue + mime, _ = mimetypes.guess_type(fp) + if not mime: + mime = 'application/octet-stream' + with open(fp, 'rb') as fh: + b64 = base64.b64encode(fh.read()).decode('utf-8') + parts.append({"type": "video_url", "video_url": f'data:{mime};base64,{b64}'}) + return json.dumps(parts, ensure_ascii=False) + +ServerEnv().build_kimi_content = build_kimi_content +