Skip to content
Merged
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
14 changes: 7 additions & 7 deletions ai/select-algorithm-dotnet/CompareAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public static void Run(AppConfiguration appConfig)
Console.WriteLine(new string('=', 60));

// Use config values with env var overrides for compare-specific settings
var databaseName = appConfig.MongoDB.DatabaseName;
var databaseName = appConfig.DocumentDB.DatabaseName;
var dataFile = appConfig.DataFiles.WithVectors;
var vectorField = appConfig.Embedding.EmbeddedField;
var dimensions = appConfig.Embedding.Dimensions;
var batchSize = appConfig.MongoDB.LoadBatchSize;
var batchSize = appConfig.DocumentDB.LoadBatchSize;
var queryText = Environment.GetEnvironmentVariable("QUERY_TEXT") ?? "luxury hotel near the beach";
var topK = int.Parse(Environment.GetEnvironmentVariable("TOP_K") ?? "5");

Expand Down Expand Up @@ -65,7 +65,7 @@ public static void Run(AppConfiguration appConfig)
Console.WriteLine("Embedding generated (reused for all searches)\n");

// Define 9 index configurations
var configs = BuildIndexConfigs(dimensions);
var configs = BuildIndexConfigs();

// Run each config sequentially: drop→create→wait→search
// DocumentDB doesn't allow multiple vector indexes of the same kind on the same field
Expand All @@ -77,7 +77,7 @@ public static void Run(AppConfiguration appConfig)
DropVectorIndexes(collection, vectorField);

// 2. Create this specific index
CreateIndex(collection, vectorField, config);
CreateIndex(collection, vectorField, dimensions, config);
Console.WriteLine($" ✓ {config.Name} created");

// 3. Search with retries while the index becomes available
Expand Down Expand Up @@ -139,7 +139,7 @@ public static void Run(AppConfiguration appConfig)
}
}

private static List<IndexConfig> BuildIndexConfigs(int dimensions)
private static List<IndexConfig> BuildIndexConfigs()
{
string[] metrics = ["COS", "L2", "IP"];
var configs = new List<IndexConfig>();
Expand Down Expand Up @@ -177,7 +177,7 @@ private static void DropVectorIndexes(IMongoCollection<BsonDocument> collection,
catch { }
}

private static void CreateIndex(IMongoCollection<BsonDocument> collection, string vectorField, IndexConfig config)
private static void CreateIndex(IMongoCollection<BsonDocument> collection, string vectorField, int dimensions, IndexConfig config)
{
// Drop existing index with same name if present
try
Expand All @@ -192,7 +192,7 @@ private static void CreateIndex(IMongoCollection<BsonDocument> collection, strin
var cosmosSearchOptions = new BsonDocument
{
{ "kind", config.Kind },
{ "dimensions", int.Parse(Environment.GetEnvironmentVariable("EMBEDDING_DIMENSIONS") ?? "1536") },
{ "dimensions", dimensions },
{ "similarity", config.Similarity }
};

Expand Down
4 changes: 2 additions & 2 deletions ai/select-algorithm-dotnet/Models/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace SelectAlgorithm.Models;
public class AppConfiguration
{
public AzureOpenAIConfiguration AzureOpenAI { get; set; } = new();
public MongoDBConfiguration MongoDB { get; set; } = new();
public DocumentDBConfiguration DocumentDB { get; set; } = new();
public EmbeddingConfiguration Embedding { get; set; } = new();
public VectorSearchConfiguration VectorSearch { get; set; } = new();
public DataFilesConfiguration DataFiles { get; set; } = new();
Expand All @@ -15,7 +15,7 @@ public class AzureOpenAIConfiguration
public string EmbeddingModel { get; set; } = "text-embedding-3-small";
}

public class MongoDBConfiguration
public class DocumentDBConfiguration
{
public string ClusterName { get; set; } = string.Empty;
public string DatabaseName { get; set; } = "Hotels";
Expand Down
44 changes: 28 additions & 16 deletions ai/select-algorithm-dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Demonstrates three vector index algorithms available in Azure DocumentDB:

1. **Configure environment:**

The .NET sample uses `appsettings.json` for configuration. After deploying with `azd up`, you can export values:
The .NET sample uses `appsettings.json` for configuration. You can set values directly in `appsettings.json`, or if you deployed with `azd up`, export your provisioned values first:

```bash
azd env get-values
```

Then update `appsettings.json` with your Azure resource values.
> **Note:** `azd` is optional. You can skip it and edit `appsettings.json` manually with your Azure resource values.

2. Edit `appsettings.json` with your configuration:

Expand All @@ -35,29 +35,43 @@ Demonstrates three vector index algorithms available in Azure DocumentDB:
"EmbeddingModel": "text-embedding-3-small",
"Endpoint": "https://<your-resource>.openai.azure.com"
},
"MongoDB": {
"DocumentDB": {
"ClusterName": "<your-cluster-name>",
"DatabaseName": "Hotels",
"LoadBatchSize": 100
},
"Embedding": {
"EmbeddedField": "DescriptionVector",
"Dimensions": 1536,
"EmbeddingSizeBatch": 16
"Dimensions": 1536
},
"DataFiles": {
"WithVectors": "../data/Hotels_Vector.json"
"WithVectors": "data/Hotels_Vector.json"
}
}
```

3. Copy the shared data file:
> **Note:** .NET configuration also supports environment variable overrides. Use double-underscore (`__`) as the hierarchy separator:
> ```bash
> export DocumentDB__ClusterName=your-cluster-name
> export DocumentDB__DatabaseName=Hotels
> export AzureOpenAI__Endpoint=https://your-resource.openai.azure.com
> export AzureOpenAI__EmbeddingModel=text-embedding-3-small
> ```
> Environment variables take precedence over `appsettings.json` values.

3. Copy the shared data file into the local `data/` directory:

```bash
mkdir -p data && cp ../data/Hotels_Vector.json data/
```

4. Sign in to Azure for passwordless authentication:

```bash
cp ../data/Hotels_Vector.json .
az login
```

4. Restore packages:
5. Restore packages:

```bash
dotnet restore
Expand All @@ -75,23 +89,21 @@ dotnet run

| Setting (appsettings.json) | Default | Description |
|---------------------------|---------|-------------|
| `MongoDB:ClusterName` | (required) | DocumentDB cluster name |
| `DocumentDB:ClusterName` | (required) | DocumentDB cluster name |
| `AzureOpenAI:Endpoint` | (required) | Azure OpenAI endpoint |
| `AzureOpenAI:EmbeddingModel` | (required) | Embedding model deployment name |
| `DataFiles:WithVectors` | `../data/Hotels_Vector.json` | Path to vectors JSON file |
| `AzureOpenAI:EmbeddingModel` | `text-embedding-3-small` | Embedding model deployment name |
| `DataFiles:WithVectors` | `data/Hotels_Vector.json` | Path to vectors JSON file |
| `Embedding:EmbeddedField` | `DescriptionVector` | Field name containing embeddings |
| `Embedding:Dimensions` | `1536` | Vector dimensions |
| `MongoDB:DatabaseName` | `Hotels` | Target database name |
| `MongoDB:LoadBatchSize` | `100` | Batch size for data loading |
| `Embedding:EmbeddingSizeBatch` | `16` | Batch size for embedding requests |
| `DocumentDB:DatabaseName` | `Hotels` | Target database name |
| `DocumentDB:LoadBatchSize` | `100` | Batch size for data loading |

**Additional environment variables for compare mode:**

| Variable | Default | Description |
|----------|---------|-------------|
| `QUERY_TEXT` | `luxury hotel near the beach` | Search query text |
| `TOP_K` | `5` | Number of results per search |
| `VERBOSE` | `false` | Show detailed per-result output |

## How It Works

Expand Down
4 changes: 2 additions & 2 deletions ai/select-algorithm-dotnet/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static class Utils
{
public static IMongoClient GetMongoClientPasswordless(AppConfiguration config)
{
var clusterName = config.MongoDB.ClusterName;
var clusterName = config.DocumentDB.ClusterName;
if (string.IsNullOrEmpty(clusterName))
throw new InvalidOperationException("MongoDB:ClusterName is required in appsettings.json");
throw new InvalidOperationException("DocumentDB:ClusterName is required in appsettings.json");

var credential = new DefaultAzureCredential();

Expand Down
9 changes: 4 additions & 5 deletions ai/select-algorithm-dotnet/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"MongoDB": {
"DocumentDB": {
"DatabaseName": "Hotels",
"ClusterName": "<your-cluster-name>",
"LoadBatchSize": 50
"LoadBatchSize": 100
},
"VectorSearch": {
"Similarity": "",
Expand All @@ -14,11 +14,10 @@
"EmbeddingModel": "text-embedding-3-small"
},
"DataFiles": {
"WithVectors": "../data/Hotels_Vector.json"
"WithVectors": "data/Hotels_Vector.json"
},
"Embedding": {
"EmbeddedField": "DescriptionVector",
"Dimensions": 1536,
"EmbeddingSizeBatch": 16
"Dimensions": 1536
}
}
10 changes: 5 additions & 5 deletions ai/select-algorithm-dotnet/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Find the [sample code](https://github.com/Azure-Samples/documentdb-samples/tree/
```bash
export AzureOpenAI__Endpoint="https://<your-resource>.openai.azure.com"
export AzureOpenAI__EmbeddingModel="text-embedding-3-small"
export MongoDB__ClusterName="<your-cluster-name>"
export DocumentDB__ClusterName="<your-cluster-name>"
export DataFiles__WithVectors="data/Hotels_Vector.json"
export AZURE_TENANT_ID="<your-tenant-id>"
```
Expand All @@ -159,7 +159,7 @@ Find the [sample code](https://github.com/Azure-Samples/documentdb-samples/tree/
```powershell
$env:AzureOpenAI__Endpoint="https://<your-resource>.openai.azure.com"
$env:AzureOpenAI__EmbeddingModel="text-embedding-3-small"
$env:MongoDB__ClusterName="<your-cluster-name>"
$env:DocumentDB__ClusterName="<your-cluster-name>"
$env:DataFiles__WithVectors="data/Hotels_Vector.json"
$env:AZURE_TENANT_ID="<your-tenant-id>"
```
Expand All @@ -171,7 +171,7 @@ Find the [sample code](https://github.com/Azure-Samples/documentdb-samples/tree/
- `<your-cluster-name>`: Your Azure DocumentDB cluster name
- `<your-tenant-id>`: Your Microsoft Entra tenant ID

These environment variables override the matching values in `appsettings.json`. For example, `MongoDB__ClusterName` overrides `MongoDB:ClusterName` and `AzureOpenAI__Endpoint` overrides `AzureOpenAI:Endpoint`.
These environment variables override the matching values in `appsettings.json`. For example, `DocumentDB__ClusterName` overrides `DocumentDB:ClusterName` and `AzureOpenAI__Endpoint` overrides `AzureOpenAI:Endpoint`.

You should always prefer passwordless authentication. For more information on setting up managed identity and the full range of your authentication options, see [Authenticate .NET apps to Azure services by using the Azure SDK for .NET](/dotnet/azure/sdk/authentication).

Expand Down Expand Up @@ -205,7 +205,7 @@ Find the [sample code](https://github.com/Azure-Samples/documentdb-samples/tree/
"Endpoint": "https://<your-resource>.openai.azure.com",
"EmbeddingModel": "text-embedding-3-small"
},
"MongoDB": {
"DocumentDB": {
"ClusterName": "<your-cluster-name>",
"DatabaseName": "Hotels",
"LoadBatchSize": 100
Expand All @@ -226,7 +226,7 @@ Find the [sample code](https://github.com/Azure-Samples/documentdb-samples/tree/
}
```

You can keep placeholder values in `appsettings.json` and override them at runtime with environment variables such as `AzureOpenAI__Endpoint` and `MongoDB__ClusterName`.
You can keep placeholder values in `appsettings.json` and override them at runtime with environment variables such as `AzureOpenAI__Endpoint` and `DocumentDB__ClusterName`.

## Create code files

Expand Down
36 changes: 12 additions & 24 deletions ai/select-algorithm-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,25 @@ This sample demonstrates how to compare different vector search algorithms (IVF,

2. **Configure environment variables:**

After deploying with `azd up`, create a `.env` file with your provisioned resource values:
After deploying with `azd up`, use the provisioned output values to set these required environment variables in your shell:

```bash
azd env get-values > .env
export DOCUMENTDB_CLUSTER_NAME=your-cluster-name
export AZURE_OPENAI_EMBEDDING_ENDPOINT=https://your-resource.openai.azure.com
export AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small
export AZURE_DOCUMENTDB_DATABASENAME=Hotels
export DATA_FILE_WITH_VECTORS=data/Hotels_Vector.json
export EMBEDDED_FIELD=DescriptionVector
export EMBEDDING_DIMENSIONS=1536
```

Alternatively, copy the example and fill in values manually:
3. **Copy the shared data file** into the local `data/` directory:

```bash
cp .env.example .env
mkdir -p data && cp ../data/Hotels_Vector.json data/
```

Required variables:
```env
DOCUMENTDB_CLUSTER_NAME=your-cluster-name
AZURE_OPENAI_EMBEDDING_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small
AZURE_DOCUMENTDB_DATABASENAME=Hotels
DATA_FILE_WITH_VECTORS=../data/Hotels_Vector.json
EMBEDDED_FIELD=DescriptionVector
EMBEDDING_DIMENSIONS=1536
```

3. **Copy the shared data file** into this directory:

```bash
cp ../data/Hotels_Vector.json .
```

The `DATA_FILE_WITH_VECTORS` env var defaults to `../data/Hotels_Vector.json`.
The `DATA_FILE_WITH_VECTORS` env var defaults to `data/Hotels_Vector.json`.

4. **Install dependencies**:

Expand Down Expand Up @@ -116,7 +105,7 @@ go run ./src/...
| `AZURE_OPENAI_EMBEDDING_ENDPOINT` | *(required)* | Azure OpenAI endpoint |
| `AZURE_OPENAI_EMBEDDING_MODEL` | `text-embedding-3-small` | Embedding model name |
| `AZURE_DOCUMENTDB_DATABASENAME` | `Hotels` | Database name |
| `DATA_FILE_WITH_VECTORS` | `../data/Hotels_Vector.json` | Path to data file |
| `DATA_FILE_WITH_VECTORS` | `data/Hotels_Vector.json` | Path to data file |
| `EMBEDDED_FIELD` | `DescriptionVector` | Field containing embeddings |
| `EMBEDDING_DIMENSIONS` | `1536` | Embedding vector dimensions |
| `LOAD_SIZE_BATCH` | `100` | Batch size for data insertion |
Expand Down Expand Up @@ -147,7 +136,6 @@ go run ./src/...

```
select-algorithm-go/
├── .env.example # Environment variable template
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── output/ # Sample output files
Expand Down
Loading
Loading