From 8feb1b1fd170b8e7ecea222138c3d146660ce706 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh Date: Wed, 19 Jul 2023 16:17:53 +0100 Subject: [PATCH] ChromaClientException and ChromaMemoryStoreException are replaced by SKException --- .../Connectors.Memory.Chroma/ChromaClient.cs | 7 +-- .../ChromaClientException.cs | 52 ------------------- .../ChromaMemoryStore.cs | 11 ++-- .../ChromaMemoryStoreException.cs | 35 ------------- .../Memory/Chroma/ChromaMemoryStoreTests.cs | 11 ++-- .../Memory/Chroma/ChromaMemoryStoreTests.cs | 3 +- 6 files changed, 19 insertions(+), 100 deletions(-) delete mode 100644 dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClientException.cs delete mode 100644 dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStoreException.cs diff --git a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClient.cs b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClient.cs index d80cfb9833c9..f431a41bbabf 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClient.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClient.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.SemanticKernel.Connectors.Memory.Chroma.Http.ApiSchema; using Microsoft.SemanticKernel.Connectors.Memory.Chroma.Http.ApiSchema.Internal; +using Microsoft.SemanticKernel.Diagnostics; namespace Microsoft.SemanticKernel.Connectors.Memory.Chroma; @@ -41,12 +42,12 @@ public ChromaClient(string endpoint, ILogger? logger = null) /// The instance used for making HTTP requests. /// Chroma server endpoint URL. /// Optional logger instance. - /// Occurs when doesn't have base address and endpoint parameter is not provided. + /// Occurs when doesn't have base address and endpoint parameter is not provided. public ChromaClient(HttpClient httpClient, string? endpoint = null, ILogger? logger = null) { if (string.IsNullOrEmpty(httpClient.BaseAddress?.AbsoluteUri) && string.IsNullOrEmpty(endpoint)) { - throw new ChromaClientException("The HttpClient BaseAddress and endpoint are both null or empty. Please ensure at least one is provided."); + throw new SKException("The HttpClient BaseAddress and endpoint are both null or empty. Please ensure at least one is provided."); } this._httpClient = httpClient; @@ -183,7 +184,7 @@ public async Task QueryEmbeddingsAsync(string collection catch (HttpRequestException e) { this._logger.LogError(e, "{0} {1} operation failed: {2}, {3}", request.Method.Method, operationName, e.Message, responseContent); - throw new ChromaClientException($"{request.Method.Method} {operationName} operation failed: {e.Message}, {responseContent}", e); + throw new SKException($"{request.Method.Method} {operationName} operation failed: {e.Message}, {responseContent}", e); } return (response, responseContent); diff --git a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClientException.cs b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClientException.cs deleted file mode 100644 index 7568718b5191..000000000000 --- a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClientException.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; -using System.Globalization; - -namespace Microsoft.SemanticKernel.Connectors.Memory.Chroma; - -/// -/// Exception to identify issues in class. -/// -public class ChromaClientException : Exception -{ - private const string CollectionDoesNotExistErrorFormat = "Collection {0} does not exist"; - private const string DeleteNonExistentCollectionErrorMessage = "list index out of range"; - - /// - /// Initializes a new instance of the class. - /// - public ChromaClientException() : base() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The message that describes the error. - public ChromaClientException(string message) : base(message) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The message that describes the error. - /// Instance of inner exception. - public ChromaClientException(string message, Exception innerException) : base(message, innerException) - { - } - - /// - /// Checks if Chroma API error means that collection does not exist. - /// - /// Collection name. - public bool CollectionDoesNotExistException(string collectionName) => - this.Message.Contains(string.Format(CultureInfo.InvariantCulture, CollectionDoesNotExistErrorFormat, collectionName)); - - /// - /// Checks if Chroma API error means that there was an attempt to delete non-existent collection. - /// - public bool DeleteNonExistentCollectionException() => - this.Message.Contains(DeleteNonExistentCollectionErrorMessage); -} diff --git a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStore.cs index 08d80d435eea..e8622d25865a 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStore.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Net.Http; using System.Runtime.CompilerServices; @@ -70,10 +71,10 @@ public async Task DeleteCollectionAsync(string collectionName, CancellationToken { await this._chromaClient.DeleteCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); } - catch (ChromaClientException e) when (e.DeleteNonExistentCollectionException()) + catch (SKException e) when (e.Message.Contains(DeleteNonExistentCollectionErrorMessage)) { this._logger.LogError("Cannot delete non-existent collection {0}", collectionName); - throw new ChromaMemoryStoreException($"Cannot delete non-existent collection {collectionName}", e); + throw new SKException($"Cannot delete non-existent collection {collectionName}", e); } } @@ -225,6 +226,8 @@ public async IAsyncEnumerable UpsertBatchAsync(string collectionName, IE private const string IncludeMetadatas = "metadatas"; private const string IncludeEmbeddings = "embeddings"; private const string IncludeDistances = "distances"; + private const string CollectionDoesNotExistErrorFormat = "Collection {0} does not exist"; + private const string DeleteNonExistentCollectionErrorMessage = "list index out of range"; private readonly ILogger _logger; private readonly IChromaClient _chromaClient; @@ -234,7 +237,7 @@ private async Task GetCollectionOrThrowAsync(string colle { return await this.GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false) ?? - throw new ChromaMemoryStoreException($"Collection {collectionName} does not exist"); + throw new SKException($"Collection {collectionName} does not exist"); } private async Task GetCollectionAsync(string collectionName, CancellationToken cancellationToken) @@ -243,7 +246,7 @@ await this.GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait( { return await this._chromaClient.GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); } - catch (ChromaClientException e) when (e.CollectionDoesNotExistException(collectionName)) + catch (SKException e) when (e.Message.Contains(string.Format(CultureInfo.InvariantCulture, CollectionDoesNotExistErrorFormat, collectionName))) { this._logger.LogError("Collection {0} does not exist", collectionName); diff --git a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStoreException.cs b/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStoreException.cs deleted file mode 100644 index cef6b78d1247..000000000000 --- a/dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaMemoryStoreException.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; - -namespace Microsoft.SemanticKernel.Connectors.Memory.Chroma; - -/// -/// Exception to identify issues in class. -/// -public class ChromaMemoryStoreException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ChromaMemoryStoreException() : base() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The message that describes the error. - public ChromaMemoryStoreException(string message) : base(message) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The message that describes the error. - /// Instance of inner exception. - public ChromaMemoryStoreException(string message, Exception innerException) : base(message, innerException) - { - } -} diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Chroma/ChromaMemoryStoreTests.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Chroma/ChromaMemoryStoreTests.cs index 79a88e3e0f91..e335526adf0b 100644 --- a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Chroma/ChromaMemoryStoreTests.cs +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Chroma/ChromaMemoryStoreTests.cs @@ -10,6 +10,7 @@ using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.Connectors.Memory.Chroma; using Microsoft.SemanticKernel.Connectors.Memory.Chroma.Http.ApiSchema; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Moq; using Xunit; @@ -107,7 +108,7 @@ public async Task ItThrowsExceptionOnNonExistentCollectionDeletionAsync() this._chromaClientMock .Setup(client => client.DeleteCollectionAsync(collectionName, CancellationToken.None)) - .Throws(new ChromaClientException(deleteNonExistentCollectionErrorMessage)); + .Throws(new SKException(deleteNonExistentCollectionErrorMessage)); var store = new ChromaMemoryStore(this._chromaClientMock.Object); @@ -115,7 +116,7 @@ public async Task ItThrowsExceptionOnNonExistentCollectionDeletionAsync() var exception = await Record.ExceptionAsync(() => store.DeleteCollectionAsync(collectionName)); // Assert - Assert.IsType(exception); + Assert.IsType(exception); Assert.Equal(expectedExceptionMessage, exception.Message); } @@ -141,7 +142,7 @@ public async Task ItReturnsFalseWhenCollectionDoesNotExistAsync() this._chromaClientMock .Setup(client => client.GetCollectionAsync(collectionName, CancellationToken.None)) - .Throws(new ChromaClientException(collectionDoesNotExistErrorMessage)); + .Throws(new SKException(collectionDoesNotExistErrorMessage)); var store = new ChromaMemoryStore(this._chromaClientMock.Object); @@ -202,7 +203,7 @@ public async Task ItThrowsExceptionOnGettingMemoryRecordFromNonExistingCollectio this._chromaClientMock .Setup(client => client.GetCollectionAsync(collectionName, CancellationToken.None)) - .Throws(new ChromaClientException(collectionDoesNotExistErrorMessage)); + .Throws(new SKException(collectionDoesNotExistErrorMessage)); var store = new ChromaMemoryStore(this._chromaClientMock.Object); @@ -210,7 +211,7 @@ public async Task ItThrowsExceptionOnGettingMemoryRecordFromNonExistingCollectio var exception = await Record.ExceptionAsync(() => store.GetAsync(collectionName, memoryRecordKey, withEmbedding: true)); // Assert - Assert.IsType(exception); + Assert.IsType(exception); Assert.Equal(collectionDoesNotExistErrorMessage, exception.Message); } diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/Chroma/ChromaMemoryStoreTests.cs b/dotnet/src/IntegrationTests/Connectors/Memory/Chroma/ChromaMemoryStoreTests.cs index 6ba48d4953f8..9d9f3d2e6447 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/Chroma/ChromaMemoryStoreTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/Chroma/ChromaMemoryStoreTests.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.Connectors.Memory.Chroma; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Xunit; @@ -119,7 +120,7 @@ public async Task ItThrowsExceptionOnNonExistentCollectionDeletionAsync() var exception = await Record.ExceptionAsync(() => this._chromaMemoryStore.DeleteCollectionAsync(collectionName)); // Assert - Assert.IsType(exception); + Assert.IsType(exception); Assert.Contains( $"Cannot delete non-existent collection {collectionName}", exception.Message,