Implement knowledge base management and enhance assistant configuration

Add CRUD functionality for knowledge bases, including routes for listing, creating, updating, and deleting knowledge bases. Update the assistant model to include foreign key references to knowledge bases and modify the assistant configuration to handle external API keys securely. Refactor related services and routes to accommodate these changes, ensuring proper handling of credential resolution and configuration normalization.
This commit is contained in:
Xin Wang
2026-06-09 08:31:39 +08:00
parent 34fba494a3
commit b444ea777c
6 changed files with 304 additions and 56 deletions

View File

@@ -7,14 +7,18 @@ JSON 用 camelCase(modelId/interfaceType/apiUrl/apiKey),Python 内部用 snake_c
from __future__ import annotations
from typing import Literal
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, model_validator
from pydantic.alias_generators import to_camel
RuntimeMode = Literal["pipeline", "realtime"]
ModelType = Literal["LLM", "ASR", "TTS", "Realtime", "Embedding"]
InterfaceType = Literal["openai", "xfyun", "dashscope", "gemini"]
AssistantType = Literal["prompt", "workflow", "dify", "fastgpt", "opencode"]
# 外部应用类型:其 config.apiKey 是该助手私有密钥,读时打码 / 写时哨兵
EXTERNAL_TYPES = {"dify", "fastgpt", "opencode"}
class CamelModel(BaseModel):
@@ -27,23 +31,86 @@ class CamelModel(BaseModel):
)
# ---------- 助手 ----------
# ---------- 各类型的 config 形态(JSON 内嵌,按 type 校验) ----------
class PromptConfig(CamelModel):
prompt: str = ""
realtime_model: str = ""
class WorkflowConfig(CamelModel):
graph: dict[str, Any] = {} # {nodes, edges, viewport};节点 data 可带 *CredentialId 覆盖
class DifyConfig(CamelModel):
api_url: str = ""
api_key: str = "" # 写时:占位符/空 → 保留旧
class FastgptConfig(CamelModel):
app_id: str = ""
api_url: str = ""
api_key: str = ""
class OpencodeConfig(CamelModel):
prompt: str = ""
api_url: str = ""
api_key: str = ""
CONFIG_BY_TYPE: dict[str, type[CamelModel]] = {
"prompt": PromptConfig,
"workflow": WorkflowConfig,
"dify": DifyConfig,
"fastgpt": FastgptConfig,
"opencode": OpencodeConfig,
}
# ---------- 助手(单表,无版本化;type 可变) ----------
class AssistantUpsert(CamelModel):
name: str
greeting: str = ""
prompt: str = ""
type: AssistantType = "prompt"
runtime_mode: RuntimeMode = "pipeline"
model: str = ""
asr: str = ""
voice: str = ""
greeting: str = ""
enable_interrupt: bool = True
# 引用注册资源(FK id;None=未选)
llm_credential_id: str | None = None
asr_credential_id: str | None = None
tts_credential_id: str | None = None
realtime_credential_id: str | None = None
knowledge_base_id: str | None = None
# 类型专属字段;校验后归一为 camelCase 存库
config: dict[str, Any] = {}
@model_validator(mode="after")
def _normalize_config(self):
model = CONFIG_BY_TYPE[self.type]
# 按当前 type 校验并裁掉无关字段,统一回 camelCase 落库
self.config = model.model_validate(self.config).model_dump(by_alias=True)
return self
class AssistantOut(AssistantUpsert):
id: str
updated_at: str | None = None
# ---------- 知识库 ----------
class KnowledgeBaseUpsert(CamelModel):
name: str
description: str = ""
embedding_credential_id: str | None = None
class KnowledgeBaseOut(KnowledgeBaseUpsert):
id: str
status: str = "active"
updated_at: str | None = None
# ---------- 模型凭证(对齐前端 ModelResource) ----------
class CredentialUpsert(CamelModel):
name: str = "" # 资源名称