- Introduce new fields for knowledge retrieval configuration in AssistantConfig and Assistant models, including mode, top_n, and score_threshold. - Implement KnowledgeRetrievalConfig schema with validation for top_n. - Update backend services and routes to handle knowledge retrieval settings. - Enhance frontend components to support knowledge retrieval configuration, including a new dialog for advanced settings. - Add tests for knowledge retrieval configuration validation and description generation.
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import unittest
|
|
|
|
from pydantic import ValidationError
|
|
|
|
from schemas import KnowledgeRetrievalConfig
|
|
from services.knowledge import extract_text, split_text
|
|
|
|
|
|
class KnowledgeTextTest(unittest.TestCase):
|
|
def test_extracts_utf8_text(self):
|
|
self.assertEqual(extract_text("说明.txt", "你好,知识库".encode()), "你好,知识库")
|
|
|
|
def test_split_text_keeps_overlap_and_all_content(self):
|
|
text = "第一段。" * 250
|
|
chunks = split_text(text, chunk_size=120, overlap=20)
|
|
self.assertGreater(len(chunks), 1)
|
|
self.assertTrue(all(chunk for chunk in chunks))
|
|
self.assertLessEqual(max(map(len, chunks)), 120)
|
|
|
|
def test_rejects_unsupported_binary_file(self):
|
|
with self.assertRaisesRegex(ValueError, "暂仅支持"):
|
|
extract_text("archive.zip", b"data")
|
|
|
|
|
|
class KnowledgeRetrievalConfigTest(unittest.TestCase):
|
|
def test_accepts_unlimited_top_n_with_threshold(self):
|
|
config = KnowledgeRetrievalConfig.model_validate(
|
|
{"mode": "on_demand", "topN": -1, "scoreThreshold": 0.65}
|
|
)
|
|
|
|
self.assertEqual(config.top_n, -1)
|
|
self.assertEqual(
|
|
config.model_dump(by_alias=True),
|
|
{"mode": "on_demand", "topN": -1, "scoreThreshold": 0.65},
|
|
)
|
|
|
|
def test_rejects_zero_top_n(self):
|
|
with self.assertRaises(ValidationError):
|
|
KnowledgeRetrievalConfig(top_n=0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|