LangChain: QuickStart Guide

Photo of author
Written By Zach Johnson

AI and tech enthusiast with a background in machine learning.

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

ModuleDescription
ModelProvides a generic interface to many foundation models
PromptManages LLM inputs
MemoryPersists state between calls of a chain/agent
ChainCombines LLMs with other components
AgentsInvolves an LLM making decisions about which actions to take
CallbackProvides a way to execute code after a chain is complete
IndexesAccesses 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:

  1. Install LangChain and extras like langchain[llms]
  2. Get API keys for LLMs like OpenAI
  3. Try the LangChain quickstart
  4. Browse the examples and tutorials
  5. 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.

AI is evolving. Don't get left behind.

AI insights delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.