第 12 章 配置 API 密钥身份验证


配置用于使用红帽 AI 推测进行身份验证的 API 密钥。

Expand
表 12.1. API 端点身份验证
端点需要身份验证

/v1/chat/completions

/v1/completions

/v1/embeddings

/health

/ping

先决条件

  • 已安装 Podman 或 Docker。
  • 已安装 NVIDIA Container Toolkit 来启用 Podman 对 GPU 的访问。
  • 您以具有 sudo 访问权限的用户身份登录。
  • 您可以访问 registry.redhat.io 并已登录。
  • 您有一个 Hugging Face 帐户,并生成了一个 Hugging Face 访问令牌。

流程

  1. 使用 API 密钥身份验证启动 inference 服务器:

    $ podman run --rm -it \
      --device nvidia.com/gpu=all \
      --security-opt=label=disable \
      --shm-size=4GB -p 8000:8000 \
      -v ./rhaii-cache:/opt/app-root/src/.cache \
      registry.redhat.io/rhaii-early-access/vllm-cuda-rhel9:3.5.0-ea.2 \
      --model RedHatAI/Llama-3.2-1B-Instruct-FP8 \
      --api-key "<your_api_key>"
  2. 使用 Authorization 标头中的 API 密钥发送请求:

    $ curl http://localhost:8000/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer <your_api_key>" \
      -d '{
        "model": "RedHatAI/Llama-3.2-1B-Instruct-FP8",
        "messages": [{"role": "user", "content": "Hello!"}]
      }'

验证

  • 成功请求会返回带有模型输出的 200 OK 响应。
  • 没有 API 密钥的请求会返回 401 Unauthorized
  • 具有无效 API 键的请求将返回 403 Forbidden
重要

API 密钥身份验证为您的 API 端点提供基本保护。这不是一个全面的安全解决方案。对于生产环境部署,请考虑使用 --ssl-keyfile ,--ssl- certfile , 和--ssl- ca-certs 服务器参数启用 SSL/TLS 加密。不要在源代码中硬编码 API 密钥。使用环境变量或 secret 管理解决方案。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部