LangChain is a Python framework for building applications with large language models (LLMs) like GPT-3 and BLOOM. It provides a modular set of tools to help you quickly build complex LLM-powered apps.
Installation
Install LangChain with pip:
pip install langchain
To enable integrations like OpenAI, also install extras:
pip install 'langchain[llms]'
Core Concepts
Module | Description |
---|---|
Model | Provides a generic interface to many foundation models |
Prompt | Manages LLM inputs |
Memory | Persists state between calls of a chain/agent |
Chain | Combines LLMs with other components |
Agents | Involves an LLM making decisions about which actions to take |
Callback | Provides a way to execute code after a chain is complete |
Indexes | Accesses external data |
LangChain is built around chaining together several key components:
Prompt Templates
Prompt templates let you define reusable templates for generating prompts sent to the LLM. For example:
from langchain import PromptTemplate
template = """
Question: {question}
Answer:
"""
prompt = PromptTemplate(template=template, input_variables=['question'])
LLM Integrations
Easily connect to LLMs like OpenAI or HuggingFace with LangChain integrations. For example:
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")
Chains
Chains let you link components like prompts and LLMs:
from langchain.chains import LLMChain
chain = LLMChain(prompt=prompt, llm=llm)
Agents
Agents dynamically choose which tools to apply based on user input and previous steps. Tools can include LLMs, databases, search, and more.
Memory
Keep track of conversation history and app state across inferences.
Modules
LangChain provides the following modular capabilities:
- Model I/O: Interact with LLMs
- Data Connection: Connect to data sources
- Chains: Combine components into sequences
- Agents: Dynamically choose tools
- Memory: Maintain state across runs
- Callbacks: Log and inspect intermediate steps
Code Examples
Here are some examples of common use cases:
Chatbot
from langchain import OpenAI, ConversationChain
llm = OpenAI(model_name="gpt-3.5-turbo")
conversation = ConversationChain(llm=llm)
conversation.run("Hello!")
# "Hi there! How can I help you today?"
Question Answering
from langchain import PromptTemplate, LLMChain, OpenAI
template = """
Question: {question}
Answer:
"""
prompt = PromptTemplate(template=template)
llm = OpenAI(model_name="text-davinci-003")
chain = LLMChain(prompt=prompt, llm=llm)
chain.run("What is the capital of France?")
# "The capital of France is Paris."
Data Analysis
from langchain.chains import SQLChain
from langchain.llms import OpenAI
from langchain.SQLDB import SQLiteDB
llm = OpenAI(model_name="text-davinci-003")
db = SQLiteDB()
chain = SQLChain(llm=llm, sql_client=db, verbose=True)
chain.run("What were the top 3 highest grossing movies in 2019?")
Getting Started
To get started with LangChain:
- Install LangChain and extras like
langchain[llms]
- Get API keys for LLMs like OpenAI
- Try the LangChain quickstart
- Browse the examples and tutorials
- Join the LangChain community to learn from other users
With its modular design and active community, LangChain provides a robust platform for unlocking the power of large language models.