Skip to main content

🦜️🔗 LangChain

LangChain is a popular open-source framework for developing applications powered by language models.

MIT License   Site

LanguagesDocsGithub
PythonDocsCode
JSDocsCode

Install

pip install langchain / yarn add langchain

Main Benefits

  • Common Patterns for chain-of-thought and prompt templating
  • Many integrations and data loaders
  • Deep integration to LangSmith monitoring (developed by the same team)

Simple Example

Python

import chromadb
from langchain.vectorstores import Chroma
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings

# Chroma code
persistent_client = chromadb.PersistentClient()
collection = persistent_client.get_or_create_collection("collection_name")
collection.add(ids=["1", "2", "3"], documents=["a", "b", "c"])

# LangChain Code
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

langchain_chroma = Chroma(
client=persistent_client,
collection_name="collection_name",
embedding_function=embedding_function,
)
# Important! - the embedding functiion passed to langchain is their wrapper, not Chroma's


print("There are", langchain_chroma._collection.count(), "in the collection")

Javascript

import { OpenAI } from "langchain/llms/openai";
import { ConversationalRetrievalQAChain } from "langchain/chains";
import { Chroma } from "langchain/vectorstores/chroma";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import * as fs from "fs";

// to run this first run a chroma server with `chroma run --path /path/to/data`

export const run = async () => {
/* Initialize the LLM to use to answer the question */
const model = new OpenAI();
/* Load in the file we want to do question answering over */
const text = fs.readFileSync("state_of_the_union.txt", "utf8");
/* Split the text into chunks */
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
/* Create the vectorstore */
const vectorStore = await Chroma.fromDocuments(docs, new OpenAIEmbeddings(), {
collectionName: "state_of_the_union",
});
/* Create the chain */
const chain = ConversationalRetrievalQAChain.fromLLM(
model,
vectorStore.asRetriever()
);
/* Ask it a question */
const question = "What did the president say about Justice Breyer?";
const res = await chain.call({ question, chat_history: [] });
console.log(res);
};

Resources

Tutorials