当前位置: 首页 > article >正文

【大语言模型_8】vllm启动的模型通过fastapi封装增加api-key验证

背景:

      vllm推理框架启动模型不具备api-key验证。需借助fastapi可以实现该功能

代码实现:

rom fastapi import FastAPI, Header, HTTPException, Request,Response
import httpx
import logging

# 创建 FastAPI 应用
app = FastAPI()
logging.basicConfig(level=logging.DEBUG)
# 配置 vLLM 的服务地址
VLLM_BASE_URL = "http://localhost:25010"

# 定义合法的 API Key 列表(可以根据需要扩展为数据库或配置文件)
VALID_API_KEYS = {"zml_123456789", "zml_1234567890"}

# 中间件:验证 API Key
# 验证 API Key
async def verify_api_key(authorization: str = Header(None)):
    # 打印接收到的 Authorization 字段
    logging.debug(f"Received Authorization header: {authorization}")

    # 检查 Authorization 是否存在且以 "Bearer " 开头
    if not authorization or not isinstance(authorization, str) or not authorization.startswith("Bearer "):
        raise HTTPException(status_code=403, detail="Invalid Authorization Header")

    # 提取 API Key
    try:
        api_key = authorization.split(" ")[1]  # 提取 "Bearer " 后的部分
    except IndexError:
        raise HTTPException(status_code=403, detail="Malformed Authorization Header")

    # 验证 API Key 是否合法
    if api_key not in VALID_API_KEYS:
        raise HTTPException(status_code=403, detail="Invalid API Key")



# 代理路由:转发请求到 vLLM
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(request: Request, path: str, authorization: str = Header(None)):
    # 验证 API Key
    logging.debug(f"Received request with path: {path}")
    logging.debug(f"Received headers: {request.headers}")
    await verify_api_key(authorization)

    # 构造转发的目标 URL
    target_url = f"{VLLM_BASE_URL}/{path}"

    # 获取请求体和查询参数
    body = await request.body()
    query_params = request.query_params

    # 使用 httpx 转发请求
    async with httpx.AsyncClient() as client:
        # 根据请求方法转发
        response = await client.request(
            method=request.method,
            url=target_url,
            params=query_params,
            data=body,
            headers={key: value for key, value in request.headers.items() if key != "host"}
        )

    # 返回 vLLM 的响应
    return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers))

三、启动

uvicorn my_fastapi:app  --host=0.0.0.0 --port=12345
# my_fastapi 为脚本名称

通过访问fastapi提供的12345即可实现改功能


http://www.kler.cn/a/597135.html

相关文章:

  • ci如何做才能做到每秒rps 为3000+
  • Doris性能优化建议
  • 失物招领|校园失物招领系统|基于Springboot的校园失物招领系统设计与实现(源码+数据库+文档)
  • 【JavaEE进阶】部署Web项目到Linux服务器
  • shell流程控制
  • 操作系统导论——第13章 抽象:地址空间
  • Python网络编程入门
  • C语言入门教程100讲(2)变量与常量
  • 随笔(1)
  • 大模型-提示词工程与架构
  • MySQL Router被HTTP流量击穿
  • linux之 内存管理(1)-armv8 内核启动页表建立过程
  • Python协程调度
  • 一文分清重载与重写:Java 编程基础的关键辨析
  • OBOO鸥柏丨广告机终端控制端KylinOS麒麟、统信UOS/鸿蒙国产系统
  • nextjs使用next-intl要注意
  • 整理和总结微信小程序的高频知识点
  • SQL Server2019安装步骤+使用+解决部分报错+卸载(超详细 附下载链接)
  • 【GeeRPC】项目总结:使用 Golang 实现 RPC 框架
  • 可以高效记录工作生活琐事的提醒APP工具