Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
"ms-azuretools.vscode-bicep",
"ms-azuretools.vscode-docker",
"ms-vscode.js-debug" ]
},
"settings": {
"remote.autoForwardPorts": true,
"dotnet.defaultSolution": "cosmos-copilot.sln"
}
}
},
"postStartCommand": "dotnet dev-certs https --trust"

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [5000, 5001],
// "portsAttributes": {
Expand All @@ -39,6 +35,9 @@
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "dotnet restore",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,3 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

appsettings.Development.json
src/cosmos-copilot.WebApp/Properties/ServiceDependencies/local/*
serviceDependencies.local.json

# Bicep compiled files
infra/**/*.json
!infra/**/main.parameters.json
127 changes: 57 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
---
page_type: sample
languages:
- azdeveloper
- bicep
- aspx-csharp
- csharp
- dockerfile
- nosql
- azdeveloper
- bicep
- aspx-csharp
- csharp
- dockerfile
- nosql
products:
- azure
- azure-cosmos-db
- azure-app-service
- azure-openai
- azure
- azure-cosmos-db
- azure-app-service
- azure-openai
urlFragment: ai-samples
name: Build Copilot app using Azure Cosmos DB for NoSQL
description: Build a Copilot app using Azure Cosmos DB for NoSQL, Azure OpenAI Service, Semantic Kernel, and .NET Aspire
description: Build a Copilot app using Azure Cosmos DB for NoSQL, Azure OpenAI Service, Azure App Service and Semantic Kernel
---

# Build a Copilot app using Azure Cosmos DB for NoSQL, Azure OpenAI Service, with Semantic Kernel
# Build a Copilot app using Azure Cosmos DB for NoSQL, Azure OpenAI Service, Azure App Service and Semantic Kernel

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/AzureCosmosDB/cosmosdb-nosql-copilot)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)]([placeholder](https://codespaces.new/AzureCosmosDB/cosmosdb-nosql-copilot))
[![Open in Dev Containers](https://img.shields.io/static/v1?style=for-the-badge&label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot)
[![Unit Tests](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/actions/workflows/unit-tests.yml)

This sample application shows how to build a multi-tenant, multi-user, Generative-AI RAG Pattern application using Azure Cosmos DB for NoSQL with its new vector database capabilities, including full-text and hybrid search with Azure OpenAI Service using Semantic Kernel. The sample provides practical guidance on many concepts you will need to design and build these types of applications. Note that some features are implemented using Cosmos DB native SDK until Semantic Kernel can support them.
This sample application shows how to build a multi-tenant, multi-user, Generative-AI RAG Pattern application using Azure Cosmos DB for NoSQL with its new vector database capabilities with Azure OpenAI Service on Azure App Service. This sample shows both using Native SDKs as well as Semantic Kernel integration. The sample provides practical guidance on many concepts you will need to design and build these types of applications.

## Important Security Notice

Expand All @@ -33,63 +32,53 @@ This template, the application code and configuration it contains, has been buil

This application demonstrates the following concepts and how to implement them:

- How to build a highly scalable, multi-tenant & user, RAG Pattern application.
- Generating embeddings and completions.
- How to build a highly scalable, multi-tenant & user, Generative-AI chat application using Azure Cosmos DB for NoSQL.
- Generating completions and embeddings using Azure OpenAI Service.
- Managing a context window (chat history) for natural conversational interactions with an LLM.
- Manage per-request token consumption for Azure OpenAI Service requests.
- Building a semantic cache for improved performance and cost.
- Implementing full-text and hybrid search using Azure Cosmos DB for NoSQL.
- Manage per-request token consumption and payload sizes for Azure OpenAI Service requests.
- Building a semantic cache using Azure Cosmos DB for NoSQL vector search for improved performance and cost.
- Using the Semantic Kernel SDK for completion and embeddings generation.
- Implementing RAG Pattern using vector search in Azure Cosmos DB for NoSQL on custom data to augment generated responses from an LLM.

### Architecture Diagram

![Architecture Diagram](./media/cosmos-nosql-copilot-diagram.png)

### User Experience

![Cosmos Copilot app user interface](./media/screenshot.png)


## Getting Started

### Prerequisites

- Azure subscription.
- Subscription access to Azure OpenAI service. Start here to [Request Access to Azure OpenAI Service](https://aka.ms/oaiapply). If you have access, see below for ensuring enough quota to deploy.
- Enroll in the [Azure Cosmos DB for NoSQL Vector Search Preview](https://learn.microsoft.com/azure/cosmos-db/nosql/vector-search#enroll-in-the-vector-search-preview-feature) (See below for more details)
- .NET 8 or above. [Download](https://dotnet.microsoft.com/download/dotnet/8.0)
- [Azure Developer CLI](https://aka.ms/azd-install)
- Visual Studio, VS Code, GitHub Codespaces or another editor to edit or view the source for this sample.

#### Deploying Azure OpenAI supported regions

The models used for this sample are **gpt-4o** and **text-3-large**. These models are not deployed in all regions and are not always present in the same region. The regions shown in the main.bicep are the known regions both models are supported in at the time this readme was last updated. To check if these models are available in additional regions, see [Azure OpenAI Service Models](https://learn.microsoft.com/azure/ai-services/openai/concepts/models)

#### Checking Azure OpenAI quota limits

For this sample to deploy successfully, there needs to be enough Azure OpenAI quota for the models used by this sample within your subscription. This sample deploys a new Azure OpenAI account with two models, **gpt-4o with 10K tokens** per minute and **text-3-large with 5k tokens** per minute. For more information on how to check your model quota and change it, see [Manage Azure OpenAI Service Quota](https://learn.microsoft.com/azure/ai-services/openai/how-to/quota)

#### Azure Subscription Permission Requirements
#### Vector search Preview details
This lab utilizes a preview feature, **Vector search for Azure Cosmos DB for NoSQL** which requires preview feature registration. Follow the below steps to register. You must be enrolled before you can deploy this solution:

1. Navigate to your Azure Cosmos DB for NoSQL resource page.
1. Select the "Features" pane under the "Settings" menu item.
1. Select for “Vector Search in Azure Cosmos DB for NoSQL”.
1. Read the description to confirm you want to enroll in the preview.
1. Select "Enable" to enroll in the Vector Search preview.

This solution deploys [user-assigned managed identities](https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview) and defines then applies Azure Cosmos DB RBAC permissions to this identity. At a minimum you will need the following Azure RBAC roles assigned to your identity in your Azure subscription or [Subscription Owner](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/privileged#owner) access which will give you both of the following.
#### Checking Azure OpenAI quota limits

- [Manged Identity Contributor](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/identity#managed-identity-contributor)
- [DocumentDB Account Contributor](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/databases#documentdb-account-contributor)
For this sample to deploy successfully, there needs to be enough Azure OpenAI quota for the models used by this sample within your subscription. This sample deploys a new Azure OpenAI account with two models, **gpt-4o with 10K tokens** per minute and **text-3-large with 5k tokens** per minute. For more information on how to check your model quota and change it, see [Manage Azure OpenAI Service Quota](https://learn.microsoft.com/azure/ai-services/openai/how-to/quota)

#### Full-Text & Hyrbrid Search Feature
#### Azure Subscription Permission Requirements

Full-text and hybrid search in Azure Cosmos DB is in Preview and only available to a subset of regions at this time. This feature is commented out in the GetChatCompletionAsync() function in the ChatService. To use this feature you must deploy this sample in either `northcentralus` or `uksouth`.
This solution deploys [user-assigned managed identities](https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview) and defines then applies Azure Cosmos DB RBAC permissions to this identity. At a minimum you will need the following Azure RBAC roles assigned to your identity in your Azure subscription or [Subscription Owner](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/privileged#owner) access which will give you both of the following.

To utilize this feature during preview, update **main.bicep** in the section below and enter either of the two regions listed above as the value for `location`

```bicep
module database 'app/database.bicep' = {
name: 'database'
scope: resourceGroup
params: {
accountName: !empty(cosmosDbAccountName) ? cosmosDbAccountName : '${abbreviations.cosmosDbAccount}-${resourceToken}'
location: 'northcentralus'
tags: tags
}
}
```
- [Manged Identity Contributor](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/identity#managed-identity-contributor)
- [DocumentDB Account Contributor](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/databases#documentdb-account-contributor)

### GitHub Codespaces

Expand Down Expand Up @@ -143,25 +132,23 @@ A related option is VS Code Dev Containers, which will open the project in your

1. Run the following command to download the solution locally to your machine:

```bash
azd init -t AzureCosmosDB/cosmosdb-nosql-copilot
```
```bash
azd init -t AzureCosmosDB/cosmosdb-nosql-copilot
```

1. From the terminal, navigate to the /infra directory in this solution.

1. Log in to AZD.

```bash
azd auth login
```
```bash
azd auth login
```

1. Provision the Azure services, build your local solution container, and deploy the application.

```bash
azd up
```

1. Follow the prompts for the subscription and select a region to deploy. **NOTE:** If intending to use the Full-Text or Hybrid search feature please see [Full-Text & Hyrbrid Search Feature](#full-text--hyrbrid-search-feature)

```bash
azd up
```

### Setting up local debugging

Expand All @@ -174,35 +161,35 @@ To modify values for the Quickstarts, locate the value of `UserSecretsId` in the
<UserSecretsId>your-guid-here</UserSecretsId>
</PropertyGroup>
```

Locate the secrets.json file and open with a text editor.

- Windows: `C:\Users\<YourUserName>\AppData\Roaming\Microsoft\UserSecrets\<UserSecretsId>\secrets.json`
- macOS/Linux: `~/.microsoft/usersecrets/<UserSecretsId>/secrets.json`


### Quickstart

Follow the Quickstarts in this solution to go through the concepts for building RAG Pattern apps and the features in this sample and how to implement them yourself.

Please see [Quickstarts](quickstart.md)


## Clean up

1. Open a terminal and navigate to the /infra directory in this solution.

1. Type azd down (--force and --purge ensure the Azure OpenAI models are deleted)

```bash
azd down --force --purge
```
1. Type azd down
```bash
azd down
```

## Guidance

### Region Availability

This template uses gpt-4o and text-embedding-3-large models which may not be available in all Azure regions. Check for [up-to-date region availability](https://learn.microsoft.com/azure/ai-services/openai/concepts/models) and select a region during deployment accordingly.
* We recommend using 'canadaeast', 'eastus', 'eastus2', 'francecentral', 'japaneast', 'norwayeast', 'polandcentral', 'southindia'
'swedencentral', 'switzerlandnorth', or 'westus3'
This template uses gpt-4o and text-embedding-3-large models which may not be available in all Azure regions. Check for [up-to-date region availability](https://learn.microsoft.com/azure/ai-services/openai/concepts/models#standard-deployment-model-availability) and select a region during deployment accordingly
* We recommend using `eastus2', 'eastus', 'japaneast', 'uksouth', 'northeurope', or 'westus3'

### Costs

Expand All @@ -220,7 +207,7 @@ Average Monthly Cost:

To learn more about the services and features demonstrated in this sample, see the following:

- [Azure Cosmos DB for NoSQL Vector Search announcement](https://aka.ms/VectorSearchGaFtsPreview)
- [Azure Cosmos DB for NoSQL Vector Search announcement](https://aka.ms/CosmosDBDiskANNBlog/)
- [Azure OpenAI Service documentation](https://learn.microsoft.com/azure/cognitive-services/openai/)
- [Semantic Kernel](https://learn.microsoft.com/semantic-kernel/overview)
- [Azure App Service documentation](https://learn.microsoft.com/azure/app-service/)
Expand Down
24 changes: 10 additions & 14 deletions azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
template: cosmos-copilot
services:
web:
project: ./src/cosmos-copilot.WebApp/cosmos-copilot.WebApp.csproj
project: ./src
language: csharp
host: appservice
hooks:
Expand All @@ -27,19 +27,17 @@ hooks:
'OpenAi:Endpoint' = $env:AZURE_OPENAI_ACCOUNT_ENDPOINT
'OpenAi:CompletionDeploymentName' = $env:AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME
'OpenAi:EmbeddingDeploymentName' = $env:AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME
'OpenAi:MaxRagTokens' = $env:AZURE_OPENAI_MAX_RAG_TOKENS
'OpenAi:MaxContextTokens' = $env:AZURE_OPENAI_MAX_CONTEXT_TOKENS
'CosmosDb:Endpoint' = $env:AZURE_COSMOS_DB_ENDPOINT
'CosmosDb:Database' = $env:AZURE_COSMOS_DB_DATABASE_NAME
'CosmosDb:ChatContainer' = $env:AZURE_COSMOS_DB_CHAT_CONTAINER_NAME
'CosmosDb:CacheContainer' = $env:AZURE_COSMOS_DB_CACHE_CONTAINER_NAME
'CosmosDb:ProductContainer' = $env:AZURE_COSMOS_DB_PRODUCT_CONTAINER_NAME
'CosmosDb:ProductDataSourceURI' = $env:AZURE_COSMOS_DB_PRODUCT_DATA_SOURCE_URI
'Chat:MaxContextWindow' = $env:AZURE_CHAT_MAX_CONTEXT_WINDOW
'CosmosDb:ProductDataSourceURI' = $env:AZURE_COSMOS_DB_PRODUCT_DATA_SOURCE
'Chat:MaxConversationTokens' = $env:AZURE_CHAT_MAX_CONVERSATION_TOKENS
'Chat:CacheSimilarityScore' = $env:AZURE_CHAT_CACHE_SIMILARITY_SCORE
'Chat:ProductMaxResults' = $env:AZURE_CHAT_PRODUCT_MAX_RESULTS
}
$userSecrets | ConvertTo-Json | dotnet user-secrets set --project ./src/cosmos-copilot.WebApp/cosmos-copilot.WebApp.csproj
$userSecrets | ConvertTo-Json | dotnet user-secrets set --project ./src/cosmos-copilot.csproj
shell: pwsh
continueOnError: false
interactive: true
Expand All @@ -49,19 +47,17 @@ hooks:
--arg openAiEndpoint $AZURE_OPENAI_ACCOUNT_ENDPOINT \
--arg openAiCompletionDeploymentName $AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME \
--arg openAiEmbeddingDeploymentName $AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME \
--arg openAiMaxRagTokens $AZURE_OPENAI_MAX_RAG_TOKENS \
--arg openAiMaxContextTokens $AZURE_OPENAI_MAX_CONTEXT_TOKENS \
--arg cosmosDbEndpoint $AZURE_COSMOS_DB_ENDPOINT \
--arg cosmosDbDatabase $AZURE_COSMOS_DB_DATABASE_NAME \
--arg cosmosDbChatContainer $AZURE_COSMOS_DB_CHAT_CONTAINER_NAME \
--arg cosmosDbCacheContainer $AZURE_COSMOS_DB_CACHE_CONTAINER_NAME \
--arg cosmosDbProductContainer $AZURE_COSMOS_DB_PRODUCT_CONTAINER_NAME \
--arg cosmosDbProductDataSourceURI $AZURE_COSMOS_DB_PRODUCT_DATA_SOURCE_URI \
--arg chatMaxContextWindow $AZURE_CHAT_MAX_CONTEXT_WINDOW \
--arg chatCacheSimilarityScore $AZURE_CHAT_CACHE_SIMILARITY_SCORE \
--arg chatProductMaxResults $AZURE_CHAT_PRODUCT_MAX_RESULTS \
'{"OpenAi:Endpoint":$openAiEndpoint,"OpenAi:CompletionDeploymentName":$openAiCompletionDeploymentName,"OpenAi:EmbeddingDeploymentName":$openAiEmbeddingDeploymentName,"OpenAi:MaxRagTokens":$openAiMaxRagTokens,"OpenAi:MaxContextTokens":$openAiMaxContextTokens,"CosmosDb:Endpoint":$cosmosDbEndpoint,"CosmosDb:Database":$cosmosDbDatabase,"CosmosDb:ChatContainer":$cosmosDbChatContainer,"CosmosDb:CacheContainer":$cosmosDbCacheContainer,"CosmosDb:ProductContainer":$cosmosDbProductContainer,"CosmosDb:ProductDataSourceURI":$cosmosDbProductDataSourceURI,"Chat:MaxContextWindow":$chatMaxContextWindow,"Chat:CacheSimilarityScore":$chatCacheSimilarityScore,"Chat:ProductMaxResults":$chatProductMaxResults}')
echo $userSecrets | dotnet user-secrets set --project ./src/cosmos-copilot.WebApp/cosmos-copilot.WebApp.csproj
--arg cosmosDbProductDataSourceURI $AZURE_COSMOS_DB_PRODUCT_DATA_SOURCE \
--arg maxConversationTokens $AZURE_CHAT_MAX_CONVERSATION_TOKENS \
--arg cacheSimilarityScore $AZURE_CHAT_CACHE_SIMILARITY_SCORE \
--arg productMaxResults $AZURE_CHAT_PRODUCT_MAX_RESULTS \
'{"OpenAi:Endpoint":$openAiEndpoint,"OpenAi:CompletionDeploymentName":$openAiCompletionDeploymentName,"OpenAi:EmbeddingDeploymentName":$openAiEmbeddingDeploymentName,"CosmosDb:Endpoint":$cosmosDbEndpoint,"CosmosDb:Database":$cosmosDbDatabase,"CosmosDb:ChatContainer":$cosmosDbChatContainer,"CosmosDb:CacheContainer":$cosmosDbCacheContainer,"CosmosDb:ProductContainer":$cosmosDbProductContainer,"CosmosDb:ProductDataSource":$cosmosDbProductDataSourceURI,"Chat:MaxConversationTokens":$maxConversationTokens,"Chat:CacheSimilarityScore":$cacheSimilarityScore,"Chat:ProductMaxResults":$productMaxResults}')
echo $userSecrets | dotnet user-secrets set --project ./src/cosmos-copilot.csproj
shell: sh
continueOnError: false
interactive: true
Loading
Loading