RAG Example Implementation in the OpenAI Showcase Application
Introduction
Retrieval augmented generation (RAG) is a framework for an AI-based search with a private or external knowledge base that combines embeddings-based knowledge retrieval with a text generation model. The starting point will be a collection of data to be considered as the private knowledge base. The final goal is that an end user of the app can ask questions about the data and the assistant responses will only be based on this knowledge base.
High-level Flow
The complete technical flow can be split up into the following three steps at a high level:
-
Prepare the knowledge base (once per document)
- Data is chunked into smaller, partially overlapping, pieces of information.
- For each data chunk, the embedding vector will be retrieved from OpenAI’s embeddings API.
- Data chunks (or their identifier) are stored in a vector database together with their embedding vector.
-
Query the knowledge base (once per search)
- User query is sent to the embeddings API to retrieve the embedding vector of the query.
- A pre-defined number of most-relevant data chunks is retrieved from the vector database. This set is selected based on cosine similarity to the user query embedding vector.
-
Invoke the text generation model (once per search)
- User query and the relevant data chunks are sent to the chat completions API.
- Through prompt engineering, the text generation model is instructed to only base the answer on the data chunks that were sent as part of the request. This prevents the model from hallucinating.
- The assistant response is returned to the user.
In summary, in the first step, you need to provide the private knowledge base, such as a text snippet. You need to prepare the content for RAG, which happens only once. If the content changes, you need to provide it again for RAG. The last two steps happen every time an end-user triggers the RAG flow, for example, by asking a question about the data.
RAG Example in the OpenAI Showcase Application
Prerequisites
Before you start experimenting with the end-to-end process, make sure that you have covered the following prerequisites:
You have access to a (remote) PostgreSQL database with the pgvector extension available.
Steps
-
Download, run, and login to the OpenAI Showcase App.
-
Go to the Retrieval Augmented Generation example and read Step 1: Introduction.
-
Set Up a PostgreSQL vector database and configure the connection in Step 2: Vector Database Configuration.
-
Go to Step 3: Knowledge Base and create embeddings from a text and store. You can use our default text about ornamental flowering plants, or paste your own content.
-
Go to Step 4: Embedding Vectors. Verify the embedding vectors have been created in your new database. If you ever want to go back to load different content instead, any existing records are replaced automatically.
-
Go to Step 5: User Prompt and do as follows:
- Ask something about the entered text. The system prompt is automatically enriched with the chunks of text from the knowledge base that are most relevant for the user query.
- Review the augmented prompt.
- Let the model run the retrieval augmented chat completion and view the results.
The OpenAI showcase application uses the Mendix PgVector Knowledge Base module from the Marketplace to run queries and statements on your remote database. If you want to know more about this or if you are looking for certain technical details, see PgVector Knowledge Base.
The OpenAI showcase application uses this module to take care of creating the required tables in the remote vector database, including the open-source extension called pgvector. If you want to know more about the extension and how the tables look like in combination with this extension, see the pgvector source code and documentation on GitHub.
If you want to know more about RAG, embeddings, or vector databases, make sure to review the links on the Resources page of the OpenAI showcase application or in the Read More section.
Building Your Own RAG Setup
This section lists some general key points that apply regardless of which architecture you choose.
If you would like to build your own RAG setup, feel free to learn from the OpenAI showcase application and start building your own app. Below you can find the key takeaways from the OpenAI showcase app:
-
For RAG, you need a storage space for high-dimensional embedding vectors outside of your normal Mendix app database. Typically, this is a remote vector database. In order to connect to it, the OpenAI showcase application uses the Mendix database connector. See Vector Database Setup for more details.
-
The OpenAI showcase application relies on a PostgreSQL database with the pgvector extension included. In such a setup you can similarly rely on the PgVector Knowledge Base module to take care of creating and executing the right queries. If you choose to use a different type of vector database, the database queries and statements, which you will have to perform, should cover at least the following:
-
Include the vector extension if applicable for the chosen database type (create extension).
-
Create tables to store the embedding vectors (create table).
-
Add new embedding vectors to tables (insert).
-
Find top-k nearest neighbors (select query; typically using cosine distance/similarity optimization as recommended by OpenAI).
-
Remove individual records (delete) or tables (drop table).
-