Add knowledge retrieval configuration to Assistant model and related components
- 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.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import unittest
|
||||
|
||||
from pydantic import ValidationError
|
||||
|
||||
from schemas import KnowledgeRetrievalConfig
|
||||
from services.knowledge import extract_text, split_text
|
||||
|
||||
|
||||
@@ -19,5 +22,22 @@ class KnowledgeTextTest(unittest.TestCase):
|
||||
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()
|
||||
|
||||
38
backend/tests/test_pipeline_knowledge.py
Normal file
38
backend/tests/test_pipeline_knowledge.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
|
||||
from models import AssistantConfig
|
||||
from services.pipecat.pipeline import _knowledge_tool_description
|
||||
|
||||
|
||||
class KnowledgeToolDescriptionTest(unittest.TestCase):
|
||||
def test_includes_bound_knowledge_scope(self):
|
||||
description = _knowledge_tool_description(
|
||||
AssistantConfig(
|
||||
knowledge_base_name="产品服务知识库",
|
||||
knowledge_base_description="产品价格、售后政策和退换货条件",
|
||||
)
|
||||
)
|
||||
|
||||
self.assertIn("知识库名称:产品服务知识库", description)
|
||||
self.assertIn("资料适用范围:产品价格、售后政策和退换货条件", description)
|
||||
self.assertIn("与该范围无关的问题不要调用", description)
|
||||
|
||||
def test_falls_back_when_metadata_is_empty(self):
|
||||
description = _knowledge_tool_description(AssistantConfig())
|
||||
|
||||
self.assertEqual(
|
||||
description,
|
||||
"在当前助手绑定的知识库中检索与问题最相关的资料片段。",
|
||||
)
|
||||
|
||||
def test_compacts_and_limits_description(self):
|
||||
description = _knowledge_tool_description(
|
||||
AssistantConfig(knowledge_base_description=("范围\n 内容 " * 200))
|
||||
)
|
||||
|
||||
self.assertNotIn("\n ", description)
|
||||
self.assertLess(len(description), 1000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user