How-to guides
Here you'll find answers to "How do I...?" types of questions. These guides are goal-oriented and concrete; they're meant to help you complete a specific task. For conceptual explanations see the Conceptual guide. For end-to-end walkthroughs see Tutorials. For comprehensive descriptions of every class and function see the API Reference.
Installation
Key features
This highlights functionality that is core to using LangChain.
- How to: return structured data from a model
- How to: use a model to call tools
- How to: stream runnables
- How to: debug your LLM apps
LangChain Expression Language is a way to create arbitrary custom chains. It is built on the Runnable protocol.
LCEL cheatsheet: For a quick overview of how to use the main LCEL primitives.
- How to: chain runnables
- How to: stream runnables
- How to: invoke runnables in parallel
- How to: add default invocation args to runnables
- How to: turn any function into a runnable
- How to: pass through inputs from one chain step to the next
- How to: configure runnable behavior at runtime
- How to: add message history (memory) to a chain
- How to: route between sub-chains
- How to: create a dynamic (self-constructing) chain
- How to: inspect runnables
- How to: add fallbacks to a runnable
- How to: migrate chains to LCEL
- How to: pass runtime secrets to a runnable
Components
These are the core building blocks you can use when building applications.
Prompt templates
Prompt Templates are responsible for formatting user input into a format that can be passed to a language model.
- How to: use few shot examples
- How to: use few shot examples in chat models
- How to: partially format prompt templates
- How to: compose prompts together
Chat models
Chat Models are newer forms of language models that take messages in and output a message.
- How to: do function/tool calling
- How to: get models to return structured output
- How to: cache model responses
- How to: get log probabilities
- How to: create a custom chat model class
- How to: stream a response back
- How to: track token usage
- How to: track response metadata across providers
- How to: let your end users choose their model
- How to: use chat model to call tools
- How to: few shot prompt tool behavior
- How to: bind model-specific formatted tools
- How to: force a specific tool call
- How to: init any model in one line
LLMs
What LangChain calls LLMs are older forms of language models that take a string in and output a string.
- How to: cache model responses
- How to: create a custom LLM class
- How to: stream a response back
- How to: track token usage
- How to: work with local LLMs
Document loaders
Document Loaders are responsible for loading documents from a variety of sources.
- How to: load CSV data
- How to: load data from a directory
- How to: load HTML data
- How to: load JSON data
- How to: load Markdown data
- How to: load Microsoft Office data
- How to: load PDF files
- How to: write a custom document loader
Text splitters
Text Splitters take a document and split into chunks that can be used for retrieval.
- How to: recursively split text
- How to: split by HTML headers
- How to: split by HTML sections
- How to: split by character
- How to: split code
- How to: split Markdown by headers
- How to: recursively split JSON
- How to: split text into semantic chunks
- How to: split by tokens
Vector stores
Vector stores are databases that can efficiently store and retrieve embeddings.
Retrievers
Retrievers are responsible for taking a query and returning relevant documents.
- How to: use a vector store to retrieve data
- How to: generate multiple queries to retrieve data for
- How to: use contextual compression to compress the data retrieved
- How to: write a custom retriever class
- How to: add similarity scores to retriever results
- How to: combine the results from multiple retrievers
- How to: reorder retrieved results to mitigate the "lost in the middle" effect
- How to: generate multiple embeddings per document
- How to: retrieve the whole document for a chunk
- How to: generate metadata filters
- How to: create a time-weighted retriever
- How to: use hybrid vector and keyword retrieval