Skip to content

Commit

Permalink
Add document filter to retrieval query to improve results on larger d…
Browse files Browse the repository at this point in the history
…ocument sets
  • Loading branch information
ErikNorenRG committed Mar 6, 2024
1 parent 7a010f1 commit a6e8dd5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/KernelMemory.MemoryStorage.SqlServer/SqlServerMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ SELECT TOP (@limit)

using (SqlCommand command = connection.CreateCommand())
{
var generatedFilters = GenerateFilters(index, command.Parameters, filters);

command.CommandText = $@"
WITH
[embedding] as
Expand All @@ -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
Expand All @@ -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()));
Expand Down

0 comments on commit a6e8dd5

Please sign in to comment.