Skip to content

Commit

Permalink
ChromaClientException and ChromaMemoryStoreException are replaced by …
Browse files Browse the repository at this point in the history
…SKException
  • Loading branch information
SergeyMenshykh committed Jul 19, 2023
1 parent 3d6cb2b commit 8feb1b1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,12 +42,12 @@ public ChromaClient(string endpoint, ILogger? logger = null)
/// <param name="httpClient">The <see cref="HttpClient"/> instance used for making HTTP requests.</param>
/// <param name="endpoint">Chroma server endpoint URL.</param>
/// <param name="logger">Optional logger instance.</param>
/// <exception cref="ChromaClientException">Occurs when <see cref="HttpClient"/> doesn't have base address and endpoint parameter is not provided.</exception>
/// <exception cref="SKException">Occurs when <see cref="HttpClient"/> doesn't have base address and endpoint parameter is not provided.</exception>
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;
Expand Down Expand Up @@ -183,7 +184,7 @@ public async Task<ChromaQueryResultModel> 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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -225,6 +226,8 @@ public async IAsyncEnumerable<string> 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;
Expand All @@ -234,7 +237,7 @@ private async Task<ChromaCollectionModel> 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<ChromaCollectionModel?> GetCollectionAsync(string collectionName, CancellationToken cancellationToken)
Expand All @@ -243,7 +246,7 @@ private async Task<ChromaCollectionModel> GetCollectionOrThrowAsync(string colle
{
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);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,15 +108,15 @@ 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);

// Act
var exception = await Record.ExceptionAsync(() => store.DeleteCollectionAsync(collectionName));

// Assert
Assert.IsType<ChromaMemoryStoreException>(exception);
Assert.IsType<SKException>(exception);
Assert.Equal(expectedExceptionMessage, exception.Message);
}

Expand All @@ -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);

Expand Down Expand Up @@ -202,15 +203,15 @@ 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);

// Act
var exception = await Record.ExceptionAsync(() => store.GetAsync(collectionName, memoryRecordKey, withEmbedding: true));

// Assert
Assert.IsType<ChromaMemoryStoreException>(exception);
Assert.IsType<SKException>(exception);
Assert.Equal(collectionDoesNotExistErrorMessage, exception.Message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -119,7 +120,7 @@ public async Task ItThrowsExceptionOnNonExistentCollectionDeletionAsync()
var exception = await Record.ExceptionAsync(() => this._chromaMemoryStore.DeleteCollectionAsync(collectionName));

// Assert
Assert.IsType<ChromaMemoryStoreException>(exception);
Assert.IsType<SKException>(exception);
Assert.Contains(
$"Cannot delete non-existent collection {collectionName}",
exception.Message,
Expand Down

0 comments on commit 8feb1b1

Please sign in to comment.