This commit is contained in:
wangmeihua 2025-08-13 15:50:31 +08:00
parent f7aca36265
commit c25a9c05ca

View File

@ -68,21 +68,39 @@ class APIService:
async def extract_triples(self, request, text: str, upappid: str, apiname: str, user: str) -> list:
"""调用三元组抽取服务"""
request_id = str(uuid.uuid4())
debug(f"Request #{request_id} started for triples extraction")
debug(f"Request #{request_id} started for triples extraction, text={text[:100]}")
try:
# 清理输入文本,移除控制字符
import re
text = re.sub(r'[\x00-\x1F\x7F]', '', text) # 移除 ASCII 控制字符
uapi = UAPI(request, DictObject(**globals()))
params_kw = {"text": text}
b = await uapi.call(upappid, apiname, user, params_kw)
d = json.loads(b.decode('utf-8'))
if d.get("object") != "list":
error(f"request #{request_id} invalid response format: {d}")
if not b:
error(f"request #{request_id} 三元组抽取服务返回空响应: upappid={upappid}, apiname={apiname}")
raise RuntimeError("三元组抽取服务返回空响应")
try:
response_text = b.decode('utf-8')
except UnicodeDecodeError as decode_err:
error(f"request #{request_id} 响应解码失败: {str(decode_err)}, 原始响应: {b[:100]}")
raise RuntimeError(f"响应解码失败: {str(decode_err)}")
debug(f"request #{request_id} 三元组抽取服务原始响应: {response_text[:500]}")
# 清理响应字符串,移除控制字符
response_text = re.sub(r'[\x00-\x1F\x7F]', '', response_text)
try:
d = json.loads(response_text)
except json.JSONDecodeError as json_err:
error(f"request #{request_id} JSON 解析失败: {str(json_err)}, 响应内容: {response_text[:500]}")
raise RuntimeError(f"JSON 解析失败: {str(json_err)}")
if d.get("object") != "list" or not d.get("data"):
error(f"request #{request_id} 三元组抽取服务响应格式错误: {d}")
raise RuntimeError("三元组抽取服务响应格式错误")
triples = d["data"]
debug(f"request #{request_id} extracted {len(triples)} triples")
return triples
except Exception as e:
error(f"request #{request_id} failed to extract triples: {str(e)}")
raise RuntimeError(f"三元组抽取服务调用失败: {str(e)}")
error(f"request #{request_id} failed to extract triples: {str(e)}, upappid={upappid}, apiname={apiname}")
return [] # 根据业务需求返回空列表
# 重排序服务 (BAAI/bge-reranker-v2-m3)
async def rerank_results(self, request, query: str, results: list, top_n: int, upappid: str, apiname: str, user: str) -> list: