RAG (Retrieval Augmented Generation) is an architectural approach that gives data as context for LLM to improve relevancy. This helps in improving the efficacy of LLM applications. Azure AI search helps in the information retrieval in RAG architecture. RAG solution using Azure AI search and Open AI is as follows:
Application sends the user request to Azure AI to retrieve relevant information from Data sources(files/DB) and additionally Open AI helps as generative AI
Let’s discuss the steps to generate RAG report using Azure AI search and Open AI in c#.
PREREQUISITE
- Azure account
- Azure AI search
- Azure OpenAI resource with gpt-35-turbo deployment model
STEPS:
1.In Azure AI search resource, select Import Data ->Choose “hotel-samples” data source(Cosmos DB).

2.Permissions:
Give the necessary permission for the user and Open AI resource to access Azure AI search

Likewise go to Open AI resource and give necessary permission to the user and AI search resource.

3.In the Azure search service ->Settings->Keys, under API access control choose “Both.”
4.Create a console application with .Net 8.0
5.Install the Nuget packages as below

In the visual studio login with the account which has been granted permission to access Azure AI search and Azure Open AI.
var credential = new DefaultAzureCredential();
var openAIClient = new OpenAIClient(new Uri(AZURE_OPENAI_ACCOUNT), credential);
var searchClient = new SearchClient(new Uri(AZURE_SEARCH_SERVICE), INDEX_NAME, credential);
Open AI Client and Search Client are created as above.
Lets assume to pass the query string in the code itself as below
string query = "Can you recommend a few hotels near the ocean with beach access and good views?";
Once search client is created, “SearchAsync” function helps in retrieving the relevant information.
Using chat completion option in Open AI, passing search results as chat messages gives the response to the query.
var chatMessages = new List
{
new ChatRequestUserMessage(“Can you recommend a few hotels near the ocean with beach access?”),
};
// Create the chat completions options
var chatCompletionsOptions = new ChatCompletionsOptions(AZURE_DEPLOYMENT_MODEL, chatMessages);
In the given sample, we have used Cosmos DB. We can also connect to Azure blob storage and get RAG solution based on the files uploaded.
Open AI and Search resource should have “Storage Blob Data Reader” permission in the storage account.
Plus, seamlessly integrate with chatbots to create dynamic, interactive reports.
REFERENCES :
Hope this blog helps in creating RAG using Azure AI search and Azure Open AI.
No Comment! Be the first one.