https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore
A sample for implementing retrieval augmented generation using Azure Open AI to generate embeddings, Azure Cosmos DB for MongoDB vCore to perform vector search, and semantic kernel.
https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore
azd-templates azure cosmosdb mongodb openai python quart rag semantic-kernel vcore vector-search
Last synced: 6 months ago
JSON representation
A sample for implementing retrieval augmented generation using Azure Open AI to generate embeddings, Azure Cosmos DB for MongoDB vCore to perform vector search, and semantic kernel.
- Host: GitHub
- URL: https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore
- Owner: john0isaac
- License: mit
- Created: 2024-02-12T12:00:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-31T06:36:16.000Z (6 months ago)
- Last Synced: 2025-03-31T17:17:15.765Z (6 months ago)
- Topics: azd-templates, azure, cosmosdb, mongodb, openai, python, quart, rag, semantic-kernel, vcore, vector-search
- Language: Bicep
- Homepage: https://techcommunity.microsoft.com/t5/educator-developer-blog/build-rag-chat-application-using-azure-openai-and-cosmos-db-for/ba-p/4055852
- Size: 90.1 MB
- Stars: 33
- Watchers: 2
- Forks: 62
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
name: RAG using Semantic Kernel with Azure OpenAI and Azure Cosmos DB for MongoDB vCore
description: A Python sample for implementing retrieval augmented generation using Azure Open AI to generate embeddings, Azure Cosmos DB for MongoDB vCore to perform vector search and semantic kernel. Deployed to Azure App service using Azure Developer CLI (azd).
languages:
- azdeveloper
- python
- bicep
- html
products:
- azure
- azure-app-service
- azure-openai
- cosmos-db
- mongodb-vcore
page_type: sample
urlFragment: rag-semantic-kernel-mongodb-vcore
---# RAG using Semantic Kernel with Azure OpenAI and Azure Cosmos DB for MongoDB vCore
A Python sample for implementing retrieval augmented generation using Azure Open AI to generate embeddings, Azure Cosmos DB for MongoDB vCore to perform vector search and semantic kernel. Deployed to Azure App service using Azure Developer CLI (azd).
[](https://www.youtube.com/watch?v=LiS9CqR0ZSk)
> 🎥 Click this image to watch the recorded reactor workshop## How to use?
1. Create the following resources on Microsoft Azure:
- Azure Cosmos DB for MongoDB vCore cluster. See the [Quick Start guide here](https://techcommunity.microsoft.com/t5/educator-developer-blog/build-rag-chat-app-using-azure-cosmos-db-for-mongodb-vcore-and/ba-p/4055852#:~:text=RAG%20Chat%20Application-,Step%201%3A%20Create%20an%20Azure%20Cosmos%20DB%20for%20MongoDB%20vCore%20Cluster,-In%20this%20step).
- Azure OpenAI resource with:
- Embedding model deployment. (ex. `text-embedding-ada-002`) See the [guide here](https://techcommunity.microsoft.com/t5/educator-developer-blog/build-rag-chat-app-using-azure-cosmos-db-for-mongodb-vcore-and/ba-p/4055852#:~:text=to%20it%20later.-,Step%202%3A%C2%A0Create%20an%20Azure%20OpenAI%20resource%20and%20Deploy%20chat%20and%20embedding%20Models,-In%20this%20step).
- Chat model deployment. (ex. `gpt-35-turbo`)1. 📝 Start here 👉 [rag-azure-openai-cosmosdb-notebook.ipynb](./rag-azure-openai-cosmosdb-notebook.ipynb)
https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore/assets/64026625/676a0e10-876f-45e6-942d-0494ac327c75
Test it inside codespaces 👇
[](https://codespaces.new/john0isaac/rag-semantic-kernel-mongodb-vcore?devcontainer_path=.devcontainer/devcontainer.json)
## Running the web app locally
To run the Quart application, follow these steps:
1. **Download the project starter code locally**
```bash
git clone https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore.git
cd rag-semantic-kernel-mongodb-vcore
```1. **Install, initialize and activate a virtualenv using:**
```bash
pip install virtualenv
python -m virtualenv .venv
source .venv/bin/activate
```>**Note** - In Windows, the `.venv` does not have a `bin` directory. Therefore, you'd use the analogous command shown below:
```bash
source .venv/Scripts/activate
```1. **Install the dependencies:**
```bash
pip install -r requirements-dev.txt
```1. **Run the [notebook](./rag-azure-openai-cosmosdb-notebook.ipynb) to generate the .env file and test out everything first**
1. **Install the app as an editable package:**
```bash
pip install -e src
```1. **Execute the following command in your terminal to start the quart app**
```bash
export QUART_APP=src.quartapp
export QUART_ENV=development
export QUART_DEBUG=true
quart run --reload
```**For Windows, use [`setx`](https://learn.microsoft.com/windows-server/administration/windows-commands/setx) command shown below:**
```powershell
setx QUART_APP src.quartapp
setx QUART_ENV development
setx QUART_DEBUG true
quart run --reload
```1. **Verify on the Browser**
Navigate to project homepage [http://127.0.0.1:5000/](http://127.0.0.1:5000/) or [http://localhost:5000](http://localhost:5000)
https://github.com/john0isaac/rag-semantic-kernel-mongodb-vcore/assets/64026625/8a7556d6-2b54-40b5-825b-06d6efd4d1ca
## Step-by-Step Deployment
Follow this guide 👉 [Build RAG Chat App using Azure Cosmos DB for MongoDB vCore and Azure OpenAI: Step-by-Step Guide](https://techcommunity.microsoft.com/t5/educator-developer-blog/build-rag-chat-app-using-azure-cosmos-db-for-mongodb-vcore-and/ba-p/4055852)

## `azd` Deployment

This repository is set up for deployment on Azure App Service (w/Azure Cosmos DB for MongoDB vCore) using the configuration files in the `infra` folder.
To deploy your own instance, follow these steps:
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/)
1. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd).
1. Login to your Azure account:
```shell
azd auth login
```1. Initialize a new `azd` environment:
```shell
azd init
```It will prompt you to provide a name (like "quart-app") that will later be used in the name of the deployed resources.
1. Provision and deploy all the resources:
```shell
azd up
```It will prompt you to login, pick a subscription, and provide a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location (like to "centralus") can help, as there may be availability constraints for some of the resources.
When azd has finished deploying, you'll see an endpoint URI in the command output. Visit that URI to browse the app! 🎉
> [!NOTE]
> If you make any changes to the app code, you can just run this command to redeploy it:
>
> ```shell
> azd deploy
> ```
>## Add the Data
1. Open the [Azure portal](https://portal.azure.com) and sign in.
1. Navigate to your App Service page.

1. Select **SSH** from the left menu then, select **Go**.
1. In the SSH terminal, run `pip install -e .` then run `python ./scripts/add_data.py`.
## Add your Own Data
The Python scrips that adds the data is configured to accept any JSON file with your data but you need to specify the following parameters when you run it:
- Data file path: Path to the JSON file that contains your data. `--file="./data/text-sample.json"` or `-f "./data/text-sample.json"`
- ID field: This is the name of the field that cosmos uses to identify your database records. `--id-field=id` or `-id id`
- Text field: This is the name of the field that will be used to generate the vector embeddings from and stored in the database. `--text-field=content` or `-txt content`
- Description field: This is the name of the description field that cosmos will store along with the embeddings. `--description-field=title` or `-desc title````bash
python ./scripts/add_data.py --file="./data/text-sample.json" --id-field=id --text-field=content --description-field=title
```### Example for Step-by-step Manual Deployment
1. Add your JSON data to the [data folder](./src/data/).
1. The workflow will trigger automatically and push your data to the Azure App service.
1. Open the [Azure portal](https://portal.azure.com) and sign in.
1. Navigate to your App Service page.

1. Select **SSH** from the left menu then, select **Go**.
1. In the SSH terminal, run the following command with the changed values to suit your data:
```bash
pip install -e .
python ./scripts/add_data.py --file="./data/text-sample.json" --id-field=id --text-field=content --description-field=title
```### Example for `azd` Deployment
1. Add your JSON data to the [data folder](./src/data/).
1. Run `azd deploy` to upload the data to Azure App Service.
1. Open the [Azure portal](https://portal.azure.com) and sign in.
1. Navigate to your App Service page.

1. Select **SSH** from the left menu then, select **Go**.
1. In the SSH terminal, run the following command with the changed values to suit your data:
```bash
pip install -e .
python ./scripts/add_data.py --file="./data/text-sample.json" --id-field=id --text-field=content --description-field=title
```