> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Huggingface 端点集成

> 使用 LangChain Python 与 Huggingface 端点 LLM 集成。

> [Hugging Face Hub](https://huggingface.co/docs/hub/index) 是一个拥有超过 12 万个模型、2 万个数据集和 5 万个演示应用（Spaces）的平台，所有内容均为开源且公开可用。这是一个在线平台，人们可以在此轻松协作并共同构建机器学习应用。

`Hugging Face Hub` 还提供各种端点来构建机器学习应用。
本示例展示了如何连接到不同的端点类型。

特别是，文本生成推理由 [文本生成推理](https://github.com/huggingface/text-generation-inference) 提供支持：这是一个定制的 Rust、Python 和 gRPC 服务器，用于实现极速文本生成推理。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_huggingface import HuggingFaceEndpoint
```

## 安装与设置

要使用，您应该已安装 `huggingface_hub` Python [包](https://huggingface.co/docs/huggingface_hub/installation)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU huggingface_hub
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 获取令牌：https://huggingface.co/docs/api-inference/quicktour#get-your-api-token

from getpass import getpass

HUGGINGFACEHUB_API_TOKEN = getpass()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
```

## 准备示例

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_huggingface import HuggingFaceEndpoint
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_classic.chains import LLMChain
from langchain_core.prompts import PromptTemplate
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
question = "Who won the FIFA World Cup in the year 1994? "

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)
```

## 示例

这是一个如何访问服务器端 [推理提供商](https://huggingface.co/docs/inference-providers) API 的 `HuggingFaceEndpoint` 集成的示例。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
repo_id = "deepseek-ai/DeepSeek-R1-0528"

llm = HuggingFaceEndpoint(
    repo_id=repo_id,
    max_length=128,
    temperature=0.5,
    huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
    provider="auto",  # 在此处设置您的提供商 hf.co/settings/inference-providers
    # provider="hyperbolic",
    # provider="nebius",
    # provider="together",
)
llm_chain = prompt | llm
print(llm_chain.invoke({"question": question}))
```

## 专用端点

免费的无服务器 API 让您可以快速实现解决方案并进行迭代，但对于重度使用场景，它可能会受到速率限制，因为负载是与其他请求共享的。

对于企业工作负载，最好使用 [推理端点 - 专用](https://huggingface.co/inference-endpoints/dedicated)。
这提供了完全托管的基础设施，提供更大的灵活性和速度。这些资源附带持续支持和正常运行时间保证，以及自动扩展等选项。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 在下方设置您的推理端点 URL
your_endpoint_url = "https://fayjubiy2xqn36z0.us-east-1.aws.endpoints.huggingface.cloud"
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
llm = HuggingFaceEndpoint(
    endpoint_url=f"{your_endpoint_url}",
    max_new_tokens=512,
    top_k=10,
    top_p=0.95,
    typical_p=0.95,
    temperature=0.01,
    repetition_penalty=1.03,
)
llm("What did foo say about bar?")
```

### 流式传输

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.callbacks import StreamingStdOutCallbackHandler
from langchain_huggingface import HuggingFaceEndpoint

llm = HuggingFaceEndpoint(
    endpoint_url=f"{your_endpoint_url}",
    max_new_tokens=512,
    top_k=10,
    top_p=0.95,
    typical_p=0.95,
    temperature=0.01,
    repetition_penalty=1.03,
    streaming=True,
)
llm("What did foo say about bar?", callbacks=[StreamingStdOutCallbackHandler()])
```

同一个 `HuggingFaceEndpoint` 类也可以与本地 [HuggingFace TGI 实例](https://github.com/huggingface/text-generation-inference/blob/main/docs/source/index.md) 一起使用来提供 LLM 服务。有关各种硬件（GPU、TPU、Gaudi...）支持的详细信息，请查看 TGI [仓库](https://github.com/huggingface/text-generation-inference/tree/main)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/llms/huggingface_endpoint.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
