[fix]1.The "read_all_config" interface returns "scene_name";2.Memory configuration for lightweight query ontology scenarios
This commit is contained in:
@@ -766,6 +766,47 @@ async def delete_scene(
|
||||
return fail(BizCode.INTERNAL_ERROR, "场景删除失败", str(e))
|
||||
|
||||
|
||||
@router.get("/scenes/simple", response_model=ApiResponse)
|
||||
async def get_scenes_simple(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""获取场景简单列表(轻量级,用于下拉选择)
|
||||
|
||||
仅返回 scene_id 和 scene_name,不加载关联数据,响应速度快。
|
||||
适用于前端下拉选择场景的场景。
|
||||
|
||||
Args:
|
||||
db: 数据库会话
|
||||
current_user: 当前用户
|
||||
|
||||
Returns:
|
||||
ApiResponse: 包含场景简单列表
|
||||
|
||||
Examples:
|
||||
GET /scenes/simple
|
||||
返回: {"items": [{"scene_id": "xxx", "scene_name": "场景1"}, ...]}
|
||||
"""
|
||||
api_logger.info(f"Simple scene list requested by user {current_user.id}")
|
||||
|
||||
try:
|
||||
workspace_id = current_user.current_workspace_id
|
||||
if not workspace_id:
|
||||
api_logger.warning(f"User {current_user.id} has no current workspace")
|
||||
return fail(BizCode.BAD_REQUEST, "请求参数无效", "当前用户没有工作空间")
|
||||
|
||||
from app.repositories.ontology_scene_repository import OntologySceneRepository
|
||||
repo = OntologySceneRepository(db)
|
||||
scenes = repo.get_simple_list(workspace_id)
|
||||
|
||||
api_logger.info(f"Simple scene list retrieved: {len(scenes)} scenes")
|
||||
return success(data={"items": scenes}, msg="查询成功")
|
||||
|
||||
except Exception as e:
|
||||
api_logger.error(f"Failed to get simple scene list: {str(e)}", exc_info=True)
|
||||
return fail(BizCode.INTERNAL_ERROR, "查询失败", str(e))
|
||||
|
||||
|
||||
@router.get("/scenes", response_model=ApiResponse)
|
||||
async def get_scenes(
|
||||
workspace_id: Optional[str] = None,
|
||||
|
||||
@@ -279,6 +279,9 @@ class MemoryConfigRepository:
|
||||
if update.config_desc is not None:
|
||||
db_config.config_desc = update.config_desc
|
||||
has_update = True
|
||||
if hasattr(update, 'scene_id') and update.scene_id is not None:
|
||||
db_config.scene_id = update.scene_id
|
||||
has_update = True
|
||||
|
||||
if not has_update:
|
||||
raise ValueError("No fields to update")
|
||||
@@ -650,28 +653,32 @@ class MemoryConfigRepository:
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def get_all(db: Session, workspace_id: Optional[uuid.UUID] = None) -> List[MemoryConfig]:
|
||||
"""获取所有配置参数
|
||||
def get_all(db: Session, workspace_id: Optional[uuid.UUID] = None) -> List[Tuple[MemoryConfig, Optional[str]]]:
|
||||
"""获取所有配置参数,包含关联的场景名称
|
||||
|
||||
Args:
|
||||
db: 数据库会话
|
||||
workspace_id: 工作空间ID,用于过滤查询结果
|
||||
|
||||
Returns:
|
||||
List[MemoryConfig]: 配置列表
|
||||
List[Tuple[MemoryConfig, Optional[str]]]: 配置列表,每项为 (配置对象, 场景名称)
|
||||
"""
|
||||
from app.models.ontology_scene import OntologyScene
|
||||
|
||||
db_logger.debug(f"查询所有配置: workspace_id={workspace_id}")
|
||||
|
||||
try:
|
||||
query = db.query(MemoryConfig)
|
||||
query = db.query(MemoryConfig, OntologyScene.scene_name).outerjoin(
|
||||
OntologyScene, MemoryConfig.scene_id == OntologyScene.scene_id
|
||||
)
|
||||
|
||||
if workspace_id:
|
||||
query = query.filter(MemoryConfig.workspace_id == workspace_id)
|
||||
|
||||
configs = query.order_by(desc(MemoryConfig.updated_at)).all()
|
||||
results = query.order_by(desc(MemoryConfig.updated_at)).all()
|
||||
|
||||
db_logger.debug(f"配置列表查询成功: 数量={len(configs)}")
|
||||
return configs
|
||||
db_logger.debug(f"配置列表查询成功: 数量={len(results)}")
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
db_logger.error(f"查询所有配置失败: workspace_id={workspace_id} - {str(e)}")
|
||||
|
||||
@@ -392,3 +392,48 @@ class OntologySceneRepository:
|
||||
exc_info=True
|
||||
)
|
||||
raise
|
||||
|
||||
def get_simple_list(self, workspace_id: UUID) -> List[dict]:
|
||||
"""获取场景简单列表(仅包含scene_id和scene_name,用于下拉选择)
|
||||
|
||||
这是一个轻量级查询,不加载关联的classes,响应速度快。
|
||||
|
||||
Args:
|
||||
workspace_id: 工作空间ID
|
||||
|
||||
Returns:
|
||||
List[dict]: 场景简单列表,每项包含scene_id和scene_name
|
||||
|
||||
Examples:
|
||||
>>> repo = OntologySceneRepository(db)
|
||||
>>> scenes = repo.get_simple_list(workspace_id)
|
||||
>>> # [{"scene_id": "xxx", "scene_name": "场景1"}, ...]
|
||||
"""
|
||||
try:
|
||||
logger.debug(f"Getting simple scene list for workspace: {workspace_id}")
|
||||
|
||||
# 只查询需要的字段,不加载关联数据
|
||||
results = self.db.query(
|
||||
OntologyScene.scene_id,
|
||||
OntologyScene.scene_name
|
||||
).filter(
|
||||
OntologyScene.workspace_id == workspace_id
|
||||
).order_by(
|
||||
OntologyScene.updated_at.desc()
|
||||
).all()
|
||||
|
||||
scenes = [
|
||||
{"scene_id": str(r.scene_id), "scene_name": r.scene_name}
|
||||
for r in results
|
||||
]
|
||||
|
||||
logger.info(f"Found {len(scenes)} scenes (simple list) in workspace {workspace_id}")
|
||||
|
||||
return scenes
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to get simple scene list: {str(e)}",
|
||||
exc_info=True
|
||||
)
|
||||
raise
|
||||
|
||||
@@ -248,8 +248,9 @@ class ConfigParamsDelete(BaseModel): # 删除配置参数模型(请求体)
|
||||
|
||||
class ConfigUpdate(BaseModel): # 更新记忆萃取引擎配置参数时使用的模型
|
||||
config_id: Union[uuid.UUID, int, str] = None
|
||||
config_name: str = Field("配置名称", description="配置名称(字符串)")
|
||||
config_desc: str = Field("配置描述", description="配置描述(字符串)")
|
||||
config_name: Optional[str] = Field(None, description="配置名称(字符串)")
|
||||
config_desc: Optional[str] = Field(None, description="配置描述(字符串)")
|
||||
scene_id: Optional[uuid.UUID] = Field(None, description="本体场景ID")
|
||||
|
||||
|
||||
class ConfigUpdateExtracted(BaseModel): # 更新记忆萃取引擎配置参数时使用的模型
|
||||
|
||||
@@ -183,11 +183,11 @@ class DataConfigService: # 数据配置服务类(PostgreSQL)
|
||||
|
||||
# --- Read All ---
|
||||
def get_all(self, workspace_id = None) -> List[Dict[str, Any]]: # 获取所有配置参数
|
||||
configs = MemoryConfigRepository.get_all(self.db, workspace_id)
|
||||
results = MemoryConfigRepository.get_all(self.db, workspace_id)
|
||||
|
||||
# 将 ORM 对象转换为字典列表
|
||||
data_list = []
|
||||
for config in configs:
|
||||
for config, scene_name in results:
|
||||
# 安全地转换 user_id 为 int
|
||||
config_id_old = None
|
||||
if config.config_id_old:
|
||||
@@ -209,7 +209,8 @@ class DataConfigService: # 数据配置服务类(PostgreSQL)
|
||||
"end_user_id": config.end_user_id,
|
||||
"config_id_old": config_id_old,
|
||||
"apply_id": config.apply_id,
|
||||
"scene_id": config.scene_id,
|
||||
"scene_id": str(config.scene_id) if config.scene_id else None,
|
||||
"scene_name": scene_name, # 新增:场景名称
|
||||
"llm_id": config.llm_id,
|
||||
"embedding_id": config.embedding_id,
|
||||
"rerank_id": config.rerank_id,
|
||||
|
||||
Reference in New Issue
Block a user