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.
LangChain Expression Language (LCEL)β
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.
- Chain runnables
- Stream runnables
- Invoke runnables in parallel
- Add default invocation args to runnables
- Turn any function into a runnable
- Pass through inputs from one chain step to the next
- Configure runnable behavior at runtime
- Add message history (memory) to a chain
- Route between sub-chains
- Create a dynamic (self-constructing) chain
- Inspect runnables
- Add fallbacks 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.
- Use few shot examples
- Use few shot examples in chat models
- Partially format the prompt templates
- Compose prompts together
Example selectorsβ
Example Selectors are responsible for selecting the correct few shot examples to pass to the prompt.
- Use example selectors
- Select examples by length
- Select examples by semantic similarity
- Select examples by semantic ngram overlap
- Select examples by maximal marginal relevance
Chat modelsβ
Chat Models are newer forms of language models that take messages in and output a message.
- Do function/tool calling
- Get models to return structured output
- Cache model responses
- Get log probabilities
- Create a custom chat model class
- Stream a response back
- Track token usage
- Track response metadata across providers
- Let your end users choose their model
LLMsβ
What LangChain calls LLMs are older forms of language models that take a string in and output a string.
- Cache model responses
- Create a custom LLM class
- Stream a response back
- Track token usage
- Work with local LLMs
Output parsersβ
Output Parsers are responsible for taking the output of an LLM and parsing into more structured format.
- Use output parsers to parse an LLM response into structured format
- Parse JSON output
- Parse XML output
- Parse YAML output
- Retry when output parsing errors occur
- Try to fix errors in output parsing
- Write a custom output parser class
Document loadersβ
Document Loaders are responsible for loading documents from a variety of sources.
- Load CSV data
- Load data from a directory
- Load HTML data
- Load JSON data
- Load Markdown data
- Load Microsoft Office data
- Load PDF files
- Write a custom document loader
Text splittersβ
Text Splitters take a document and split into chunks that can be used for retrieval.
- Recursively split text
- Split by HTML headers
- Split by HTML sections
- Split by character
- Split code
- Split Markdown by headers
- Recursively split JSON
- Split text into semantic chunks
- Split by tokens
Embedding modelsβ
Embedding Models take a piece of text and create a numerical representation of it.
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.
- Use a vector store to retrieve data
- Generate multiple queries to retrieve data for
- Use contextual compression to compress the data retrieved
- Write a custom retriever class
- Add similarity scores to retriever results
- Combine the results from multiple retrievers
- Reorder retrieved results to mitigate the "lost in the middle" effect
- Generate multiple embeddings per document
- Retrieve the whole document for a chunk
- Generate metadata filters
- Create a time-weighted retriever
- Use hybrid vector and keyword retrieval
Indexingβ
Indexing is the process of keeping your vectorstore in-sync with the underlying data source.
Toolsβ
LangChain Tools contain a description of the tool (to pass to the language model) as well as the implementation of the function to call).
- Create custom tools
- Use built-in tools and built-in toolkits
- Use a chat model to call tools
- Add ad-hoc tool calling capability to LLMs and chat models
- Add a human in the loop to tool usage
- Handle errors when calling tools
Multimodalβ
Agentsβ
For in depth how-to guides for agents, please check out LangGraph documentation.
Callbacksβ
- Pass in callbacks at runtime
- Attach callbacks to a module
- Pass callbacks into a module constructor
- Create custom callback handlers
- Use callbacks in async environments
Customβ
All of LangChain components can easily be extended to support your own versions.
- Create a custom chat model class
- Create a custom LLM class
- Write a custom retriever class
- Write a custom document loader
- Write a custom output parser class
- Create custom callback handlers
- Define a custom tool
Use casesβ
These guides cover use-case specific details.
Q&A with RAGβ
Retrieval Augmented Generation (RAG) is a way to connect LLMs to external sources of data.
Extractionβ
Extraction is when you use LLMs to extract structured information from unstructured text.
Chatbotsβ
Chatbots involve using an LLM to have a conversation.
Query analysisβ
Query Analysis is the task of using an LLM to generate a query to send to a retriever.
- Add examples to the prompt
- Handle cases where no queries are generated
- Handle multiple queries
- Handle multiple retrievers
- Construct filters
- Deal with high cardinality categorical variables
Q&A over SQL + CSVβ
You can use LLMs to do question answering over tabular data.
Q&A over graph databasesβ
You can use an LLM to do question answering over graph databases.