Skip to content

HuggingFace Inference API

HuggingFace Inference API 提供对数千种开源模型的即时访问,支持多种任务和模型类型。

支持的模型类型

对话模型

  • meta-llama/Llama-2-70b-chat-hf - Llama 2 对话模型
  • mistralai/Mixtral-8x7B-Instruct-v0.1 - Mixtral 指令模型
  • microsoft/DialoGPT-large - 对话 GPT

代码模型

  • bigcode/starcoder - StarCoder 代码模型
  • Salesforce/codegen-16B-multi - CodeGen 多语言
  • microsoft/CodeGPT-small-py - Python 代码 GPT

文本生成模型

  • bigscience/bloom - BLOOM 大模型
  • EleutherAI/gpt-neox-20b - GPT-NeoX
  • facebook/opt-30b - OPT 模型

多语言模型

  • google/flan-t5-xxl - FLAN-T5
  • bert-base-multilingual-cased - 多语言 BERT

配置

基础配置

config.yaml~/.bytebuddy/config.yaml 中配置:

yaml
models:
  - name: "hf-llama"
    provider: "huggingfaceinferenceapi"
    model: "meta-llama/Llama-2-70b-chat-hf"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096

代码生成配置

yaml
models:
  - name: "hf-starcoder"
    provider: "huggingfaceinferenceapi"
    model: "bigcode/starcoder"
    apiKey: "${HF_API_KEY}"
    roles: ["edit", "apply"]
    defaultCompletionOptions:
      temperature: 0.2
      maxTokens: 2048

多模型配置

yaml
models:
  - name: "hf-chat"
    provider: "huggingfaceinferenceapi"
    model: "meta-llama/Llama-2-70b-chat-hf"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096

  - name: "hf-code"
    provider: "huggingfaceinferenceapi"
    model: "bigcode/starcoder"
    apiKey: "${HF_API_KEY}"
    roles: ["edit"]
    defaultCompletionOptions:
      temperature: 0.2
      maxTokens: 2048

  - name: "hf-multilingual"
    provider: "huggingfaceinferenceapi"
    model: "google/flan-t5-xxl"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.5
      maxTokens: 1024

配置字段

必需字段

  • name: 模型配置的唯一标识符
  • provider: 设置为 "huggingfaceinferenceapi"
  • model: 模型标识符(HuggingFace 模型路径)
  • apiKey: HuggingFace API 密钥

可选字段

  • apiBase: API 端点(默认:https://api-inference.huggingface.co
  • roles: 模型角色 [chat, edit, apply, autocomplete]
  • defaultCompletionOptions:
    • temperature: 控制随机性 (0-2)
    • maxTokens: 最大令牌数
    • topP: 核采样参数
    • topK: 采样候选数量
    • repetitionPenalty: 重复惩罚

环境变量

bash
# ~/.bashrc 或 ~/.zshrc
export HF_API_KEY="your-huggingface-api-key"

获取 API 密钥

  1. 访问 HuggingFace
  2. 注册账户并登录
  3. 导航到 Settings > Access Tokens
  4. 创建新的访问令牌
  5. 将令牌保存到环境变量

使用场景配置

通用对话

yaml
models:
  - name: "general-chat"
    provider: "huggingfaceinferenceapi"
    model: "mistralai/Mixtral-8x7B-Instruct-v0.1"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096

代码助手

yaml
models:
  - name: "code-assistant"
    provider: "huggingfaceinferenceapi"
    model: "bigcode/starcoder"
    apiKey: "${HF_API_KEY}"
    roles: ["edit", "apply"]
    defaultCompletionOptions:
      temperature: 0.2
      maxTokens: 2048

多语言翻译

yaml
models:
  - name: "translator"
    provider: "huggingfaceinferenceapi"
    model: "google/flan-t5-xxl"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.3
      maxTokens: 1024

高级配置

自定义端点

yaml
models:
  - name: "custom-endpoint"
    provider: "huggingfaceinferenceapi"
    model: "meta-llama/Llama-2-70b-chat-hf"
    apiBase: "https://api-inference.huggingface.co"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]

推理端点(专用)

yaml
models:
  - name: "dedicated-endpoint"
    provider: "huggingfaceinferenceapi"
    model: "meta-llama/Llama-2-70b-chat-hf"
    apiBase: "https://your-endpoint.endpoints.huggingface.cloud"
    apiKey: "${HF_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.5
      maxTokens: 4096

模型发现

浏览 HuggingFace Hub 查找可用模型:

故障排除

常见错误

  1. 401 Unauthorized: 检查 API 密钥是否正确
  2. 503 Service Unavailable: 模型正在加载,请稍候重试
  3. 429 Too Many Requests: 达到速率限制
  4. Model Not Found: 确认模型路径正确

调试步骤

  1. 验证 API 密钥格式和有效性
  2. 检查模型标识符是否正确
  3. 确认模型在 HuggingFace Hub 上可用
  4. 查看 HuggingFace 状态页面
  5. 检查速率限制和配额

使用限制

  • 免费层: 每月 30,000 次调用
  • 付费层: 更高的调用限制
  • 并发限制: 同时进行的请求数有限制
  • 模型加载: 首次请求可能需要等待模型加载

最佳实践

1. 模型选择

  • 根据任务需求选择合适的模型
  • 考虑模型大小和响应时间
  • 优先使用验证过的流行模型
  • 查看模型卡片了解使用限制

2. 性能优化

  • 速度优先: 选择较小的模型
  • 质量优先: 选择较大的模型
  • 生产环境: 考虑使用专用推理端点
  • 实现请求缓存机制

3. 成本管理

  • 监控 API 使用量
  • 选择合适大小的模型
  • 考虑使用推理端点(按小时计费)
  • 设置配额警报

4. 错误处理

  • 实现重试机制
  • 处理模型加载等待
  • 优雅地处理 API 限制
  • 记录错误日志

推理端点

对于生产环境,建议使用专用推理端点:

优势

  • 保证可用性
  • 更低延迟
  • 无冷启动
  • 更高吞吐量

配置示例

yaml
models:
  - name: "production-endpoint"
    provider: "huggingfaceinferenceapi"
    model: "meta-llama/Llama-2-70b-chat-hf"
    apiBase: "https://your-endpoint.endpoints.huggingface.cloud"
    apiKey: "${HF_ENDPOINT_TOKEN}"
    roles: ["chat", "edit"]
    defaultCompletionOptions:
      temperature: 0.5
      maxTokens: 4096