šŸ“• Build AI Agents using LangGraph.js is now out!

LangChain.js, Node.js, and OpenAI Setup from Scratch

Since I am using Node.js as the primary tool to demonstrate LangChain.js examples on this site, I thought it would be useful to provide a brief walkthrough on setting up a basic LangChain.js and Node.js example app from scratch.

By the end of this tutorial, we will have all the necessary scaffolding in place and install the required libraries to allow us to ask ChatGPT a simple question using LangChain.js and Node.js.

Before we start, ensure that Node.js and NPM are installed on your machine.

We'll begin by creating a new folder to host our files:

mkdir langchain-node
cd langchain-node

The first step is to initialize the Node app. Run the following command in the langchain-node folder:

npm init -y

Once the initialization is complete and the package.json file is created, we can install the required libraries. We need langchain, dotenv, and @langchain/openai:

npm i langchain dotenv @langchain/openai

The dotenv library is used to read and pass the OPENAI_API_KEY variable. To enable this, we need to create a .env file with the following content:

OPENAI_API_KEY=your_key_value_here

You can obtain your API key from OpenAI.

Next, we'll create the JavaScript file that will interact with the GPT model. Let's name it index.js.

At this point, your langchain-node folder should contain the following files:

langchain-node
  ā”œā”€ā”€ node_modules
  ā”œā”€ā”€ .env
  ā”œā”€ā”€ index.js
  ā”œā”€ā”€ package-lock.json
  └── package.json

Open index.js and add the following code:

import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

dotenv.config();
const model = new ChatOpenAI();
const response = await model.invoke('What is the age of Donald Duck?');
console.log(response);

Before running this code, we need to configure Node.js to use import statements. To do this, open package.json and add the following line to the root object:

"type": "module",

If you omit this line, you will encounter the following error:

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1383:18)
    at Module._compile (node:internal/modules/cjs/loader:1412:20)
    at Module._extensions..js (node:internal/modules/cjs/loader:1551:10)
    ....

With everything set up, it's time to run our example:

node index.js

You should see the following output in the console:

AIMessage {
  "id": "chatcmpl-9uEIHFPTiHmGSmsCL9M0yRZGW8MYW",
  "content": "Donald Duck was created in 1934, so he is technically 86 years old as of 2020. However, cartoon characters are fictional and do not age like humans do.",
  "additional_kwargs": {},
  "response_metadata": {
    "tokenUsage": {
      "completionTokens": 37,
      "promptTokens": 15,
      "totalTokens": 52
    },
    "finish_reason": "stop"
  },
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 15,
    "output_tokens": 37,
    "total_tokens": 52
  }
}

And there you have it! This is how you can set up a basic app from scratch using Node.js, LangChain.js, and the OpenAI GPT model.

You can find the full code for this article on my GitHub.

If you want to dive deeper, consider exploring other topics such as ChatGPT Vision API, StructuredOutputParsers, or how to execute JavaScript code generated by an LLM. Happy coding!

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js


Leave a Reply

Your email address will not be published. Required fields are marked *