# 推理服务提供商

Hugging Face 的模型页面为数千个模型提供按量付费的推理服务，因此您可以直接在浏览器中试用所有这些模型。该服务由 Inference Providers 提供支持，PRO 用户以及 Team & Enterprise 组织每月享有包含的额度。

Inference Providers 为开发者提供简洁、统一的访问方式，可调用数百个机器学习模型，由最佳的无服务器推理合作伙伴提供支持。👉 **完整文档请访问 [Inference Providers 文档](https://huggingface.co/docs/inference-providers)**。

## Hub 上的推理服务提供商

Inference Providers 与 Hugging Face Hub 深度集成，您可以通过以下几种方式使用它：

- **交互式小部件** - 通过交互式小部件直接在模型页面上测试模型，其底层使用 Inference Providers。请查看 [DeepSeek-R1-0528 模型页面](https://huggingface.co/deepseek-ai/DeepSeek-R1-0528) 以获取示例。
- **推理游乐场** - 轻松使用您的提示词测试和比较聊天补全模型。查看 [推理游乐场](https://huggingface.co/playground) 以开始使用。
- **搜索** - 在[模型页面](https://huggingface.co/models?inference_provider=all)上按推理提供商筛选模型，以查找通过特定提供商可用的模型。
- **Data Studio** - 使用 AI 探索 Hub 上的数据集。在您的心仪数据集上查看 [Data Studio](https://huggingface.co/datasets/fka/awesome-chatgpt-prompts/viewer?views%5B%5D=train)。

## 使用推理提供商进行构建

您可以使用我们的 SDK 或 HTTP 客户端将 Inference Providers 集成到您自己的应用程序中。以下是使用 Python 和 JavaScript 的快速入门指南，更多详情请参阅 [Inference Providers 文档](https://huggingface.co/docs/inference-providers)。

您可以使用我们的 Python SDK 与 Inference Providers 进行交互。

```python
from huggingface_hub import InferenceClient

import os

client = InferenceClient(
    api_key=os.environ["HF_TOKEN"],
    provider="auto",   # Automatically selects best provider
)

# Chat completion
completion = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3-0324",
    messages=[{"role": "user", "content": "A story about hiking in the mountains"}]
)

# Image generation
image = client.text_to_image(
    prompt="A serene lake surrounded by mountains at sunset, photorealistic style",
    model="black-forest-labs/FLUX.1-dev"
)

```

或者，您可以直接使用兼容 OpenAI API 的客户端。

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="/static-proxy?url=https%3A%2F%2Frouter.huggingface.co%2Fv1",
    api_key=os.environ["HF_TOKEN"],
)

completion = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3-0324",
    messages=[
        {
            "role": "user",
            "content": "A story about hiking in the mountains"
        }
    ],
)
```

> [!WARNING]
> OpenAI API 兼容客户端不支持图像生成。

您可以使用我们的 JavaScript SDK 与 Inference Providers 进行交互。

```javascript
import { InferenceClient } from "@huggingface/inference";

const client = new InferenceClient(process.env.HF_TOKEN);

const chatCompletion = await client.chatCompletion({
    provider: "auto",  // Automatically selects best provider  
    model: "deepseek-ai/DeepSeek-V3-0324",
    messages: [{ role: "user", content: "Hello!" }]
});

const imageBlob = await client.textToImage({
  model: "black-forest-labs/FLUX.1-dev",
  inputs:
    "A serene lake surrounded by mountains at sunset, photorealistic style",
});
```

或者，您可以直接使用兼容 OpenAI API 的客户端。

```javascript
import { OpenAI } from "openai";

const client = new OpenAI({
  baseURL: "/static-proxy?url=https%3A%2F%2Frouter.huggingface.co%2Fv1",
  apiKey: process.env.HF_TOKEN,
});

const completion = await client.chat.completions.create({
  model: "meta-llama/Llama-3.1-8B-Instruct",
  messages: [{ role: "user", content: "A story about hiking in the mountains" }],
});

```

> [!WARNING]
> OpenAI API 兼容客户端不支持图像生成。

您需要一个具有推理权限的 Hugging Face 令牌。请在 [Settings > Tokens](https://huggingface.co/settings/tokens/new?ownUserPermissions=inference.serverless.write&tokenType=fineGrained) 中创建一个。

### Inference Providers 的工作原理

如需深入了解 Inference Providers，请参阅 [Inference Providers 文档](https://huggingface.co/docs/inference-providers)。以下是一些关键资源：

- **[快速入门](https://huggingface.co/docs/inference-providers)**
- **[定价与计费指南](https://huggingface.co/docs/inference-providers/pricing)**
- **[Hub 集成详情](https://huggingface.co/docs/inference-providers/hub-integration)**

### HF-Inference API 是什么？

HF-Inference API 是通过 Inference Providers 可用的提供商之一。它此前被称为“Inference API (serverless)”，其底层由 [Inference Endpoints](https://huggingface.co/docs/inference-endpoints/index) 提供支持。

如需了解 HF-Inference 提供商的更多详细信息，请查看其[专属页面](https://huggingface.co/docs/inference-providers/providers/hf-inference)。

