HuggingFaceEndpoint Class — langchain Architecture
Architecture documentation for the HuggingFaceEndpoint class in huggingface_endpoint.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD e6b830e0_e492_47f6_6d0a_65d69c36da10["HuggingFaceEndpoint"] b2c7d2a5_0852_93df_c3e1_828c36a88999["LLM"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|extends| b2c7d2a5_0852_93df_c3e1_828c36a88999 a9bcc907_7e24_581e_4eae_5cd5ceb419c1["huggingface_endpoint.py"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|defined in| a9bcc907_7e24_581e_4eae_5cd5ceb419c1 b596ca66_6498_941e_5d78_6d567661596b["build_extra()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| b596ca66_6498_941e_5d78_6d567661596b 2c3620f2_8054_b511_54d8_cdae832892e9["validate_environment()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 2c3620f2_8054_b511_54d8_cdae832892e9 8e24ce72_41ef_c5c2_c345_4569d6774bb0["_default_params()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 8e24ce72_41ef_c5c2_c345_4569d6774bb0 d298f75e_044d_77dc_97da_4d178195d213["_identifying_params()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| d298f75e_044d_77dc_97da_4d178195d213 06aea143_f124_d79d_f4e1_5aab93e8d429["_llm_type()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 06aea143_f124_d79d_f4e1_5aab93e8d429 d3d02905_01f3_3fb8_f596_5694650af72c["_invocation_params()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| d3d02905_01f3_3fb8_f596_5694650af72c 477387e3_0b93_4ca4_57d9_1d292e64d8f9["_call()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 477387e3_0b93_4ca4_57d9_1d292e64d8f9 56f985d3_9992_f0cb_ad40_e11190fde3bb["_acall()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 56f985d3_9992_f0cb_ad40_e11190fde3bb 5b897346_9859_18d1_14ba_55cddb3d7da7["_stream()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| 5b897346_9859_18d1_14ba_55cddb3d7da7 cde77cc5_1e1f_9bb4_ed01_e6c99050c598["_astream()"] e6b830e0_e492_47f6_6d0a_65d69c36da10 -->|method| cde77cc5_1e1f_9bb4_ed01_e6c99050c598
Relationship Graph
Source Code
libs/partners/huggingface/langchain_huggingface/llms/huggingface_endpoint.py lines 29–455
class HuggingFaceEndpoint(LLM):
"""Hugging Face Endpoint. This works with any model that supports text generation (i.e. text completion) task.
To use this class, you should have installed the `huggingface_hub` package, and
the environment variable `HUGGINGFACEHUB_API_TOKEN` set with your API token,
or given as a named parameter to the constructor.
Example:
```python
# Basic Example (no streaming)
model = HuggingFaceEndpoint(
endpoint_url="http://localhost:8010/",
max_new_tokens=512,
top_k=10,
top_p=0.95,
typical_p=0.95,
temperature=0.01,
repetition_penalty=1.03,
huggingfacehub_api_token="my-api-key",
)
print(model.invoke("What is Deep Learning?"))
# Streaming response example
from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
callbacks = [StreamingStdOutCallbackHandler()]
model = HuggingFaceEndpoint(
endpoint_url="http://localhost:8010/",
max_new_tokens=512,
top_k=10,
top_p=0.95,
typical_p=0.95,
temperature=0.01,
repetition_penalty=1.03,
callbacks=callbacks,
streaming=True,
huggingfacehub_api_token="my-api-key",
)
print(model.invoke("What is Deep Learning?"))
# Basic Example (no streaming) with Mistral-Nemo-Base-2407 model using a third-party provider (Novita).
model = HuggingFaceEndpoint(
repo_id="mistralai/Mistral-Nemo-Base-2407",
provider="novita",
max_new_tokens=100,
do_sample=False,
huggingfacehub_api_token="my-api-key",
)
print(model.invoke("What is Deep Learning?"))
```
""" # noqa: E501
endpoint_url: str | None = None
"""Endpoint URL to use. If repo_id is not specified then this needs to given or
should be pass as env variable in `HF_INFERENCE_ENDPOINT`"""
repo_id: str | None = None
"""Repo to use. If endpoint_url is not specified then this needs to given"""
provider: str | None = None
"""Name of the provider to use for inference with the model specified in `repo_id`.
e.g. "cerebras". if not specified, Defaults to "auto" i.e. the first of the
providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
available providers can be found in the [huggingface_hub documentation](https://huggingface.co/docs/huggingface_hub/guides/inference#supported-providers-and-tasks)."""
huggingfacehub_api_token: str | None = Field(
default_factory=from_env("HUGGINGFACEHUB_API_TOKEN", default=None)
)
max_new_tokens: int = 512
"""Maximum number of generated tokens"""
top_k: int | None = None
"""The number of highest probability vocabulary tokens to keep for
top-k-filtering."""
top_p: float | None = 0.95
"""If set to < 1, only the smallest set of most probable tokens with probabilities
that add up to `top_p` or higher are kept for generation."""
typical_p: float | None = 0.95
Extends
Source
Frequently Asked Questions
What is the HuggingFaceEndpoint class?
HuggingFaceEndpoint is a class in the langchain codebase, defined in libs/partners/huggingface/langchain_huggingface/llms/huggingface_endpoint.py.
Where is HuggingFaceEndpoint defined?
HuggingFaceEndpoint is defined in libs/partners/huggingface/langchain_huggingface/llms/huggingface_endpoint.py at line 29.
What does HuggingFaceEndpoint extend?
HuggingFaceEndpoint extends LLM.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free