diff --git a/src/Connectors.Memory.SqlServer/SqlServerClient.cs b/src/Connectors.Memory.SqlServer/SqlServerClient.cs index 187c0fe..e68eb66 100644 --- a/src/Connectors.Memory.SqlServer/SqlServerClient.cs +++ b/src/Connectors.Memory.SqlServer/SqlServerClient.cs @@ -349,7 +349,8 @@ FOR JSON AUTO [similarity] INNER JOIN {this.GetFullTableName(this._configuration.MemoryTableName)} ON [similarity].[memory_id] = {this.GetFullTableName(this._configuration.MemoryTableName)}.[id] - WHERE cosine_similarity >= @min_relevance_score"; + WHERE [cosine_similarity] >= @min_relevance_score + ORDER BY [cosine_similarity] desc"; cmd.Parameters.AddWithValue("@vector", embedding); cmd.Parameters.AddWithValue("@collection", collectionName); diff --git a/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs b/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs index a177404..4f0ac3a 100644 --- a/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs +++ b/src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs @@ -321,8 +321,9 @@ SELECT DISTINCT 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, cmd.Parameters, filters)}"; + AND [cosine_similarity] >= @min_relevance_score + {GenerateFilters(index, cmd.Parameters, filters)} + ORDER BY [cosine_similarity] desc"; cmd.Parameters.AddWithValue("@vector", JsonSerializer.Serialize(embedding.Data.ToArray())); cmd.Parameters.AddWithValue("@index", index);