Spaces:
Paused
Paused
Download experiments/skill_knowledge_router.py from dellweb3/vibeCodingP2: direct link, hf CLI and curl.
- Browser
- Download file 9.47 kB
-
https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/skill_knowledge_router.py
- Command line
-
hf download hf://spaces/dellweb3/vibeCodingP2/experiments/skill_knowledge_router.py
-
curl -L -o skill_knowledge_router.py https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/skill_knowledge_router.py
9.47 kB
| """ | |
| ZKCoding V0.1 | |
| Skill + Knowledge 智能推荐原型 | |
| 运行: | |
| python experiments/skill_knowledge_router.py | |
| 运行自测: | |
| python experiments/skill_knowledge_router.py --self-test | |
| """ | |
| import sys | |
| # 1. Demo 中现有的 Skill | |
| SKILLS = { | |
| "general": "普通对话(不执行工具)", | |
| "erp": "ERP 订单助手", | |
| "wms": "WMS 库存助手", | |
| "device": "设备工单助手", | |
| "java_erp": "Java 编程 · ERP 采购报价", | |
| "java_wms": "Java 编程 · WMS 库存流水", | |
| "java_device": "Java 编程 · 设备工单与巡检", | |
| "java_calculator": "Java 编程 · 网页计算器", | |
| "java_pdf": "Java 编程 · PDF读取与解析", | |
| "verified_erp": "Java 编程 · ERP 采购报价 · 已验证源码技能", | |
| } | |
| # 2. Demo 中现有的 Knowledge | |
| KNOWLEDGE = { | |
| "rules": "Java 编码规约", | |
| "basics": "Java 基础知识", | |
| "integration": "Java 集成知识", | |
| "erp": "ERP 订单知识库", | |
| "wms": "WMS 库存知识库", | |
| "device": "设备工单知识库", | |
| "sap": "SAP 接口参考", | |
| } | |
| # 3. 按业务领域整理特征词 | |
| # 分数越高,说明该词越有辨识度 | |
| DOMAIN_WORDS = { | |
| "erp": { | |
| "erp": 4, | |
| "采购报价": 5, | |
| "采购订单": 5, | |
| "供应商": 4, | |
| "采购": 3, | |
| "报价": 3, | |
| "审批": 2, | |
| "收货": 2, | |
| "付款": 2, | |
| "订单": 1, | |
| }, | |
| "wms": { | |
| "wms": 4, | |
| "库存流水": 5, | |
| "库存": 4, | |
| "仓库": 3, | |
| "入库": 4, | |
| "出库": 4, | |
| "盘点": 3, | |
| "库存余额": 5, | |
| "商品进出": 3, | |
| "剩余数量": 2, | |
| "sku": 4, | |
| }, | |
| "device": { | |
| "设备运维": 5, | |
| "设备巡检": 5, | |
| "设备工单": 5, | |
| "巡检": 4, | |
| "维修": 3, | |
| "工单": 3, | |
| "运维": 3, | |
| "故障": 2, | |
| "设备": 2, | |
| }, | |
| "calculator": { | |
| "计算器": 5, | |
| "网页计算器": 6, | |
| "四则运算": 4, | |
| }, | |
| "pdf": { | |
| "pdf": 5, | |
| "pdfbox": 5, | |
| "文档解析": 4, | |
| "文件解析": 3, | |
| "提取pdf": 5, | |
| }, | |
| } | |
| # 4. 判断用户是否需要生成代码 | |
| CODING_WORDS = [ | |
| "帮我写", "写一个", "生成代码", | |
| "编写代码", "开发一个", "实现一个", | |
| "帮我开发", "创建一个", "搭建一个", | |
| "修改代码", "生成java", "写java", | |
| "java项目", "java程序", "java网站", | |
| "spring boot项目", "编程实现", | |
| "开发", "编写", "生成代码", | |
| "实现", "写代码", "构建", | |
| ] | |
| # Java 技术问题,但不一定需要执行编程 Skill | |
| JAVA_WORDS = [ | |
| "java", "spring", "spring boot", "maven", | |
| "jdk", "junit", "jdbc", "controller", | |
| "rest api", "接口", "类名", "方法名", | |
| ] | |
| INTEGRATION_WORDS = [ | |
| "api", "rest", "http", "json", "maven", | |
| "依赖", "文件解析", "pdfbox", | |
| "集成", "接口对接", "jdbc", | |
| ] | |
| def get_domain_scores(question): | |
| """给每个业务领域打分。""" | |
| text = question.lower() | |
| scores = {} | |
| for domain, words in DOMAIN_WORDS.items(): | |
| score = 0 | |
| for word, weight in words.items(): | |
| if word.lower() in text: | |
| score += weight | |
| scores[domain] = score | |
| return scores | |
| def recommend(question): | |
| """根据问题推荐 Skill 和 Knowledge。""" | |
| text = question.strip().lower() | |
| if not text: | |
| return { | |
| "skill": None, | |
| "knowledge": [], | |
| "domain": None, | |
| "confidence": "低", | |
| "reasons": ["请先输入需求"], | |
| "needs_confirmation": True, | |
| } | |
| scores = get_domain_scores(text) | |
| # 找到最可能的业务领域 | |
| ranked = sorted( | |
| scores, | |
| key=lambda name: scores[name], | |
| reverse=True, | |
| ) | |
| domain = ranked[0] if scores[ranked[0]] > 0 else None | |
| top_score = scores[domain] if domain else 0 | |
| is_coding = any( | |
| word in text for word in CODING_WORDS | |
| ) | |
| is_java = any( | |
| word in text for word in JAVA_WORDS | |
| ) | |
| is_integration = any( | |
| word in text for word in INTEGRATION_WORDS | |
| ) | |
| reasons = [] | |
| # 5. 推荐 Skill | |
| skill = SKILLS["general"] | |
| if is_coding: | |
| coding_map = { | |
| "erp": "java_erp", | |
| "wms": "java_wms", | |
| "device": "java_device", | |
| "calculator": "java_calculator", | |
| "pdf": "java_pdf", | |
| } | |
| if domain in coding_map: | |
| skill = SKILLS[coding_map[domain]] | |
| reasons.append( | |
| "识别为编程任务,推荐对应 Java 编程 Skill" | |
| ) | |
| else: | |
| reasons.append( | |
| "识别到编程需求,但没有足够信息匹配具体 Skill" | |
| ) | |
| elif domain in ("erp", "wms", "device"): | |
| skill = SKILLS[domain] | |
| reasons.append( | |
| "识别为业务查询或业务操作,推荐业务助手" | |
| ) | |
| else: | |
| reasons.append( | |
| "暂不需要执行特定 Skill,使用普通对话" | |
| ) | |
| # 6. 推荐 Knowledge | |
| knowledge = [] | |
| def add_knowledge(key): | |
| name = KNOWLEDGE[key] | |
| if name not in knowledge: | |
| knowledge.append(name) | |
| if is_coding: | |
| add_knowledge("rules") | |
| reasons.append("编程任务需要参考 Java 编码规约") | |
| if is_java: | |
| add_knowledge("basics") | |
| if is_integration: | |
| add_knowledge("integration") | |
| # 同时涉及多个业务领域时,允许推荐多个知识库 | |
| for name in ("erp", "wms", "device"): | |
| if scores[name] > 0: | |
| add_knowledge(name) | |
| if "sap" in text: | |
| add_knowledge("sap") | |
| reasons.append("检测到 SAP 接口相关需求") | |
| # 7. 可复用的已验证源码技能 | |
| if ( | |
| domain == "erp" | |
| and is_coding | |
| and any( | |
| word in text | |
| for word in ["复用", "已验证", "已有源码"] | |
| ) | |
| ): | |
| skill = SKILLS["verified_erp"] | |
| reasons.append( | |
| "用户希望复用已验证的 ERP 源码技能" | |
| ) | |
| # 8. 判断推荐是否需要进一步确认 | |
| tied = ( | |
| domain is not None | |
| and sum( | |
| 1 for score in scores.values() | |
| if score == top_score | |
| ) > 1 | |
| ) | |
| if domain is None: | |
| confidence = "低" | |
| elif tied or top_score < 3: | |
| confidence = "低" | |
| elif top_score < 6: | |
| confidence = "中" | |
| else: | |
| confidence = "高" | |
| needs_confirmation = ( | |
| confidence != "高" | |
| or (is_coding and domain is None) | |
| ) | |
| # 目前置信度仅基于规则分数,不是统计概率 | |
| return { | |
| "skill": skill, | |
| "knowledge": knowledge, | |
| "domain": domain, | |
| "confidence": confidence, | |
| "reasons": reasons, | |
| "needs_confirmation": needs_confirmation, | |
| } | |
| def show_result(result): | |
| """展示推荐结果。""" | |
| print("\n========== ZKCoding 推荐结果 ==========") | |
| print("Skill:", result["skill"] or "暂无") | |
| print( | |
| "Knowledge:", | |
| "、".join(result["knowledge"]) or "暂无推荐", | |
| ) | |
| print("匹配领域:", result["domain"] or "未识别") | |
| print("规则匹配强度:", result["confidence"]) | |
| print("\n推荐原因:") | |
| for reason in result["reasons"]: | |
| print("-", reason) | |
| print("\n注意:建议用户确认后再执行。") | |
| print("=======================================\n") | |
| def run_tests(): | |
| """简单自测,不调用任何模型。""" | |
| cases = [ | |
| ( | |
| "帮我用 Java 开发 ERP 采购报价系统", | |
| "java_erp", | |
| "erp", | |
| ), | |
| ( | |
| "帮我写一个 WMS 库存流水接口", | |
| "java_wms", | |
| "wms", | |
| ), | |
| ( | |
| "帮我开发设备巡检工单系统", | |
| "java_device", | |
| "device", | |
| ), | |
| ( | |
| "帮我开发一个网页计算器", | |
| "java_calculator", | |
| "calculator", | |
| ), | |
| ( | |
| "帮我开发一个 PDF 解析程序", | |
| "java_pdf", | |
| "pdf", | |
| ), | |
| ( | |
| "查询采购订单的付款状态", | |
| "erp", | |
| "erp", | |
| ), | |
| ( | |
| "查询仓库库存余额", | |
| "wms", | |
| "wms", | |
| ), | |
| ( | |
| "查询设备维修工单", | |
| "device", | |
| "device", | |
| ), | |
| ] | |
| success = 0 | |
| for question, expected_skill, expected_domain in cases: | |
| result = recommend(question) | |
| expected_name = SKILLS[expected_skill] | |
| correct = ( | |
| result["skill"] == expected_name | |
| and result["domain"] == expected_domain | |
| ) | |
| print("问题:", question) | |
| print("结果:", "PASS" if correct else "FAIL") | |
| print("推荐:", result["skill"], "\n") | |
| if correct: | |
| success += 1 | |
| print(f"自测结果: {success}/{len(cases)}") | |
| return success == len(cases) | |
| if __name__ == "__main__": | |
| if "--self-test" in sys.argv: | |
| if not run_tests(): | |
| sys.exit(1) | |
| else: | |
| while True: | |
| question = input( | |
| "请输入需求(输入 exit 退出):" | |
| ).strip() | |
| if question.lower() == "exit": | |
| break | |
| result = recommend(question) | |
| show_result(result) | |