Skip to content

Commit 58327fc

Browse files
authored
.Net: Vector Store: Allowing json serializer options to be passed down from vector store options and user agent string. (#8296)
### Motivation and Context See #8086 See #7580 To make it easier for users who are using the default construction for the azure ai search client to also use custom json serializer settings, adding this as an option to the vector store options class and using it when both when constructing the azure ai search client and the collection. Also adding the semantic kernel user agent string to the azure ai search client when constructing it. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent c3ce629 commit 58327fc

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchServiceCollectionExtensions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
using System;
44
using Azure;
55
using Azure.Core;
6+
using Azure.Core.Serialization;
7+
using Azure.Search.Documents;
68
using Azure.Search.Documents.Indexes;
79
using Microsoft.Extensions.DependencyInjection;
810
using Microsoft.SemanticKernel.Connectors.AzureAISearch;
911
using Microsoft.SemanticKernel.Data;
12+
using Microsoft.SemanticKernel.Http;
1013

1114
namespace Microsoft.SemanticKernel;
1215

@@ -59,9 +62,19 @@ public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollec
5962
serviceId,
6063
(sp, obj) =>
6164
{
62-
var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential);
6365
var selectedOptions = options ?? sp.GetService<AzureAISearchVectorStoreOptions>();
6466

67+
// Build options for the Azure AI Search client and construct it.
68+
var searchClientOptions = new SearchClientOptions();
69+
searchClientOptions.Diagnostics.ApplicationId = HttpHeaderConstant.Values.UserAgent;
70+
if (selectedOptions?.JsonSerializerOptions != null)
71+
{
72+
searchClientOptions.Serializer = new JsonObjectSerializer(selectedOptions.JsonSerializerOptions);
73+
}
74+
75+
var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);
76+
77+
// Construct the vector store.
6578
return new AzureAISearchVectorStore(
6679
searchIndexClient,
6780
selectedOptions);
@@ -88,9 +101,19 @@ public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollec
88101
serviceId,
89102
(sp, obj) =>
90103
{
91-
var searchIndexClient = new SearchIndexClient(endpoint, credential);
92104
var selectedOptions = options ?? sp.GetService<AzureAISearchVectorStoreOptions>();
93105

106+
// Build options for the Azure AI Search client and construct it.
107+
var searchClientOptions = new SearchClientOptions();
108+
searchClientOptions.Diagnostics.ApplicationId = HttpHeaderConstant.Values.UserAgent;
109+
if (selectedOptions?.JsonSerializerOptions != null)
110+
{
111+
searchClientOptions.Serializer = new JsonObjectSerializer(selectedOptions.JsonSerializerOptions);
112+
}
113+
114+
var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);
115+
116+
// Construct the vector store.
94117
return new AzureAISearchVectorStore(
95118
searchIndexClient,
96119
selectedOptions);

dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchVectorStore.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public IVectorStoreRecordCollection<TKey, TRecord> GetCollection<TKey, TRecord>(
5656
return this._options.VectorStoreCollectionFactory.CreateVectorStoreRecordCollection<TKey, TRecord>(this._searchIndexClient, name, vectorStoreRecordDefinition);
5757
}
5858

59-
var recordCollection = new AzureAISearchVectorStoreRecordCollection<TRecord>(this._searchIndexClient, name, new AzureAISearchVectorStoreRecordCollectionOptions<TRecord>() { VectorStoreRecordDefinition = vectorStoreRecordDefinition }) as IVectorStoreRecordCollection<TKey, TRecord>;
59+
var recordCollection = new AzureAISearchVectorStoreRecordCollection<TRecord>(
60+
this._searchIndexClient,
61+
name,
62+
new AzureAISearchVectorStoreRecordCollectionOptions<TRecord>()
63+
{
64+
JsonSerializerOptions = this._options.JsonSerializerOptions,
65+
VectorStoreRecordDefinition = vectorStoreRecordDefinition
66+
}) as IVectorStoreRecordCollection<TKey, TRecord>;
67+
6068
return recordCollection!;
6169
}
6270

dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchVectorStoreOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System.Text.Json;
4+
using Azure.Search.Documents.Indexes;
5+
36
namespace Microsoft.SemanticKernel.Connectors.AzureAISearch;
47

58
/// <summary>
@@ -11,4 +14,11 @@ public sealed class AzureAISearchVectorStoreOptions
1114
/// An optional factory to use for constructing <see cref="AzureAISearchVectorStoreRecordCollection{TRecord}"/> instances, if a custom record collection is required.
1215
/// </summary>
1316
public IAzureAISearchVectorStoreRecordCollectionFactory? VectorStoreCollectionFactory { get; init; }
17+
18+
/// <summary>
19+
/// Gets or sets the JSON serializer options to use when converting between the data model and the Azure AI Search record.
20+
/// Note that when using the default mapper and you are constructing your own <see cref="SearchIndexClient"/>, you will need
21+
/// to provide the same set of <see cref="System.Text.Json.JsonSerializerOptions"/> both here and when constructing the <see cref="SearchIndexClient"/>.
22+
/// </summary>
23+
public JsonSerializerOptions? JsonSerializerOptions { get; init; } = null;
1424
}

dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchVectorStoreRecordCollectionOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public sealed class AzureAISearchVectorStoreRecordCollectionOptions<TRecord>
3333

3434
/// <summary>
3535
/// Gets or sets the JSON serializer options to use when converting between the data model and the Azure AI Search record.
36-
/// Note that when using the default mapper, you will need to provide the same set of <see cref="System.Text.Json.JsonSerializerOptions"/> both here and when constructing the <see cref="SearchIndexClient"/>.
36+
/// Note that when using the default mapper and you are constructing your own <see cref="SearchIndexClient"/>, you will need
37+
/// to provide the same set of <see cref="System.Text.Json.JsonSerializerOptions"/> both here and when constructing the <see cref="SearchIndexClient"/>.
3738
/// </summary>
3839
public JsonSerializerOptions? JsonSerializerOptions { get; init; } = null;
3940
}

0 commit comments

Comments
 (0)