How to Set Up LangChain with Hugging Face in Node.js

In this post, we will walk through the steps to set up a development environment for JavaScript using Node.js and npm. This setup is crucial for working with various libraries and frameworks, such as LangChain, especially when integrating with models from Hugging Face.

If you want to learn the basics of LangChain, go through this article: Basics of LangChain

Step 1: Install Node.js and npm

To get started, you need to install Node.js, which includes npm (Node package manager). You can download the latest version from the Node.js official website.

After installation, you can verify if Node.js and npm were installed correctly by running the following commands in your terminal:

node -v
npm -v

If both commands return version numbers, you’re all set!

Step 2: Create a New Node.js Project

Next, create a new directory for your project and navigate to it:

mkdir langchain-huggingface-project
cd langchain-huggingface-project

Initialize a new Node.js project with the following command:

npm init -y

This will create a package.json file, which will manage your project dependencies.

Step 3: Install Required Packages

You must install the necessary packages to work with LangChain and Hugging Face. Run the following command to install them:

npm install langchain @huggingface/inference dotenv
  • langchain: A framework for building applications powered by LLMs (Large Language Models).
  • @huggingface/inference: A package to interact with Hugging Face models.
  • dotenv: A package to load environment variables from a .env file.

Step 4: Get Your Hugging Face API Key

To use Hugging Face models, you’ll need an API key. You can generate this key by signing up at Hugging Face and retrieving it from your account settings.

Step 5: Create a .env File

At the root of your project, create a .env file to store your API key securely:

touch .env

Add your Hugging Face API key to the .env file:

HUGGINGFACE_API_KEY=your_huggingface_api_key_here

Step 6: Create Your Main JavaScript File

Now, create an index.js file, which will be the entry point of your project:

touch index.js

In index.js, write the following code to set up and use the Hugging Face model:

// Import necessary modules
require('dotenv').config();
const { HuggingFaceInference } = require('langchain/llms/huggingface_inference');

// Initialize the Hugging Face model with your API key from the .env file
const model = new HuggingFaceInference({
  apiKey: process.env.HUGGINGFACE_API_KEY,  // Use the API key from .env
  model: 'gpt2',  // Replace this with your desired model from Hugging Face
});

// Function to run a query with the model
async function run() {
  // Send a query to the model
  const response = await model.call('What is the capital of India?');
  console.log(response);  // Expected output: "Tokyo"
}

// Execute the run function
run();

Step 7: Run Your Project

To execute your project, run:

node index.js

You should see the response from the Hugging Face model, which, in this case, would be “Delhi” when asking about the capital of India.

Congratulations! You have successfully setup your first LLM application. Happy Coding 🙂