Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<PackageVersion Include="Docker.DotNet" Version="3.125.15" />
<!-- Plugins -->
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.3.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.4" />
<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" />
Expand All @@ -154,6 +154,7 @@
<!-- Memory stores -->
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.48.0-preview.0" />
<PackageVersion Include="Pgvector" Version="0.3.1" />
<PackageVersion Include="sqlite-vec" Version="0.1.7-alpha.2" />
<PackageVersion Include="NRedisStack" Version="0.12.0" />
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1" />
<PackageVersion Include="Testcontainers" Version="4.4.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFrameworks>net8.0;netstandard2.0;net462</TargetFrameworks>
<VersionSuffix>preview</VersionSuffix>
<NoWarn>$(NoWarn);CS1591</NoWarn> <!-- A temporary workaround for https://github.com/jeffhandley/sqlite-dist/pull/1 -->
</PropertyGroup>

<!-- IMPORT NUGET PACKAGE SHARED PROPERTIES -->
Expand All @@ -27,6 +28,7 @@
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<PackageReference Include="Microsoft.Data.Sqlite" />
<PackageReference Include="System.Numerics.Tensors" />
<PackageReference Include="sqlite-vec" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public sealed class SqliteCollection<TKey, TRecord> : VectorStoreCollection<TKey
/// <summary>Table name in SQLite for vector properties.</summary>
private readonly string _vectorTableName;

/// <summary>The sqlite_vec extension name to use.</summary>
private readonly string _vectorSearchExtensionName;

/// <inheritdoc />
public override string Name { get; }

Expand Down Expand Up @@ -84,7 +81,6 @@ public SqliteCollection(
this.Name = name;

options ??= SqliteCollectionOptions.Default;
this._vectorSearchExtensionName = options.VectorSearchExtensionName ?? SqliteConstants.VectorSearchExtensionName;

// Escape both table names before exposing them to anything that may build SQL commands.
this._dataTableName = name.EscapeIdentifier();
Expand Down Expand Up @@ -543,7 +539,7 @@ private async ValueTask<SqliteConnection> GetConnectionAsync(CancellationToken c
{
var connection = new SqliteConnection(this._connectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
connection.LoadExtension(this._vectorSearchExtensionName);
connection.LoadVector();
return connection;
}

Expand Down Expand Up @@ -599,13 +595,9 @@ await this.CreateTableAsync(connection, this._dataTableName, dataTableColumns, i

if (this._vectorPropertiesExist)
{
var extensionName = !string.IsNullOrWhiteSpace(this._vectorSearchExtensionName) ?
this._vectorSearchExtensionName :
SqliteConstants.VectorSearchExtensionName;

List<SqliteColumn> vectorTableColumns = SqlitePropertyMapping.GetColumns(this._model.Properties, data: false);

await this.CreateVirtualTableAsync(connection, this._vectorTableName, vectorTableColumns, ifNotExists, extensionName!, cancellationToken)
await this.CreateVirtualTableAsync(connection, this._vectorTableName, vectorTableColumns, ifNotExists, cancellationToken)
.ConfigureAwait(false);
}
}
Expand All @@ -623,11 +615,11 @@ private Task<int> CreateTableAsync(SqliteConnection connection, string tableName
cancellationToken);
}

private Task<int> CreateVirtualTableAsync(SqliteConnection connection, string tableName, List<SqliteColumn> columns, bool ifNotExists, string extensionName, CancellationToken cancellationToken)
private Task<int> CreateVirtualTableAsync(SqliteConnection connection, string tableName, List<SqliteColumn> columns, bool ifNotExists, CancellationToken cancellationToken)
{
const string OperationName = "CreateVirtualTable";

using var command = SqliteCommandBuilder.BuildCreateVirtualTableCommand(connection, tableName, columns, ifNotExists, extensionName);
using var command = SqliteCommandBuilder.BuildCreateVirtualTableCommand(connection, tableName, columns, ifNotExists);

return connection.ExecuteWithErrorHandlingAsync(
this._collectionMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ public static DbCommand BuildCreateVirtualTableCommand(
SqliteConnection connection,
string tableName,
IReadOnlyList<SqliteColumn> columns,
bool ifNotExists,
string extensionName)
bool ifNotExists)
{
var builder = new StringBuilder();

builder.AppendLine($"CREATE VIRTUAL TABLE {(ifNotExists ? "IF NOT EXISTS " : string.Empty)}\"{tableName}\" USING {extensionName}(");
builder.AppendLine($"CREATE VIRTUAL TABLE {(ifNotExists ? "IF NOT EXISTS " : string.Empty)}\"{tableName}\" USING vec0(");

// The vector extension is currently uncapable of handling quoted identifiers.
builder.AppendLine(string.Join(",\n", columns.Select(column => GetColumnDefinition(column, quote: false))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ internal static class SqliteConstants
{
internal const string VectorStoreSystemName = "sqlite";

/// <summary>
/// SQLite extension name for vector search.
/// More information here: <see href="https://github.com/asg017/sqlite-vec"/>.
/// </summary>
public const string VectorSearchExtensionName = "vec0";

/// <summary>A <see cref="HashSet{T}"/> of types that vector properties on the provided model may have.</summary>
public static readonly HashSet<Type> SupportedVectorTypes =
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public sealed class SqliteVectorStore : VectorStore
/// <summary>A general purpose definition that can be used to construct a collection when needing to proxy schema agnostic operations.</summary>
private static readonly VectorStoreRecordDefinition s_generalPurposeDefinition = new() { Properties = [new VectorStoreKeyProperty("Key", typeof(string))] };

/// <summary>SQLite extension name for vector search operations.</summary>
private readonly string? _vectorSearchExtensionName;

/// <summary>Custom virtual table name to store vectors.</summary>
private readonly string? _vectorVirtualTableName;

Expand All @@ -48,7 +45,6 @@ public SqliteVectorStore(string connectionString, SqliteVectorStoreOptions? opti
this._connectionString = connectionString;

options ??= SqliteVectorStoreOptions.Default;
this._vectorSearchExtensionName = options.VectorSearchExtensionName;
this._vectorVirtualTableName = options.VectorVirtualTableName;
this._embeddingGenerator = options.EmbeddingGenerator;

Expand All @@ -74,7 +70,6 @@ public override VectorStoreCollection<TKey, TRecord> GetCollection<TKey, TRecord
new()
{
VectorStoreRecordDefinition = vectorStoreRecordDefinition,
VectorSearchExtensionName = this._vectorSearchExtensionName,
VectorVirtualTableName = this._vectorVirtualTableName,
EmbeddingGenerator = this._embeddingGenerator
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ public sealed class SqliteVectorStoreOptions
{
internal static readonly SqliteVectorStoreOptions Default = new();

/// <summary>
/// SQLite extension name for vector search operations.
/// If not specified, the default "vec0" extension name will be used.
/// More information here: <see href="https://github.com/asg017/sqlite-vec"/>.
/// </summary>
public string? VectorSearchExtensionName { get; set; }

/// <summary>
/// Custom virtual table name to store vectors.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void ItBuildsCreateVirtualTableCommand(bool ifNotExists)
{
// Arrange
const string TableName = "TestTable";
const string ExtensionName = "TestExtension";

var columns = new List<SqliteColumn>
{
Expand All @@ -82,12 +81,12 @@ public void ItBuildsCreateVirtualTableCommand(bool ifNotExists)
};

// Act
var command = SqliteCommandBuilder.BuildCreateVirtualTableCommand(this._connection, TableName, columns, ifNotExists, ExtensionName);
var command = SqliteCommandBuilder.BuildCreateVirtualTableCommand(this._connection, TableName, columns, ifNotExists);

// Assert
Assert.Contains("CREATE VIRTUAL TABLE", command.CommandText);
Assert.Contains(TableName, command.CommandText);
Assert.Contains($"USING {ExtensionName}", command.CommandText);
Assert.Contains("USING vec0", command.CommandText);

Assert.Equal(ifNotExists, command.CommandText.Contains("IF NOT EXISTS"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ namespace SqliteVecIntegrationTests.Support;

internal static class SqliteTestEnvironment
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not removed this class because I was not able to test it on Full Framework because I can't even load the sqlite itself on Full Framework (not the extension):

System.TypeInitializationException : The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
---- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
-------- System.Exception : Library e_sqlite3 not found
plat: win
suffix: DLL
possibilities (3):
    1) C:\Users\adsitnik\AppData\Local\Temp\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\assembly\dl3\909e6049\001ea6ec_8604db01\runtimes\win-x64\native\e_sqlite3.dll
    2) C:\Users\adsitnik\AppData\Local\Temp\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\assembly\dl3\909e6049\001ea6ec_8604db01\e_sqlite3.dll
    3) D:/projects/forks/semantic-kernel/dotnet/src/VectorDataIntegrationTests/SqliteVecIntegrationTests/bin/Debug/net472/e_sqlite3.dll
win TryLoad: C:\Users\adsitnik\AppData\Local\Temp\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\assembly\dl3\909e6049\001ea6ec_8604db01\runtimes\win-x64\native\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
   at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
win TryLoad: C:\Users\adsitnik\AppData\Local\Temp\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\2f6a5f17-a57d-462f-b7e5-ce89e5b9d363\assembly\dl3\909e6049\001ea6ec_8604db01\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
   at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
win TryLoad: D:/projects/forks/semantic-kernel/dotnet/src/VectorDataIntegrationTests/SqliteVecIntegrationTests/bin/Debug/net472/e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
   at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
NOT FOUND


  Stack Trace: 
SqliteConnection.ctor(String connectionString)
<GetNonExistingCollectionAsync>d__9`1.MoveNext() line 152
<GetConnectionAsync>d__27.MoveNext() line 540
--- End of stack trace from previous location where exception was thrown ---
ExceptionDispatchInfo.Throw()
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ValueTask`1.get_Result()
<DeleteCollectionAsync>d__15.MoveNext() line 138
--- End of stack trace from previous location where exception was thrown ---
ExceptionDispatchInfo.Throw()
<17 more frames...>
----- Inner Stack Trace -----
RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
MethodBase.Invoke(Object obj, Object[] parameters)
SqliteConnection.cctor()
----- Inner Stack Trace -----
NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags)
Batteries_V2.MakeDynamic(String name, Int32 flags)
Batteries_V2.DoDynamic_cdecl(String name, Int32 flags)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh... OK...

I'm hoping this is somehow related to the test environment/xunit... Can I ask you to do a quick test from a full framework console program, with Microsoft.Data.Sqlite and the new nuget package, to make sure it actually works for users? Otherwise we might have an actual issue here (we specifically worked on making the nuget on full framework, but I no longer remember the exact details...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reported the bug: dotnet/efcore#36069

{
/// <summary>
/// SQLite extension name for vector search.
/// More information here: <see href="https://github.com/asg017/sqlite-vec"/>.
/// </summary>
private const string VectorSearchExtensionName = "vec0";

private static bool? s_isSqliteVecInstalled;

internal static bool TryLoadSqliteVec(SqliteConnection connection)
Expand All @@ -26,7 +20,7 @@ internal static bool TryLoadSqliteVec(SqliteConnection connection)

try
{
connection.LoadExtension(VectorSearchExtensionName);
connection.LoadVector();
s_isSqliteVecInstalled = true;
}
catch (SqliteException)
Expand Down
Loading