Retrieval-Augmented Generation (RAG)

Retrieval-Augmented Generation (RAG)

Valid from Datafari 7.0

Introduction

As we have been working on the implementation of a RAG (Retrieval Augmented Generation) solution into Datafari, we came up with a new feature: “Datafari RagAPI”. RagAPI is a collection of Java classes and methods designed to handle RAG-related processes within Datafari. For more AI-related features, see also AI Powered Datafari API .

RAG processes can be triggered using the AiPowered API endpoints.

All our AI features can be used calling the proper API endpoint, or by using the AI chatbot widget available on Datafari UIv2.

At the core of AiPowered API are ChatModels, a set of Langchain4j’s classes that act as interfaces between Datafari and external APIs leveraging Large Language Models (LLMs). These services allow integration with third-party AI providers like OpenAI API, Mistral Cloud, our local solutions such as LocalAI.

This documentation covers the details of the RAG processes.

What is RAG?

RAG stands for Retrieval-Augmented Generation. It is the exploitation of a Large Language Model in order to generate the response to a user question or prompt leveraging (or potentially limiting to) extra contextual information provided with the prompt (these contextual information can be relevant documents or chunks of documents, coming from sources such as Datafari search results). In our Datafari case, we purposedly ask the LLM to restrict its answer to the knowledge contained in the extra contextual information (or at least we try to).

Here are some sources, for a better understanding of RAG:

Classic search (BM25) VS Vector Search

The “Retrieval” part of the RAG is an important step. In this step, a search is processed to identify a list of documents that may contain the desired information, and extract relevant fragments that can be interpreted by the LLM. In our own terms, the “classic” method is the keywords-based search, implemented in Datafari. The vector search is based on Machine Learning algorithms to capture the meaning and the context of unstructured data, converted into vectors. The advantage of vector search is to “understand” natural language queries, thus finding more relevant documents, that may not necessarily use the same terms as the ones in the query.

Datafari currently offers three different approaches to RAG retrieval:

  • Keyword-based Search (classic BM25): Full documents are retrieved using a traditional BM25 Datafari search, followed by a chunking process.

  • Solr Vector Search: During indexing, documents are pre-chunked, and each chunk is vectorized. The classic keyword-based search is replaced by a fully vector-based retrieval process, using Text to Vector Solr features. Short size chunks are returned, instead of whole documents.

  • Hybrid Search (RRF): Combines results from both BM25 and Vector searches, with and “Reciprocal Ranking Fusion” algorithm.

How does RAG work in Datafari ?

The RAG process can be started through two different Datafari API endpoints:

  1. Query reception

A “RAG” query is received from the user, through one of the API endpoints.

  1. History retrieval (optional)

If “chat memory” is enabled, the chat history is retrieved from the request to be used in the prompts.

  1. Query rewriting (optional)

If “query rewriting” is enabled, the search query is rewritten by the LLM before the Solr search (source retrieval).

  • This option can be enabled for BM25 only, vector search only, or both. When enabled for both, two search queries are generated using two different prompts.

  • The rewritten query is only used during the retrieval step.

  • The initial user query is still used in RAG process, in the “Prompting” step, and provided to the LLM as context for response generation.

  • If “chat memory” is enabled, the conversation history is used for query rewriting.

  1. Source retrieval

Documents are retrieved from Solr using Datafari search. The retrieval process can use Vector Search technology, classic BM25 Search, or Hybrid Search (RRF). If the “query rewriting” feature is enabled, the rewritten query is used for vector search search. Otherwise, the initial user query used.

  1. Chunking

Any document content (or document extract, in case of vector search) larger than the maximum chunk size defined in configuration is chunked into smaller pieces. Each piece is called a “chunk”.

  1. Prompting

A list of prompts (including instructions for the model, relevant documents chunks and the user query) is prepared and sent to the LLM External Service.

If the prompt exceeds the length limit for a single request, each chunk is processed separately. Once all chunks have been handled, the LLM is invoked again to generate a final, consolidated response.
This process should be optimized soon to process multiple chunks at once.

  1. Response Generation

The LLM generates a text response.

  1. Response formatting

Datafari will format the webservice response into a standard JSON, attach the sources used by the LLM, and send it to the user.

 

image-20241107-164327.png