fix(market): 强化场景分类器解析兜底,避免合法 id 误判为 None
This commit is contained in:
1 parent
fe051bd9f3
commit
dc3077b5ae
1 file changed
+8
-2
@@ -253,8 +253,9 @@ def _classify_scene(name: str, description: Optional[str], content: Optional[str
|
||||
options = "、".join(f"{s['id']}({s['label']}:{s.get('desc','')})" for s in MARKET_SCENES)
|
||||
sys_prompt = (
|
||||
"你是 AI 应用市场的『技能场景分类器』。请站在使用者视角,根据应用名称、描述与内容,"
|
||||
"判断它最贴合下列哪一类【岗位 / 职能 / 业务场景】,并归类到对应 id:"
|
||||
f"{options}。\n只返回一个 JSON 对象:{{\"capability\": \"<id>\"}},不要任何额外解释或 Markdown。"
|
||||
"判断它最贴合下列哪一类【岗位 / 职能 / 业务场景】,并只输出对应英文 id:"
|
||||
f"{options}。\n必须只返回如下 JSON,且 capability 的值只能是上面列出的英文 id(小写),"
|
||||
"不要返回中文标签、不要任何解释或 Markdown 代码块:{\"capability\": \"<id>\"}"
|
||||
)
|
||||
user_text = f"名称:{name}\n描述:{description or ''}\n内容摘要:{(content or '')[:2000]}"
|
||||
messages = [
|
||||
@@ -272,9 +273,14 @@ def _classify_scene(name: str, description: Optional[str], content: Optional[str
|
||||
return None
|
||||
data = r.json()
|
||||
text_out = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
# 主解析:标准 JSON 的 capability 字段
|
||||
m = re.search(r'"capability"\s*:\s*"([a-z_]+)"', text_out)
|
||||
if m and m.group(1) in _VALID_SCENE_IDS:
|
||||
return m.group(1)
|
||||
# 兜底:在输出中扫描任一合法 id(容错标签/未加引号/多余文本等情况)
|
||||
for sid in _VALID_SCENE_IDS:
|
||||
if re.search(r'(?<![a-z])' + re.escape(sid) + r'(?![a-z])', text_out):
|
||||
return sid
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
Reference in new issue
Block a user