Tutorial: Building AI-powered apps with LangChain.js
3 min read
Artificial Intelligence (AI) has become an integral part of modern development, empowering us to create applications that enhance the quality of human life. To expedite this process, leveraging cutting-edge technologies is crucial.
LangChain is a framework for developing applications powered by large language models. It enables applications that:
Are context-aware: connect a language model to sources of context (prompt instructions, few shot examples, content to ground its response in, etc.)
Reason: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.)
LangChain collaborates seamlessly with various Large Language Models (LLMs), including OpenAI’s GPT, HuggingFace, and more.
In this tutorial, we’ll explore LangChain.js, a comprehensive framework streamlining the creation of AI-powered applications by consolidating essential components.
Follow this step-by-step tutorial to build an emoji explainer app with the help of LangChain.js.
Building an emoji explainer app
This app will analyze the emotions behind a given emoji or series of emojis, providing insightful interpretations.
Prerequisites
Node.js: Ensure you have Node.js installed on your machine. If not, download it here.
API Key: Obtain an API key from your preferred LLM. In this example, I’ll use OpenAI. Follow this guide to create an OpenAI API key.
Getting started
- Open your preferred folder in the code editor (I’ll be using VS Code editor), then run the following commands in the terminal.
npm init -y
npm i langchain dotenv
The first command npm init -y
will generate a package.json
file. The second command npm i langchain dotenv
will install the required dependencies.
- Edit the generated
package.json
file by adding a “type” property with the value “module” to indicate ECMAScript modules (ESM):
"type": "module"
Create two new files in the project folder: index.js (to house the code) and
.env
(to store the API key).In the
.env
file, add your API key:
OPENAI_API_KEY=YOUR_OPEN_AI_KEY_GOES_HERE
- In the index.js file, import the necessary dependencies and set up the app:
// Import dependencies
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
import * as dotenv from "dotenv";
dotenv.config();
// Define the template for LLM instructions
const template = `... (Your detailed template here) ...`;
// Create a prompt template
const promptTemplate = new PromptTemplate({
template,
inputVariables: ["emojis"],
});
// Create an OpenAI model with a temperature of 0.5
const openAIModel = new OpenAI({
temperature: 0.5,
});
// Combine the LLM with the prompt template using a chain
const llmChain = new LLMChain({
llm: openAIModel,
prompt: promptTemplate,
});
// Call the chain with the emojis to be explained
const result = await llmChain.call({
emojis: "😂🤣",
});
// Log the result to the console
console.log(result.text);
- In the end, run the app in the terminal by the following command:
node index.js
You will see a result in the console, providing an interpretation of the inputted emojis.
Wrap up!
Langchain.js simplifies the development of AI-powered applications, making it accessible to JavaScript developers as well. Feel free to explore and ask any questions in the comments—I’ll be quick to respond.