Skip to content

Commit ddd0d3c

Browse files
New vector search quickstart (#43894)
* New vector search quickstart --------- Co-authored-by: Genevieve Warren <[email protected]>
1 parent 02b71ef commit ddd0d3c

File tree

10 files changed

+375
-142
lines changed

10 files changed

+375
-142
lines changed

docs/ai/quickstarts/includes/prerequisites-azure-openai.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.topic: include
77

88
## Prerequisites
99

10-
- .NET 8 SDK - [Install the .NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
10+
- .NET 8.0 SDK or higher - [Install the .NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
1111
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free).
1212
- Access to [Azure OpenAI service](/azure/ai-services/openai/overview#how-do-i-get-access-to-azure-openai).
1313
- Azure Developer CLI (Optional) - [Install or update the Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd).

docs/ai/quickstarts/includes/prerequisites-openai.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ ms.topic: include
77

88
## Prerequisites
99

10-
- .NET 8.0 SDK - [Install the .NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
10+
- .NET 8.0 SDK or higher - [Install the .NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
1111
- An [API key from OpenAI](https://platform.openai.com/docs/quickstart/account-setup) so you can run this sample.
12-
- On Windows, PowerShell `v7+` is required. To validate your version, run `pwsh` in a terminal. It should return the current version. If it returns an error, execute the following command: `dotnet tool update --global PowerShell`.

docs/ai/quickstarts/quickstart-ai-chat-with-data.md

Lines changed: 112 additions & 138 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.VectorData;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace VectorDataAI
9+
{
10+
internal class CloudService
11+
{
12+
[VectorStoreRecordKey]
13+
public int Key { get; set; }
14+
15+
[VectorStoreRecordData]
16+
public string Name { get; set; }
17+
18+
[VectorStoreRecordData]
19+
public string Description { get; set; }
20+
21+
[VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)]
22+
public ReadOnlyMemory<float> Vector { get; set; }
23+
}
24+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Azure.AI.OpenAI;
2+
using Azure.Identity;
3+
using Microsoft.Extensions.AI;
4+
using Microsoft.Extensions.VectorData;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.SemanticKernel.Connectors.InMemory;
7+
using VectorDataAI;
8+
9+
var cloudServices = new List<CloudService>()
10+
{
11+
new CloudService
12+
{
13+
Key=0,
14+
Name="Azure App Service",
15+
Description="Host .NET, Java, Node.js, and Python web applications and APIs in a fully managed Azure service. You only need to deploy your code to Azure. Azure takes care of all the infrastructure management like high availability, load balancing, and autoscaling."
16+
},
17+
new CloudService
18+
{
19+
Key=1,
20+
Name="Azure Service Bus",
21+
Description="A fully managed enterprise message broker supporting both point to point and publish-subscribe integrations. It's ideal for building decoupled applications, queue-based load leveling, or facilitating communication between microservices."
22+
},
23+
new CloudService
24+
{
25+
Key=2,
26+
Name="Azure Blob Storage",
27+
Description="Azure Blob Storage allows your applications to store and retrieve files in the cloud. Azure Storage is highly scalable to store massive amounts of data and data is stored redundantly to ensure high availability."
28+
},
29+
new CloudService
30+
{
31+
Key=3,
32+
Name="Microsoft Entra ID",
33+
Description="Manage user identities and control access to your apps, data, and resources.."
34+
},
35+
new CloudService
36+
{
37+
Key=4,
38+
Name="Azure Key Vault",
39+
Description="Store and access application secrets like connection strings and API keys in an encrypted vault with restricted access to make sure your secrets and your application aren't compromised."
40+
},
41+
new CloudService
42+
{
43+
Key=5,
44+
Name="Azure AI Search",
45+
Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization."
46+
}
47+
};
48+
49+
// Load the configuration values
50+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
51+
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
52+
string model = config["AZURE_OPENAI_GPT_NAME"];
53+
54+
// Create the embedding generator
55+
IEmbeddingGenerator<string, Embedding<float>> generator =
56+
new AzureOpenAIClient(
57+
new Uri(endpoint),
58+
new DefaultAzureCredential())
59+
.AsEmbeddingGenerator(modelId: model);
60+
61+
// Create and populate the vector store
62+
var vectorStore = new InMemoryVectorStore();
63+
var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices");
64+
await cloudServicesStore.CreateCollectionIfNotExistsAsync();
65+
66+
foreach (var service in cloudServices)
67+
{
68+
service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description);
69+
await cloudServicesStore.UpsertAsync(service);
70+
}
71+
72+
// Convert a search query to a vector and search the vector store
73+
var query = "Which Azure service should I use to store my Word documents?";
74+
var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query);
75+
76+
var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions()
77+
{
78+
Top = 1,
79+
VectorPropertyName = "Vector"
80+
});
81+
82+
await foreach (var result in results.Results)
83+
{
84+
Console.WriteLine($"Name: {result.Record.Name}");
85+
Console.WriteLine($"Description: {result.Record.Description}");
86+
Console.WriteLine($"Vector match score: {result.Score}");
87+
Console.WriteLine();
88+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.Identity" Version="1.13.1" />
12+
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.1-preview.1.24570.5" />
14+
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.0.0-preview.1.24523.1" />
15+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.31.0-preview" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.VectorData;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace VectorDataAI
9+
{
10+
internal class CloudService
11+
{
12+
[VectorStoreRecordKey]
13+
public int Key { get; set; }
14+
15+
[VectorStoreRecordData]
16+
public string Name { get; set; }
17+
18+
[VectorStoreRecordData]
19+
public string Description { get; set; }
20+
21+
[VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)]
22+
public ReadOnlyMemory<float> Vector { get; set; }
23+
}
24+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Microsoft.Extensions.AI;
2+
using OpenAI;
3+
using Microsoft.Extensions.VectorData;
4+
using Microsoft.SemanticKernel.Connectors.InMemory;
5+
using VectorDataAI;
6+
using System.ClientModel;
7+
using Microsoft.Extensions.Configuration;
8+
9+
var cloudServices = new List<CloudService>()
10+
{
11+
new CloudService
12+
{
13+
Key=0,
14+
Name="Azure App Service",
15+
Description="Host .NET, Java, Node.js, and Python web applications and APIs in a fully managed Azure service. You only need to deploy your code to Azure. Azure takes care of all the infrastructure management like high availability, load balancing, and autoscaling."
16+
},
17+
new CloudService
18+
{
19+
Key=1,
20+
Name="Azure Service Bus",
21+
Description="A fully managed enterprise message broker supporting both point to point and publish-subscribe integrations. It's ideal for building decoupled applications, queue-based load leveling, or facilitating communication between microservices."
22+
},
23+
new CloudService
24+
{
25+
Key=2,
26+
Name="Azure Blob Storage",
27+
Description="Azure Blob Storage allows your applications to store and retrieve files in the cloud. Azure Storage is highly scalable to store massive amounts of data and data is stored redundantly to ensure high availability."
28+
},
29+
new CloudService
30+
{
31+
Key=3,
32+
Name="Microsoft Entra ID",
33+
Description="Manage user identities and control access to your apps, data, and resources.."
34+
},
35+
new CloudService
36+
{
37+
Key=4,
38+
Name="Azure Key Vault",
39+
Description="Store and access application secrets like connection strings and API keys in an encrypted vault with restricted access to make sure your secrets and your application aren't compromised."
40+
},
41+
new CloudService
42+
{
43+
Key=5,
44+
Name="Azure AI Search",
45+
Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization."
46+
}
47+
};
48+
49+
// Load the configuration values
50+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
51+
string model = config["ModelName"];
52+
string key = config["OpenAIKey"];
53+
54+
// Create the embedding generator
55+
IEmbeddingGenerator<string, Embedding<float>> generator =
56+
new OpenAIClient(new ApiKeyCredential(key))
57+
.AsEmbeddingGenerator(modelId: model);
58+
59+
// Create and populate the vector store
60+
var vectorStore = new InMemoryVectorStore();
61+
var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices");
62+
await cloudServicesStore.CreateCollectionIfNotExistsAsync();
63+
64+
foreach (var service in cloudServices)
65+
{
66+
service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description);
67+
await cloudServicesStore.UpsertAsync(service);
68+
}
69+
70+
// Convert a search query to a vector and search the vector store
71+
var query = "Which Azure service should I use to store my Word documents?";
72+
var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query);
73+
74+
var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions()
75+
{
76+
Top = 1,
77+
VectorPropertyName = "Vector"
78+
});
79+
80+
await foreach (var result in results.Results)
81+
{
82+
Console.WriteLine($"Name: {result.Record.Name}");
83+
Console.WriteLine($"Description: {result.Record.Description}");
84+
Console.WriteLine($"Vector match score: {result.Score}");
85+
Console.WriteLine();
86+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.1-preview.1.24570.5" />
12+
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.0.0-preview.1.24523.1" />
13+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.31.0-preview" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
16+
</ItemGroup>
17+
18+
</Project>

docs/ai/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ items:
1919
href: quickstarts/quickstart-openai-summarize-text.md
2020
- name: Build a chat app
2121
href: quickstarts/get-started-openai.md
22-
- name: Create an app to chat about your data
22+
- name: Build a .NET AI vector search app
2323
href: quickstarts/quickstart-ai-chat-with-data.md
2424
- name: Execute a local .NET function
2525
href: quickstarts/quickstart-azure-openai-tool.md

0 commit comments

Comments
 (0)