!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
        *   [Quickstart](/v0.1/docs/use_cases/question_answering/quickstart/)
        *   [Per-User Retrieval](/v0.1/docs/use_cases/question_answering/per_user/)
        *   [Add chat history](/v0.1/docs/use_cases/question_answering/chat_history/)
        *   [Citations](/v0.1/docs/use_cases/question_answering/citations/)
        *   [Using agents](/v0.1/docs/use_cases/question_answering/conversational_retrieval_agents/)
        *   [Using local models](/v0.1/docs/use_cases/question_answering/local_retrieval_qa/)
        *   [Returning sources](/v0.1/docs/use_cases/question_answering/sources/)
        *   [Streaming](/v0.1/docs/use_cases/question_answering/streaming/)
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   Q&A with RAG

On this page

Q&A with RAG
============

Overview[​](#overview "Direct link to Overview")
------------------------------------------------

One of the most powerful applications enabled by LLMs is sophisticated question-answering (Q&A) chatbots. These are applications that can answer questions about specific source information. These applications use a technique known as Retrieval Augmented Generation, or RAG.

### What is RAG?[​](#what-is-rag "Direct link to What is RAG?")

RAG is a technique for augmenting LLM knowledge with additional data.

LLMs can reason about wide-ranging topics, but their knowledge is limited to the public data up to a specific point in time that they were trained on. If you want to build AI applications that can reason about private data or data introduced after a model's cutoff date, you need to augment the knowledge of the model with the specific information it needs. The process of bringing the appropriate information and inserting it into the model prompt is known as Retrieval Augmented Generation (RAG).

LangChain has a number of components designed to help build Q&A applications, and RAG applications more generally.

Note: Here we focus on Q&A for unstructured data. Two RAG use cases which we cover elsewhere are:

*   [Q&A over SQL data](/v0.1/docs/use_cases/sql/)
*   [Q&A over code](/v0.1/docs/use_cases/code_understanding/) (e.g., TypeScript)

RAG Architecture[​](#rag-architecture "Direct link to RAG Architecture")
------------------------------------------------------------------------

A typical RAG application has two main components:

**Indexing**: a pipeline for ingesting data from a source and indexing it. _This usually happens offline_.

**Retrieval and generation**: the actual RAG chain, which takes the user query at run time and retrieves the relevant data from the index, then passes that to the model.

The most common full sequence from raw data to answer looks like:

**Indexing**

1.  **Load**: First we need to load our data. This is done with [DocumentLoaders](/v0.1/docs/modules/data_connection/document_loaders/).
2.  **Split**: [Text splitters](/v0.1/docs/modules/data_connection/document_transformers/) break large `Documents` into smaller chunks. This is useful both for indexing data and for passing it in to a model, since large chunks are harder to search over and won't fit in a model's finite context window.
3.  **Store**: We need somewhere to store and index our splits, so that they can later be searched over. This is often done using a [VectorStore](/v0.1/docs/modules/data_connection/vectorstores/) and [Embeddings](/v0.1/docs/modules/data_connection/text_embedding/) model.

![Indexing](/v0.1/assets/images/rag_indexing-8160f90a90a33253d0154659cf7d453f.png)

**Retrieval and generation**

1.  **Retrieve**: Given a user input, relevant splits are retrieved from storage using a [Retriever](/v0.1/docs/modules/data_connection/retrievers/).
2.  **Generate**: A [ChatModel](/v0.1/docs/modules/model_io/chat/) / [LLM](/v0.1/docs/modules/model_io/llms/) produces an answer using a prompt that includes the question and the retrieved data

![Retrieval generation](/v0.1/assets/images/rag_retrieval_generation-1046a4668d6bb08786ef73c56d4f228a.png)

Table of contents[​](#table-of-contents "Direct link to Table of contents")
---------------------------------------------------------------------------

*   [Quickstart](/v0.1/docs/use_cases/question_answering/quickstart/): We recommend starting here. Many of the following guides assume you fully understand the architecture shown in the Quickstart.
*   [Returning sources](/v0.1/docs/use_cases/question_answering/sources/): How to return the source documents used in a particular generation.
*   [Citations](/v0.1/docs/use_cases/question_answering/citations/): How to cite which parts of the source documents are referenced in a particular generation.
*   [Streaming](/v0.1/docs/use_cases/question_answering/streaming/): How to stream final answers as well as intermediate steps.
*   [Adding chat history](/v0.1/docs/use_cases/question_answering/chat_history/): How to add chat history to a Q&A app.
*   [Per-user retrieval](/v0.1/docs/use_cases/question_answering/per_user/): How to do retrieval when each user has their own private data.
*   [Using agents](/v0.1/docs/use_cases/question_answering/conversational_retrieval_agents/): How to use agents for Q&A.
*   [Using local models](/v0.1/docs/use_cases/question_answering/local_retrieval_qa/): How to use local models for Q&A.

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Deal with High Cardinality Categoricals

](/v0.1/docs/use_cases/query_analysis/how_to/high_cardinality/)[

Next

Quickstart

](/v0.1/docs/use_cases/question_answering/quickstart/)

*   [Overview](#overview)
    *   [What is RAG?](#what-is-rag)
*   [RAG Architecture](#rag-architecture)
*   [Table of contents](#table-of-contents)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.