-
Notifications
You must be signed in to change notification settings - Fork 4.6k
.Net: Switch to using interfaces for search instead of query objects. #8690
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
westey-m
merged 6 commits into
microsoft:feature-vector-search
from
westey-m:vector-search-8-switch-to-search-interfaces
Sep 13, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3fa3cb
Switch to using interfaces for search instead of query objects.
westey-m 1d0dfcf
Remove example that needs reworking before release.
westey-m 9bce42b
Merge feature branch into pr branch.
westey-m f652952
Fix sample after azure open ai migration.
westey-m 0b08795
Update adr with design decision.
westey-m 4310e11
Addressing pr comments.
westey-m 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.SemanticKernel.Connectors.AzureOpenAI; | ||
| using Microsoft.SemanticKernel.Data; | ||
| using Microsoft.SemanticKernel.Embeddings; | ||
|
|
||
| namespace Memory; | ||
|
|
||
| /// <summary> | ||
| /// A simple example showing how to ingest data into a vector store and then use vector search to find related records to a given string. | ||
| /// | ||
| /// The example shows the following steps: | ||
| /// 1. Create an embedding generator. | ||
| /// 2. Create a Volatile Vector Store. | ||
| /// 3. Ingest some data into the vector store. | ||
| /// 4. Search the vector store with various text and filtering options. | ||
| /// </summary> | ||
| public class VectorSearch_Simple(ITestOutputHelper output) : BaseTest(output) | ||
| { | ||
| [Fact] | ||
| public async Task ExampleAsync() | ||
| { | ||
| // Create an embedding generation service. | ||
| var textEmbeddingGenerationService = new AzureOpenAITextEmbeddingGenerationService( | ||
| TestConfiguration.AzureOpenAIEmbeddings.DeploymentName, | ||
| TestConfiguration.AzureOpenAIEmbeddings.Endpoint, | ||
| TestConfiguration.AzureOpenAIEmbeddings.ApiKey); | ||
|
|
||
| // Initiate the docker container and construct the vector store. | ||
| var vectorStore = new VolatileVectorStore(); | ||
|
|
||
| // Get and create collection if it doesn't exist. | ||
| var collection = vectorStore.GetCollection<ulong, Glossary>("skglossary"); | ||
| await collection.CreateCollectionIfNotExistsAsync(); | ||
|
|
||
| // Create glossary entries and generate embeddings for them. | ||
| var glossaryEntries = CreateGlossaryEntries().ToList(); | ||
| var tasks = glossaryEntries.Select(entry => Task.Run(async () => | ||
| { | ||
| entry.DefinitionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(entry.Definition); | ||
| })); | ||
| await Task.WhenAll(tasks); | ||
|
|
||
| // Upsert the glossary entries into the collection and return their keys. | ||
| var upsertedKeysTasks = glossaryEntries.Select(x => collection.UpsertAsync(x)); | ||
| var upsertedKeys = await Task.WhenAll(upsertedKeysTasks); | ||
|
|
||
| var vectorSearch = collection as IVectorizedSearch<Glossary>; | ||
|
|
||
| // Search the collection using a vector search. | ||
| var searchString = "What is an Application Programming Interface"; | ||
| var searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString); | ||
| var searchResult = await vectorSearch!.VectorizedSearchAsync(searchVector, new() { Limit = 1 }).ToListAsync(); | ||
|
|
||
| Console.WriteLine("Search string: " + searchString); | ||
| Console.WriteLine("Result: " + searchResult.First().Record.Definition); | ||
| Console.WriteLine(); | ||
|
|
||
| // Search the collection using a vector search. | ||
| searchString = "What is Retrieval Augmented Generation"; | ||
| searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString); | ||
| searchResult = await vectorSearch!.VectorizedSearchAsync(searchVector, new() { Limit = 1 }).ToListAsync(); | ||
|
|
||
| Console.WriteLine("Search string: " + searchString); | ||
| Console.WriteLine("Result: " + searchResult.First().Record.Definition); | ||
| Console.WriteLine(); | ||
|
|
||
| // Search the collection using a vector search with pre-filtering. | ||
| searchString = "What is Retrieval Augmented Generation"; | ||
| searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString); | ||
| var filter = new VectorSearchFilter().EqualTo(nameof(Glossary.Category), "External Definitions"); | ||
| searchResult = await vectorSearch!.VectorizedSearchAsync(searchVector, new() { Limit = 3, Filter = filter }).ToListAsync(); | ||
|
|
||
| Console.WriteLine("Search string: " + searchString); | ||
| Console.WriteLine("Number of results: " + searchResult.Count); | ||
| Console.WriteLine("Result 1 Score: " + searchResult[0].Score); | ||
| Console.WriteLine("Result 1: " + searchResult[0].Record.Definition); | ||
| Console.WriteLine("Result 2 Score: " + searchResult[1].Score); | ||
| Console.WriteLine("Result 2: " + searchResult[1].Record.Definition); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sample model class that represents a glossary entry. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Note that each property is decorated with an attribute that specifies how the property should be treated by the vector store. | ||
| /// This allows us to create a collection in the vector store and upsert and retrieve instances of this class without any further configuration. | ||
| /// </remarks> | ||
| private sealed class Glossary | ||
| { | ||
| [VectorStoreRecordKey] | ||
| public ulong Key { get; set; } | ||
|
|
||
| [VectorStoreRecordData(IsFilterable = true)] | ||
| public string Category { get; set; } | ||
|
|
||
| [VectorStoreRecordData] | ||
| public string Term { get; set; } | ||
|
|
||
| [VectorStoreRecordData] | ||
| public string Definition { get; set; } | ||
|
|
||
| [VectorStoreRecordVector(1536)] | ||
| public ReadOnlyMemory<float> DefinitionEmbedding { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create some sample glossary entries. | ||
| /// </summary> | ||
| /// <returns>A list of sample glossary entries.</returns> | ||
| private static IEnumerable<Glossary> CreateGlossaryEntries() | ||
| { | ||
| yield return new Glossary | ||
| { | ||
| Key = 1, | ||
| Category = "External Definitions", | ||
| Term = "API", | ||
| Definition = "Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data." | ||
| }; | ||
|
|
||
| yield return new Glossary | ||
| { | ||
| Key = 2, | ||
| Category = "Core Definitions", | ||
| Term = "Connectors", | ||
| Definition = "Connectors allow you to integrate with various services provide AI capabilities, including LLM, AudioToText, TextToAudio, Embedding generation, etc." | ||
| }; | ||
|
|
||
| yield return new Glossary | ||
| { | ||
| Key = 3, | ||
| Category = "External Definitions", | ||
| Term = "RAG", | ||
| Definition = "Retrieval Augmented Generation - a term that refers to the process of retrieving additional data to provide as context to an LLM to use when generating a response (completion) to a user’s question (prompt)." | ||
| }; | ||
| } | ||
| } |
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
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
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
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.