-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New vector search quickstart (#43894)
* New vector search quickstart --------- Co-authored-by: Genevieve Warren <[email protected]>
- Loading branch information
1 parent
02b71ef
commit ddd0d3c
Showing
10 changed files
with
375 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
docs/ai/quickstarts/snippets/chat-with-data/azure-openai/CloudService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.VectorData; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VectorDataAI | ||
{ | ||
internal class CloudService | ||
{ | ||
[VectorStoreRecordKey] | ||
public int Key { get; set; } | ||
|
||
[VectorStoreRecordData] | ||
public string Name { get; set; } | ||
|
||
[VectorStoreRecordData] | ||
public string Description { get; set; } | ||
|
||
[VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)] | ||
public ReadOnlyMemory<float> Vector { get; set; } | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
docs/ai/quickstarts/snippets/chat-with-data/azure-openai/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Azure.AI.OpenAI; | ||
using Azure.Identity; | ||
using Microsoft.Extensions.AI; | ||
using Microsoft.Extensions.VectorData; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.SemanticKernel.Connectors.InMemory; | ||
using VectorDataAI; | ||
|
||
var cloudServices = new List<CloudService>() | ||
{ | ||
new CloudService | ||
{ | ||
Key=0, | ||
Name="Azure App Service", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=1, | ||
Name="Azure Service Bus", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=2, | ||
Name="Azure Blob Storage", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=3, | ||
Name="Microsoft Entra ID", | ||
Description="Manage user identities and control access to your apps, data, and resources.." | ||
}, | ||
new CloudService | ||
{ | ||
Key=4, | ||
Name="Azure Key Vault", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=5, | ||
Name="Azure AI Search", | ||
Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization." | ||
} | ||
}; | ||
|
||
// Load the configuration values | ||
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); | ||
string endpoint = config["AZURE_OPENAI_ENDPOINT"]; | ||
string model = config["AZURE_OPENAI_GPT_NAME"]; | ||
|
||
// Create the embedding generator | ||
IEmbeddingGenerator<string, Embedding<float>> generator = | ||
new AzureOpenAIClient( | ||
new Uri(endpoint), | ||
new DefaultAzureCredential()) | ||
.AsEmbeddingGenerator(modelId: model); | ||
|
||
// Create and populate the vector store | ||
var vectorStore = new InMemoryVectorStore(); | ||
var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices"); | ||
await cloudServicesStore.CreateCollectionIfNotExistsAsync(); | ||
|
||
foreach (var service in cloudServices) | ||
{ | ||
service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description); | ||
await cloudServicesStore.UpsertAsync(service); | ||
} | ||
|
||
// Convert a search query to a vector and search the vector store | ||
var query = "Which Azure service should I use to store my Word documents?"; | ||
var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query); | ||
|
||
var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions() | ||
{ | ||
Top = 1, | ||
VectorPropertyName = "Vector" | ||
}); | ||
|
||
await foreach (var result in results.Results) | ||
{ | ||
Console.WriteLine($"Name: {result.Record.Name}"); | ||
Console.WriteLine($"Description: {result.Record.Description}"); | ||
Console.WriteLine($"Vector match score: {result.Score}"); | ||
Console.WriteLine(); | ||
} |
20 changes: 20 additions & 0 deletions
20
docs/ai/quickstarts/snippets/chat-with-data/azure-openai/VectorDataAI.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" Version="1.13.1" /> | ||
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.1-preview.1.24570.5" /> | ||
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.0.0-preview.1.24523.1" /> | ||
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.31.0-preview" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
24 changes: 24 additions & 0 deletions
24
docs/ai/quickstarts/snippets/chat-with-data/openai/CloudService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.VectorData; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VectorDataAI | ||
{ | ||
internal class CloudService | ||
{ | ||
[VectorStoreRecordKey] | ||
public int Key { get; set; } | ||
|
||
[VectorStoreRecordData] | ||
public string Name { get; set; } | ||
|
||
[VectorStoreRecordData] | ||
public string Description { get; set; } | ||
|
||
[VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)] | ||
public ReadOnlyMemory<float> Vector { get; set; } | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
docs/ai/quickstarts/snippets/chat-with-data/openai/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.Extensions.AI; | ||
using OpenAI; | ||
using Microsoft.Extensions.VectorData; | ||
using Microsoft.SemanticKernel.Connectors.InMemory; | ||
using VectorDataAI; | ||
using System.ClientModel; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
var cloudServices = new List<CloudService>() | ||
{ | ||
new CloudService | ||
{ | ||
Key=0, | ||
Name="Azure App Service", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=1, | ||
Name="Azure Service Bus", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=2, | ||
Name="Azure Blob Storage", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=3, | ||
Name="Microsoft Entra ID", | ||
Description="Manage user identities and control access to your apps, data, and resources.." | ||
}, | ||
new CloudService | ||
{ | ||
Key=4, | ||
Name="Azure Key Vault", | ||
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." | ||
}, | ||
new CloudService | ||
{ | ||
Key=5, | ||
Name="Azure AI Search", | ||
Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization." | ||
} | ||
}; | ||
|
||
// Load the configuration values | ||
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); | ||
string model = config["ModelName"]; | ||
string key = config["OpenAIKey"]; | ||
|
||
// Create the embedding generator | ||
IEmbeddingGenerator<string, Embedding<float>> generator = | ||
new OpenAIClient(new ApiKeyCredential(key)) | ||
.AsEmbeddingGenerator(modelId: model); | ||
|
||
// Create and populate the vector store | ||
var vectorStore = new InMemoryVectorStore(); | ||
var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices"); | ||
await cloudServicesStore.CreateCollectionIfNotExistsAsync(); | ||
|
||
foreach (var service in cloudServices) | ||
{ | ||
service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description); | ||
await cloudServicesStore.UpsertAsync(service); | ||
} | ||
|
||
// Convert a search query to a vector and search the vector store | ||
var query = "Which Azure service should I use to store my Word documents?"; | ||
var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query); | ||
|
||
var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions() | ||
{ | ||
Top = 1, | ||
VectorPropertyName = "Vector" | ||
}); | ||
|
||
await foreach (var result in results.Results) | ||
{ | ||
Console.WriteLine($"Name: {result.Record.Name}"); | ||
Console.WriteLine($"Description: {result.Record.Description}"); | ||
Console.WriteLine($"Vector match score: {result.Score}"); | ||
Console.WriteLine(); | ||
} |
18 changes: 18 additions & 0 deletions
18
docs/ai/quickstarts/snippets/chat-with-data/openai/VectorDataAI.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.1-preview.1.24570.5" /> | ||
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.0.0-preview.1.24523.1" /> | ||
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.31.0-preview" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters