Skip to content

Commit 534a60a

Browse files
committed
New vector search quickstart
1 parent 5a56445 commit 534a60a

File tree

7 files changed

+366
-137
lines changed

7 files changed

+366
-137
lines changed

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

Lines changed: 106 additions & 137 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 cloudService = 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 movies = vectorStore.GetCollection<int, CloudService>("movies");
64+
await movies.CreateCollectionIfNotExistsAsync();
65+
66+
foreach (var movie in cloudService)
67+
{
68+
movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description);
69+
await movies.UpsertAsync(movie);
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 movies.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 cloudService = 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 movies = vectorStore.GetCollection<int, CloudService>("movies");
62+
await movies.CreateCollectionIfNotExistsAsync();
63+
64+
foreach (var movie in cloudService)
65+
{
66+
movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description);
67+
await movies.UpsertAsync(movie);
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 movies.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>

0 commit comments

Comments
 (0)