Spaces:
Paused
Paused
File size: 2,567 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 |
"""
ZKCoding Knowledge + Embedding 集成实验
使用原项目的 KnowledgeCatalog 和 SQLite Repository,
然后调用自己编写的 KnowledgeReranker。
运行:
python -m experiments.knowledge_integration_test
"""
import tempfile
from pathlib import Path
from src.adapters.indexes.knowledge_sqlite import (
SqliteKnowledgeRepository,
)
from src.modules.knowledge.application.catalog import (
KnowledgeCatalog,
)
from src.modules.skill.domain.models import Actor
from experiments.knowledge_reranker import KnowledgeReranker
def main():
# 1. 创建测试用户
actor = Actor(
tenant_id="test-tenant",
user_id="developer",
roles=[],
)
# 2. 创建临时数据库
with tempfile.TemporaryDirectory() as temp_dir:
folder = Path(temp_dir)
repo = SqliteKnowledgeRepository(
folder / "index.sqlite"
)
# 3. 复用组长已有的测试数据生成函数
from src.modules.knowledge.tests.test_knowledge import (
write_fixture,
)
corpus_dir = folder / "corpus"
catalog_path = write_fixture(corpus_dir)
repo.import_corpus(
corpus_dir,
actor.tenant_id,
catalog_path,
)
# 4. 创建真正的 Knowledge Service
catalog = KnowledgeCatalog(repo)
# 5. 调用原有 SQLite FTS 检索
query = "inventory"
records = catalog.search(
actor,
query,
["fixture-java"],
)
print("\n【SQLite 检索结果】")
print("找到文档数量:", len(records))
for record in records:
print(
record["citation_id"],
record["title"],
record["source_url"],
)
# 6. 调用我们新增的 Embedding 重排序
reranker = KnowledgeReranker()
ranked = reranker.rerank(
query,
records,
)
print("\n【Embedding 重排序结果】")
for record in ranked:
print(
record["citation_id"],
"相似度:",
round(record["semantic_score"], 4),
"来源:",
record["source_url"],
)
# 7. 验证引用信息没有丢失
assert len(ranked) == len(records)
assert all(
r["source_url"] for r in ranked
)
print("\n集成测试通过:搜索与重排序已连接!")
if __name__ == "__main__":
main()
|