diff --git a/docs/03_implement_vector_search_in_cosmos_db_nosql/0303.md b/docs/03_implement_vector_search_in_cosmos_db_nosql/0303.md index 8317ad340..ab9eb6e9d 100644 --- a/docs/03_implement_vector_search_in_cosmos_db_nosql/0303.md +++ b/docs/03_implement_vector_search_in_cosmos_db_nosql/0303.md @@ -17,8 +17,9 @@ In the previous task, you added vector embeddings to property maintenance reques The key tasks are as follows: -1. Generate query text embeddings using the `/Vectorize` endpoint of the ContosoSuitesWebAPI. -2. Provide a method named `ExecuteVectorSearch` in the `VectorizationService` for executing a vector search query against Azure Cosmos DB. The method should accept vectorized query text (named `queryVector`), the maximum number of results to return (named `max_results`), and the minimum similarity score (named `minimum_similarity_score`) and execute a query using the `VectorDistance()` function. It should return a list of `VectorSearchResult` objects. The count should limit the number of results returned. The query executed in Cosmos DB should use the following pattern: +1. Add a new .NET user secret called `CosmosDB:ConnectionString`. The value of this should be your Cosmos DB connection string. Add another user secret called `AzureOpenAI:EmbeddingDeploymentName`, which should take on a value of `text-embedding-ada-002`. +2. Generate query text embeddings using the `/Vectorize` endpoint of the ContosoSuitesWebAPI. +3. Provide a method named `ExecuteVectorSearch` in the `VectorizationService` for executing a vector search query against Azure Cosmos DB. The method should accept vectorized query text (named `queryVector`), the maximum number of results to return (named `max_results`), and the minimum similarity score (named `minimum_similarity_score`) and execute a query using the `VectorDistance()` function. It should return a list of `VectorSearchResult` objects. The count should limit the number of results returned. The query executed in Cosmos DB should use the following pattern: ```csharp var query = $"SELECT c.hotel_id AS HotelId, c.hotel AS Hotel, c.details AS Details, c.source AS Source, VectorDistance(c.request_vector, [{string.Join(",", queryVector)}]) AS SimilarityScore FROM c"; @@ -26,8 +27,8 @@ The key tasks are as follows: query += $" ORDER BY VectorDistance(c.request_vector, [{string.Join(",", queryVector)}])"; ``` -3. Update the `/VectorSearch` route handler in the `Program.cs` file to call the `ExecuteVectorSearch` method on the `VectorizationService` and return the results. -4. Implement vector search capabilities to the UI and submit the following queries for maintenance requests: +4. Update the `/VectorSearch` route handler in the `Program.cs` file to call the `ExecuteVectorSearch` method on the `VectorizationService` and return the results. +5. Implement vector search capabilities to the UI and submit the following queries for maintenance requests: - Set the search query to "Show me rooms where the air conditioning unit is malfunctioning" and enter 10 as the max result count and select a minimum similarity score of 0.80. - Execute the same query a second time, this time setting the minimum similarity score to 0.82 and observe how that setting can be used to reduce the number of irrelevant results. - Search for "Rooms at the Oceanview Inn where there are air conditioning issues" and enter 5 as the maximum number of results to return and set the minimum similarity score slider to 0.85. @@ -58,27 +59,15 @@ The key tasks are as follows:
Expland this section to view the solution -- The steps to generate vector embeddings for query text using the ContosoSuitesWebAPI's `/Vectorize` endpoint are as follows: - - In Visual Studio Code, navigate to the `src\ContosoSuitesWebAPI` project in the explorer pane on the left-hand side, then locate and open the `appsettings.development.json` file. The file should look similar to the following: +- To add the user secrets, run the following command: - ```json - "AzureOpenAI": { - "Endpoint": "[YOUR_AZURE_OPENAI_ENDPOINT]", - "ApiKey": "[YOUR_AZURE_OPENAI_KEY]", - "DeploymentName": "gpt-4o", - "EmbeddingDeploymentName": "text-embedding-ada-002" - }, - "CosmosDB": { - "ConnectionString": "[YOUR_COSMOS_DB_CONNECTION_STRING]", - "DatabaseName": "ContosoSuites", - "MaintenanceRequestsContainerName": "MaintenanceRequests" - }, - "SQLCONNSTR_ContosoSuites": "[YOUR_AZURE_SQL_DB_CONNECTION_STRING]" - ``` + ```sh + dotnet user-secrets set "CosmosDB:ConnectionString" "{YOUR_CONNECTION_STRING}" + dotnet user-secrets set "AzureOpenAI:EmbeddingDeploymentName" "text-embedding-ada-002" + ``` - - In the `appsettings.development.json` file, update the settings to provide your Cosmos DB connection string and your Azure OpenAI endpoint and key. Note, the deployment name is preset, but if it differs in your environment, this will need to be set to the value you configured. - - Save the `appsettings.development.json` file. - - In Visual Studio Code, open a new terminal window and change the directory to `scr\ContotoSuitesWebAPI`. +- The steps to generate vector embeddings for query text using the ContosoSuitesWebAPI's `/Vectorize` endpoint are as follows: + - In Visual Studio Code, open a new terminal window and change the directory to `src\ContotoSuitesWebAPI`. - At the terminal prompt, enter the following command to run the API locally: ```bash @@ -86,6 +75,10 @@ The key tasks are as follows: ``` - Once the API has started, as indicated by output in the terminal stating `Now listening on: http://localhost:5292`, open a web browser and navigate to the [Swagger UI page for the API](http://localhost:5292/swagger/). + + {: .note } + > If you are using a GitHub Codespaces instance, open the website in a browser and navigate to the **/swagger** URL. + - On the Swagger UI page, expand the `/Vectorize` endpoint block. ![The Swagger UI page is displayed with the expand button for the Vectorize endpoint highlighted.](../../media/Solution/0303-web-api-swagger-ui.png) @@ -118,7 +111,7 @@ The key tasks are as follows: - In Visual Studio Code, stop the API project by selecting the teminal window where it is runnig and pressing CTRL+C. - To provide a method in the `VectorizationService` and an API endpoint for executing a vector search query against Azure Cosmos DB: - - In Visual Studio Code, open the `IVectoriztionService.cs` file in the `src\ContosoSuitesWebAPI` folder and complete `Exercise 3 Task 3 TODO #1` by uncommenting the interface definition for the `ExecuteVectorSearch` method. + - In Visual Studio Code, open the `IVectorizationService.cs` file in the `src\ContosoSuitesWebAPI` folder and complete `Exercise 3 Task 3 TODO #1` by uncommenting the interface definition for the `ExecuteVectorSearch` method. - Next, open the `VectorizationService.cs` file in the `src\ContosoSuitesWebAPI` folder and complete `Exercise 3 Task 3 TODO #2` by uncommenting the method definition for the `ExecuteVectorSearch` method. - To complete the code for the exposing `/VectorSearch` endpoint on the API: