refactor: 客服agent的切片结构调整重构

This commit is contained in:
2026-09-11 17:31:16 +08:00
parent b1b764eac9
commit e561c97a4e
20 changed files with 3309 additions and 230 deletions
+31 -40
View File
@@ -5,6 +5,7 @@ import time
from pathlib import Path
from unittest.mock import AsyncMock
from rag.embedding import EMBEDDING_DIMENSION
from rag.events import KNOWLEDGE_UPDATE_EVENT
from service.knowledge_base.upload import KnowledgeUploadService, UploadValidationError
@@ -34,21 +35,17 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
with tempfile.TemporaryDirectory() as tmp:
publisher = AsyncMock()
milvus = AsyncMock()
embedder = AsyncMock(return_value=[[0.0] * 768])
embedder = AsyncMock(return_value=[[0.0] * EMBEDDING_DIMENSION])
service = KnowledgeUploadService(
storage_dir=tmp,
milvus_client=milvus,
embedder=embedder,
publisher=publisher,
)
preview = await service.preview(
filename="faq.md",
content="Q: 什么是基金?\nA: 一种集合投资工具。".encode(),
strategy="qa_pair",
)
result = await service.confirm(
upload_id=preview["upload_id"],
filename="faq.md",
content="Q: 什么是基金?\nA: 一种集合投资工具。".encode(),
title="FAQ",
doc_id="doc-upload-1",
collection_name="fin_faq",
@@ -58,24 +55,23 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(result["doc_id"], "doc-upload-1")
publisher.assert_awaited_once()
self.assertEqual(publisher.await_args.args[0], KNOWLEDGE_UPDATE_EVENT)
self.assertFalse(Path(tmp, preview["stored_filename"]).exists())
self.assertEqual(len(list(Path(tmp).glob("*.md"))), 0)
self.assertEqual(len(list(Path(tmp).glob("*.json"))), 0)
async def test_confirm_passes_custom_chunk_config_to_ingestion(self):
with tempfile.TemporaryDirectory() as tmp:
milvus = AsyncMock()
embedder = AsyncMock(side_effect=lambda texts: [[0.0] * 768 for _ in texts])
embedder = AsyncMock(side_effect=lambda texts: [[0.0] * EMBEDDING_DIMENSION for _ in texts])
service = KnowledgeUploadService(
storage_dir=tmp,
milvus_client=milvus,
embedder=embedder,
publisher=AsyncMock(),
)
preview = await service.preview(
"doc.md", "一二三四五六七八九十十一十二".encode(), strategy="default"
)
await service.confirm(
upload_id=preview["upload_id"],
filename="doc.md",
content="一二三四五六七八九十十一十二".encode(),
title="Doc",
doc_id="doc-config",
collection_name="fin_fund_doc",
@@ -93,16 +89,14 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
service = KnowledgeUploadService(
storage_dir=tmp,
milvus_client=milvus,
embedder=AsyncMock(return_value=[[0.0] * 768]),
embedder=AsyncMock(return_value=[[0.0] * EMBEDDING_DIMENSION]),
publisher=publisher,
)
preview = await service.preview(
"policy.md", b"policy text", strategy="default"
)
with self.assertRaises(RuntimeError):
await service.confirm(
upload_id=preview["upload_id"],
filename="policy.md",
content=b"policy text",
title="Policy",
doc_id="doc-event-failure",
collection_name="fin_policy",
@@ -134,14 +128,15 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
embedder=AsyncMock(),
publisher=AsyncMock(),
)
preview = await service.preview(
"faq.md", b"Q: Q\nA: A", strategy="qa_pair"
)
with self.assertRaises(UploadValidationError):
await service.confirm(
upload_id=preview["upload_id"], title="FAQ", doc_id="d1",
collection_name="evil_collection", strategy="qa_pair",
filename="faq.md",
content=b"Q: Q\nA: A",
title="FAQ",
doc_id="d1",
collection_name="evil_collection",
strategy="qa_pair",
)
async def test_confirm_rejects_duplicate_doc_id_before_embedding(self):
@@ -154,14 +149,15 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
publisher=AsyncMock(),
document_exists=lambda doc_id: True,
)
preview = await service.preview(
"faq.md", b"Q: Q\nA: A", strategy="qa_pair"
)
with self.assertRaises(UploadValidationError):
await service.confirm(
upload_id=preview["upload_id"], title="FAQ", doc_id="d1",
collection_name="fin_faq", strategy="qa_pair",
filename="faq.md",
content=b"Q: Q\nA: A",
title="FAQ",
doc_id="d1",
collection_name="fin_faq",
strategy="qa_pair",
)
embedder.assert_not_awaited()
@@ -187,28 +183,23 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
self.assertFalse(path.exists())
self.assertFalse(manifest.exists())
async def test_confirm_rejects_expired_upload(self):
async def test_confirm_rejects_unsupported_extension(self):
with tempfile.TemporaryDirectory() as tmp:
service = KnowledgeUploadService(
storage_dir=tmp,
upload_ttl_seconds=60,
milvus_client=AsyncMock(),
embedder=AsyncMock(),
publisher=AsyncMock(),
)
preview = await service.preview("faq.md", b"Q: Q\nA: A", strategy="qa_pair")
path = Path(tmp, preview["stored_filename"])
old = time.time() - 120
os.utime(path, (old, old))
os.utime(Path(tmp, f"{preview['upload_id']}.json"), (old, old))
with self.assertRaises(UploadValidationError):
await service.confirm(
upload_id=preview["upload_id"],
title="FAQ",
doc_id="expired-doc",
filename="script.exe",
content=b"bad",
title="Bad",
doc_id="bad-doc",
collection_name="fin_faq",
strategy="qa_pair",
strategy="default",
)
async def test_delete_document_removes_vectors_and_publishes_update(self):
@@ -287,4 +278,4 @@ class KnowledgeUploadTests(unittest.IsolatedAsyncioTestCase):
if __name__ == "__main__":
unittest.main()
unittest.main()