-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New vector search quickstart #43894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexwolfmsft
merged 14 commits into
dotnet:main
from
alexwolfmsft:vector-data-quickstart
Dec 18, 2024
Merged
New vector search quickstart #43894
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
534a60a
New vector search quickstart
alexwolfmsft ac673fa
fixes
alexwolfmsft 9c4ab09
edits
alexwolfmsft 4205acd
tweaks
alexwolfmsft d017e1e
fix link
alexwolfmsft 0af5d8a
Apply suggestions from code review
alexwolfmsft b934e54
fix title
alexwolfmsft 4685a9b
update variable name
alexwolfmsft 8159778
fixes
alexwolfmsft 302df9e
title edit
alexwolfmsft 922931a
metadata edits
alexwolfmsft 02cf49b
code fixes
alexwolfmsft c926f0d
update article
alexwolfmsft 26d29fa
fix header
alexwolfmsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 cloudService = 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 movies = vectorStore.GetCollection<int, CloudService>("movies"); | ||
| await movies.CreateCollectionIfNotExistsAsync(); | ||
|
|
||
| foreach (var movie in cloudService) | ||
| { | ||
| movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description); | ||
| await movies.UpsertAsync(movie); | ||
| } | ||
|
|
||
| // 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 movies.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 hidden or 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 hidden or 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 hidden or 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 cloudService = 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 movies = vectorStore.GetCollection<int, CloudService>("movies"); | ||
| await movies.CreateCollectionIfNotExistsAsync(); | ||
|
|
||
| foreach (var movie in cloudService) | ||
| { | ||
| movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description); | ||
| await movies.UpsertAsync(movie); | ||
| } | ||
|
|
||
| // 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 movies.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 hidden or 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.