v0.21.1-fastapi
This commit is contained in:
53
api/apps/models/auth_dependencies.py
Normal file
53
api/apps/models/auth_dependencies.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional
|
||||
from fastapi import Depends, Header, Security, HTTPException, status
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from api import settings
|
||||
from api.utils.api_utils import get_json_result
|
||||
|
||||
# 创建 HTTPBearer 安全方案(auto_error=False 允许我们自定义错误处理)
|
||||
http_bearer = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
def get_current_user(credentials: Optional[HTTPAuthorizationCredentials] = Security(http_bearer)):
|
||||
"""FastAPI 依赖注入:获取当前用户(替代 Flask 的 login_required 和 current_user)
|
||||
|
||||
使用 Security(http_bearer) 可以让 FastAPI 自动在 OpenAPI schema 中添加安全要求,
|
||||
这样 Swagger UI 就会显示授权输入框并自动在请求中添加 Authorization 头。
|
||||
"""
|
||||
# 延迟导入以避免循环导入
|
||||
from api.apps.__init___fastapi import get_current_user_from_token
|
||||
|
||||
if not credentials:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Authorization header is required"
|
||||
)
|
||||
|
||||
# HTTPBearer 已经提取了 Bearer token,credentials.credentials 就是 token 本身
|
||||
authorization = credentials.credentials
|
||||
|
||||
user = get_current_user_from_token(authorization)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or expired token"
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
129
api/apps/models/canvas_models.py
Normal file
129
api/apps/models/canvas_models.py
Normal file
@@ -0,0 +1,129 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional, List, Dict, Any, Union
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class DeleteCanvasRequest(BaseModel):
|
||||
"""删除画布请求"""
|
||||
canvas_ids: List[str] = Field(..., description="画布ID列表")
|
||||
|
||||
|
||||
class SaveCanvasRequest(BaseModel):
|
||||
"""保存画布请求"""
|
||||
dsl: Union[str, Dict[str, Any]] = Field(..., description="DSL配置")
|
||||
title: str = Field(..., description="画布标题")
|
||||
id: Optional[str] = Field(default=None, description="画布ID(更新时提供)")
|
||||
canvas_category: Optional[str] = Field(default=None, description="画布类别")
|
||||
description: Optional[str] = Field(default=None, description="描述")
|
||||
permission: Optional[str] = Field(default=None, description="权限")
|
||||
avatar: Optional[str] = Field(default=None, description="头像")
|
||||
|
||||
|
||||
class CompletionRequest(BaseModel):
|
||||
"""完成/运行画布请求"""
|
||||
id: str = Field(..., description="画布ID")
|
||||
query: Optional[str] = Field(default="", description="查询内容")
|
||||
files: Optional[List[str]] = Field(default=[], description="文件列表")
|
||||
inputs: Optional[Dict[str, Any]] = Field(default={}, description="输入参数")
|
||||
user_id: Optional[str] = Field(default=None, description="用户ID")
|
||||
|
||||
|
||||
class RerunRequest(BaseModel):
|
||||
"""重新运行请求"""
|
||||
id: str = Field(..., description="流水线ID")
|
||||
dsl: Dict[str, Any] = Field(..., description="DSL配置")
|
||||
component_id: str = Field(..., description="组件ID")
|
||||
|
||||
|
||||
class ResetCanvasRequest(BaseModel):
|
||||
"""重置画布请求"""
|
||||
id: str = Field(..., description="画布ID")
|
||||
|
||||
|
||||
class InputFormQuery(BaseModel):
|
||||
"""获取输入表单查询参数"""
|
||||
id: str = Field(..., description="画布ID")
|
||||
component_id: str = Field(..., description="组件ID")
|
||||
|
||||
|
||||
class DebugRequest(BaseModel):
|
||||
"""调试请求"""
|
||||
id: str = Field(..., description="画布ID")
|
||||
component_id: str = Field(..., description="组件ID")
|
||||
params: Dict[str, Any] = Field(..., description="参数")
|
||||
|
||||
|
||||
class TestDBConnectRequest(BaseModel):
|
||||
"""测试数据库连接请求"""
|
||||
db_type: str = Field(..., description="数据库类型")
|
||||
database: str = Field(..., description="数据库名")
|
||||
username: str = Field(..., description="用户名")
|
||||
host: str = Field(..., description="主机")
|
||||
port: str = Field(..., description="端口")
|
||||
password: str = Field(..., description="密码")
|
||||
|
||||
|
||||
class ListCanvasQuery(BaseModel):
|
||||
"""列出画布查询参数"""
|
||||
keywords: Optional[str] = Field(default="", description="关键词")
|
||||
page: Optional[int] = Field(default=0, description="页码")
|
||||
page_size: Optional[int] = Field(default=0, description="每页大小")
|
||||
orderby: Optional[str] = Field(default="create_time", description="排序字段")
|
||||
desc: Optional[str] = Field(default="true", description="是否降序")
|
||||
canvas_category: Optional[str] = Field(default=None, description="画布类别")
|
||||
owner_ids: Optional[str] = Field(default="", description="所有者ID列表(逗号分隔)")
|
||||
|
||||
|
||||
class SettingRequest(BaseModel):
|
||||
"""画布设置请求"""
|
||||
id: str = Field(..., description="画布ID")
|
||||
title: str = Field(..., description="标题")
|
||||
permission: str = Field(..., description="权限")
|
||||
description: Optional[str] = Field(default=None, description="描述")
|
||||
avatar: Optional[str] = Field(default=None, description="头像")
|
||||
|
||||
|
||||
class TraceQuery(BaseModel):
|
||||
"""追踪查询参数"""
|
||||
canvas_id: str = Field(..., description="画布ID")
|
||||
message_id: str = Field(..., description="消息ID")
|
||||
|
||||
|
||||
class ListSessionsQuery(BaseModel):
|
||||
"""列出会话查询参数"""
|
||||
user_id: Optional[str] = Field(default=None, description="用户ID")
|
||||
page: Optional[int] = Field(default=1, description="页码")
|
||||
page_size: Optional[int] = Field(default=30, description="每页大小")
|
||||
keywords: Optional[str] = Field(default=None, description="关键词")
|
||||
from_date: Optional[str] = Field(default=None, description="开始日期")
|
||||
to_date: Optional[str] = Field(default=None, description="结束日期")
|
||||
orderby: Optional[str] = Field(default="update_time", description="排序字段")
|
||||
desc: Optional[str] = Field(default="true", description="是否降序")
|
||||
dsl: Optional[str] = Field(default="true", description="是否包含DSL")
|
||||
|
||||
|
||||
class DownloadQuery(BaseModel):
|
||||
"""下载查询参数"""
|
||||
id: str = Field(..., description="文件ID")
|
||||
created_by: str = Field(..., description="创建者ID")
|
||||
|
||||
|
||||
class UploadQuery(BaseModel):
|
||||
"""上传查询参数"""
|
||||
url: Optional[str] = Field(default=None, description="URL(可选,用于从URL下载)")
|
||||
|
||||
80
api/apps/models/chunk_models.py
Normal file
80
api/apps/models/chunk_models.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional, List, Any, Union
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
|
||||
class ListChunksRequest(BaseModel):
|
||||
"""列出文档块请求"""
|
||||
doc_id: str = Field(..., description="文档ID")
|
||||
page: Optional[int] = Field(default=1, description="页码")
|
||||
size: Optional[int] = Field(default=30, description="每页大小")
|
||||
keywords: Optional[str] = Field(default="", description="关键词")
|
||||
available_int: Optional[int] = Field(default=None, description="可用状态")
|
||||
|
||||
|
||||
class SetChunkRequest(BaseModel):
|
||||
"""设置文档块请求"""
|
||||
doc_id: str = Field(..., description="文档ID")
|
||||
chunk_id: str = Field(..., description="块ID")
|
||||
content_with_weight: str = Field(..., description="内容")
|
||||
important_kwd: Optional[List[str]] = Field(default=None, description="重要关键词列表")
|
||||
question_kwd: Optional[List[str]] = Field(default=None, description="问题关键词列表")
|
||||
tag_kwd: Optional[str] = Field(default=None, description="标签关键词")
|
||||
tag_feas: Optional[Any] = Field(default=None, description="标签特征")
|
||||
available_int: Optional[int] = Field(default=None, description="可用状态")
|
||||
|
||||
|
||||
class SwitchChunksRequest(BaseModel):
|
||||
"""切换文档块状态请求"""
|
||||
doc_id: str = Field(..., description="文档ID")
|
||||
chunk_ids: List[str] = Field(..., description="块ID列表")
|
||||
available_int: int = Field(..., description="可用状态")
|
||||
|
||||
|
||||
class DeleteChunksRequest(BaseModel):
|
||||
"""删除文档块请求"""
|
||||
doc_id: str = Field(..., description="文档ID")
|
||||
chunk_ids: List[str] = Field(..., description="块ID列表")
|
||||
|
||||
|
||||
class CreateChunkRequest(BaseModel):
|
||||
"""创建文档块请求"""
|
||||
doc_id: str = Field(..., description="文档ID")
|
||||
content_with_weight: str = Field(..., description="内容")
|
||||
important_kwd: Optional[List[str]] = Field(default=[], description="重要关键词列表")
|
||||
question_kwd: Optional[List[str]] = Field(default=[], description="问题关键词列表")
|
||||
tag_feas: Optional[Any] = Field(default=None, description="标签特征")
|
||||
|
||||
|
||||
class RetrievalTestRequest(BaseModel):
|
||||
"""检索测试请求"""
|
||||
kb_id: Union[str, List[str]] = Field(..., description="知识库ID,可以是字符串或列表")
|
||||
question: str = Field(..., description="问题")
|
||||
page: Optional[int] = Field(default=1, description="页码")
|
||||
size: Optional[int] = Field(default=30, description="每页大小")
|
||||
doc_ids: Optional[List[str]] = Field(default=[], description="文档ID列表")
|
||||
use_kg: Optional[bool] = Field(default=False, description="是否使用知识图谱")
|
||||
top_k: Optional[int] = Field(default=1024, description="Top K")
|
||||
cross_languages: Optional[List[str]] = Field(default=[], description="跨语言列表")
|
||||
search_id: Optional[str] = Field(default="", description="搜索ID")
|
||||
rerank_id: Optional[str] = Field(default=None, description="重排序模型ID")
|
||||
keyword: Optional[bool] = Field(default=False, description="是否使用关键词")
|
||||
similarity_threshold: Optional[float] = Field(default=0.0, description="相似度阈值")
|
||||
vector_similarity_weight: Optional[float] = Field(default=0.3, description="向量相似度权重")
|
||||
highlight: Optional[bool] = Field(default=False, description="是否高亮")
|
||||
|
||||
204
api/apps/models/document_models.py
Normal file
204
api/apps/models/document_models.py
Normal file
@@ -0,0 +1,204 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional, Literal, List
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
|
||||
|
||||
class CreateDocumentRequest(BaseModel):
|
||||
"""创建文档请求
|
||||
|
||||
支持两种解析类型:
|
||||
- parse_type=1: 使用内置解析器,需要 parser_id,pipeline_id 为空
|
||||
- parse_type=2: 使用自定义 pipeline,需要 pipeline_id,parser_id 为空
|
||||
如果不提供 parse_type,则从知识库继承解析配置
|
||||
"""
|
||||
name: str
|
||||
kb_id: str
|
||||
parse_type: Optional[Literal[1, 2]] = Field(default=None, description="解析类型:1=内置解析器,2=自定义pipeline,None=从知识库继承")
|
||||
parser_id: Optional[str] = Field(default="", description="解析器ID,parse_type=1时必需")
|
||||
pipeline_id: Optional[str] = Field(default="", description="流水线ID,parse_type=2时必需")
|
||||
parser_config: Optional[dict] = None
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_parse_type_fields(self):
|
||||
"""根据 parse_type 验证相应字段"""
|
||||
if self.parse_type is not None:
|
||||
if self.parse_type == 1:
|
||||
# parse_type=1: 需要 parser_id,pipeline_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if parser_id_val.strip() == "":
|
||||
raise ValueError("parse_type=1时,parser_id不能为空")
|
||||
if pipeline_id_val.strip() != "":
|
||||
raise ValueError("parse_type=1时,pipeline_id必须为空")
|
||||
elif self.parse_type == 2:
|
||||
# parse_type=2: 需要 pipeline_id,parser_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if pipeline_id_val.strip() == "":
|
||||
raise ValueError("parse_type=2时,pipeline_id不能为空")
|
||||
if parser_id_val.strip() != "":
|
||||
raise ValueError("parse_type=2时,parser_id必须为空")
|
||||
return self
|
||||
|
||||
|
||||
class ChangeParserRequest(BaseModel):
|
||||
"""修改文档解析器请求
|
||||
|
||||
支持两种解析类型:
|
||||
- parse_type=1: 使用内置解析器,需要 parser_id,pipeline_id 为空
|
||||
- parse_type=2: 使用自定义 pipeline,需要 pipeline_id,parser_id 为空
|
||||
"""
|
||||
doc_id: str
|
||||
parse_type: Literal[1, 2] = Field(..., description="解析类型:1=内置解析器,2=自定义pipeline")
|
||||
parser_id: Optional[str] = Field(default="", description="解析器ID,parse_type=1时必需")
|
||||
pipeline_id: Optional[str] = Field(default="", description="流水线ID,parse_type=2时必需")
|
||||
parser_config: Optional[dict] = None
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_parse_type_fields(self):
|
||||
"""根据 parse_type 验证相应字段"""
|
||||
if self.parse_type == 1:
|
||||
# parse_type=1: 需要 parser_id,pipeline_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if parser_id_val.strip() == "":
|
||||
raise ValueError("parse_type=1时,parser_id不能为空")
|
||||
if pipeline_id_val.strip() != "":
|
||||
raise ValueError("parse_type=1时,pipeline_id必须为空")
|
||||
elif self.parse_type == 2:
|
||||
# parse_type=2: 需要 pipeline_id,parser_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if pipeline_id_val.strip() == "":
|
||||
raise ValueError("parse_type=2时,pipeline_id不能为空")
|
||||
if parser_id_val.strip() != "":
|
||||
raise ValueError("parse_type=2时,parser_id必须为空")
|
||||
return self
|
||||
|
||||
|
||||
class WebCrawlRequest(BaseModel):
|
||||
"""网页爬取请求"""
|
||||
kb_id: str
|
||||
name: str
|
||||
url: str
|
||||
|
||||
|
||||
class ListDocumentsQuery(BaseModel):
|
||||
"""列出文档查询参数"""
|
||||
kb_id: str
|
||||
keywords: Optional[str] = ""
|
||||
page: Optional[int] = 0
|
||||
page_size: Optional[int] = 0
|
||||
orderby: Optional[str] = "create_time"
|
||||
desc: Optional[str] = "true"
|
||||
create_time_from: Optional[int] = 0
|
||||
create_time_to: Optional[int] = 0
|
||||
|
||||
|
||||
class ListDocumentsBody(BaseModel):
|
||||
"""列出文档请求体"""
|
||||
run_status: Optional[List[str]] = []
|
||||
types: Optional[List[str]] = []
|
||||
suffix: Optional[List[str]] = []
|
||||
|
||||
|
||||
class FilterDocumentsRequest(BaseModel):
|
||||
"""过滤文档请求"""
|
||||
kb_id: str
|
||||
keywords: Optional[str] = ""
|
||||
suffix: Optional[List[str]] = []
|
||||
run_status: Optional[List[str]] = []
|
||||
types: Optional[List[str]] = []
|
||||
|
||||
|
||||
class GetDocumentInfosRequest(BaseModel):
|
||||
"""获取文档信息请求"""
|
||||
doc_ids: List[str]
|
||||
|
||||
|
||||
class ChangeStatusRequest(BaseModel):
|
||||
"""修改文档状态请求"""
|
||||
doc_ids: List[str]
|
||||
status: str # "0" 或 "1"
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_status(self):
|
||||
if self.status not in ["0", "1"]:
|
||||
raise ValueError('Status must be either 0 or 1!')
|
||||
return self
|
||||
|
||||
|
||||
class DeleteDocumentRequest(BaseModel):
|
||||
"""删除文档请求"""
|
||||
doc_id: str | List[str] # 支持单个或列表
|
||||
|
||||
|
||||
class RunDocumentRequest(BaseModel):
|
||||
"""运行文档解析请求"""
|
||||
doc_ids: List[str]
|
||||
run: str # TaskStatus 值
|
||||
delete: Optional[bool] = False
|
||||
|
||||
|
||||
class RenameDocumentRequest(BaseModel):
|
||||
"""重命名文档请求"""
|
||||
doc_id: str
|
||||
name: str
|
||||
|
||||
|
||||
class ChangeParserSimpleRequest(BaseModel):
|
||||
"""简单修改解析器请求(兼容旧逻辑)"""
|
||||
doc_id: str
|
||||
parser_id: Optional[str] = None
|
||||
pipeline_id: Optional[str] = None
|
||||
parser_config: Optional[dict] = None
|
||||
|
||||
|
||||
class UploadAndParseRequest(BaseModel):
|
||||
"""上传并解析请求(仅用于验证 conversation_id)"""
|
||||
conversation_id: str
|
||||
|
||||
|
||||
class ParseRequest(BaseModel):
|
||||
"""解析请求"""
|
||||
url: Optional[str] = None
|
||||
|
||||
|
||||
class SetMetaRequest(BaseModel):
|
||||
"""设置元数据请求"""
|
||||
doc_id: str
|
||||
meta: str # JSON 字符串
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_meta(self):
|
||||
import json
|
||||
try:
|
||||
meta_dict = json.loads(self.meta)
|
||||
if not isinstance(meta_dict, dict):
|
||||
raise ValueError("Only dictionary type supported.")
|
||||
for k, v in meta_dict.items():
|
||||
if not isinstance(v, (str, int, float)):
|
||||
raise ValueError(f"The type is not supported: {v}")
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError(f"Json syntax error: {e}")
|
||||
return self
|
||||
|
||||
159
api/apps/models/kb_models.py
Normal file
159
api/apps/models/kb_models.py
Normal file
@@ -0,0 +1,159 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional, List, Dict, Any, Literal
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
|
||||
|
||||
class CreateKnowledgeBaseRequest(BaseModel):
|
||||
"""创建知识库请求
|
||||
|
||||
支持两种解析类型:
|
||||
- parse_type=1: 使用内置解析器,需要 parser_id,pipeline_id 为空
|
||||
- parse_type=2: 使用自定义 pipeline,需要 pipeline_id,parser_id 为空
|
||||
"""
|
||||
name: str
|
||||
parse_type: Literal[1, 2] = Field(..., description="解析类型:1=内置解析器,2=自定义pipeline")
|
||||
embd_id: str = Field(..., description="嵌入模型ID")
|
||||
parser_id: Optional[str] = Field(default="", description="解析器ID,parse_type=1时必需")
|
||||
pipeline_id: Optional[str] = Field(default="", description="流水线ID,parse_type=2时必需")
|
||||
description: Optional[str] = None
|
||||
pagerank: Optional[int] = None
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_parse_type_fields(self):
|
||||
"""根据 parse_type 验证相应字段"""
|
||||
if self.parse_type == 1:
|
||||
# parse_type=1: 需要 parser_id,pipeline_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if parser_id_val.strip() == "":
|
||||
raise ValueError("parse_type=1时,parser_id不能为空")
|
||||
if pipeline_id_val.strip() != "":
|
||||
raise ValueError("parse_type=1时,pipeline_id必须为空")
|
||||
elif self.parse_type == 2:
|
||||
# parse_type=2: 需要 pipeline_id,parser_id 必须为空
|
||||
parser_id_val = self.parser_id or ""
|
||||
pipeline_id_val = self.pipeline_id or ""
|
||||
|
||||
if pipeline_id_val.strip() == "":
|
||||
raise ValueError("parse_type=2时,pipeline_id不能为空")
|
||||
if parser_id_val.strip() != "":
|
||||
raise ValueError("parse_type=2时,parser_id必须为空")
|
||||
return self
|
||||
|
||||
|
||||
class UpdateKnowledgeBaseRequest(BaseModel):
|
||||
"""更新知识库请求"""
|
||||
kb_id: str
|
||||
name: str
|
||||
description: str
|
||||
parser_id: str
|
||||
pagerank: Optional[int] = None
|
||||
# 其他可选字段,但排除 id, tenant_id, created_by, create_time, update_time, create_date, update_date
|
||||
|
||||
|
||||
class DeleteKnowledgeBaseRequest(BaseModel):
|
||||
"""删除知识库请求"""
|
||||
kb_id: str
|
||||
|
||||
|
||||
class ListKnowledgeBasesQuery(BaseModel):
|
||||
"""列出知识库查询参数"""
|
||||
keywords: Optional[str] = ""
|
||||
page: Optional[int] = 0
|
||||
page_size: Optional[int] = 0
|
||||
parser_id: Optional[str] = None
|
||||
orderby: Optional[str] = "create_time"
|
||||
desc: Optional[str] = "true"
|
||||
|
||||
|
||||
class ListKnowledgeBasesBody(BaseModel):
|
||||
"""列出知识库请求体"""
|
||||
owner_ids: Optional[List[str]] = []
|
||||
|
||||
|
||||
class RemoveTagsRequest(BaseModel):
|
||||
"""删除标签请求"""
|
||||
tags: List[str]
|
||||
|
||||
|
||||
class RenameTagRequest(BaseModel):
|
||||
"""重命名标签请求"""
|
||||
from_tag: str
|
||||
to_tag: str
|
||||
|
||||
|
||||
class ListPipelineLogsQuery(BaseModel):
|
||||
"""列出流水线日志查询参数"""
|
||||
kb_id: str
|
||||
keywords: Optional[str] = ""
|
||||
page: Optional[int] = 0
|
||||
page_size: Optional[int] = 0
|
||||
orderby: Optional[str] = "create_time"
|
||||
desc: Optional[str] = "true"
|
||||
create_date_from: Optional[str] = ""
|
||||
create_date_to: Optional[str] = ""
|
||||
|
||||
|
||||
class ListPipelineLogsBody(BaseModel):
|
||||
"""列出流水线日志请求体"""
|
||||
operation_status: Optional[List[str]] = []
|
||||
types: Optional[List[str]] = []
|
||||
suffix: Optional[List[str]] = []
|
||||
|
||||
|
||||
class ListPipelineDatasetLogsQuery(BaseModel):
|
||||
"""列出流水线数据集日志查询参数"""
|
||||
kb_id: str
|
||||
page: Optional[int] = 0
|
||||
page_size: Optional[int] = 0
|
||||
orderby: Optional[str] = "create_time"
|
||||
desc: Optional[str] = "true"
|
||||
create_date_from: Optional[str] = ""
|
||||
create_date_to: Optional[str] = ""
|
||||
|
||||
|
||||
class ListPipelineDatasetLogsBody(BaseModel):
|
||||
"""列出流水线数据集日志请求体"""
|
||||
operation_status: Optional[List[str]] = []
|
||||
|
||||
|
||||
class DeletePipelineLogsQuery(BaseModel):
|
||||
"""删除流水线日志查询参数"""
|
||||
kb_id: str
|
||||
|
||||
|
||||
class DeletePipelineLogsBody(BaseModel):
|
||||
"""删除流水线日志请求体"""
|
||||
log_ids: List[str]
|
||||
|
||||
|
||||
class RunGraphragRequest(BaseModel):
|
||||
"""运行 GraphRAG 请求"""
|
||||
kb_id: str
|
||||
|
||||
|
||||
class RunRaptorRequest(BaseModel):
|
||||
"""运行 RAPTOR 请求"""
|
||||
kb_id: str
|
||||
|
||||
|
||||
class RunMindmapRequest(BaseModel):
|
||||
"""运行 Mindmap 请求"""
|
||||
kb_id: str
|
||||
|
||||
101
api/apps/models/llm_models.py
Normal file
101
api/apps/models/llm_models.py
Normal file
@@ -0,0 +1,101 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class SetApiKeyRequest(BaseModel):
|
||||
"""设置 API Key 请求"""
|
||||
llm_factory: str = Field(..., description="LLM 工厂名称")
|
||||
api_key: str = Field(..., description="API Key")
|
||||
base_url: Optional[str] = Field(default="", description="API Base URL")
|
||||
model_type: Optional[str] = Field(default=None, description="模型类型")
|
||||
llm_name: Optional[str] = Field(default=None, description="LLM 名称")
|
||||
|
||||
|
||||
class AddLLMRequest(BaseModel):
|
||||
"""添加 LLM 请求"""
|
||||
llm_factory: str = Field(..., description="LLM 工厂名称")
|
||||
model_type: str = Field(..., description="模型类型")
|
||||
llm_name: str = Field(..., description="LLM 名称")
|
||||
api_key: Optional[str] = Field(default="x", description="API Key")
|
||||
api_base: Optional[str] = Field(default="", description="API Base URL")
|
||||
max_tokens: Optional[int] = Field(default=None, description="最大 Token 数")
|
||||
|
||||
# VolcEngine 特殊字段
|
||||
ark_api_key: Optional[str] = None
|
||||
endpoint_id: Optional[str] = None
|
||||
|
||||
# Tencent Hunyuan 特殊字段
|
||||
hunyuan_sid: Optional[str] = None
|
||||
hunyuan_sk: Optional[str] = None
|
||||
|
||||
# Tencent Cloud 特殊字段
|
||||
tencent_cloud_sid: Optional[str] = None
|
||||
tencent_cloud_sk: Optional[str] = None
|
||||
|
||||
# Bedrock 特殊字段
|
||||
bedrock_ak: Optional[str] = None
|
||||
bedrock_sk: Optional[str] = None
|
||||
bedrock_region: Optional[str] = None
|
||||
|
||||
# XunFei Spark 特殊字段
|
||||
spark_api_password: Optional[str] = None
|
||||
spark_app_id: Optional[str] = None
|
||||
spark_api_secret: Optional[str] = None
|
||||
spark_api_key: Optional[str] = None
|
||||
|
||||
# BaiduYiyan 特殊字段
|
||||
yiyan_ak: Optional[str] = None
|
||||
yiyan_sk: Optional[str] = None
|
||||
|
||||
# Fish Audio 特殊字段
|
||||
fish_audio_ak: Optional[str] = None
|
||||
fish_audio_refid: Optional[str] = None
|
||||
|
||||
# Google Cloud 特殊字段
|
||||
google_project_id: Optional[str] = None
|
||||
google_region: Optional[str] = None
|
||||
google_service_account_key: Optional[str] = None
|
||||
|
||||
# Azure-OpenAI 特殊字段
|
||||
api_version: Optional[str] = None
|
||||
|
||||
# OpenRouter 特殊字段
|
||||
provider_order: Optional[str] = None
|
||||
|
||||
|
||||
class DeleteLLMRequest(BaseModel):
|
||||
"""删除 LLM 请求"""
|
||||
llm_factory: str = Field(..., description="LLM 工厂名称")
|
||||
llm_name: str = Field(..., description="LLM 名称")
|
||||
|
||||
|
||||
class DeleteFactoryRequest(BaseModel):
|
||||
"""删除工厂请求"""
|
||||
llm_factory: str = Field(..., description="LLM 工厂名称")
|
||||
|
||||
|
||||
class MyLLMsQuery(BaseModel):
|
||||
"""获取我的 LLMs 查询参数"""
|
||||
include_details: Optional[str] = Field(default="false", description="是否包含详细信息")
|
||||
|
||||
|
||||
class ListLLMsQuery(BaseModel):
|
||||
"""列出 LLMs 查询参数"""
|
||||
model_type: Optional[str] = Field(default=None, description="模型类型过滤")
|
||||
|
||||
99
api/apps/models/mcp_models.py
Normal file
99
api/apps/models/mcp_models.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#
|
||||
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class ListMCPServersQuery(BaseModel):
|
||||
"""列出MCP服务器查询参数"""
|
||||
keywords: Optional[str] = Field(default="", description="关键词")
|
||||
page: Optional[int] = Field(default=0, description="页码")
|
||||
page_size: Optional[int] = Field(default=0, description="每页大小")
|
||||
orderby: Optional[str] = Field(default="create_time", description="排序字段")
|
||||
desc: Optional[str] = Field(default="true", description="是否降序")
|
||||
|
||||
|
||||
class ListMCPServersBody(BaseModel):
|
||||
"""列出MCP服务器请求体"""
|
||||
mcp_ids: Optional[List[str]] = Field(default=[], description="MCP服务器ID列表")
|
||||
|
||||
|
||||
class CreateMCPServerRequest(BaseModel):
|
||||
"""创建MCP服务器请求"""
|
||||
name: str = Field(..., description="服务器名称")
|
||||
url: str = Field(..., description="服务器URL")
|
||||
server_type: str = Field(..., description="服务器类型")
|
||||
headers: Optional[Dict[str, Any]] = Field(default={}, description="请求头")
|
||||
variables: Optional[Dict[str, Any]] = Field(default={}, description="变量")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
|
||||
class UpdateMCPServerRequest(BaseModel):
|
||||
"""更新MCP服务器请求"""
|
||||
mcp_id: str = Field(..., description="MCP服务器ID")
|
||||
name: Optional[str] = Field(default=None, description="服务器名称")
|
||||
url: Optional[str] = Field(default=None, description="服务器URL")
|
||||
server_type: Optional[str] = Field(default=None, description="服务器类型")
|
||||
headers: Optional[Dict[str, Any]] = Field(default=None, description="请求头")
|
||||
variables: Optional[Dict[str, Any]] = Field(default=None, description="变量")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
|
||||
class DeleteMCPServersRequest(BaseModel):
|
||||
"""删除MCP服务器请求"""
|
||||
mcp_ids: List[str] = Field(..., description="MCP服务器ID列表")
|
||||
|
||||
|
||||
class ImportMCPServersRequest(BaseModel):
|
||||
"""批量导入MCP服务器请求"""
|
||||
mcpServers: Dict[str, Any] = Field(..., description="MCP服务器配置字典")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
|
||||
class ExportMCPServersRequest(BaseModel):
|
||||
"""批量导出MCP服务器请求"""
|
||||
mcp_ids: List[str] = Field(..., description="MCP服务器ID列表")
|
||||
|
||||
|
||||
class ListMCPToolsRequest(BaseModel):
|
||||
"""列出MCP工具请求"""
|
||||
mcp_ids: List[str] = Field(..., description="MCP服务器ID列表")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
|
||||
class TestMCPToolRequest(BaseModel):
|
||||
"""测试MCP工具请求"""
|
||||
mcp_id: str = Field(..., description="MCP服务器ID")
|
||||
tool_name: str = Field(..., description="工具名称")
|
||||
arguments: Dict[str, Any] = Field(..., description="工具参数")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
|
||||
class CacheMCPToolsRequest(BaseModel):
|
||||
"""缓存MCP工具请求"""
|
||||
mcp_id: str = Field(..., description="MCP服务器ID")
|
||||
tools: List[Dict[str, Any]] = Field(..., description="工具列表")
|
||||
|
||||
|
||||
class TestMCPRequest(BaseModel):
|
||||
"""测试MCP服务器请求(不需要登录)"""
|
||||
url: str = Field(..., description="服务器URL")
|
||||
server_type: str = Field(..., description="服务器类型")
|
||||
headers: Optional[Dict[str, Any]] = Field(default={}, description="请求头")
|
||||
variables: Optional[Dict[str, Any]] = Field(default={}, description="变量")
|
||||
timeout: Optional[float] = Field(default=10.0, description="超时时间")
|
||||
|
||||
Reference in New Issue
Block a user