diff --git a/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs b/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs index 95a97f7..b102220 100644 --- a/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs +++ b/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs @@ -306,6 +306,8 @@ SELECT TOP (@limit) using (SqlCommand command = connection.CreateCommand()) { + var generatedFilters = GenerateFilters(index, command.Parameters, filters); + command.CommandText = $@" WITH [embedding] as @@ -331,6 +333,10 @@ SELECT TOP (@limit) [embedding] INNER JOIN {this.GetFullTableName($"{this._config.EmbeddingsTableName}_{index}")} ON [embedding].vector_value_id = {this.GetFullTableName($"{this._config.EmbeddingsTableName}_{index}")}.vector_value_id + INNER JOIN + {this.GetFullTableName(this._config.MemoryTableName)} ON {this.GetFullTableName($"{this._config.EmbeddingsTableName}_{index}")}.[memory_id] = {this.GetFullTableName(this._config.MemoryTableName)}.[id] + WHERE 1=1 + {generatedFilters} GROUP BY {this.GetFullTableName($"{this._config.EmbeddingsTableName}_{index}")}.[memory_id] ORDER BY @@ -348,7 +354,7 @@ INNER JOIN {this.GetFullTableName(this._config.MemoryTableName)} ON [similarity].[memory_id] = {this.GetFullTableName(this._config.MemoryTableName)}.[id] WHERE 1=1 AND [cosine_similarity] >= @min_relevance_score - {GenerateFilters(index, command.Parameters, filters)} + {generatedFilters} ORDER BY [cosine_similarity] desc"; command.Parameters.AddWithValue("@vector", JsonSerializer.Serialize(embedding.Data.ToArray()));