Refactor backend to support interface-definition driven model resources
- Introduce a new model structure for managing interface definitions and model resources, enhancing the backend's capability to handle various service integrations. - Update the Makefile to reflect changes in database seeding and resource management commands. - Remove the deprecated credentials management routes and replace them with a unified model registry API. - Modify existing routes and schemas to align with the new model structure, ensuring seamless integration with the frontend. - Enhance database seeding scripts to populate new model resources and their configurations. - Update README documentation to reflect the new architecture and usage instructions for model resources and interface definitions.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""面向前端的请求/响应 DTO。与 DB 模型解耦,**响应里的 key 一律打码**。
|
||||
|
||||
凭证 DTO 字段对齐前端 ComponentsModelsPage 的 ModelResource:
|
||||
模型资源 DTO 字段对齐前端 ComponentsModelsPage 的 ModelResource:
|
||||
JSON 用 camelCase(modelId/interfaceType/apiUrl/apiKey),Python 内部用 snake_case,
|
||||
靠 Pydantic alias 自动互转。FastAPI 响应默认 by_alias=True,所以出参也是 camelCase。
|
||||
"""
|
||||
@@ -9,12 +9,11 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, model_validator
|
||||
from pydantic import BaseModel, ConfigDict, Field, 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 是该助手私有密钥,读时打码 / 写时哨兵
|
||||
@@ -49,11 +48,7 @@ class AssistantUpsert(CamelModel):
|
||||
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
|
||||
model_resource_ids: dict[ModelType, str] = Field(default_factory=dict)
|
||||
knowledge_base_id: str | None = None
|
||||
|
||||
# 瘦类型专属(真列);按 type 取用,无关字段写入时清零
|
||||
@@ -62,7 +57,7 @@ class AssistantUpsert(CamelModel):
|
||||
api_key: str = "" # 写时:占位符/空 → 保留旧(哨兵)
|
||||
app_id: str = ""
|
||||
# workflow 专属:图
|
||||
graph: dict[str, Any] = {}
|
||||
graph: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _strip_irrelevant_fields(self):
|
||||
@@ -84,7 +79,7 @@ class AssistantOut(AssistantUpsert):
|
||||
class KnowledgeBaseUpsert(CamelModel):
|
||||
name: str
|
||||
description: str = ""
|
||||
embedding_credential_id: str | None = None
|
||||
embedding_model_resource_id: str | None = None
|
||||
|
||||
|
||||
class KnowledgeBaseOut(KnowledgeBaseUpsert):
|
||||
@@ -93,55 +88,32 @@ class KnowledgeBaseOut(KnowledgeBaseUpsert):
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
# ---------- 模型凭证(对齐前端 ModelResource) ----------
|
||||
class CredentialUpsert(CamelModel):
|
||||
name: str = "" # 资源名称
|
||||
model_id: str = "" # 模型ID
|
||||
type: ModelType # LLM/ASR/TTS/Realtime/Embedding
|
||||
interface_type: InterfaceType = "openai" # openai/xfyun/dashscope/gemini
|
||||
api_url: str = ""
|
||||
api_key: str = "" # 写时:占位符/空表示不改
|
||||
voice: str = "" # TTS
|
||||
speed: float = 1.0 # TTS
|
||||
language: str = "" # ASR
|
||||
# ---------- 接口定义驱动的统一模型资源 ----------
|
||||
class InterfaceDefinitionOut(CamelModel):
|
||||
interface_type: str
|
||||
name: str
|
||||
capability: ModelType
|
||||
field_schema: dict[str, Any]
|
||||
enabled: bool
|
||||
version: int
|
||||
|
||||
|
||||
class ModelResourceUpsert(CamelModel):
|
||||
name: str
|
||||
interface_type: str
|
||||
values: dict[str, Any] = Field(default_factory=dict)
|
||||
secrets: dict[str, Any] = Field(default_factory=dict)
|
||||
enabled: bool = True
|
||||
is_default: bool = False
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _strip_irrelevant_options(self):
|
||||
if self.type != "TTS":
|
||||
self.voice = ""
|
||||
self.speed = 1.0
|
||||
if self.type != "ASR":
|
||||
self.language = ""
|
||||
return self
|
||||
|
||||
|
||||
class CredentialOut(CamelModel):
|
||||
class ModelResourceOut(ModelResourceUpsert):
|
||||
id: str
|
||||
name: str
|
||||
model_id: str
|
||||
type: str
|
||||
interface_type: str
|
||||
api_url: str
|
||||
api_key: str # 读时:打码后的值
|
||||
voice: str
|
||||
speed: float
|
||||
language: str
|
||||
is_default: bool
|
||||
capability: ModelType
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
class CredentialTestRequest(CamelModel):
|
||||
model_id: str
|
||||
type: ModelType
|
||||
interface_type: InterfaceType = "openai"
|
||||
api_url: str
|
||||
api_key: str = ""
|
||||
voice: str = ""
|
||||
speed: float = 1.0
|
||||
language: str = ""
|
||||
|
||||
|
||||
class CredentialTestResult(CamelModel):
|
||||
class ModelResourceTestResult(CamelModel):
|
||||
ok: bool
|
||||
latency_ms: int | None = None
|
||||
message: str
|
||||
|
||||
Reference in New Issue
Block a user