From 78c89a0cceea22383ce47fe44606c83f291f3e50 Mon Sep 17 00:00:00 2001 From: ymq Date: Fri, 11 Sep 2026 22:12:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(rag=5Fclient):=20=E6=96=B0=E5=A2=9E=20rag?= =?UTF-8?q?=5Fembed=5Ftexts=20=E6=89=B9=E9=87=8F=E5=90=91=E9=87=8F?= =?UTF-8?q?=E5=8C=96=E9=80=9A=E9=81=93=E2=80=94=E2=80=94=E8=B0=83=20rag=20?= =?UTF-8?q?/api/embed.dspy,=E6=8C=8910=E6=9D=A1=E5=88=86=E5=9D=97,?= =?UTF-8?q?=E4=BB=BB=E4=B8=80=E5=9D=97=E5=A4=B1=E8=B4=A5=E6=95=B4=E4=BD=93?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=B8=A6=E5=9D=97=E5=8F=B7(=E6=8C=96?= =?UTF-8?q?=E6=8E=98=E6=89=B9=E6=AC=A1=E7=8A=B6=E6=80=81=E6=9C=BA=E9=9C=80?= =?UTF-8?q?=E5=8F=AF=E8=A1=8C=E5=8A=A8=E6=8A=A5=E9=94=99,=E7=A6=81?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=88=90=E5=8A=9F=E9=9D=99=E9=BB=98=E4=B8=A2?= =?UTF-8?q?=E6=95=B0=E6=8D=AE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipeline_service/rag_client.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pipeline_service/rag_client.py b/pipeline_service/rag_client.py index b74d4ac..fbd45d9 100644 --- a/pipeline_service/rag_client.py +++ b/pipeline_service/rag_client.py @@ -222,6 +222,30 @@ async def rag_kb_list(project_id): return await _rag_call(project_id, "kb_list.dspy", payload={}) +async def rag_embed_texts(project_id, texts, batch_size=10): + """批量文本向量化(rag 对外 API:/rag/api/embed.dspy,凭据单点在 rag)。 + + 以项目 owner 身份调用;内部按 batch_size 分块循环,返回 + (vectors 列表, '') 或 (None, 错误)。任一分块失败立即整体失败并带块号—— + 调用方(挖掘批次)需要可行动的报错,禁止部分成功静默丢数据。 + """ + if not texts: + return None, "texts 为空" + all_vecs = [] + bs = max(1, min(int(batch_size or 10), 10)) + for i in range(0, len(texts), bs): + chunk = texts[i:i + bs] + data, err = await _rag_call(project_id, "embed.dspy", payload={"texts": chunk}) + if err: + return None, "embedding 失败@块%d-%d: %s" % (i, i + len(chunk) - 1, err) + vecs = (data or {}).get("vectors") if isinstance(data, dict) else None + if not vecs or len(vecs) != len(chunk): + return None, "embedding 返回数量不符@块%d-%d(%s/%d)" % ( + i, i + len(chunk) - 1, len(vecs or []), len(chunk)) + all_vecs.extend(vecs) + return all_vecs, "" + + async def rag_doc_upload(project_id, kb_id, file_path, file_name=""): """以 owner 身份上传文件入库(API 模式:原始字节 + query 参数)。""" import os