Spaces:
Paused
Paused
File size: 9,466 Bytes
2f6097e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | """
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)
|