Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<PackageVersion Include="DuckDB.NET.Data.Full" Version="1.2.0" />
<PackageVersion Include="DuckDB.NET.Data" Version="1.1.3" />
<PackageVersion Include="MongoDB.Driver" Version="2.30.0" />
<PackageVersion Include="LiteDB" Version="6.0.0-prerelease.63" />
<PackageVersion Include="Microsoft.Graph" Version="5.90.0" />
<PackageVersion Include="Microsoft.OpenApi" Version="1.6.24" />
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.24" />
Expand Down
2 changes: 2 additions & 0 deletions dotnet/SK-dotnet.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<Project Path="src/VectorData/InMemory/InMemory.csproj" />
<Project Path="src/VectorData/Milvus/Milvus.csproj" />
<Project Path="src/VectorData/MongoDB/MongoDB.csproj" />
<Project Path="src/VectorData/LiteDb/LiteDb.csproj" />
<Project Path="src/VectorData/PgVector/PgVector.csproj" />
<Project Path="src/VectorData/Pinecone/Pinecone.csproj" />
<Project Path="src/VectorData/Qdrant/Qdrant.csproj" />
Expand Down Expand Up @@ -324,6 +325,7 @@
<Project Path="test/VectorData/InMemory.UnitTests/InMemory.UnitTests.csproj" />
<Project Path="test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj" />
<Project Path="test/VectorData/MongoDB.UnitTests/MongoDB.UnitTests.csproj" />
<Project Path="test/VectorData/LiteDb.UnitTests/LiteDb.UnitTests.csproj" />
<Project Path="test/VectorData/PgVector.ConformanceTests/PgVector.ConformanceTests.csproj" />
<Project Path="test/VectorData/PgVector.UnitTests/PgVector.UnitTests.csproj" />
<Project Path="test/VectorData/Pinecone.ConformanceTests/Pinecone.ConformanceTests.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<ProjectReference Include="..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\src\VectorData\AzureAISearch\AzureAISearch.csproj" />
<ProjectReference Include="..\..\src\VectorData\InMemory\InMemory.csproj" />
<ProjectReference Include="..\..\src\VectorData\LiteDb\LiteDb.csproj" />
<ProjectReference Include="..\..\src\VectorData\Qdrant\Qdrant.csproj" />
<ProjectReference Include="..\..\src\VectorData\Redis\Redis.csproj" />
<ProjectReference Include="..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
Expand Down
4 changes: 4 additions & 0 deletions dotnet/samples/GettingStartedWithVectorStores/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project contains a step by step guide to get started using Vector Stores wi

The examples can be run as integration tests but their code can also be copied to stand-alone programs.

## Step 5 — LiteDB advanced scenario

`Step5_LiteDb_AdvancedScenario` shows how to combine transactional batch upserts, per-property embedding generators and metrics, and the extended filter surface (`Contains`, `StartsWith`, `EndsWith`, `IN`) when working with the LiteDB connector.

## Configuring Secrets

Most of the examples will require secrets and credentials, to access OpenAI, Azure OpenAI,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.LiteDb;

namespace GettingStartedWithVectorStores;

/// <summary>
/// Demonstrates advanced LiteDB usage: per-property generators and metrics, transactional batch upserts, and expressive filters.
/// </summary>
public sealed class Step5_LiteDb_AdvancedScenario(ITestOutputHelper output) : BaseTest(output)
{
private static readonly string[] FilterCities = new[] { "Seattle", "Portland" };

[Fact]
public async Task RunEndToEndScenarioAsync()
{
var services = new ServiceCollection();
services.AddSingleton<IEmbeddingGenerator<string, Embedding<float>>>(new SampleEmbeddingGenerator("store"));
services.AddLiteDbVectorStore(_ => new LiteDbVectorStoreOptions
{
DisposeDatabase = false,
DistanceMetric = LiteDbDistanceMetric.Cosine,
AutoCreateVectorIndexes = true,
CollectionNamePrefix = "sample_"
});
services.AddLiteDbCollection<string, SampleHotel>("hotels", _ => new VectorStoreCollectionDefinition
{
Properties =
{
new VectorStoreKeyProperty(nameof(SampleHotel.HotelId), typeof(string)),
new VectorStoreDataProperty(nameof(SampleHotel.City), typeof(string)),
new VectorStoreDataProperty(nameof(SampleHotel.Tags), typeof(List<string>)),
new VectorStoreDataProperty(nameof(SampleHotel.Description), typeof(string)),
new VectorStoreVectorProperty(nameof(SampleHotel.Overview), typeof(string), 3)
{
DistanceFunction = DistanceFunction.DotProductSimilarity,
EmbeddingGenerator = new SampleEmbeddingGenerator("overview")
},
new VectorStoreVectorProperty(nameof(SampleHotel.Amenities), typeof(string), 3)
{
DistanceFunction = DistanceFunction.EuclideanDistance,
EmbeddingGenerator = new SampleEmbeddingGenerator("amenities")
}
}
});

using var provider = services.BuildServiceProvider();
var collection = provider.GetRequiredService<VectorStoreCollection<string, SampleHotel>>();
await collection.EnsureCollectionExistsAsync();

var hotels = new[]
{
new SampleHotel
{
HotelId = "alpha",
City = "Seattle",
Description = "Waterfront spa resort",
Tags = new List<string> { "spa", "rooftop" },
Overview = "0.9,0.05,0.05",
Amenities = "0.2,0.5,0.3"
},
new SampleHotel
{
HotelId = "beta",
City = "Portland",
Description = "Modern downtown escape",
Tags = new List<string> { "boutique", "spa" },
Overview = "0.7,0.1,0.2",
Amenities = "0.6,0.2,0.2"
},
new SampleHotel
{
HotelId = "gamma",
City = "San Francisco",
Description = "Historic harbor view",
Tags = new List<string> { "historic", "view" },
Overview = "0.1,0.8,0.1",
Amenities = "0.3,0.1,0.6"
}
};

// Batch upsert executes inside a transaction – either all records are stored or none are.
await collection.UpsertAsync(hotels);

#pragma warning disable CA1866 // the literal "t" is clearer for sample filtering.
var searchOptions = new VectorSearchOptions<SampleHotel>
{
VectorProperty = h => h.Overview,
Filter = h => (h.Tags.Contains("spa") || h.City!.StartsWith("Sea"))
&& FilterCities.Contains(h.City!)
&& h.Description!.EndsWith("t"),
IncludeVectors = false
};
#pragma warning restore CA1866

var results = await collection.SearchAsync("0.8,0.1,0.1", top: 2, searchOptions).ToListAsync();

foreach (var result in results)
{
this.WriteLine($"Hotel: {result.Record.HotelId} in {result.Record.City} (score {result.Score:F3})");
}

Assert.Single(results);
Assert.Equal("alpha", results[0].Record.HotelId);
}

private sealed class SampleHotel
{
[VectorStoreKey]
public string? HotelId { get; set; }

[VectorStoreData]
public string? City { get; set; }

[VectorStoreData]
public string? Description { get; set; }

[VectorStoreData]
public List<string> Tags { get; set; } = new();

[VectorStoreVector(Dimensions: 3)]
public string? Overview { get; set; }

[VectorStoreVector(Dimensions: 3)]
public string? Amenities { get; set; }
}

private sealed class SampleEmbeddingGenerator(string tag) : IEmbeddingGenerator<string, Embedding<float>>
{
public Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(IEnumerable<string> values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
{
var embeddings = new GeneratedEmbeddings<Embedding<float>>();
foreach (var value in values)
{
embeddings.Add(new Embedding<float>(ParseVector(value, tag)));
}

return Task.FromResult(embeddings);
}

public Task<Embedding<float>> GenerateAsync(string value, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
=> Task.FromResult(new Embedding<float>(ParseVector(value, tag)));

public object? GetService(Type serviceType, object? serviceKey = null)
=> null;

public void Dispose()
{
}

private static float[] ParseVector(string value, string tagPrefix)
{
var values = value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(float.Parse)
.ToArray();

// Different prefixes allow callers to verify generator precedence in diagnostics.
return tagPrefix switch
{
"overview" => values,
_ => values.Select(v => Math.Clamp(v + 0.05f, 0f, 1f)).ToArray()
};
}
}
}
37 changes: 37 additions & 0 deletions dotnet/src/VectorData/LiteDb/LiteDb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>Microsoft.SemanticKernel.Connectors.LiteDb</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<VersionSuffix>preview</VersionSuffix>
</PropertyGroup>

<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />

<PropertyGroup>
<Title>LiteDB provider for Microsoft.Extensions.VectorData</Title>
<Description>LiteDB provider for Microsoft.Extensions.VectorData by Semantic Kernel</Description>
<PackageReadmeFile>VECTORDATA-CONNECTORS-NUGET.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="$(RepoRoot)/dotnet/nuget/VECTORDATA-CONNECTORS-NUGET.md" Link="VECTORDATA-CONNECTORS-NUGET.md" Pack="true" PackagePath="." />
</ItemGroup>

<ItemGroup>
<PackageReference Include="LiteDB" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<PackageReference Include="System.Numerics.Tensors" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VectorData.Abstractions\VectorData.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="SemanticKernel.Connectors.LiteDb.UnitTests" />
</ItemGroup>

</Project>
Loading