100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
|
|
# =============================================================================
|
||
|
|
# 企微IT智能服务台 — 会话标注 API
|
||
|
|
# =============================================================================
|
||
|
|
# 说明:会话标注接口
|
||
|
|
# 1. POST /api/annotations — 创建标注
|
||
|
|
# 2. GET /api/annotations/{conversation_id} — 获取会话的标注列表
|
||
|
|
# =============================================================================
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from uuid import UUID
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.database import get_db
|
||
|
|
from app.models.agent import Agent
|
||
|
|
from app.models.conversation_annotation import ConversationAnnotation
|
||
|
|
from app.schemas.conversation_annotation import (
|
||
|
|
AnnotationCreate,
|
||
|
|
AnnotationResponse,
|
||
|
|
)
|
||
|
|
from app.utils.response import AppException, ERR_NOT_FOUND, success_response
|
||
|
|
|
||
|
|
from app.api.agents import get_current_agent
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
# 创建路由器
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
# --------------------------------------------------------------------------
|
||
|
|
# POST /api/annotations — 创建标注
|
||
|
|
# --------------------------------------------------------------------------
|
||
|
|
@router.post("/annotations")
|
||
|
|
async def create_annotation(
|
||
|
|
body: AnnotationCreate,
|
||
|
|
agent: Agent = Depends(get_current_agent),
|
||
|
|
db: AsyncSession = Depends(get_db),
|
||
|
|
):
|
||
|
|
"""创建会话标注。
|
||
|
|
|
||
|
|
坐席对AI回复进行标注(有用/无用)。
|
||
|
|
|
||
|
|
Args:
|
||
|
|
body: 创建请求体
|
||
|
|
agent: 当前坐席
|
||
|
|
db: 数据库会话
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Dict: 统一响应格式,包含创建的标注
|
||
|
|
"""
|
||
|
|
annotation = ConversationAnnotation(
|
||
|
|
conversation_id=body.conversation_id,
|
||
|
|
agent_id=agent.id,
|
||
|
|
message_id=body.message_id,
|
||
|
|
feedback=body.feedback,
|
||
|
|
comment=body.comment,
|
||
|
|
)
|
||
|
|
db.add(annotation)
|
||
|
|
await db.flush()
|
||
|
|
|
||
|
|
logger.info(f"创建会话标注: conversation={body.conversation_id}, feedback={body.feedback}")
|
||
|
|
|
||
|
|
data = AnnotationResponse.model_validate(annotation).model_dump()
|
||
|
|
return success_response(data=data)
|
||
|
|
|
||
|
|
|
||
|
|
# --------------------------------------------------------------------------
|
||
|
|
# GET /api/annotations/{conversation_id} — 获取会话的标注列表
|
||
|
|
# --------------------------------------------------------------------------
|
||
|
|
@router.get("/annotations/{conversation_id}")
|
||
|
|
async def list_annotations(
|
||
|
|
conversation_id: str,
|
||
|
|
agent: Agent = Depends(get_current_agent),
|
||
|
|
db: AsyncSession = Depends(get_db),
|
||
|
|
):
|
||
|
|
"""获取会话的所有标注。
|
||
|
|
|
||
|
|
Args:
|
||
|
|
conversation_id: 会话ID
|
||
|
|
agent: 当前坐席
|
||
|
|
db: 数据库会话
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Dict: 统一响应格式,包含标注列表
|
||
|
|
"""
|
||
|
|
stmt = (
|
||
|
|
select(ConversationAnnotation)
|
||
|
|
.where(ConversationAnnotation.conversation_id == conversation_id)
|
||
|
|
.order_by(ConversationAnnotation.created_at.desc())
|
||
|
|
)
|
||
|
|
|
||
|
|
result = await db.execute(stmt)
|
||
|
|
annotations = list(result.scalars().all())
|
||
|
|
|
||
|
|
data = [AnnotationResponse.model_validate(a).model_dump() for a in annotations]
|
||
|
|
return success_response(data={"items": data})
|