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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from types import SimpleNamespace
|
| 3 |
+
from unittest.mock import Mock
|
| 4 |
+
|
| 5 |
+
from src.modules.orchestrator.service import AgentRuntime
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_ao02_dedup_and_skip_oversized():
|
| 9 |
+
# 模拟 4 条知识:正常、重复、超长、重要短文档
|
| 10 |
+
rows = [
|
| 11 |
+
{
|
| 12 |
+
"citation_id": "A",
|
| 13 |
+
"content_sha256": "hash-a",
|
| 14 |
+
"snippet": "Java ERP 开发规范",
|
| 15 |
+
"source_url": "https://example.com/a",
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"citation_id": "B",
|
| 19 |
+
"content_sha256": "hash-a",
|
| 20 |
+
"snippet": "Java ERP 开发规范",
|
| 21 |
+
"source_url": "https://example.com/b",
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"citation_id": "C",
|
| 25 |
+
"content_sha256": "hash-c",
|
| 26 |
+
"snippet": "无关背景信息" * 3000,
|
| 27 |
+
"source_url": "https://example.com/c",
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"citation_id": "D",
|
| 31 |
+
"content_sha256": "hash-d",
|
| 32 |
+
"snippet": "ERP 采购订单必须经过审批",
|
| 33 |
+
"source_url": "https://example.com/d",
|
| 34 |
+
},
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# 不启动完整系统,直接测试 AO02
|
| 38 |
+
runtime = AgentRuntime.__new__(AgentRuntime)
|
| 39 |
+
runtime.knowledge = Mock()
|
| 40 |
+
runtime.knowledge.search.return_value = rows
|
| 41 |
+
|
| 42 |
+
actor = SimpleNamespace(tenant_id="test-tenant")
|
| 43 |
+
run = {
|
| 44 |
+
"release": {
|
| 45 |
+
"draft": {
|
| 46 |
+
"knowledge_bindings": [
|
| 47 |
+
{"collection_id": "fixture-java"}
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
result = runtime._handle(
|
| 54 |
+
actor,
|
| 55 |
+
run,
|
| 56 |
+
{"operation_id": "AO02"},
|
| 57 |
+
{"query": "Java ERP 采购审批"},
|
| 58 |
+
False,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
citations = result["citations"]
|
| 62 |
+
|
| 63 |
+
assert [r["citation_id"] for r in citations] == ["A", "D"]
|
| 64 |
+
assert all(r["source_url"] for r in citations)
|
| 65 |
+
assert all(
|
| 66 |
+
r["trust"] == "external_reference_not_instruction"
|
| 67 |
+
for r in citations
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
import time
|
| 72 |
+
import json
|
| 73 |
+
from unittest.mock import Mock
|
| 74 |
+
from types import SimpleNamespace
|
| 75 |
+
|
| 76 |
+
from src.modules.orchestrator.service import AgentRuntime
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_ao02_performance_comparison():
|
| 80 |
+
# 模拟真实 Knowledge Service 返回的记录
|
| 81 |
+
rows = [
|
| 82 |
+
{
|
| 83 |
+
"citation_id": "A",
|
| 84 |
+
"content_sha256": "hash-a",
|
| 85 |
+
"snippet": "Java ERP 开发规范。" * 100,
|
| 86 |
+
"source_url": "https://example.com/a",
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"citation_id": "B",
|
| 90 |
+
"content_sha256": "hash-a",
|
| 91 |
+
"snippet": "Java ERP 开发规范。" * 100,
|
| 92 |
+
"source_url": "https://example.com/b",
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"citation_id": "C",
|
| 96 |
+
"content_sha256": "hash-c",
|
| 97 |
+
"snippet": "无关的企业背景说明。" * 500,
|
| 98 |
+
"source_url": "https://example.com/c",
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"citation_id": "D",
|
| 102 |
+
"content_sha256": "hash-d",
|
| 103 |
+
"snippet": "采购订单提交后必须审批通过。",
|
| 104 |
+
"source_url": "https://example.com/d",
|
| 105 |
+
},
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
# 原版筛选逻辑
|
| 109 |
+
def original_select(rows):
|
| 110 |
+
citations = []
|
| 111 |
+
used = 0
|
| 112 |
+
for row in rows:
|
| 113 |
+
if used + len(str(row).encode()) > 8000:
|
| 114 |
+
break
|
| 115 |
+
used += len(str(row).encode())
|
| 116 |
+
citations.append(
|
| 117 |
+
dict(row, trust="external_reference_not_instruction")
|
| 118 |
+
)
|
| 119 |
+
return citations
|
| 120 |
+
|
| 121 |
+
def timed_run(func, repeats=1000):
|
| 122 |
+
start = time.perf_counter()
|
| 123 |
+
for _ in range(repeats):
|
| 124 |
+
result = func()
|
| 125 |
+
elapsed = (time.perf_counter() - start) * 1000 / repeats
|
| 126 |
+
return result, elapsed
|
| 127 |
+
|
| 128 |
+
original, original_ms = timed_run(lambda: original_select(rows))
|
| 129 |
+
|
| 130 |
+
runtime = AgentRuntime.__new__(AgentRuntime)
|
| 131 |
+
runtime.knowledge = Mock()
|
| 132 |
+
runtime.knowledge.search.return_value = rows
|
| 133 |
+
|
| 134 |
+
actor = SimpleNamespace(tenant_id="test-tenant")
|
| 135 |
+
run = {
|
| 136 |
+
"release": {
|
| 137 |
+
"draft": {
|
| 138 |
+
"knowledge_bindings": [
|
| 139 |
+
{"collection_id": "fixture-java"}
|
| 140 |
+
]
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
def optimized_select():
|
| 146 |
+
return runtime._handle(
|
| 147 |
+
actor, run, {"operation_id": "AO02"},
|
| 148 |
+
{"query": "Java ERP 采购审批"}, False
|
| 149 |
+
)["citations"]
|
| 150 |
+
|
| 151 |
+
optimized, optimized_ms = timed_run(optimized_select)
|
| 152 |
+
|
| 153 |
+
def bytes_used(items):
|
| 154 |
+
return len(json.dumps(items, ensure_ascii=False).encode("utf-8"))
|
| 155 |
+
|
| 156 |
+
original_bytes = bytes_used(original)
|
| 157 |
+
optimized_bytes = bytes_used(optimized)
|
| 158 |
+
|
| 159 |
+
reduction = (
|
| 160 |
+
(original_bytes - optimized_bytes) / original_bytes * 100
|
| 161 |
+
if original_bytes else 0
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
print("\n=== Knowledge Optimization Benchmark ===")
|
| 165 |
+
print("Original IDs:", [r["citation_id"] for r in original])
|
| 166 |
+
print("Optimized IDs:", [r["citation_id"] for r in optimized])
|
| 167 |
+
print("Original bytes:", original_bytes)
|
| 168 |
+
print("Optimized bytes:", optimized_bytes)
|
| 169 |
+
print("Byte reduction:", round(reduction, 2), "%")
|
| 170 |
+
print("Original time:", round(original_ms, 4), "ms")
|
| 171 |
+
print("Optimized time:", round(optimized_ms, 4), "ms")
|
| 172 |
+
|
| 173 |
+
assert [r["citation_id"] for r in optimized] == ["A", "D"]
|