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:
Xin Wang
2026-07-12 18:57:56 +08:00
parent 7ee3e22152
commit 7c9a18c806
13 changed files with 465 additions and 47 deletions

View File

@@ -17,6 +17,7 @@ RuntimeMode = Literal["pipeline", "realtime"]
ModelType = Literal["LLM", "ASR", "TTS", "Realtime", "Embedding", "Agent"]
AssistantType = Literal["prompt", "workflow", "dify", "fastgpt", "opencode"]
TurnEndStrategy = Literal["silence", "smart_turn"]
KnowledgeRetrievalMode = Literal["automatic", "on_demand"]
ToolType = Literal["end_call", "http"]
ToolStatus = Literal["active", "archived", "draft"]
ToolParameterType = Literal["string", "number", "integer", "boolean", "object", "array"]
@@ -62,6 +63,19 @@ class TurnConfig(CamelModel):
turn_detection: TurnDetectionConfig = Field(default_factory=TurnDetectionConfig)
class KnowledgeRetrievalConfig(CamelModel):
mode: KnowledgeRetrievalMode = "automatic"
top_n: int = Field(default=5, ge=-1)
score_threshold: float = Field(default=0.0, ge=0.0, le=1.0)
@field_validator("top_n")
@classmethod
def validate_top_n(cls, value: int) -> int:
if value == 0:
raise ValueError("topN 必须为 -1 或大于 0")
return value
# 各 type 允许的瘦字段(其余字段写入时清零,防止跨类型脏数据)
ALLOWED_FIELDS: dict[str, set[str]] = {
"prompt": {"prompt"},
@@ -85,6 +99,9 @@ class AssistantUpsert(CamelModel):
model_resource_ids: dict[ModelType, str] = Field(default_factory=dict)
knowledge_base_id: str | None = None
knowledge_retrieval_config: KnowledgeRetrievalConfig = Field(
default_factory=KnowledgeRetrievalConfig
)
tool_ids: list[str] = Field(default_factory=list)
# 瘦类型专属(真列);按 type 取用,无关字段写入时清零