Skip to content

Commit

Permalink
.Net: Vector Store: Allowing json serializer options to be passed dow…
Browse files Browse the repository at this point in the history
…n 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 😄
  • Loading branch information
westey-m authored Aug 21, 2024
1 parent c3ce629 commit 58327fc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
using System;
using Azure;
using Azure.Core;
using Azure.Core.Serialization;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel.Connectors.AzureAISearch;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Http;

namespace Microsoft.SemanticKernel;

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

// Build options for the Azure AI Search client and construct it.
var searchClientOptions = new SearchClientOptions();
searchClientOptions.Diagnostics.ApplicationId = HttpHeaderConstant.Values.UserAgent;
if (selectedOptions?.JsonSerializerOptions != null)
{
searchClientOptions.Serializer = new JsonObjectSerializer(selectedOptions.JsonSerializerOptions);
}

var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);

// Construct the vector store.
return new AzureAISearchVectorStore(
searchIndexClient,
selectedOptions);
Expand All @@ -88,9 +101,19 @@ public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollec
serviceId,
(sp, obj) =>
{
var searchIndexClient = new SearchIndexClient(endpoint, credential);
var selectedOptions = options ?? sp.GetService<AzureAISearchVectorStoreOptions>();

// Build options for the Azure AI Search client and construct it.
var searchClientOptions = new SearchClientOptions();
searchClientOptions.Diagnostics.ApplicationId = HttpHeaderConstant.Values.UserAgent;
if (selectedOptions?.JsonSerializerOptions != null)
{
searchClientOptions.Serializer = new JsonObjectSerializer(selectedOptions.JsonSerializerOptions);
}

var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);

// Construct the vector store.
return new AzureAISearchVectorStore(
searchIndexClient,
selectedOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ public IVectorStoreRecordCollection<TKey, TRecord> GetCollection<TKey, TRecord>(
return this._options.VectorStoreCollectionFactory.CreateVectorStoreRecordCollection<TKey, TRecord>(this._searchIndexClient, name, vectorStoreRecordDefinition);
}

var recordCollection = new AzureAISearchVectorStoreRecordCollection<TRecord>(this._searchIndexClient, name, new AzureAISearchVectorStoreRecordCollectionOptions<TRecord>() { VectorStoreRecordDefinition = vectorStoreRecordDefinition }) as IVectorStoreRecordCollection<TKey, TRecord>;
var recordCollection = new AzureAISearchVectorStoreRecordCollection<TRecord>(
this._searchIndexClient,
name,
new AzureAISearchVectorStoreRecordCollectionOptions<TRecord>()
{
JsonSerializerOptions = this._options.JsonSerializerOptions,
VectorStoreRecordDefinition = vectorStoreRecordDefinition
}) as IVectorStoreRecordCollection<TKey, TRecord>;

return recordCollection!;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json;
using Azure.Search.Documents.Indexes;

namespace Microsoft.SemanticKernel.Connectors.AzureAISearch;

/// <summary>
Expand All @@ -11,4 +14,11 @@ public sealed class AzureAISearchVectorStoreOptions
/// An optional factory to use for constructing <see cref="AzureAISearchVectorStoreRecordCollection{TRecord}"/> instances, if a custom record collection is required.
/// </summary>
public IAzureAISearchVectorStoreRecordCollectionFactory? VectorStoreCollectionFactory { get; init; }

/// <summary>
/// Gets or sets the JSON serializer options to use when converting between the data model and the Azure AI Search record.
/// Note that when using the default mapper and you are constructing your own <see cref="SearchIndexClient"/>, you will need
/// to provide the same set of <see cref="System.Text.Json.JsonSerializerOptions"/> both here and when constructing the <see cref="SearchIndexClient"/>.
/// </summary>
public JsonSerializerOptions? JsonSerializerOptions { get; init; } = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public sealed class AzureAISearchVectorStoreRecordCollectionOptions<TRecord>

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

0 comments on commit 58327fc

Please sign in to comment.