Skip to content

Commit

Permalink
Merge branch 'issue-6761-ONNX-gen-ai-Connector' of https://github.com…
Browse files Browse the repository at this point in the history
…/nmoeller/semantic-kernel into issue-6761-ONNX-gen-ai-Connector
  • Loading branch information
nmoeller committed Sep 28, 2024
2 parents 673d446 + 8ec2fe4 commit 0b7d49e
Show file tree
Hide file tree
Showing 25 changed files with 707 additions and 99 deletions.
3 changes: 1 addition & 2 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Text", "Text", "{EB2C141A-AE5F-4080-8790-13EB16323CEF}"
ProjectSection(SolutionItems) = preProject
src\InternalUtilities\src\Text\JsonOptionsCache.cs = src\InternalUtilities\src\Text\JsonOptionsCache.cs
src\InternalUtilities\src\Text\ReadOnlyMemoryConverter.cs = src\InternalUtilities\src\Text\ReadOnlyMemoryConverter.cs
src\InternalUtilities\src\Text\SseData.cs = src\InternalUtilities\src\Text\SseData.cs
src\InternalUtilities\src\Text\SseJsonParser.cs = src\InternalUtilities\src\Text\SseJsonParser.cs
src\InternalUtilities\src\Text\SseLine.cs = src\InternalUtilities\src\Text\SseLine.cs
Expand Down Expand Up @@ -1059,4 +1058,4 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
EndGlobalSection
EndGlobal
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public sealed class AzureCosmosDBNoSQLGenericDataModelMapperTests
public void MapFromDataToStorageModelMapsAllSupportedTypes()
{
// Arrange
var test = JsonSerializer.Serialize(s_byteVector);

var sut = new AzureCosmosDBNoSQLGenericDataModelMapper(
s_vectorStoreRecordDefinition,
s_storagePropertyNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.AzureAISearch;

Expand Down Expand Up @@ -60,7 +59,6 @@ internal sealed class AzureAISearchMemoryRecord
/// Content embedding
/// </summary>
[JsonPropertyName(EmbeddingField)]
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Embedding { get; set; }

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

using System;
using System.Text.Json.Serialization;
using Kusto.Cloud.Platform.Utils;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Kusto;

Expand All @@ -26,7 +24,6 @@ public sealed class KustoMemoryRecord
/// <summary>
/// Source content embedding.
/// </summary>
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Embedding { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Pinecone;

Expand All @@ -30,7 +29,6 @@ public sealed class Query
/// <summary>
/// Vector dense data. This should be the same length as the dimension of the index being queried.
/// </summary>
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Vector { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Pinecone;

Expand All @@ -24,7 +23,6 @@ public class SparseVectorData
/// </summary>
/// <value>The corresponding values of the sparse data, which must be the same length as the indices.</value>
[JsonPropertyName("values")]
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Values { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class PineconeDocument
/// Vector dense data. This should be the same length as the dimension of the index being queried.
/// </summary>
[JsonPropertyName("values")]
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Values { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Qdrant;

Expand All @@ -26,7 +25,6 @@ internal sealed class ScoredPoint
public Dictionary<string, object> Payload { get; set; }

[JsonPropertyName("vector")]
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Vector { get; }

[JsonConstructor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Qdrant;

Expand All @@ -23,7 +22,6 @@ public class QdrantVectorRecord
/// The embedding data.
/// </summary>
[JsonPropertyName("embedding")]
[JsonConverter(typeof(ReadOnlyMemoryConverter))]
public ReadOnlyMemory<float> Embedding { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Weaviate;

Expand All @@ -14,7 +13,6 @@ internal static class HttpRequest
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { JsonOptionsCache.ReadOnlyMemoryConverter },
};

public static HttpRequestMessage CreateGetRequest(string url, object? payload = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class WeaviateUpsertCollectionObjectBatchRequest
[JsonConstructor]
public WeaviateUpsertCollectionObjectBatchRequest() { }

public WeaviateUpsertCollectionObjectBatchRequest(List<JsonNode> collectionObjects)
public WeaviateUpsertCollectionObjectBatchRequest(List<JsonObject> collectionObjects)
{
this.CollectionObjects = collectionObjects;
}
Expand All @@ -23,7 +23,7 @@ public WeaviateUpsertCollectionObjectBatchRequest(List<JsonNode> collectionObjec
public List<string> Fields { get; set; } = [WeaviateConstants.ReservedKeyPropertyName];

[JsonPropertyName("objects")]
public List<JsonNode>? CollectionObjects { get; set; }
public List<JsonObject>? CollectionObjects { get; set; }

public HttpRequestMessage Build()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.SemanticKernel.Data;

namespace Microsoft.SemanticKernel.Connectors.Weaviate;

/// <summary>
/// A mapper that maps between the generic Semantic Kernel data model and the model that the data is stored under, within Weaviate.
/// </summary>
internal sealed class WeaviateGenericDataModelMapper : IVectorStoreRecordMapper<VectorStoreGenericDataModel<Guid>, JsonObject>
{
/// <summary>The name of the Weaviate collection.</summary>
private readonly string _collectionName;

/// <summary>A <see cref="VectorStoreRecordKeyProperty"/> property of record definition.</summary>
private readonly VectorStoreRecordKeyProperty _keyProperty;

/// <summary>A collection of <see cref="VectorStoreRecordDataProperty"/> properties of record definition.</summary>
private readonly List<VectorStoreRecordDataProperty> _dataProperties;

/// <summary>A collection of <see cref="VectorStoreRecordVectorProperty"/> properties of record definition.</summary>
private readonly List<VectorStoreRecordVectorProperty> _vectorProperties;

/// <summary>A dictionary that maps from a property name to the storage name.</summary>
private readonly Dictionary<string, string> _storagePropertyNames;

/// <summary>A <see cref="JsonSerializerOptions"/> for serialization/deserialization of record properties.</summary>
private readonly JsonSerializerOptions _jsonSerializerOptions;

/// <summary>
/// Initializes a new instance of the <see cref="WeaviateGenericDataModelMapper"/> class.
/// </summary>
/// <param name="collectionName">The name of the Weaviate collection</param>
/// <param name="keyProperty">A <see cref="VectorStoreRecordKeyProperty"/> property of record definition.</param>
/// <param name="dataProperties">A collection of <see cref="VectorStoreRecordDataProperty"/> properties of record definition.</param>
/// <param name="vectorProperties">A collection of <see cref="VectorStoreRecordVectorProperty"/> properties of record definition.</param>
/// <param name="storagePropertyNames">A dictionary that maps from a property name to the storage name.</param>
/// <param name="jsonSerializerOptions">A <see cref="JsonSerializerOptions"/> for serialization/deserialization of record properties.</param>
public WeaviateGenericDataModelMapper(
string collectionName,
VectorStoreRecordKeyProperty keyProperty,
List<VectorStoreRecordDataProperty> dataProperties,
List<VectorStoreRecordVectorProperty> vectorProperties,
Dictionary<string, string> storagePropertyNames,
JsonSerializerOptions jsonSerializerOptions)
{
Verify.NotNullOrWhiteSpace(collectionName);
Verify.NotNull(keyProperty);
Verify.NotNull(dataProperties);
Verify.NotNull(vectorProperties);
Verify.NotNull(storagePropertyNames);
Verify.NotNull(jsonSerializerOptions);

this._collectionName = collectionName;
this._keyProperty = keyProperty;
this._dataProperties = dataProperties;
this._vectorProperties = vectorProperties;
this._storagePropertyNames = storagePropertyNames;
this._jsonSerializerOptions = jsonSerializerOptions;
}

public JsonObject MapFromDataToStorageModel(VectorStoreGenericDataModel<Guid> dataModel)
{
Verify.NotNull(dataModel);

// Transform generic data model to Weaviate object model.
var weaviateObjectModel = new JsonObject
{
{ WeaviateConstants.ReservedCollectionPropertyName, JsonValue.Create(this._collectionName) },
{ WeaviateConstants.ReservedKeyPropertyName, dataModel.Key },
{ WeaviateConstants.ReservedDataPropertyName, new JsonObject() },
{ WeaviateConstants.ReservedVectorPropertyName, new JsonObject() },
};

// Populate data properties.
foreach (var property in this._dataProperties)
{
if (dataModel.Data is not null && dataModel.Data.TryGetValue(property.DataModelPropertyName, out var dataValue))
{
var storagePropertyName = this._storagePropertyNames[property.DataModelPropertyName];

weaviateObjectModel[WeaviateConstants.ReservedDataPropertyName]![storagePropertyName] = dataValue is not null ?
JsonSerializer.SerializeToNode(dataValue, property.PropertyType, this._jsonSerializerOptions) :
null;
}
}

// Populate vector properties.
foreach (var property in this._vectorProperties)
{
if (dataModel.Vectors is not null && dataModel.Vectors.TryGetValue(property.DataModelPropertyName, out var vectorValue))
{
var storagePropertyName = this._storagePropertyNames[property.DataModelPropertyName];

weaviateObjectModel[WeaviateConstants.ReservedVectorPropertyName]![storagePropertyName] = vectorValue is not null ?
JsonSerializer.SerializeToNode(vectorValue, property.PropertyType, this._jsonSerializerOptions) :
null;
}
}

return weaviateObjectModel;
}

public VectorStoreGenericDataModel<Guid> MapFromStorageToDataModel(JsonObject storageModel, StorageToDataModelMapperOptions options)
{
Verify.NotNull(storageModel);

// Create variables to store the response properties.
var key = storageModel[WeaviateConstants.ReservedKeyPropertyName]?.GetValue<Guid>();

if (!key.HasValue)
{
throw new VectorStoreRecordMappingException("No key property was found in the record retrieved from storage.");
}

var dataProperties = new Dictionary<string, object?>();
var vectorProperties = new Dictionary<string, object?>();

// Populate data properties.
foreach (var property in this._dataProperties)
{
var storagePropertyName = this._storagePropertyNames[property.DataModelPropertyName];
var jsonObject = storageModel[WeaviateConstants.ReservedDataPropertyName] as JsonObject;

if (jsonObject is not null && jsonObject.TryGetPropertyValue(storagePropertyName, out var dataValue))
{
dataProperties.Add(property.DataModelPropertyName, dataValue.Deserialize(property.PropertyType, this._jsonSerializerOptions));
}
}

// Populate vector properties.
if (options.IncludeVectors)
{
foreach (var property in this._vectorProperties)
{
var storagePropertyName = this._storagePropertyNames[property.DataModelPropertyName];
var jsonObject = storageModel[WeaviateConstants.ReservedVectorPropertyName] as JsonObject;

if (jsonObject is not null && jsonObject.TryGetPropertyValue(storagePropertyName, out var vectorValue))
{
vectorProperties.Add(property.DataModelPropertyName, vectorValue.Deserialize(property.PropertyType, this._jsonSerializerOptions));
}
}
}

return new VectorStoreGenericDataModel<Guid>(key.Value) { Data = dataProperties, Vectors = vectorProperties };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.Http;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Text;

namespace Microsoft.SemanticKernel.Connectors.Weaviate;

Expand Down Expand Up @@ -53,7 +52,6 @@ public partial class WeaviateMemoryStore : IMemoryStore
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { JsonOptionsCache.ReadOnlyMemoryConverter }
};

private readonly HttpClient _httpClient;
Expand Down
Loading

0 comments on commit 0b7d49e

Please sign in to comment.