Skip to content

Commit 8ed6195

Browse files
docs(huggingface): conform chat page to required headers
1 parent 3f9f2e9 commit 8ed6195

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

docs/docs/integrations/chat/huggingface.mdx

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@ title: Hugging Face (chat)
33
sidebar_label: Hugging Face
44
---
55

6-
## Chat models with Hugging Face
6+
## Overview
7+
8+
This page shows how to use Hugging Face models as chat models in LangChain.
9+
10+
## Setup
11+
12+
Install the required packages:
13+
14+
```bash
15+
pip install langchain-huggingface transformers
16+
```
17+
18+
> For Hugging Face pipelines, prefer `max_new_tokens` (not `max_tokens`). The pipeline will use CPU/GPU automatically depending on availability.
19+
20+
## Instantiation
721

822
### Option 1 (works today): pipeline → wrap with `ChatHuggingFace`
923

@@ -15,24 +29,17 @@ from langchain_huggingface import ChatHuggingFace
1529
pipe = pipeline(
1630
"text-generation",
1731
model="microsoft/Phi-3-mini-4k-instruct",
18-
do_sample=False, # deterministic (similar to temperature=0)
32+
do_sample=False, # deterministic
1933
max_new_tokens=128, # HF uses max_new_tokens (not max_tokens)
2034
)
2135

2236
# Wrap the pipeline as a LangChain chat model
2337
llm = ChatHuggingFace(llm=pipe)
24-
25-
print(llm.invoke("Say hi in one sentence.").content)
2638
```
2739

28-
:::note
29-
- **Install**: `pip install langchain-huggingface transformers`
30-
- For Hugging Face pipelines prefer `max_new_tokens` (not `max_tokens`).
31-
:::
32-
3340
### Option 2 (coming after fix): `init_chat_model(..., model_provider="huggingface")`
3441

35-
Once available, you’ll be able to initialize via `init_chat_model`:
42+
Once available in your version, you can initialize via `init_chat_model`:
3643

3744
```python
3845
from langchain.chat_models import init_chat_model
@@ -44,10 +51,36 @@ llm = init_chat_model(
4451
do_sample=False,
4552
max_new_tokens=128,
4653
)
54+
```
4755

48-
print(llm.invoke("Say hi in one sentence.").content)
56+
> If your version doesn’t support this yet, use **Option 1** above.
57+
58+
## Invocation
59+
60+
```python
61+
msg = llm.invoke("Say hi in one sentence.")
62+
print(msg.content)
63+
```
64+
65+
## Chaining
66+
67+
```python
68+
from langchain_core.prompts import ChatPromptTemplate
69+
70+
prompt = ChatPromptTemplate.from_messages([
71+
("system", "You are helpful."),
72+
("human", "{question}"),
73+
])
74+
75+
chain = prompt | llm
76+
result = chain.invoke({"question": "What is the capital of France?"})
77+
print(result.content)
4978
```
5079

51-
This path depends on a bug fix tracked for Hugging Face chat initialization. If your version doesn’t support it yet, use Option 1 above.
80+
## API reference
81+
82+
- `langchain_huggingface.ChatHuggingFace`
83+
- `transformers.pipeline` (Hugging Face)
84+
- `langchain.chat_models.init_chat_model` (when available for Hugging Face)
5285

5386

0 commit comments

Comments
 (0)