跳转至

HTTP 路由

cg_rag_http 将 CG_RAG 服务注册到 FastAPI 应用。默认前缀为 /cg-rag,主要路由包括健康检查、profile 列表、预热、检索重排、受约束生成和完整 RAG 调用。

路由概览

  • GET /cg-rag/health:返回服务健康状态、active scope、profile 和生成配置状态。
  • GET /cg-rag/profiles:返回可用检索 profile。
  • POST /cg-rag/warmup:预热一个或多个检索 scope。
  • POST /cg-rag/retrieve-rerank:执行召回和重排,不调用生成模型。
  • POST /cg-rag/constrained-generate:基于调用方传入的候选条文执行受约束生成。
  • POST /cg-rag/rag:执行召回、重排和受约束生成。
  • POST /cg-rag/answer/rag 的兼容别名。

cg_rag_http

CgHttpRequestError

Bases: ValueError

源代码位于: eval_rag_results/scripts/question_generated_app/cg_rag_http.py
class CgHttpRequestError(ValueError):
    def __init__(self, message: str, *, details: dict[str, Any] | None = None) -> None:
        super().__init__(message)
        self.details = details or {}

register_cg_rag_routes(app: FastAPI, service: Any, *, prefix: str = '/cg-rag') -> None

源代码位于: eval_rag_results/scripts/question_generated_app/cg_rag_http.py
def register_cg_rag_routes(app: FastAPI, service: Any, *, prefix: str = "/cg-rag") -> None:
    install_request_id_middleware(app)
    route_prefix = _clean_prefix(prefix)

    @app.get(f"{route_prefix}/health")
    def cg_rag_health() -> Any:
        try:
            return _with_request_id(service.health())
        except Exception as exc:
            return _service_error_response(exc)

    @app.get(f"{route_prefix}/profiles")
    def cg_rag_profiles() -> Any:
        try:
            return service.profiles()
        except Exception as exc:
            return _service_error_response(exc)

    @app.post(f"{route_prefix}/warmup")
    async def cg_rag_warmup(request: Request) -> Any:
        parsed, error = await _parse_post_request(CgWarmupRequest, request)
        if error is not None:
            return error
        try:
            return _with_request_id(await run_in_threadpool(service.warmup, parsed.scopes))
        except Exception as exc:
            return _service_error_response(exc)

    @app.post(f"{route_prefix}/retrieve-rerank")
    async def cg_rag_retrieve_rerank(request: Request) -> Any:
        parsed, error = await _parse_post_request(CgRetrieveRerankRequest, request)
        if error is not None:
            return error
        try:
            return _with_request_id(await run_in_threadpool(service.retrieve_rerank, parsed))
        except Exception as exc:
            return _service_error_response(exc)

    @app.post(f"{route_prefix}/constrained-generate")
    async def cg_rag_constrained_generate(request: Request) -> Any:
        parsed, error = await _parse_post_request(CgConstrainedGenerateRequest, request)
        if error is not None:
            return error
        try:
            return _with_request_id(await run_in_threadpool(service.constrained_generate, parsed))
        except Exception as exc:
            return _service_error_response(exc)

    @app.post(f"{route_prefix}/rag")
    async def cg_rag(request: Request) -> Any:
        parsed, error = await _parse_post_request(CgRagRequest, request)
        if error is not None:
            return error
        try:
            return _with_request_id(await run_in_threadpool(service.rag, parsed))
        except Exception as exc:
            return _service_error_response(exc)

    @app.post(f"{route_prefix}/answer")
    async def cg_rag_answer(request: Request) -> Any:
        parsed, error = await _parse_post_request(CgRagRequest, request)
        if error is not None:
            return error
        try:
            return _with_request_id(await run_in_threadpool(service.rag, parsed))
        except Exception as exc:
            return _service_error_response(exc)