ChatHuggingFace Class — langchain Architecture
Architecture documentation for the ChatHuggingFace class in huggingface.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 3494c9ae_57ba_b07e_6111_547ce9e47031["ChatHuggingFace"] d009a608_c505_bd50_7200_0de8a69ba4b7["BaseChatModel"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|extends| d009a608_c505_bd50_7200_0de8a69ba4b7 fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|extends| fcfa55b0_4a86_fa31_a156_3c38c76a0a9b e0e879bf_e732_8d0f_6ce2_3d40e66f4eb3["HumanMessage"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|extends| e0e879bf_e732_8d0f_6ce2_3d40e66f4eb3 5c3ab140_4d2a_43d4_63cb_89f4c4b1a8d6["SystemMessage"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|extends| 5c3ab140_4d2a_43d4_63cb_89f4c4b1a8d6 08730ff7_b226_3fa8_95b3_951f5dd02f6f["huggingface.py"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|defined in| 08730ff7_b226_3fa8_95b3_951f5dd02f6f 0052cf48_4520_d917_cd87_ce41dcf3c082["__init__()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| 0052cf48_4520_d917_cd87_ce41dcf3c082 46064c38_b0cf_5d47_e8d8_63c262297fe1["_inherit_llm_properties()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| 46064c38_b0cf_5d47_e8d8_63c262297fe1 bc6b3745_104e_57cc_f1f7_a0e3bf60af68["validate_llm()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| bc6b3745_104e_57cc_f1f7_a0e3bf60af68 f6500ef0_569c_9359_8794_c968cbde7819["_set_model_profile()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| f6500ef0_569c_9359_8794_c968cbde7819 c9b1e29e_d535_f367_1c98_8c108a24ebe8["from_model_id()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| c9b1e29e_d535_f367_1c98_8c108a24ebe8 a7bcdf52_deae_3d72_4e1e_5d716c5bcbf4["_create_chat_result()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| a7bcdf52_deae_3d72_4e1e_5d716c5bcbf4 c5dad0da_9d6a_6fee_0105_7c6f48062a56["_generate()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| c5dad0da_9d6a_6fee_0105_7c6f48062a56 8ae020a8_956f_d088_5363_530d4da9a17c["_agenerate()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| 8ae020a8_956f_d088_5363_530d4da9a17c be304d31_238e_0d69_2a29_2d0cd95cf12b["_should_stream_usage()"] 3494c9ae_57ba_b07e_6111_547ce9e47031 -->|method| be304d31_238e_0d69_2a29_2d0cd95cf12b
Relationship Graph
Source Code
libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py lines 324–1247
class ChatHuggingFace(BaseChatModel):
r"""Hugging Face LLM's as ChatModels.
Works with `HuggingFaceTextGenInference`, `HuggingFaceEndpoint`,
`HuggingFaceHub`, and `HuggingFacePipeline` LLMs.
Upon instantiating this class, the model_id is resolved from the url
provided to the LLM, and the appropriate tokenizer is loaded from
the HuggingFace Hub.
Setup:
Install `langchain-huggingface` and ensure your Hugging Face token
is saved.
```bash
pip install langchain-huggingface
```
```python
from huggingface_hub import login
login() # You will be prompted for your HF key, which will then be saved locally
```
Key init args — completion params:
llm:
LLM to be used.
Key init args — client params:
custom_get_token_ids:
Optional encoder to use for counting tokens.
metadata:
Metadata to add to the run trace.
tags:
Tags to add to the run trace.
verbose:
Whether to print out response text.
See full list of supported init args and their descriptions in the params
section.
Instantiate:
```python
from langchain_huggingface import HuggingFaceEndpoint,
ChatHuggingFace
model = HuggingFaceEndpoint(
repo_id="microsoft/Phi-3-mini-4k-instruct",
task="text-generation",
max_new_tokens=512,
do_sample=False,
repetition_penalty=1.03,
)
chat = ChatHuggingFace(llm=model, verbose=True)
```
Invoke:
```python
messages = [
("system", "You are a helpful translator. Translate the user
sentence to French."),
("human", "I love programming."),
]
chat(...).invoke(messages)
```
```python
AIMessage(content='Je ai une passion pour le programme.\n\nIn
French, we use "ai" for masculine subjects and "a" for feminine
subjects. Since "programming" is gender-neutral in English, we
will go with the masculine "programme".\n\nConfirmation: "J\'aime
le programme." is more commonly used. The sentence above is
technically accurate, but less commonly used in spoken French as
"ai" is used less frequently in everyday speech.',
response_metadata={'token_usage': ChatCompletionOutputUsage
(completion_tokens=100, prompt_tokens=55, total_tokens=155),
'model': '', 'finish_reason': 'length'},
id='run-874c24b7-0272-4c99-b259-5d6d7facbc56-0')
```
Source
Frequently Asked Questions
What is the ChatHuggingFace class?
ChatHuggingFace is a class in the langchain codebase, defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py.
Where is ChatHuggingFace defined?
ChatHuggingFace is defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py at line 324.
What does ChatHuggingFace extend?
ChatHuggingFace extends BaseChatModel, AIMessage, HumanMessage, SystemMessage.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free