Site icon PicDataset

LangChain: QuickStart Guide

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.


Install LangChain with pip:

pip install langchain

To enable integrations like OpenAI, also install extras:

pip install 'langchain[llms]'

Core Concepts

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}

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 let you link components like prompts and LLMs:

from langchain.chains import LLMChain

chain = LLMChain(prompt=prompt, llm=llm)


Agents dynamically choose which tools to apply based on user input and previous steps. Tools can include LLMs, databases, search, and more.


Keep track of conversation history and app state across inferences.


LangChain provides the following modular capabilities:

Code Examples

Here are some examples of common use cases:


from langchain import OpenAI, ConversationChain 

llm = OpenAI(model_name="gpt-3.5-turbo")
conversation = ConversationChain(llm=llm)"Hello!")  
# "Hi there! How can I help you today?"

Question Answering

from langchain import PromptTemplate, LLMChain, OpenAI

template = """
Question: {question}
prompt = PromptTemplate(template=template)

llm = OpenAI(model_name="text-davinci-003")
chain = LLMChain(prompt=prompt, llm=llm)"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)"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.

Exit mobile version