|
| 1 | +--- |
| 2 | +applyTo: "nosql-*/**" |
| 3 | +--- |
| 4 | +# Running Samples — CLI Invocation (Cosmos DB NoSQL Vector Search) |
| 5 | + |
| 6 | +Environment variables are loaded from a `.env` file in each sample directory. Run `azd up` first to provision infrastructure — the CLI commands below only run data-plane application code. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +```bash |
| 11 | +# 1. Deploy infrastructure (creates account, database, 2 containers with vector policies, RBAC) |
| 12 | +azd up |
| 13 | + |
| 14 | +# 2. Copy and populate the .env file in the sample directory |
| 15 | +cp sample.env .env |
| 16 | +# Edit .env with values from your azd deployment |
| 17 | +``` |
| 18 | + |
| 19 | +Infrastructure is provisioned once. Both containers (`hotels_diskann`, `hotels_quantizedflat`) persist across runs. |
| 20 | + |
| 21 | +## Environment Variables |
| 22 | + |
| 23 | +Create a `.env` file in the sample directory with these variables: |
| 24 | + |
| 25 | +| Variable | Purpose | Example | |
| 26 | +|----------|---------|---------| |
| 27 | +| `AZURE_COSMOSDB_ENDPOINT` | Cosmos DB account endpoint | `https://myaccount.documents.azure.com:443/` | |
| 28 | +| `AZURE_COSMOSDB_DATABASENAME` | Database name | `Hotels` | |
| 29 | +| `AZURE_OPENAI_EMBEDDING_ENDPOINT` | Azure OpenAI endpoint | `https://myoai.openai.azure.com/` | |
| 30 | +| `AZURE_OPENAI_EMBEDDING_MODEL` | Embedding model name | `text-embedding-3-small` | |
| 31 | +| `AZURE_OPENAI_EMBEDDING_DEPLOYMENT` | Embedding deployment name | `text-embedding-3-small` | |
| 32 | +| `AZURE_OPENAI_EMBEDDING_API_VERSION` | API version | `2024-08-01-preview` | |
| 33 | +| `DATA_FILE_WITH_VECTORS` | Path to pre-embedded data JSON | `../data/HotelsData_toCosmosDB_Vector.json` | |
| 34 | +| `DATA_FILE_WITHOUT_VECTORS` | Path to raw data JSON | `../data/HotelsData_toCosmosDB.JSON` | |
| 35 | +| `EMBEDDED_FIELD` | Vector field name | `DescriptionVector` | |
| 36 | +| `EMBEDDING_DIMENSIONS` | Vector dimensions | `1536` | |
| 37 | +| `VECTOR_ALGORITHM` | Algorithm to use | `diskann` or `quantizedflat` | |
| 38 | +| `VECTOR_DISTANCE_FUNCTION` | Distance function | `cosine`, `euclidean`, or `dotproduct` | |
| 39 | +| `AZURE_SUBSCRIPTION_ID` | Azure subscription ID | Set by `azd env get-values` | |
| 40 | +| `AZURE_TENANT_ID` | Azure AD tenant ID | Set by `azd env get-values` | |
| 41 | +| `AZURE_RESOURCE_GROUP` | Resource group name | Set by `azd env get-values` | |
| 42 | +| `AZURE_ENV_NAME` | azd environment name | Set by `azd env get-values` | |
| 43 | +| `AZURE_LOCATION` | Azure region | Set by `azd env get-values` | |
| 44 | +| `AZURE_OPENAI_SERVICE` | Azure OpenAI service name | Set by `azd env get-values` | |
| 45 | + |
| 46 | +## SDK Packages per Language |
| 47 | + |
| 48 | +| Language | Cosmos DB SDK | Azure OpenAI SDK | |
| 49 | +|----------|--------------|-----------------| |
| 50 | +| TypeScript | `@azure/cosmos` (v4.5+) | `openai` (v5+) | |
| 51 | +| .NET | `Microsoft.Azure.Cosmos` | `Azure.AI.OpenAI` | |
| 52 | +| Python | `azure-cosmos` | `openai` (v1.57+, NOT v5) | |
| 53 | +| Java | `com.azure:azure-cosmos` | `com.azure:azure-ai-openai` | |
| 54 | +| Go | `github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos` | `github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai` (v0.7.1) | |
| 55 | + |
| 56 | +## TypeScript / Node.js |
| 57 | + |
| 58 | +```bash |
| 59 | +# Install dependencies |
| 60 | +npm install |
| 61 | + |
| 62 | +# Build |
| 63 | +npm run build |
| 64 | + |
| 65 | +# Run with diskANN container |
| 66 | +npm run start:diskann |
| 67 | + |
| 68 | +# Run with quantizedFlat container |
| 69 | +npm run start:quantizedflat |
| 70 | + |
| 71 | +# Clean up inserted documents |
| 72 | +npm run delete:all |
| 73 | +``` |
| 74 | + |
| 75 | +The `.env` file is loaded natively via `node --env-file .env` (Node.js 20+). No `dotenv` package needed. |
| 76 | + |
| 77 | +### ESM Module Pattern |
| 78 | + |
| 79 | +TypeScript samples use ESM (`"type": "module"` in `package.json`): |
| 80 | +- Requires `.js` extensions in import paths (even for `.ts` source files) |
| 81 | +- Uses `import.meta.url` for `__dirname` equivalent |
| 82 | +- Requires Node.js 20+ |
| 83 | + |
| 84 | +## Python |
| 85 | + |
| 86 | +```bash |
| 87 | +# Install dependencies |
| 88 | +pip install -r requirements.txt |
| 89 | + |
| 90 | +# Run sample (set env vars before running, or use shell export) |
| 91 | +python src/vector_search.py |
| 92 | + |
| 93 | +# Clean up inserted documents |
| 94 | +python src/delete_all.py |
| 95 | +``` |
| 96 | + |
| 97 | +Set `VECTOR_ALGORITHM=diskann` or `VECTOR_ALGORITHM=quantizedflat` as an environment variable (or in `.env` and load via `export $(cat .env | xargs)`) to select the container. Do NOT use `python-dotenv` — use `os.environ` to read env vars. |
| 98 | + |
| 99 | +## Go |
| 100 | + |
| 101 | +```bash |
| 102 | +# Run sample (pass env vars at invocation, or source .env first) |
| 103 | +# Example: source .env && go run ./cmd/vector-search/... |
| 104 | +go run ./cmd/vector-search/... |
| 105 | +``` |
| 106 | + |
| 107 | +Set `VECTOR_ALGORITHM` as an environment variable before running. Go uses `os.Getenv()` — no dotenv package needed. |
| 108 | + |
| 109 | +## Java |
| 110 | + |
| 111 | +```bash |
| 112 | +# Compile and run (env vars passed via shell or system properties) |
| 113 | +mvn compile exec:java |
| 114 | +``` |
| 115 | + |
| 116 | +Configure `VECTOR_ALGORITHM` as an environment variable or system property to select `hotels_diskann` or `hotels_quantizedflat`. |
| 117 | + |
| 118 | +## .NET |
| 119 | + |
| 120 | +```bash |
| 121 | +# Run sample (reads appsettings.json + env var overrides) |
| 122 | +dotnet run |
| 123 | +``` |
| 124 | + |
| 125 | +Set overrides as environment variables. `appsettings.json` holds defaults; env vars take precedence. Do NOT use a dotenv package — .NET uses `appsettings.json` natively. |
| 126 | + |
| 127 | +## Loading Variables from `azd` Environment |
| 128 | + |
| 129 | +If you've already run `azd up`, values are available directly from the azd environment: |
| 130 | + |
| 131 | +**Bash:** |
| 132 | +```bash |
| 133 | +# Load all variables from azd environment |
| 134 | +eval $(azd env get-values) |
| 135 | + |
| 136 | +# Then run (TypeScript example) |
| 137 | +npm run start:diskann |
| 138 | +``` |
| 139 | + |
| 140 | +**PowerShell:** |
| 141 | +```powershell |
| 142 | +# Load all variables from azd environment |
| 143 | +azd env get-values | ForEach-Object { |
| 144 | + $parts = $_ -split '=', 2 |
| 145 | + [Environment]::SetEnvironmentVariable($parts[0], $parts[1].Trim('"')) |
| 146 | +} |
| 147 | +
|
| 148 | +# Then run (TypeScript example) |
| 149 | +npm run start:diskann |
| 150 | +``` |
| 151 | + |
| 152 | +## Expected Output |
| 153 | + |
| 154 | +Samples print results per container: |
| 155 | + |
| 156 | +``` |
| 157 | +--- Vector Search: hotels_diskann --- |
| 158 | +Query: "luxury hotel with ocean view" |
| 159 | +Results: |
| 160 | + HotelName: Grand Resort & Spa | Rating: 4.8 | SimilarityScore: 0.8234 |
| 161 | + HotelName: Oceanview Lodge | Rating: 4.5 | SimilarityScore: 0.7891 |
| 162 | + ... |
| 163 | +
|
| 164 | +--- Vector Search: hotels_quantizedflat --- |
| 165 | +Query: "luxury hotel with ocean view" |
| 166 | +Results: |
| 167 | + HotelName: Grand Resort & Spa | Rating: 4.8 | SimilarityScore: 0.8234 |
| 168 | + HotelName: Oceanview Lodge | Rating: 4.5 | SimilarityScore: 0.7891 |
| 169 | + ... |
| 170 | +``` |
| 171 | + |
| 172 | +Scores should be consistent between algorithms for the same query (ranking may vary slightly due to approximation). |
| 173 | + |
| 174 | +## Troubleshooting |
| 175 | + |
| 176 | +| Error | Cause | Fix | |
| 177 | +|-------|-------|-----| |
| 178 | +| `401 Unauthorized` | RBAC not assigned | Run `azd up` or assign custom "Write to Azure Cosmos DB for NoSQL data plane" role | |
| 179 | +| `Container not found` | Infrastructure not provisioned | Run `azd up` first | |
| 180 | +| `disableKeyBasedAuth` error | Using connection string | Use `DefaultAzureCredential` — key auth is disabled | |
| 181 | +| Missing module / package not found | Dependencies not installed | Run `npm install` / `pip install -r requirements.txt` / `mvn install` | |
| 182 | +| Wrong dimensions error | Mismatched embedding model | Check `EMBEDDING_DIMENSIONS` matches model output (default: `1536`) | |
| 183 | +| `.env` not found | Missing config file | Copy `sample.env` to `.env` and populate values | |
0 commit comments