diff --git a/webapp/api/score.py b/webapp/api/score.py index fa46335..966b57f 100644 --- a/webapp/api/score.py +++ b/webapp/api/score.py @@ -34,16 +34,60 @@ def _check_auth(authorization: str | None, token: str) -> None: response_model=ScoreResponse, summary="单题实时评分(Dify 外部 Tool)", responses={ - 200: {"description": "各指标得分和加权综合得分。"}, + 200: { + "description": "各指标得分、加权综合得分及耗时。", + "content": { + "application/json": { + "example": { + "scores": { + "faithfulness": 0.875, + "answer_relevancy": 0.920, + "context_recall": 0.810, + "context_precision": 0.850, + }, + "weighted_score": 0.8638, + "latency_ms": 3420, + "skipped_metrics": [], + "error": None, + } + } + }, + }, 401: {"description": "配置了 SCORE_API_TOKEN 但未提供有效 Bearer token。"}, - 422: {"description": "请求参数校验失败。"}, + 422: {"description": "请求参数校验失败(必填字段缺失或 metrics 名称不合法)。"}, }, ) def score_sample( request: ScoreRequest, authorization: Annotated[str | None, Header()] = None, ) -> ScoreResponse: - """Accept one QA sample, run RAGAS metrics synchronously, and return scores.""" + """接受单条问答记录,同步运行 RAGAS 指标打分,实时返回各指标得分。 + + **主要用途**:供 Dify 外部 Tool 调用。Dify Agent 在生成回答后,将 + `(question, answer, contexts)` 发送到此端点,即可获得 RAGAS 质量评分, + 用于日志记录、质量监控或触发 Agent 自我改进流程。 + + **contexts 格式**:多个检索片段用 `context_separator`(默认 `" |||| "`)拼接为一个字符串, + 服务端自动拆分后传入 RAGAS 管道。 + + **ground_truth 可选**: + - 提供时:所有指定指标均参与计算。 + - 缺失时:自动跳过依赖参考答案的指标(`context_recall`、 + `factual_correctness`、`semantic_similarity`、`noise_sensitivity`), + 跳过的指标在响应的 `skipped_metrics` 列表中列出,对应 `scores` 值为 `null`。 + + **支持的 RAGAS 指标**: + - `faithfulness` — 回答与检索片段的事实一致性 + - `answer_relevancy` — 回答与问题的相关性 + - `context_recall` — 参考答案覆盖到的检索内容比例(需 ground_truth) + - `context_precision` — 检索片段中与答案相关的部分占比 + - `noise_sensitivity` — 对无关噪声片段的敏感度(需 ground_truth) + - `factual_correctness` — 回答与参考答案的事实准确性(需 ground_truth) + - `semantic_similarity` — 回答与参考答案的语义相似度(需 ground_truth) + + **鉴权**:若 `.env` 中配置了 `SCORE_API_TOKEN`,需在请求头携带 + `Authorization: Bearer `;留空则无需鉴权(适合内网部署)。 + """ settings = _get_settings() # Require Bearer auth only when the deployment configured a shared token. diff --git a/webapp/server.py b/webapp/server.py index ff2a7e9..edda9bc 100644 --- a/webapp/server.py +++ b/webapp/server.py @@ -92,7 +92,7 @@ def create_app() -> FastAPI: "- **报告 API** — 查询历史运行记录与评估报告\n\n" "> **快速开始**:调用 `POST /api/pipeline/jobs` 传入 PDF 文件夹路径即可启动完整评估流程。" ), - version="0.2.0", + version="0.3.0", openapi_tags=OPENAPI_TAGS, )