Get AI summaries of any video or article — Sign up free
#1-Getting Started Building Generative AI Using HuggingFace Open Source Models And Langchain thumbnail

#1-Getting Started Building Generative AI Using HuggingFace Open Source Models And Langchain

Krish Naik·
5 min read

Based on Krish Naik's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Install the Hugging Face–LangChain partner package and supporting libraries (Hugging Face Hub, Transformers, Accelerate, bitsandbytes, LangChain) before building model calls.

Briefing

A new Hugging Face–LangChain integration is making it far easier to call large language models hosted on Hugging Face without downloading them locally. The walkthrough centers on a partner package (imported as “LangChain Hugging Face”) that plugs into LangChain workflows, letting developers authenticate with a Hugging Face token, select a model via its repo ID, and generate answers through a simple API-style interface. For anyone building generative AI apps, the practical payoff is speed and convenience: model access becomes “endpoint + repo ID + parameters,” rather than a multi-step setup involving separate Hugging Face Hub and Transformers plumbing.

The setup begins with installing the required libraries: the Hugging Face–LangChain partner package, Hugging Face Hub, Transformers, Accelerate, bitsandbytes, and LangChain. Authentication then becomes the critical gate. In Google Colab, the process uses Hugging Face access tokens stored as Colab “secrets,” retrieved in code via `google.colab.userdata.get(...)`. The transcript also notes an alternative approach using environment variables. With credentials in place, the code demonstrates using `HuggingFaceEndpoint` from the integrated package.

Model calling is shown in two main modes. First is API access through Hugging Face endpoints. The workflow sets an environment variable for the Hugging Face token, defines a `repo_id` for the target model, and instantiates `HuggingFaceEndpoint` with generation controls such as `max_length` and `temperature`. Once configured, the model is invoked directly using an `invoke` call—starting with Mistral’s instruct model (repo ID copied from Hugging Face) and then switching to a newer Mistral instruct release (repo ID for “mistral 7 million instruct v0.3” as shown). Responses arrive quickly without local model downloads, and the transcript flags that free usage is limited by request quotas.

Second is local inference using Hugging Face Transformers pipeline. Here, the approach downloads a smaller model into the local cache—illustrated with `gpt2`—by loading an `AutoModelForCausalLM` and an `AutoTokenizer`, then wrapping them in a `pipeline` configured for `text-generation`. The pipeline is parameterized with `max_new_tokens` and can be directed to GPU or CPU using the `device` argument (`device=0` for GPU, `device=-1` for CPU). The transcript also shows how to combine this with LangChain by building prompt templates and running them through an LLM chain.

To connect model calls to application logic, the transcript demonstrates LangChain prompt templates and chains. A custom prompt template (“question: … answer: … think step by step”) is created with an input variable, then executed via `LLMChain` using the configured Hugging Face model. A sample question about the Cricket World Cup 2011 returns “India,” illustrating how prompt formatting and model invocation work together.

Overall, the core message is operational: the Hugging Face–LangChain integration streamlines generative AI development by standardizing authentication and model access, while still supporting the classic local Transformers pipeline for smaller models when downloading is feasible.

Cornell Notes

The walkthrough shows how to build generative AI calls using Hugging Face models from LangChain with a new partner package. It starts by installing the integration plus supporting libraries, then authenticates using a Hugging Face token stored in Google Colab secrets (or environment variables). For hosted models, it uses `HuggingFaceEndpoint` with a model `repo_id` and generation parameters, then calls the model via `invoke` to get text responses quickly without local downloads. It also demonstrates local inference using Transformers `pipeline` with `AutoModelForCausalLM`, `AutoTokenizer`, and `device` settings for GPU vs CPU. Finally, it ties everything together with LangChain prompt templates and `LLMChain` to control how questions are formatted before generation.

What problem does the Hugging Face–LangChain partner package solve compared with manually wiring Hugging Face Hub and Transformers?

It streamlines model access so developers can call Hugging Face-hosted models through LangChain with less setup. Instead of separately installing and coordinating Hugging Face Hub, Transformers, and pipeline logic, the integration provides `HuggingFaceEndpoint` as a direct, endpoint-style interface. The workflow becomes: authenticate with a Hugging Face token, choose a model by `repo_id`, set generation parameters (e.g., `max_length`, `temperature`), and invoke the model from LangChain.

How does authentication work in the Colab-based setup, and why does it matter?

Authentication relies on a Hugging Face access token created in Hugging Face settings. In Google Colab, the token is stored as a secret via “Add new secret,” then retrieved in code using `from google.colab import userdata` and `userdata.get(<token_name>)`. The token is then used to set an environment variable (e.g., `os.environ[...] = ...`) so `HuggingFaceEndpoint` can validate requests. Without this token, model calls to Hugging Face endpoints won’t authenticate.

What is the hosted-model calling flow using `HuggingFaceEndpoint`?

The flow is: (1) set the Hugging Face token in the environment, (2) copy the target model’s `repo_id` from Hugging Face (examples include Mistral instruct models), (3) create `llm = HuggingFaceEndpoint(repo_id=..., max_length=..., temperature=..., token=...)`, and (4) call `llm.invoke(<prompt or question>)`. The transcript shows direct invocation returning answers like a definition of machine learning and a generative response to “do generative AI.”

How does local inference differ from endpoint inference, and when is each approach appropriate?

Local inference uses Transformers to download model weights and run them from the local cache. The transcript warns that large models (e.g., Mistral 7B instruct) can be too heavy for RAM/disk constraints, so local download is better for smaller models. Endpoint inference avoids local downloads and is faster to start, but free usage is limited by request quotas. The transcript demonstrates local inference with `gpt2` using `pipeline` for `text-generation`.

How do prompt templates and `LLMChain` fit into the workflow?

Prompt templates let developers control the exact text sent to the model. The transcript uses a template like `question: {question} answer: let's think step by step` and creates a `PromptTemplate` with an input variable named `question`. Then `LLMChain(llm=llm, prompt=prompt)` runs the model using the formatted prompt. The example question (“Who won the Cricket World Cup in year 2011?”) returns “India,” showing how prompt formatting affects the output.

What does the `device` parameter do in the Transformers `pipeline` approach?

In the local `pipeline` setup, `device` selects where inference runs. The transcript uses `device=0` to use the GPU and `device=-1` to use the CPU. This is paired with `max_new_tokens` to control generation length, and the resulting pipeline can be invoked through LangChain-style chaining.

Review Questions

  1. When using `HuggingFaceEndpoint`, which three inputs are essential to generate text (authentication, model selection, and generation parameters), and where does each appear in the code flow?
  2. What trade-offs determine whether to use endpoint inference or local Transformers `pipeline` inference?
  3. How does a LangChain `PromptTemplate` change the model’s behavior compared with calling `invoke` directly with a raw question?

Key Points

  1. 1

    Install the Hugging Face–LangChain partner package and supporting libraries (Hugging Face Hub, Transformers, Accelerate, bitsandbytes, LangChain) before building model calls.

  2. 2

    Create a Hugging Face access token and store it in Google Colab secrets (or environment variables) so endpoint calls can authenticate.

  3. 3

    Use `HuggingFaceEndpoint` with a model `repo_id` plus generation settings like `max_length` and `temperature` to call hosted models via `invoke`.

  4. 4

    Switch models by changing only the `repo_id`, keeping the endpoint calling pattern the same.

  5. 5

    For local inference, load `AutoModelForCausalLM` and `AutoTokenizer`, then wrap them in a Transformers `pipeline` for `text-generation`.

  6. 6

    Control local inference hardware with `device=0` (GPU) and `device=-1` (CPU), and control output length with `max_new_tokens`.

  7. 7

    Use LangChain `PromptTemplate` and `LLMChain` to standardize how questions are formatted before generation.

Highlights

Hosted inference becomes a one-liner pattern: `HuggingFaceEndpoint(repo_id=..., max_length=..., temperature=..., token=...)` followed by `invoke`—no local model download required.
Colab secrets provide a clean way to retrieve Hugging Face tokens in code using `userdata.get(...)`, enabling authenticated API calls.
Transformers `pipeline` supports both CPU and GPU execution via the `device` parameter, making local experimentation straightforward for smaller models.
Prompt templates plus `LLMChain` let developers enforce consistent “question → answer” formatting and reasoning cues like “let’s think step by step.”

Topics

  • Hugging Face Endpoints
  • LangChain Prompt Templates
  • Transformers Pipeline
  • Model Authentication
  • Generative AI Setup

Mentioned