94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
|
|
"""
|
|||
|
|
反馈数据 API — 用户反馈分析、记录查询、反例生成
|
|||
|
|
"""
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import logging
|
|||
|
|
from typing import Optional
|
|||
|
|
|
|||
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|||
|
|
from sqlalchemy.orm import Session
|
|||
|
|
|
|||
|
|
from app.core.database import get_db
|
|||
|
|
from app.api.auth import get_current_user
|
|||
|
|
from app.models.user import User
|
|||
|
|
from app.services.feedback_learner import feedback_learner
|
|||
|
|
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
router = APIRouter(prefix="/api/v1/feedback", tags=["feedback"])
|
|||
|
|
|
|||
|
|
|
|||
|
|
@router.get("/analysis")
|
|||
|
|
def get_feedback_analysis(
|
|||
|
|
days: int = Query(7, ge=1, le=90, description="统计天数"),
|
|||
|
|
agent_name: Optional[str] = Query(None, description="按 Agent 筛选"),
|
|||
|
|
current_user: User = Depends(get_current_user),
|
|||
|
|
):
|
|||
|
|
"""获取反馈分析报告 — 信号分布、负面率、策略建议。"""
|
|||
|
|
result = feedback_learner.analyze_feedback_patterns(agent_name=agent_name, days=days)
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
|
|||
|
|
@router.get("/records")
|
|||
|
|
def list_feedback_records(
|
|||
|
|
agent_name: Optional[str] = Query(None, description="按 Agent 筛选"),
|
|||
|
|
signal_type: Optional[str] = Query(None, description="按信号类型筛选"),
|
|||
|
|
days: int = Query(30, ge=1, le=365, description="统计天数"),
|
|||
|
|
limit: int = Query(50, ge=1, le=200, description="返回条数"),
|
|||
|
|
offset: int = Query(0, ge=0, description="偏移量"),
|
|||
|
|
db: Session = Depends(get_db),
|
|||
|
|
current_user: User = Depends(get_current_user),
|
|||
|
|
):
|
|||
|
|
"""查询反馈记录列表。"""
|
|||
|
|
from datetime import datetime, timedelta
|
|||
|
|
from app.models.feedback_record import FeedbackRecord
|
|||
|
|
|
|||
|
|
since = datetime.now() - timedelta(days=days)
|
|||
|
|
q = db.query(FeedbackRecord).filter(FeedbackRecord.created_at >= since)
|
|||
|
|
|
|||
|
|
if agent_name:
|
|||
|
|
q = q.filter(FeedbackRecord.agent_name == agent_name)
|
|||
|
|
if signal_type:
|
|||
|
|
q = q.filter(FeedbackRecord.signal_type == signal_type)
|
|||
|
|
|
|||
|
|
total = q.count()
|
|||
|
|
records = (
|
|||
|
|
q.order_by(FeedbackRecord.created_at.desc())
|
|||
|
|
.offset(offset)
|
|||
|
|
.limit(limit)
|
|||
|
|
.all()
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
"total": total,
|
|||
|
|
"limit": limit,
|
|||
|
|
"offset": offset,
|
|||
|
|
"items": [
|
|||
|
|
{
|
|||
|
|
"id": r.id,
|
|||
|
|
"user_id": r.user_id,
|
|||
|
|
"signal_type": r.signal_type,
|
|||
|
|
"severity": r.severity,
|
|||
|
|
"execution_log_id": r.execution_log_id,
|
|||
|
|
"agent_name": r.agent_name,
|
|||
|
|
"original_output": (r.original_output or "")[:200],
|
|||
|
|
"user_correction": (r.user_correction or "")[:200],
|
|||
|
|
"learned": r.learned,
|
|||
|
|
"lesson_summary": r.lesson_summary,
|
|||
|
|
"created_at": r.created_at.isoformat() if r.created_at else None,
|
|||
|
|
}
|
|||
|
|
for r in records
|
|||
|
|
],
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
@router.get("/negative-examples/{agent_name}")
|
|||
|
|
def get_negative_examples(
|
|||
|
|
agent_name: str,
|
|||
|
|
limit: int = Query(5, ge=1, le=20, description="返回条数"),
|
|||
|
|
current_user: User = Depends(get_current_user),
|
|||
|
|
):
|
|||
|
|
"""获取指定 Agent 的反例(用于改进 system prompt)。"""
|
|||
|
|
examples = feedback_learner.generate_negative_examples(agent_name=agent_name, limit=limit)
|
|||
|
|
return {"agent_name": agent_name, "count": len(examples), "examples": examples}
|