Spaces:
Paused
Paused
Download src/modules/orchestrator/tests/test_knowledge_selection.py from dellweb3/vibeCodingP2: direct link, hf CLI and curl.
- Browser
- Download file 5.12 kB
-
https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/src/modules/orchestrator/tests/test_knowledge_selection.py
- Command line
-
hf download hf://spaces/dellweb3/vibeCodingP2/src/modules/orchestrator/tests/test_knowledge_selection.py
-
curl -L -o test_knowledge_selection.py https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/src/modules/orchestrator/tests/test_knowledge_selection.py
5.12 kB
| from types import SimpleNamespace | |
| from unittest.mock import Mock | |
| from src.modules.orchestrator.service import AgentRuntime | |
| def test_ao02_dedup_and_skip_oversized(): | |
| # 模拟 4 条知识:正常、重复、超长、重要短文档 | |
| rows = [ | |
| { | |
| "citation_id": "A", | |
| "content_sha256": "hash-a", | |
| "snippet": "Java ERP 开发规范", | |
| "source_url": "https://example.com/a", | |
| }, | |
| { | |
| "citation_id": "B", | |
| "content_sha256": "hash-a", | |
| "snippet": "Java ERP 开发规范", | |
| "source_url": "https://example.com/b", | |
| }, | |
| { | |
| "citation_id": "C", | |
| "content_sha256": "hash-c", | |
| "snippet": "无关背景信息" * 3000, | |
| "source_url": "https://example.com/c", | |
| }, | |
| { | |
| "citation_id": "D", | |
| "content_sha256": "hash-d", | |
| "snippet": "ERP 采购订单必须经过审批", | |
| "source_url": "https://example.com/d", | |
| }, | |
| ] | |
| # 不启动完整系统,直接测试 AO02 | |
| runtime = AgentRuntime.__new__(AgentRuntime) | |
| runtime.knowledge = Mock() | |
| runtime.knowledge.search.return_value = rows | |
| actor = SimpleNamespace(tenant_id="test-tenant") | |
| run = { | |
| "release": { | |
| "draft": { | |
| "knowledge_bindings": [ | |
| {"collection_id": "fixture-java"} | |
| ] | |
| } | |
| } | |
| } | |
| result = runtime._handle( | |
| actor, | |
| run, | |
| {"operation_id": "AO02"}, | |
| {"query": "Java ERP 采购审批"}, | |
| False, | |
| ) | |
| citations = result["citations"] | |
| assert [r["citation_id"] for r in citations] == ["A", "D"] | |
| assert all(r["source_url"] for r in citations) | |
| assert all( | |
| r["trust"] == "external_reference_not_instruction" | |
| for r in citations | |
| ) | |
| import time | |
| import json | |
| from unittest.mock import Mock | |
| from types import SimpleNamespace | |
| from src.modules.orchestrator.service import AgentRuntime | |
| def test_ao02_performance_comparison(): | |
| # 模拟真实 Knowledge Service 返回的记录 | |
| rows = [ | |
| { | |
| "citation_id": "A", | |
| "content_sha256": "hash-a", | |
| "snippet": "Java ERP 开发规范。" * 100, | |
| "source_url": "https://example.com/a", | |
| }, | |
| { | |
| "citation_id": "B", | |
| "content_sha256": "hash-a", | |
| "snippet": "Java ERP 开发规范。" * 100, | |
| "source_url": "https://example.com/b", | |
| }, | |
| { | |
| "citation_id": "C", | |
| "content_sha256": "hash-c", | |
| "snippet": "无关的企业背景说明。" * 500, | |
| "source_url": "https://example.com/c", | |
| }, | |
| { | |
| "citation_id": "D", | |
| "content_sha256": "hash-d", | |
| "snippet": "采购订单提交后必须审批通过。", | |
| "source_url": "https://example.com/d", | |
| }, | |
| ] | |
| # 原版筛选逻辑 | |
| def original_select(rows): | |
| citations = [] | |
| used = 0 | |
| for row in rows: | |
| if used + len(str(row).encode()) > 8000: | |
| break | |
| used += len(str(row).encode()) | |
| citations.append( | |
| dict(row, trust="external_reference_not_instruction") | |
| ) | |
| return citations | |
| def timed_run(func, repeats=1000): | |
| start = time.perf_counter() | |
| for _ in range(repeats): | |
| result = func() | |
| elapsed = (time.perf_counter() - start) * 1000 / repeats | |
| return result, elapsed | |
| original, original_ms = timed_run(lambda: original_select(rows)) | |
| runtime = AgentRuntime.__new__(AgentRuntime) | |
| runtime.knowledge = Mock() | |
| runtime.knowledge.search.return_value = rows | |
| actor = SimpleNamespace(tenant_id="test-tenant") | |
| run = { | |
| "release": { | |
| "draft": { | |
| "knowledge_bindings": [ | |
| {"collection_id": "fixture-java"} | |
| ] | |
| } | |
| } | |
| } | |
| def optimized_select(): | |
| return runtime._handle( | |
| actor, run, {"operation_id": "AO02"}, | |
| {"query": "Java ERP 采购审批"}, False | |
| )["citations"] | |
| optimized, optimized_ms = timed_run(optimized_select) | |
| def bytes_used(items): | |
| return len(json.dumps(items, ensure_ascii=False).encode("utf-8")) | |
| original_bytes = bytes_used(original) | |
| optimized_bytes = bytes_used(optimized) | |
| reduction = ( | |
| (original_bytes - optimized_bytes) / original_bytes * 100 | |
| if original_bytes else 0 | |
| ) | |
| print("\n=== Knowledge Optimization Benchmark ===") | |
| print("Original IDs:", [r["citation_id"] for r in original]) | |
| print("Optimized IDs:", [r["citation_id"] for r in optimized]) | |
| print("Original bytes:", original_bytes) | |
| print("Optimized bytes:", optimized_bytes) | |
| print("Byte reduction:", round(reduction, 2), "%") | |
| print("Original time:", round(original_ms, 4), "ms") | |
| print("Optimized time:", round(optimized_ms, 4), "ms") | |
| assert [r["citation_id"] for r in optimized] == ["A", "D"] | |