Langchain — Javascript/Typescript?

Gunjan
2 min readMay 17, 2023

--

langchain using typescript

Harrison Chase is the author of langchain and you can find the github project here. https://github.com/hwchase17/langchain.

The project makes it easier to develop applications using large language models. The package provides a generic interface for accessing large language models from OpenAI, Azure AI , Google AI, and Hugging Face, amongst others. Other features include prompt management and a chaining.

Python is an excellent language to work with but I had a typescript project I wanted to use langchain in. Thats when I found the javascript docs for langhain at https://js.langchain.com/docs/. Further langchain typescript bindings allow us to use it in typeScript projects!

Chaining is a feature of langchain that allows you to combine multiple prompts and LLMs to create more complex outputs.

First we all know the drill , please install using npm.

npm install langchain

Now lets generate a paragraph on how to promote kindness:

We will write an Agent that does the heavy lifting of talking to the LLMs.

We create one chain in this example that executes the agent.

import { Chain, LangChain, KindnessAgent } from "langchain";

// Create a chain
const chain = new Chain([
new KindnessAgent(),
]);

// Generate an output
const output = chain.generate();

// Print the output
console.log(output);

The code for the agent is as follows.

import { Agent, LangChain, OpenAIGPT3, GoogleBard, Translate } from "langchain";

// Create an agent
class KindnessAgent extends Agent {
constructor() {
super();
this.openai_gpt3 = new OpenAIGPT3();
this.google_bard = new GoogleBard();
this.translate = new Translate("fr");
}

// Generate a paragraph about kindness
async generate_para() {
const prompt = "Write a paragraph about kindness";
const output = await this.openai_gpt3.generate(prompt);
return output;
}

// Refine the paragraph
async refine_para(para) {
const prompt = "Edit this paragraph";
const output = await this.google_bard.generate(para, prompt);
return output;
}

// Translate the paragraph
async translate_para(para) {
const output = await this.translate.translate(para);
return output;
}

// Run the agent
async run() {
const para = await this.generate_para();
const refined_para = await this.refine_para(para);
const translated_para = await this.translate_poem(refined_para);

console.log(translated_para);
}
}

There are three steps here

  1. Generate the paragraph using ChatGPT
  2. Edit the paragraph using Google Bard
  3. Translate to French.

Now all you need is API keys

Create a .env file and add the keys there.

CHATGPT_API_KEY=YOUR_CHATGPT_API_KEY
GOOGLE_BARD_API_KEY=YOUR_GOOGLE_BARD_API_KEY

Don’t forget to restart the project if you have it running .

You should be up and langchaining in typescript!

Disclaimer

The views and opinions expressed in this post are solely my own and do not necessarily reflect the views and opinions of my employer. This post is for personal learning purposes only and should not be construed as professional advice.

--

--

Gunjan
Gunjan

No responses yet