2024-03-26
In the world of programming, integrating artificial intelligence (AI) into your projects can seem daunting. However, with the right tools and a bit of guidance, it can be a straightforward and rewarding process. In this article, we'll explore how to use Langchain with OpenAI to add AI capabilities to your applications.
NOTE that instead of openai other LLM's can be used.
Langchain is a library that simplifies the process of integrating AI models, like those provided by OpenAI, into your applications. It provides a set of tools that make it easier to send messages to AI models and interpret their responses.
To begin, you'll need to set up your development environment. This involves installing necessary packages and setting up environment variables. We'll be using Deno, a modern runtime for JavaScript and TypeScript, to run our example
Deno is a secure runtime for JavaScript and TypeScript that is built on V8, the same engine that powers Google Chrome.
Since version 1.28, Deno has native support for importing npm packages. This is done by importing using npm: specifiers.
Example packages.json
. We use here ES module instead of javascript module.
{
"type": "module",
"dependencies": {
"chalk": "^5.3.0",
"dotenv": "^16.4.5",
"langchain": "^0.1.30"
}
}
Once Deno is installed, you'll need to install a few npm packages:
Install these packages:
npm i dotenv
npm i langchain
npm i chalk
.env
fileCreate a .env file in your project directory and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
Make sure to replace your_openai_api_key_here with your actual API key from OpenAI.
Now, let's write some TypeScript code to use Langchain with OpenAI. Save the following code as langchain-openai.ts:
import chalk from "chalk";
import "dotenv/config";
import { ChatOpenAI } from "npm:@langchain/openai";
import { HumanMessage } from "npm:@langchain/core/messages";
console.log("# START ");
// will use default env name OPENAI_API_KEY which is defined in .env
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo-1106",
temperature: 0.9,
});
const response = await model.invoke([
new HumanMessage("write kotlin hello world.")
]);
console.log(chalk.bgGreenBright(response.content));
console.log("# END");
This script initializes the Langchain model, sends a message to the AI asking it to write a "Hello, World!" program in Kotlin, and then prints the AI's response with a green background.
To run your script, use the following command:
deno run --allow-env --allow-sys --allow-read --allow-net langchain-openai.ts
# START
fun main() {
println("Hello, World!")
}
# END