Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

HuggingFace

最終更新 Markdown で表示Agent セットアップ

HuggingFace は、機械学習モデルの構築、デプロイ、学習を支援します。

エンドポイント

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

URL の構成

HuggingFace Inference 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 Gateway のアカウント ID
  • AI Gateway のゲートウェイ名
  • 有効な HuggingFace API トークン
  • 使う HuggingFace モデルの名前

cURL

リクエストbash
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 ライブラリを使う場合は、推論エンドポイントを次のように設定できます。

JavaScriptjs
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);

役に立ちましたか?