Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Warning
The In-Memory Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Warning
The Semantic Kernel Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Warning
The Semantic Kernel Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Overview
The In-Memory Vector Store connector is a Vector Store implementation provided by Semantic Kernel that uses no external database and stores data in memory. This Vector Store is useful for prototyping scenarios or where high-speed in-memory operations are required.
The connector has the following characteristics.
| Feature Area | Support |
|---|---|
| Collection maps to | In-memory dictionary |
| Supported key property types | Any type that can be compared |
| Supported data property types | Any type |
| Supported vector property types |
|
| Supported index types | Flat |
| Supported distance functions |
|
| Supported filter clauses |
|
| Supports multiple vectors in a record | Yes |
| IsIndexed supported? | Yes |
| IsFullTextIndexed supported? | Yes |
| StorageName supported? | No, since storage is in-memory and data reuse is therefore not possible, custom naming is not applicable. |
| HybridSearch supported? | No |
Getting started
Add the Semantic Kernel Core nuget package to your project.
dotnet add package Microsoft.SemanticKernel.Connectors.InMemory --prerelease
You can add the vector store to the dependency injection container available on the KernelBuilder or to the IServiceCollection dependency injection container using extension methods provided by Semantic Kernel.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
// Using Kernel Builder.
var kernelBuilder = Kernel
.CreateBuilder();
kernelBuilder.Services
.AddInMemoryVectorStore();
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddInMemoryVectorStore();
You can construct an InMemory Vector Store instance directly.
using Microsoft.SemanticKernel.Connectors.InMemory;
var vectorStore = new InMemoryVectorStore();
It is possible to construct a direct reference to a named collection.
using Microsoft.SemanticKernel.Connectors.InMemory;
var collection = new InMemoryCollection<string, Hotel>("skhotels");
Overview
Warning
The In-Memory Vector Store has support for custom filters that can be expressed as Python lambda functions, these functions are executed in the same process as the main application, and therefore can execute arbitrary code. We filter for certain allowed operations, but you should not let filters be set by untrusted sources, including by LLM inputs. See the Filtering section for more details.
The In-Memory Vector Store connector is a Vector Store implementation provided by Semantic Kernel that uses no external database and stores data in memory. This Vector Store is useful for prototyping scenarios or where high-speed in-memory operations are required.
The connector has the following characteristics.
| Feature Area | Support |
|---|---|
| Collection maps to | In-memory dictionary |
| Supported key property types | Any that is allowed to be a dict key, see the python documentation for details here |
| Supported data property types | Any type |
| Supported vector property types | list[float | int] | numpy array |
| Supported index types | Flat |
| Supported distance functions |
|
| Supports multiple vectors in a record | Yes |
| is_filterable supported? | Yes |
| is_full_text_searchable supported? | Yes |
Getting started
Add the Semantic Kernel package to your project.
pip install semantic-kernel
You can create the store and collections from there, or create the collections directly.
In the snippets below, it is assumed that you have a data model class defined named 'DataModel'.
from semantic_kernel.connectors.in_memory import InMemoryVectorStore
vector_store = InMemoryVectorStore()
vector_collection = vector_store.get_collection(record_type=DataModel, collection_name="collection_name")
It is possible to construct a direct reference to a named collection.
from semantic_kernel.connectors.in_memory import InMemoryCollection
vector_collection = InMemoryCollection(record_type=DataModel, collection_name="collection_name")
Filtering
Warning
The In-Memory Vector Store has support for custom filters that can be expressed as Python lambda functions, these functions are executed in the same process as the main application, and therefore can execute arbitrary code. We filter for certain allowed operations, but you should not let filters be set by untrusted sources, including by LLM inputs.
The In-Memory connector uses an allowlist approach for filter security. Only the following operations are permitted in filter expressions:
Allowed operations
| Category | Allowed Operations |
|---|---|
| Comparisons | ==, !=, <, <=, >, >=, in, not in, is, is not |
| Boolean operations | and, or, not |
| Data access | Attribute access (e.g., x.field), subscript access (e.g., x['field']), slicing |
| Literals | Constants, lists, tuples, sets, dictionaries |
| Basic arithmetic | +, -, *, /, %, // |
Allowed functions
The following built-in functions and methods can be used in filter expressions:
- Type conversion:
str,int,float,bool - Aggregation:
len,abs,min,max,sum,any,all - String methods:
lower,upper,strip,startswith,endswith,contains - Dictionary methods:
get,keys,values,items
Examples
# Simple equality filter
results = await collection.search(vector, filter="lambda x: x.category == 'electronics'")
# Numeric comparison
results = await collection.search(vector, filter="lambda x: x.price < 100")
# Boolean combination
results = await collection.search(vector, filter="lambda x: x.in_stock and x.rating >= 4.0")
# String method
results = await collection.search(vector, filter="lambda x: x.name.startswith('A')")
# Membership test
results = await collection.search(vector, filter="lambda x: x.status in ['active', 'pending']")
Coming soon
More info coming soon.