Skip to content
Cloudflare Docs
非官方翻译 - 此文档为非官方中文翻译版本,仅供参考。如有疑问请以 英文官方文档 为准。

HuggingFace

HuggingFace 帮助用户构建、部署和训练机器学习模型。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface

URL 结构

在向 HuggingFace 推理 API 发出请求时,将您当前使用的 URL 中的 https://api-inference.huggingface.co/models/ 替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface。请注意,您要访问的模型应该紧跟其后,例如 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface/bigcode/starcoder

前提条件

在向 HuggingFace 发出请求时,确保您具有以下内容:

  • 您的 AI 网关账户 ID。
  • 您的 AI 网关网关名称。
  • 一个有效的 HuggingFace API 令牌。
  • 您要使用的 HuggingFace 模型的名称。

示例

cURL

请求
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface/bigcode/starcoder \
--header 'Authorization: Bearer {hf_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"inputs": "console.log"
}'

在 JavaScript 中使用 HuggingFace.js 库

如果您使用 HuggingFace.js 库,您可以这样设置您的推理端点:

JavaScript
import { HfInferenceEndpoint } from "@huggingface/inference";
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const model = "gpt2";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/huggingface/${model}`;
const apiToken = env.HF_API_TOKEN;
const hf = new HfInferenceEndpoint(baseURL, apiToken);