mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Resolves: https://github.com/microsoft/semantic-kernel/issues/7607 This PR contains a .NET notebook for vector stores demonstrating main functionality and components. It also contains several links with more information about vector stores on Microsoft Learn portal. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: westey <[email protected]>
18 KiB
18 KiB
In [ ]:
#r "nuget: Microsoft.SemanticKernel, 1.24.1"
#r "nuget: Microsoft.SemanticKernel.Connectors.InMemory, 1.24.1-preview"
#r "nuget: Microsoft.Extensions.VectorData.Abstractions, 9.0.0-preview.1.24518.1"
#r "nuget: System.Linq.Async, 6.0.1"
#!import config/Settings.cs
using Microsoft.SemanticKernel;
using Kernel = Microsoft.SemanticKernel.Kernel;
#pragma warning disable SKEXP0010
var builder = Kernel.CreateBuilder();
// Configure AI service credentials used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
{
builder.AddAzureOpenAITextEmbeddingGeneration("text-embedding-ada-002", azureEndpoint, apiKey);
}
else
{
builder.AddOpenAITextEmbeddingGeneration("text-embedding-ada-002", apiKey, orgId);
}
var kernel = builder.Build();In [7]:
using Microsoft.Extensions.VectorData;
public sealed class Glossary
{
[VectorStoreRecordKey]
public ulong Key { get; set; }
[VectorStoreRecordData]
public string Term { get; set; }
[VectorStoreRecordData]
public string Definition { get; set; }
[VectorStoreRecordVector(Dimensions: 1536)]
public ReadOnlyMemory<float> DefinitionEmbedding { get; set; }
}In [21]:
public sealed class GlossaryWithoutAttributes
{
public ulong Key { get; set; }
public string Term { get; set; }
public string Definition { get; set; }
public ReadOnlyMemory<float> DefinitionEmbedding { get; set; }
}
var recordDefinition = new VectorStoreRecordDefinition()
{
Properties = new List<VectorStoreRecordProperty>()
{
new VectorStoreRecordKeyProperty("Key", typeof(ulong)),
new VectorStoreRecordDataProperty("Term", typeof(string)),
new VectorStoreRecordDataProperty("Definition", typeof(string)),
new VectorStoreRecordVectorProperty("DefinitionEmbedding", typeof(ReadOnlyMemory<float>)) { Dimensions = 1536 }
}
};In [9]:
using Microsoft.SemanticKernel.Connectors.InMemory;
#pragma warning disable SKEXP0020
// Define vector store
var vectorStore = new InMemoryVectorStore();
// Get a collection instance using vector store
var collection = vectorStore.GetCollection<ulong, Glossary>("skglossary");
// Get a collection instance by initializing it directly
var collection2 = new InMemoryVectorStoreRecordCollection<ulong, Glossary>("skglossary");In [10]:
await collection.CreateCollectionIfNotExistsAsync();In [11]:
var glossaryEntries = new List<Glossary>()
{
new Glossary()
{
Key = 1,
Term = "API",
Definition = "Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data."
},
new Glossary()
{
Key = 2,
Term = "Connectors",
Definition = "Connectors allow you to integrate with various services provide AI capabilities, including LLM, AudioToText, TextToAudio, Embedding generation, etc."
},
new Glossary()
{
Key = 3,
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)."
}
};In [14]:
using Microsoft.SemanticKernel.Embeddings;
#pragma warning disable SKEXP0001
var textEmbeddingGenerationService = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
var tasks = glossaryEntries.Select(entry => Task.Run(async () =>
{
entry.DefinitionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(entry.Definition);
}));
await Task.WhenAll(tasks);In [ ]:
await foreach (var key in collection.UpsertBatchAsync(glossaryEntries))
{
Console.WriteLine(key);
}In [ ]:
var options = new GetRecordOptions() { IncludeVectors = true };
await foreach (var record in collection.GetBatchAsync(keys: [1, 2, 3], options))
{
Console.WriteLine($"Key: {record.Key}");
Console.WriteLine($"Term: {record.Term}");
Console.WriteLine($"Definition: {record.Definition}");
Console.WriteLine($"Definition Embedding: {JsonSerializer.Serialize(record.DefinitionEmbedding)}");
}In [18]:
#pragma warning disable SKEXP0001
var searchString = "I want to learn more about Connectors";
var searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString);In [ ]:
var searchResult = await collection.VectorizedSearchAsync(searchVector);
await foreach (var result in searchResult.Results)
{
Console.WriteLine($"Search score: {result.Score}");
Console.WriteLine($"Key: {result.Record.Key}");
Console.WriteLine($"Term: {result.Record.Term}");
Console.WriteLine($"Definition: {result.Record.Definition}");
Console.WriteLine("=========");
}