Skip to the content.

LangChain

Information

Introduction

LangChain is an open-source framework designed to simplify the creation of applications using large language models (LLMs). It provides a standard interface for chains, many integrations with other tools, and end-to-end chains for common applications. LangChain allows developers to build applications that are:

What is it for?

LangChain provides several modules that address different aspects of building LLM applications:

Usage

Python

LangChain’s original and most feature-complete implementation is in Python.

Installation:

pip install langchain
# or for a more modular installation
pip install langchain-core langchain-community

Basic Example:

from langchain_openai import OpenAI
from langchain_core.prompts import PromptTemplate

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
chain = prompt | llm

print(chain.invoke({"product": "colorful socks"}))

Node.js / TypeScript

LangChain.js brings the power of the LangChain framework to the JavaScript/TypeScript ecosystem, enabling LLM applications in the browser or on the server with Node.js.

Installation:

npm install langchain @langchain/core @langchain/community

Basic Example:

import {OpenAI} from "@langchain/openai";
import {PromptTemplate} from "@langchain/core/prompts";

const model = new OpenAI({temperature: 0.9});
const template = "What is a good name for a company that makes {product}?";
const prompt = PromptTemplate.fromTemplate(template);

const chain = prompt.pipe(model);

const res = await chain.invoke({product: "colorful socks"});
console.log(res);

Java

In the Java ecosystem, the most prominent implementation inspired by LangChain is LangChain4j.

Installation (Maven):


<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
    <version>0.35.0</version>
</dependency>

Basic Example:

import dev.langchain4j.model.openai.OpenAiChatModel;

public class Main {
    public static void main(String[] args) {
        OpenAiChatModel model = OpenAiChatModel.withApiKey("your-api-key");
        String response = model.generate("What is a good name for a company that makes colorful socks?");
        System.out.println(response);
    }
}

Similar Software

Several other frameworks and libraries offer similar capabilities for building LLM-powered applications:

Configuration

Configuration typically involves setting environment variables for API keys (e.g., OPENAI_API_KEY) or using provider-specific configuration objects in code.

See also