From 3e4987face021378cf2c05441dd03fc43b24e4e8 Mon Sep 17 00:00:00 2001 From: alexmg Date: Wed, 30 Oct 2024 10:25:03 +1000 Subject: [PATCH 1/2] Fix embeddings not being returned in Azure AI Search memory --- .../AzureAISearch/AzureAISearchMemory.cs | 26 ++++++++++++------- .../Internals/AzureAISearchMemoryRecord.cs | 4 +-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs b/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs index 07dfde26f..e93d5028c 100644 --- a/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs +++ b/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs @@ -190,6 +190,7 @@ await client.IndexDocumentsAsync( FilterMode = VectorFilterMode.PreFilter } }; + AddSelectFields(options, withEmbeddings); if (limit > 0) { @@ -253,6 +254,7 @@ public async IAsyncEnumerable GetListAsync( var client = this.GetSearchClient(index); SearchOptions options = new(); + AddSelectFields(options, withEmbeddings); if (limit > 0) { @@ -515,7 +517,7 @@ private SearchIndex PrepareIndexSchema(string index, MemoryDbSchema schema) * - searchable: Full-text searchable, subject to lexical analysis such as word-breaking during indexing. * - filterable: Filterable fields of type Edm.String or Collection(Edm.String) don't undergo word-breaking. * - facetable: Used for counting. Fields of type Edm.String that are filterable, "sortable", or "facetable" can be at most 32kb. */ - SearchField? vectorField = null; + VectorSearchField? vectorField = null; foreach (var field in schema.Fields) { switch (field.Type) @@ -525,15 +527,10 @@ private SearchIndex PrepareIndexSchema(string index, MemoryDbSchema schema) throw new AzureAISearchMemoryException($"Unsupported field type {field.Type:G}"); case MemoryDbField.FieldType.Vector: - vectorField = new SearchField(field.Name, SearchFieldDataType.Collection(SearchFieldDataType.Single)) + vectorField = new VectorSearchField(field.Name, field.VectorSize, VectorSearchProfileName) { - IsKey = false, - IsFilterable = false, - IsSearchable = true, - IsFacetable = false, - IsSortable = false, - VectorSearchDimensions = field.VectorSize, - VectorSearchProfileName = VectorSearchProfileName, + IsHidden = false, + IsStored = true }; break; @@ -630,6 +627,17 @@ at Azure.Search.Documents.SearchClient.SearchInternal[T](SearchOptions options, return indexSchema; } + private static void AddSelectFields(SearchOptions options, bool withEmbeddings) + { + options.Select.Add(AzureAISearchMemoryRecord.IdField); + options.Select.Add(AzureAISearchMemoryRecord.TagsField); + options.Select.Add(AzureAISearchMemoryRecord.PayloadField); + if (withEmbeddings) + { + options.Select.Add(AzureAISearchMemoryRecord.VectorField); + } + } + private static double ScoreToCosineSimilarity(double score) { return 2 - (1 / score); diff --git a/extensions/AzureAISearch/AzureAISearch/Internals/AzureAISearchMemoryRecord.cs b/extensions/AzureAISearch/AzureAISearch/Internals/AzureAISearchMemoryRecord.cs index 68fd29283..7252b4284 100644 --- a/extensions/AzureAISearch/AzureAISearch/Internals/AzureAISearchMemoryRecord.cs +++ b/extensions/AzureAISearch/AzureAISearch/Internals/AzureAISearchMemoryRecord.cs @@ -15,8 +15,8 @@ internal sealed class AzureAISearchMemoryRecord { internal const string IdField = "id"; internal const string VectorField = "embedding"; - private const string TagsField = "tags"; - private const string PayloadField = "payload"; + internal const string TagsField = "tags"; + internal const string PayloadField = "payload"; private static readonly JsonSerializerOptions s_jsonOptions = new() { From ed4b1b867c155d3fdbb55bcd6d302bdc67bd568d Mon Sep 17 00:00:00 2001 From: Devis Lucato Date: Tue, 29 Oct 2024 19:07:06 -0700 Subject: [PATCH 2/2] Rename function --- .../AzureAISearch/AzureAISearch/AzureAISearchMemory.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs b/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs index e93d5028c..82e28e5c7 100644 --- a/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs +++ b/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs @@ -190,7 +190,7 @@ await client.IndexDocumentsAsync( FilterMode = VectorFilterMode.PreFilter } }; - AddSelectFields(options, withEmbeddings); + DefineFieldsToSelect(options, withEmbeddings); if (limit > 0) { @@ -254,7 +254,7 @@ public async IAsyncEnumerable GetListAsync( var client = this.GetSearchClient(index); SearchOptions options = new(); - AddSelectFields(options, withEmbeddings); + DefineFieldsToSelect(options, withEmbeddings); if (limit > 0) { @@ -627,7 +627,7 @@ at Azure.Search.Documents.SearchClient.SearchInternal[T](SearchOptions options, return indexSchema; } - private static void AddSelectFields(SearchOptions options, bool withEmbeddings) + private static void DefineFieldsToSelect(SearchOptions options, bool withEmbeddings) { options.Select.Add(AzureAISearchMemoryRecord.IdField); options.Select.Add(AzureAISearchMemoryRecord.TagsField);