This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.
Introduction
In this article I will guide you on how to build a Generative AI application in Microsoft Fabric.
This guide will walk you through implementing a RAG (Retrieval Augmented Generation) system in Microsoft Fabric using Azure OpenAI and Microsoft Fabric Eventhouse as your vector store.
Why MS Fabric Eventhouse?
Fabric Eventhouse is built using the Kusto Engine that delivers top-notch performance for similarity search at high scale.
If you are looking to build a RAG application with a large number of embeddings vectors, look no more, using MS Fabric you can leverage the processing power for building the Vector Database and the high performant engine powering Fabric Eventhouse DB.
If you want to know more about using Fabric Eventhouse as a Vector store here are some links.
Azure Data Explorer for Vector Similarity Search
Optimizing Vector Similarity Search on Azure Data Explorer – Performance Update
Optimizing Vector Similarity Searches at Scale
What is RAG - Retrieval Augmented Generation?
Large Language Models (LLMs) excel in creating text that resembles human writing.
Initially, LLMs are equipped with a broad spectrum of knowledge from extensive datasets used for their training. This grants them flexibility but may not provide the specialized focus or knowledge necessary in certain topics.
Retrieval Augmented Generation (RAG) is a technique that improves the pertinence and precision of LLMs by incorporating real-time, relevant information into their responses. With RAG, an LLM is boosted by a search system that sifts through unstructured text to find information, which then refines the LLM's replies.
What is a Vector Database?
The Vector Database is a vital component in the retrieval process in RAG, facilitating the quick and effective identification of relevant text sections in response to a query, based on how closely they match the search terms.
Vector DBs are data stores optimized for storing and processing vector data. Vector data can refer to data types such as geometric shapes, spatial data, or more abstract high-dimensional data used in machine learning applications, such as embeddings.
These databases are designed to efficiently handle operations such as similarity search, nearest neighbour search, and other operations that are common when dealing with high-dimensional vector spaces.
For example, in machine learning, it's common to convert text, images, or other complex data into high-dimensional vectors using models like word embeddings, image embeddings, etc. To efficiently search and compare these vectors, a vector database or vector store with specialized indexing and search algorithms would be used.
In our case we will use Azure OpenAI Ada Embeddings model to create embeddings, which are vector representations of the text we are indexing and storing in Microsoft Fabric Eventhouse DB.
The code
The code can be found here.
We will use the Moby Dick book from the Gutenberg project in PDF format as our knowledge base.
We will read the PDF file, cut the text into chunks of 1000 characters and calculate the embeddings for each chunk, then we will store the text and the embeddings in our Vector Database (Fabric Eventhouse)
We will then ask questions and get answers from our Vector DB and send the question and answers to Azure OpenAI GPT4 to get a response in natural language.
Processing the files and indexing the embeddings
We will do this once – only to create the embeddings and then save them into our Vector Database – Fabric Eventhouse
- Read files from Fabric Lakehouse
- Create embeddings from the text using Azure OpenAI ada Embeddings model
- Save the text and embeddings in our Fabric Eventhouse DB
RAG - Getting answers
Every time we want to search for answers from our knowledge base, we will:
- Create the embeddings for the question and search our Fabric Eventhouse for the answers, using Similarity search
- Combining the question and the retrieved answers from our Vector Database, we will call Azure OpenAI GPT4 model to get “natural language” answer.
Prerequisites
To follow this guide, you will need to ensure that you have access to the following services and have the necessary credentials and keys set up.
- Microsoft Fabric.
- Azure OpenAI Studio to manage and deploy OpenAI models.
Setup
Create a Fabric Workspace
Create a Lakehouse
Upload the moby dick pdf file
Create an Eventhouse DB called “GenAI_eventhouse”
Click on the DB name and then “Explore your data” on the top-right side
Create the “bookEmbeddings” table
Paste the following command and run it
Import our notebook
Grab your Azure openAI endpoint and secret key and paste it in the notebook, replace your models deployment names if needed.
Get the Eventhouse URI and paste it as “KUSTO_URI” in the notebook
Connect the notebook to the Lakehouse
Let’s run our notebook
This will install all the python libraries we need
Run cell 2 after configuring the environment variables for:
Run cell 3
Here we create an Azure OpenAI client and define a function to calculate embeddings
Run cell 4
Read the file, divide it into 1000 chars chunks
Run cell 5
Save the text chunks to a pandas dataframe
Run cell 6
Calculate embeddings
Run cell 7
Write the data to MS Fabric Eventhouse
Let’s check the data was saved to our Vector Database
Go to the Eventhouse and run this query
Go back to the notebook and run the rest of the cells
Creates a function to call GPT4 for a NL answer
Creates a function to retrieve answers using embeddings with similarity search
Retrieves 2 answers from Eventhouse
Concatenates the answers
Creates a prompt for GPT4 with the question and the 2 answers
That’s it, you have built your very first RAG app using MS Fabric
All the code can be found here.
Thanks
Denise