Spaces:
Paused
Paused
Download experiments/knowledge_integration_test.py from dellweb3/vibeCodingP2: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/knowledge_integration_test.py
- Command line
-
hf download hf://spaces/dellweb3/vibeCodingP2/experiments/knowledge_integration_test.py
-
curl -L -o knowledge_integration_test.py https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/knowledge_integration_test.py
2.57 kB
| """ | |
| 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() | |