Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (2024)

Introduction

Retrieval Augmented Generation (RAG) is a way of making chat applications intelligently retrieve a subset of data from your data store to provide specific, contextual knowledge to the large language model to support how it answers a user’s prompt and ground its responses to your specific use case.​ Azure Cosmos DB for Mongo DB vCore is one of the only Azure databases that provides built-in vector search at any scale which makes it easy for you to store your semi-structured data and query it at the same time and place with the powerful capabilities of speed and scalability that Azure Cosmos DB gives to you. You no longer need to store your data somewhere and perform a search over it in another place as Azure Cosmos DB was built for AI-DrivenApplications to do everything you need in one place.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (1)

Scenario

Imagine you are experimenting with Azure OpenAI large language models to develop your company's RAG chat application. You want to integrate cutting-edge LLM technology quickly and easily into your apps. You heard about the Semantic Kernel and that it provides a way to orchestrate your AI-driven chat flow in some very simple steps. You've created a powerful chat application using the Semantic Kernel but you want to make it grounded to your company's data. You may have heard about RAG and how to generate vector embeddings which are a bunch of numbers that you can compare the similarity between and provide the LLM with a specific source of information to respond from. You also heard that vector search is not supported by all databases so you'll need a way to work around this luckily Azure Cosmos DB for MongoDB vCore is one of the few databases that has built-in vector search. You decided to take your company's data and convert it to JSON to store it in the database and deploy a simple chat application on Azure App Service to compare the vector search alone and the RAG flow and share it with others to test it.

What will you learn?

In this blog, you'll learn to:

  • Create an Azure Cosmos DB for MongoDB vCore cluster.
  • Create an embeddings and a chat deployment in Azure OpenAI studio.
  • Create an Azure App service website with Continous deployment from GitHub.
  • Configure Azure App service application settings to enable communication between Azure resources.
  • Configure GitHub workflow to work successfully.

What is the main objective?

Build a RAG Chat Web Application using the Semantic Kernel, Azure OpenAI, and Azure Cosmos DB for MongoDB vCore: Step-by-Step Guide

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (2)

Prerequisites

  • An Azure subscription.
    • If you don’t already have one, you can sign up for anAzure free account.
    • For students, you can use the freeAzure for Students offerwhich doesn’t require a credit card only your school email.
  • Access to Azure OpenAI in the desired Azure subscription.
    • Azure OpenAI requires registration and is currently only available to approved enterprise customers and partners.See Limited access to Azure OpenAI Servicefor more information.
    • You can apply for access to Azure OpenAI by completing the form athttps://aka.ms/oai/access.
  • A GitHub Account.

Summary of the steps:

Step 1: Create an Azure Cosmos DB for MongoDB vCore Cluster

Step 2: Create an Azure OpenAI resource and Deploy chat and embedding Models

Step 3: Create an Azure App Service andDeploy the RAG Chat Application

Step 1: Create an Azure Cosmos DB for MongoDB vCore Cluster

In this step, you'll:

  • Open theAzure Portal.
  • Create an Azure Cosmos DB for MongoDB vCore Cluster.

Open theAzure Portal

1. Visit the Azure Portalhttps://portal.azure.comin your browserandsign in.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (3)

Now you are inside theAzure portal!

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (4)

Create a new Azure Cosmos DB for MongoDB vCore Cluster

In this step, you create an Azure Cosmos DB for MongoDB vCore Cluster to store your data, vector embedding, and perform vector search.

1.Typemongodb vcorein thesearch barat the top of the portal page and selectAzure Cosmos DB for MongoDB (vCore) from the available options.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (5)

2. Select Createfrom the toolbar to start provisioning your new cluster.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (6)

3.Add the following information to create a resource:

WhatValue
SubscriptionUse your preferred subscription. It's advised to use the same subscription across all the resources that communicate with each other on Azure.
Resource groupSelect Create newto create a new resource group. Enter a unique name for the resource group.
Cluster nameEnter a globally unique name.
LocationSelect a region close to you for the best response time. For example, SelectUK South.
MongoDB versionSelect the latest available version of MongoDB.


Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (7)

4. Select Configure to configure your cluster tier.

5.Add the following information to configure the cluster tier. You can scale it up later:

WhatValue
Cluster tierSelect M25 tier, 2 (Burstable) vCores.
Storage

Select 32 GiB.

6. Select Save.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (8)

7. Enter the cluster AdminUsername and Password and store them in a secure location.

8. Select Next to configure the networking settings.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (9)

9. Select Allow Public Access from Azure services and resources within the Azure to this cluster.

10. Select Add current IP address to the firewall rules to allow local access to the cluster.

11. Select Review + create.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (10)

12.Confirm your configuration settings and selectCreateto start provisioning the resource.

Note: The cluster creation can take up to 10 minutes. It's recommended to move on with the rest of the steps and get back to it later.

Step 2:Create an Azure OpenAI resource and Deploy chat and embedding Models

In this step, you'll:

  • Create an Azure OpenAI resource.
  • Create chat and embedding model deployments.

Create an Azure OpenAI resource

In this step, you create an Azure OpenAI Service resource that enables you to interact with different large language models (LLMs).

1.Typeopenaiin thesearch barat the top of the portal page and selectAzure OpenAIfrom the available options.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (11)

2. Select Create from the toolbar to provision a new OpenAI resource.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (12)

3. Add the following information to create a resource:

WhatValue
SubscriptionUse the same subscription you used to apply for Azure OpenAI access.
Resource groupUse the resource group you created in the previous step.
RegionSelect a region close to you for the best response time. For example, SelectUK South.
NameEnter a globally unique name.
Pricing tier

Select S0. Currently, this is the only available pricing tier.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (13)

4. Now that the basic information is added, selectNextto confirm your details and proceed to the next page.

5. SelectNextto confirm your network details.

6. SelectNextto confirm your tag details.

7. Confirm your configuration settings and selectCreateto start provisioning the resource. Wait for the deployment to finish.

8. After the deployment finishes, selectGo to resourceto inspect your created resource. Here, you can manage your resource and find important information like the endpoint URL and API keys.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (14)

Create chat and embedding model deployments

In this step, you create an Azure OpenAI embedding model deployment and a chat model deployment.Creating a deployment on your previously provisioned resource allows you to generate text embeddings (i.e. numerical representation for text) and have a natural language conversation with your data.

1. SelectGo to Azure OpenAIStudiofrom the toolbar to open the studio.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (15)

2. SelectCreate new deploymentto go to the deployments tab.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (16)

3. Select+ Create new deploymentfrom the toolbar. ADeploy modelwindow opens.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (17)

4.Add the following information to create a chat model deployment:

WhatValue
Select a modelSelectgpt-35-turbo.
Model versionSelect0301.
Deployment nameAdd a name that's unique for this cloud instance. For example,chat-modelbecause this model type is optimized for having conversations.

5. SelectCreate.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (18)

6. Select+ Create new deploymentfrom the toolbar. ADeploy modelwindow opens.

7.Add the following information to create an embedding model deployment:

WhatValue
Select a modelSelecttext-embedding-ada-002.
Model versionSelect2.
Deployment nameAdd a name that's unique for this cloud instance. For example,embedding-modelbecause this model type is optimized for creating embeddings.

8. SelectCreate.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (19)

Step 3: Create an Azure App Service andDeploy the RAG Chat Application

In this step, you'll:

  • Fork the sample Repository on GitHub.
  • Create an Azure App service resource with a deployment from GitHub.
  • Modify Azure App service Application settings in the Azure portal.
  • Configure the Workflow to deploy your application from GitHub.
  • Test the website before and After adding the data.

Fork the sample Repository on GitHub

In this step, you create a copy from the source code on your GitHub account to be able to edit it and use it later.

1. Visit the samplegithub.com/john0isaac/rag-semantic-kernel-mongodb-vcorein your browserandsign in.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (20)

2. SelectForkfrom the top of the sample page.

3. Select an owner for the fork then, selectCreate fork.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (21)

Create an Azure App service resource with a deployment from GitHub

In this step, you create an Azure App service resource and connect it with your GitHub account to deploy a Python application.

1.Type app servicein thesearch barat the top of the portal page and selectApp Servicesfrom the available options.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (22)

2. Select Create Web App from the toolbar to start provisioning a new web application.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (23)

3.Add the following information to fill in the basic configuration of the application:

WhatValue
SubscriptionUse the same subscription you used to apply for Azure OpenAI access.
Resource groupUse the same resource group you created before.
NameEnter a unique name for your website. For example,rag-mongodb-demo.
Publish?SelectCode. This option specifies whether your deployment consists of code or a container.
Runtime stackSelect Python 3.10.
Operating SystemSelectLinux.
RegionSelectUK South. This is the region where the rest of the resources you created reside.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (24)

4. Add the following information to create the app service plan. You can scale it up later:

WhatValue
Linux PlanSelect a pre-existing plan or create a new plan.
Pricing Plan

Select Basic B1.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (25)

5. Select Deployment from the toolbar to move to the deployment configuration tab.

6. Add the following information to enable continuous deployment from GitHub:

WhatValue
Continuous deploymentSelectEnable.
GitHub accountSelect your GitHub account.
OrganizationSelect your organization. If you are using your personal account then select it.
RepositorySelect rag-semantic-kernel-mongodb-vcore.
BranchSelectmain.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (26)

7. Select Review + create.

8. Confirm your configuration settings and selectCreateto start provisioning the resource. Wait for the deployment to finish.

9. After the deployment finishes, selectGo to resourceto inspect your created resource. Here, you can manage your resource and find important information like the application settings and logs.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (27)

Modify Azure App service Application settings in the Azure portal

In this step, you configure the Application settings to make the website able to communicate with other cloud resources.

1. In the Web App resource, selectConfigurationfrom the left side menu.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (28)

2. Select+ New applicationsettingto add new environment variables to the function configuration.

3. Add the following names and values one by one and selectOk. Make sure to add your own values.

These application settings are for the Azure OpenAI resources that you created:

WhatValue
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME<chatModelDeploymentName>
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME<embeddingModelDeploymentName>
AZURE_OPENAI_DEPLOYMENT_NAME<azureOpenAiResourceName>
AZURE_OPENAI_ENDPOINThttps://<azureOpenAiResourceName>.openai.azure.com/
AZURE_OPENAI_API_KEY<azureOpenAiResourceKey>

You can get the Azure OpenAI key from the Azure OpenAI resource page.

Select Keys and Endpoint and copy any of the available keys.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (29)

These application settings are for Azure Cosmos DB for MongoDB vCore:

AZCOSMOS_APImongo-vcore
AZCOSMOS_CONNSTRmongodb+srv://<mongoAdminUser>:<mongoAdminPassword>@<mongoClusterName>.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000

You can get the Cosmo DB connection string from the Azure Cosmos DB for MongoDB vCore resource page.

Select Connection stringsand copy the connection string. Make sure to replace the user and password with the ones you created.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (30)

These application settings are new and will be created when the application starts:

AZCOSMOS_DATABASE_NAME<cosmosDatabaseName>
AZCOSMOS_CONTAINER_NAME<cosmosContainerName>

Any value should work for them.

4. Select Save.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (31)

5. Select General settingsto edit the application startup command.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (32)

6. Type entrypoint.shin the startup command field and select Save.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (33)

Configure the Workflow to deploy your application from GitHub

In this step, you modify the GitHub deployment workflow to point to the folder that contains the application.

1. Visit your forked repository on GitHub and notice the failing workflow.

2.Open the function workflow file.github/workflows/main_ragmongodbdemo.yml.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (34)

3.Open the file and select the pen icon to edit it.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (35)

4. Modify lines 31 and 36 to the following:

31run: cd src && pip install -r ./requirements.txt
36run: cd src && zip ../release.zip ./* -r

5. SelectCommit changes, and review your commit message and description. SelectCommit changes.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (36)

6. Select Actions to review the workflow run status.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (37)

Test the website before and After adding the data

In this step, you test the application before adding the data, add the data, and test again.

1. Select the workflow name to open it and get the website URL.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (38)

2. Type in the chat message What is Azure Functions?and it should respond with I don't know.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (39)

3. Navigate to your Azure App service resource page and select SSH.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (40)

4. Select Go to open a new SSH page.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (41)

5.In the SSH terminal, run this command:

python ./scripts/add_data.py

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (42)

6.Navigate back to the live website and type in the chat message What is Azure Functions?and it should respond with the correct answer now.

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (43)

Congratulations!! You successfully built the full application.

If you want to learn how to add your own data see this guideon the repository's main readme.

Clean Up

Once you finish experimenting onMicrosoft Azure you might want to delete the resources to not consume any more money from your subscription.

You can delete the resource group and it will delete everything inside it or delete the resources one by one that's totally up to you.

Conclusion

Congratulations! You've learned how to create an Azure Cosmos DB for MongoDB vCore cluster, how to create an Azure OpenAI resource, how to deploy an embedding model and a chat model from Azure OpenAI studio, how to create an Azure App service and configure continuous deployment with GitHub, and how to modify application settings to enable the communication across Azure resources. By using these technologies, you can build a RAG chat application with the option to perform vector search too over your own data and provide grounded (relevant) responses.

Next steps

Documentation

  • What is Azure OpenAI Service? - Azure AI services
  • Azure OpenAI Service embeddings - Azure OpenAI - embeddings and cosine similarity
  • Azure Cosmos DB for MongoDB vCore documentation
  • Vector Search - Azure Cosmos DB for MongoDB vCore
  • Semantic Kernel documentation

Training Content

  • Develop Generative AI solutions with Azure OpenAI Service
  • Azure Cosmos DB for MongoDB

Found this useful? Share it with others and follow me to get updates on:

Feel free to share your comments and/or inquiries in the comment section below..

See you in futuredemos!

Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide (2024)
Top Articles
Serebii.net Pokédex - #089 Muk
Jeanne D Travis Md
Victory Road Radical Red
Dannys U Pull - Self-Service Automotive Recycling
Genesis Parsippany
Occupational therapist
How to know if a financial advisor is good?
Konkurrenz für Kioske: 7-Eleven will Minisupermärkte in Deutschland etablieren
BULLETIN OF ANIMAL HEALTH AND PRODUCTION IN AFRICA
Walgreens Alma School And Dynamite
Mndot Road Closures
Weather In Moon Township 10 Days
Hillside Funeral Home Washington Nc Obituaries
Help with Choosing Parts
People Portal Loma Linda
Busted Newspaper S Randolph County Dirt The Press As Pawns
Buff Cookie Only Fans
Katherine Croan Ewald
Vipleaguenba
Bridge.trihealth
Webcentral Cuny
Homeaccess.stopandshop
R. Kelly Net Worth 2024: The King Of R&B's Rise And Fall
Hannaford To-Go: Grocery Curbside Pickup
Play Tetris Mind Bender
Olivia Maeday
The Boogeyman (Film, 2023) - MovieMeter.nl
Jesus Revolution Showtimes Near Regal Stonecrest
Harrison County Wv Arrests This Week
Publix Christmas Dinner 2022
Lesson 1.1 Practice B Geometry Answers
Vadoc Gtlvisitme App
Redbox Walmart Near Me
First Light Tomorrow Morning
Rise Meadville Reviews
Devin Mansen Obituary
The best Verizon phones for 2024
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Miami Vice turns 40: A look back at the iconic series
Best GoMovies Alternatives
Tfn Powerschool
R: Getting Help with R
Fedex Passport Locations Near Me
Coffee County Tag Office Douglas Ga
Embry Riddle Prescott Academic Calendar
Skyward Cahokia
Matt Brickman Wikipedia
Yosemite Sam Hood Ornament
Fine Taladorian Cheese Platter
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Wrentham Outlets Hours Sunday
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6171

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.