-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Added implementation of Weaviate connector for new memory design (
#8403) ### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Related: #6527 In this PR: - Implemented `IVectorStore` - Implemented `IVectorStoreRecordCollection<TKey, TRecord>` - Weaviate default record mapper - `Options` classes - Extension methods for DI - Integration tests - Unit tests ### 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 😄 --------- Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
- Loading branch information
1 parent
684309d
commit 2200e00
Showing
53 changed files
with
3,048 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...t/src/Connectors/Connectors.Memory.Weaviate/Converters/WeaviateDateTimeOffsetConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
/// <summary> | ||
/// Converts datetime type to RFC 3339 formatted string. | ||
/// </summary> | ||
internal sealed class WeaviateDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | ||
{ | ||
private const string DateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.fffK"; | ||
|
||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var dateString = reader.GetString(); | ||
|
||
if (string.IsNullOrWhiteSpace(dateString)) | ||
{ | ||
return default; | ||
} | ||
|
||
return DateTimeOffset.Parse(dateString, CultureInfo.InvariantCulture); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString(DateTimeFormat, CultureInfo.InvariantCulture)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...nnectors/Connectors.Memory.Weaviate/Converters/WeaviateNullableDateTimeOffsetConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
/// <summary> | ||
/// Converts datetime type to RFC 3339 formatted string. | ||
/// </summary> | ||
internal sealed class WeaviateNullableDateTimeOffsetConverter : JsonConverter<DateTimeOffset?> | ||
{ | ||
private const string DateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.fffK"; | ||
|
||
public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return null; | ||
} | ||
|
||
var dateString = reader.GetString(); | ||
|
||
if (string.IsNullOrWhiteSpace(dateString)) | ||
{ | ||
return null; | ||
} | ||
|
||
return DateTimeOffset.Parse(dateString, CultureInfo.InvariantCulture); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options) | ||
{ | ||
if (value.HasValue) | ||
{ | ||
writer.WriteStringValue(value.Value.ToString(DateTimeFormat, CultureInfo.InvariantCulture)); | ||
} | ||
else | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateCreateCollectionSchemaRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateCreateCollectionSchemaRequest | ||
{ | ||
private const string ApiRoute = "schema"; | ||
|
||
[JsonConstructor] | ||
public WeaviateCreateCollectionSchemaRequest() { } | ||
|
||
public WeaviateCreateCollectionSchemaRequest(WeaviateCollectionSchema collectionSchema) | ||
{ | ||
this.CollectionName = collectionSchema.CollectionName; | ||
this.VectorConfigurations = collectionSchema.VectorConfigurations; | ||
this.Properties = collectionSchema.Properties; | ||
} | ||
|
||
[JsonPropertyName("class")] | ||
public string? CollectionName { get; set; } | ||
|
||
[JsonPropertyName("vectorConfig")] | ||
public Dictionary<string, WeaviateCollectionSchemaVectorConfig>? VectorConfigurations { get; set; } | ||
|
||
[JsonPropertyName("properties")] | ||
public List<WeaviateCollectionSchemaProperty>? Properties { get; set; } | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreatePostRequest(ApiRoute, this); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateDeleteCollectionSchemaRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateDeleteCollectionSchemaRequest(string collectionName) | ||
{ | ||
private const string ApiRoute = "schema"; | ||
|
||
[JsonIgnore] | ||
public string CollectionName { get; set; } = collectionName; | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreateDeleteRequest($"{ApiRoute}/{this.CollectionName}"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
dotnet/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateDeleteObjectBatchRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateDeleteObjectBatchRequest | ||
{ | ||
private const string ApiRoute = "batch/objects"; | ||
|
||
[JsonConstructor] | ||
public WeaviateDeleteObjectBatchRequest() { } | ||
|
||
public WeaviateDeleteObjectBatchRequest(WeaviateQueryMatch match) | ||
{ | ||
this.Match = match; | ||
} | ||
|
||
[JsonPropertyName("match")] | ||
public WeaviateQueryMatch? Match { get; set; } | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreateDeleteRequest(ApiRoute, this); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
dotnet/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateDeleteObjectRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateDeleteObjectRequest(string collectionName, Guid id) | ||
{ | ||
private const string ApiRoute = "objects"; | ||
|
||
[JsonIgnore] | ||
public string CollectionName { get; set; } = collectionName; | ||
|
||
[JsonIgnore] | ||
public Guid Id { get; set; } = id; | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreateDeleteRequest($"{ApiRoute}/{this.CollectionName}/{this.Id}"); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...et/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateGetCollectionObjectRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateGetCollectionObjectRequest(string collectionName, Guid id, bool includeVectors) | ||
{ | ||
private const string ApiRoute = "objects"; | ||
private const string IncludeQueryParameterName = "include"; | ||
private const string IncludeVectorQueryParameterValue = "vector"; | ||
|
||
[JsonIgnore] | ||
public string CollectionName { get; set; } = collectionName; | ||
|
||
[JsonIgnore] | ||
public Guid Id { get; set; } = id; | ||
|
||
[JsonIgnore] | ||
public bool IncludeVectors { get; set; } = includeVectors; | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
var uri = $"{ApiRoute}/{this.CollectionName}/{this.Id}"; | ||
|
||
if (this.IncludeVectors) | ||
{ | ||
uri += $"?{IncludeQueryParameterName}={IncludeVectorQueryParameterValue}"; | ||
} | ||
|
||
return HttpRequest.CreateGetRequest(uri); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...et/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateGetCollectionSchemaRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateGetCollectionSchemaRequest(string collectionName) | ||
{ | ||
private const string ApiRoute = "schema"; | ||
|
||
[JsonIgnore] | ||
public string CollectionName { get; set; } = collectionName; | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreateGetRequest($"{ApiRoute}/{this.CollectionName}"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...t/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateGetCollectionSchemaResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateGetCollectionSchemaResponse | ||
{ | ||
[JsonPropertyName("class")] | ||
public string? CollectionName { get; set; } | ||
} |
15 changes: 15 additions & 0 deletions
15
dotnet/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateGetCollectionsRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateGetCollectionsRequest | ||
{ | ||
private const string ApiRoute = "schema"; | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreateGetRequest(ApiRoute, this); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
dotnet/src/Connectors/Connectors.Memory.Weaviate/HttpV2/WeaviateGetCollectionsResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateGetCollectionsResponse | ||
{ | ||
[JsonPropertyName("classes")] | ||
public List<WeaviateCollectionSchema>? Collections { get; set; } | ||
} |
32 changes: 32 additions & 0 deletions
32
...onnectors/Connectors.Memory.Weaviate/HttpV2/WeaviateUpsertCollectionObjectBatchRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.Weaviate; | ||
|
||
internal sealed class WeaviateUpsertCollectionObjectBatchRequest | ||
{ | ||
private const string ApiRoute = "batch/objects"; | ||
|
||
[JsonConstructor] | ||
public WeaviateUpsertCollectionObjectBatchRequest() { } | ||
|
||
public WeaviateUpsertCollectionObjectBatchRequest(List<JsonNode> collectionObjects) | ||
{ | ||
this.CollectionObjects = collectionObjects; | ||
} | ||
|
||
[JsonPropertyName("fields")] | ||
public List<string> Fields { get; set; } = [WeaviateConstants.ReservedKeyPropertyName]; | ||
|
||
[JsonPropertyName("objects")] | ||
public List<JsonNode>? CollectionObjects { get; set; } | ||
|
||
public HttpRequestMessage Build() | ||
{ | ||
return HttpRequest.CreatePostRequest(ApiRoute, this); | ||
} | ||
} |
Oops, something went wrong.