80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
|
|
"""
|
|||
|
|
RAG Lab endpoints for debugging and experimentation.
|
|||
|
|
[AC-ASA-05] RAG experiment debugging with retrieval results and prompt visualization.
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import logging
|
|||
|
|
from typing import Annotated, Any, List
|
|||
|
|
|
|||
|
|
from fastapi import APIRouter, Depends
|
|||
|
|
from fastapi.responses import JSONResponse
|
|||
|
|
|
|||
|
|
from app.core.tenant import get_tenant_id
|
|||
|
|
from app.models import ErrorResponse
|
|||
|
|
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
router = APIRouter(prefix="/admin/rag", tags=["RAG Lab"])
|
|||
|
|
|
|||
|
|
|
|||
|
|
@router.post(
|
|||
|
|
"/experiments/run",
|
|||
|
|
operation_id="runRagExperiment",
|
|||
|
|
summary="Run RAG debugging experiment",
|
|||
|
|
description="[AC-ASA-05] Trigger RAG experiment with retrieval and prompt generation.",
|
|||
|
|
responses={
|
|||
|
|
200: {"description": "Experiment results with retrieval and prompt"},
|
|||
|
|
401: {"description": "Unauthorized", "model": ErrorResponse},
|
|||
|
|
403: {"description": "Forbidden", "model": ErrorResponse},
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
async def run_rag_experiment(
|
|||
|
|
tenant_id: Annotated[str, Depends(get_tenant_id)],
|
|||
|
|
query: str,
|
|||
|
|
kb_ids: List[str],
|
|||
|
|
params: dict = None,
|
|||
|
|
) -> JSONResponse:
|
|||
|
|
"""
|
|||
|
|
[AC-ASA-05] Run RAG experiment and return retrieval results with final prompt.
|
|||
|
|
"""
|
|||
|
|
logger.info(
|
|||
|
|
f"[AC-ASA-05] Running RAG experiment: tenant={tenant_id}, "
|
|||
|
|
f"query={query}, kb_ids={kb_ids}"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
mock_retrieval_results = [
|
|||
|
|
{
|
|||
|
|
"content": "产品价格根据套餐不同有所差异,基础版每月99元,专业版每月299元。",
|
|||
|
|
"score": 0.92,
|
|||
|
|
"source": "product_manual.pdf",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"content": "企业版提供定制化服务,请联系销售获取报价。",
|
|||
|
|
"score": 0.85,
|
|||
|
|
"source": "pricing_guide.docx",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"content": "所有套餐均支持7天无理由退款。",
|
|||
|
|
"score": 0.78,
|
|||
|
|
"source": "faq.pdf",
|
|||
|
|
},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
mock_final_prompt = f"""基于以下检索到的信息,回答用户问题:
|
|||
|
|
|
|||
|
|
用户问题:{query}
|
|||
|
|
|
|||
|
|
检索结果:
|
|||
|
|
1. [Score: 0.92] 产品价格根据套餐不同有所差异,基础版每月99元,专业版每月299元。
|
|||
|
|
2. [Score: 0.85] 企业版提供定制化服务,请联系销售获取报价。
|
|||
|
|
3. [Score: 0.78] 所有套餐均支持7天无理由退款。
|
|||
|
|
|
|||
|
|
请基于以上信息生成专业、准确的回答。"""
|
|||
|
|
|
|||
|
|
return JSONResponse(
|
|||
|
|
content={
|
|||
|
|
"retrievalResults": mock_retrieval_results,
|
|||
|
|
"finalPrompt": mock_final_prompt,
|
|||
|
|
}
|
|||
|
|
)
|