Skip to content

Commit

Permalink
Update E3T3
Browse files Browse the repository at this point in the history
  • Loading branch information
feaselkl committed Sep 13, 2024
1 parent 36c7934 commit f4c83f7
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions docs/03_implement_vector_search_in_cosmos_db_nosql/0303.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ 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";
query += $" WHERE VectorDistance(c.request_vector, [{string.Join(",", queryVector)}]) > {minimum_similarity_score}";
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.
Expand Down Expand Up @@ -58,34 +59,26 @@ The key tasks are as follows:
<details markdown="block">
<summary>Expland this section to view the solution</summary>

- 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
dotnet run
```

- 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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f4c83f7

Please sign in to comment.