Retrieval augmented generation

edit

Retrieval augmented generation

edit

Retrieval Augmented Generation (RAG) is a technique for improving language model responses by grounding the model with additional, verifiable sources of information. It works by first retrieving relevant context from an external datastore, which is then added to the model’s context window.

RAG is a form of in-context learning, where the model learns from information provided at inference time. Compared to fine-tuning or continuous pre-training, RAG can be implemented more quickly and cheaply, and offers several advantages.

RAG sits at the intersection of information retrieval and generative AI

RAG sits at the intersection of information retrieval and generative AI. Elasticsearch is an excellent tool for implementing RAG, because it offers various retrieval capabilities, such as full-text search, vector search, and hybrid search, as well as other tools like filtering, aggregations, and security features.

Advantages of RAG

edit

Implementing RAG with Elasticsearch has several advantages:

  • Improved context: Enables grounding the language model with additional, up-to-date, and/or private data.
  • Reduced hallucination: Helps minimize factual errors by enabling models to cite authoritative sources.
  • Cost efficiency: Requires less maintenance compared to fine-tuning or continuously pre-training models.
  • Built-in security: Controls data access by leveraging Elasticsearch’s user authorization features, such as role-based access control and field/document-level security.
  • Simplified response parsing: Eliminates the need for custom parsing logic by letting the language model handle parsing Elasticsearch responses and formatting the retrieved context.
  • Flexible implementation: Works with basic full-text search, and can be gradually updated to add more advanced and computationally intensive semantic search capabilities.

RAG system overview

edit

The following diagram illustrates a simple RAG system using Elasticsearch.

Components of a simple RAG system using Elasticsearch

The workflow is as follows:

  1. The user submits a query.
  2. Elasticsearch retrieves relevant documents using full-text search, vector search, or hybrid search.
  3. The language model processes the context and generates a response, using custom instructions. Examples of custom instructions include "Cite a source" or "Provide a concise summary of the content field in markdown format."
  4. The model returns the final response to the user.

A more advanced setup might include query rewriting between steps 1 and 2. This intermediate step could use one or more additional language models with different instructions to reformulate queries for more specific and detailed responses.

Getting started

edit

Start building RAG applications quickly with Playground, which seamlessly integrates Elasticsearch with language model providers. The Playground UI enables you to build, test, and deploy RAG interfaces on top of your Elasticsearch indices.

Playground automatically selects the best retrieval methods for your data, while providing full control over the final Elasticsearch queries and language model instructions. You can also download the underlying Python code to integrate with your existing applications.

Learn more in the Playground documentation and try the interactive lab for hands-on experience.

Learn more

edit

Learn more about building RAG systems using Elasticsearch in these blog posts: