|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using Microsoft.SemanticKernel.Connectors.Memory.Chroma.Http.ApiSchema; |
| 14 | +using Microsoft.SemanticKernel.Connectors.Memory.Chroma.Http.ApiSchema.Internal; |
| 15 | + |
| 16 | +namespace Microsoft.SemanticKernel.Connectors.Memory.Chroma; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// An implementation of a client for the Chroma Vector DB. This class is used to |
| 20 | +/// create, delete, and get embeddings data from Chroma Vector DB instance. |
| 21 | +/// </summary> |
| 22 | +#pragma warning disable CA1001 // Types that own disposable fields should be disposable. Explanation - In this case, there is no need to dispose because either the NonDisposableHttpClientHandler or a custom HTTP client is being used. |
| 23 | +public class ChromaClient : IChromaClient |
| 24 | +#pragma warning restore CA1001 // Types that own disposable fields should be disposable. Explanation - In this case, there is no need to dispose because either the NonDisposableHttpClientHandler or a custom HTTP client is being used. |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="ChromaClient"/> class. |
| 28 | + /// </summary> |
| 29 | + /// <param name="endpoint">Chroma server endpoint URL.</param> |
| 30 | + /// <param name="logger">Optional logger instance.</param> |
| 31 | + public ChromaClient(string endpoint, ILogger? logger = null) |
| 32 | + { |
| 33 | + this._httpClient = new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false); |
| 34 | + this._endpoint = endpoint; |
| 35 | + this._logger = logger ?? NullLogger<ChromaClient>.Instance; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="ChromaClient"/> class. |
| 40 | + /// </summary> |
| 41 | + /// <param name="httpClient">The <see cref="HttpClient"/> instance used for making HTTP requests.</param> |
| 42 | + /// <param name="endpoint">Chroma server endpoint URL.</param> |
| 43 | + /// <param name="logger">Optional logger instance.</param> |
| 44 | + /// <exception cref="ChromaClientException">Occurs when <see cref="HttpClient"/> doesn't have base address and endpoint parameter is not provided.</exception> |
| 45 | + public ChromaClient(HttpClient httpClient, string? endpoint = null, ILogger? logger = null) |
| 46 | + { |
| 47 | + if (string.IsNullOrEmpty(httpClient.BaseAddress?.AbsoluteUri) && string.IsNullOrEmpty(endpoint)) |
| 48 | + { |
| 49 | + throw new ChromaClientException("The HttpClient BaseAddress and endpoint are both null or empty. Please ensure at least one is provided."); |
| 50 | + } |
| 51 | + |
| 52 | + this._httpClient = httpClient; |
| 53 | + this._endpoint = endpoint; |
| 54 | + this._logger = logger ?? NullLogger<ChromaClient>.Instance; |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + public async Task CreateCollectionAsync(string collectionName, CancellationToken cancellationToken = default) |
| 59 | + { |
| 60 | + this._logger.LogDebug("Creating collection {0}", collectionName); |
| 61 | + |
| 62 | + using var request = CreateCollectionRequest.Create(collectionName).Build(); |
| 63 | + |
| 64 | + await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public async Task<ChromaCollectionModel?> GetCollectionAsync(string collectionName, CancellationToken cancellationToken = default) |
| 69 | + { |
| 70 | + this._logger.LogDebug("Getting collection {0}", collectionName); |
| 71 | + |
| 72 | + using var request = GetCollectionRequest.Create(collectionName).Build(); |
| 73 | + |
| 74 | + (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 75 | + |
| 76 | + var collection = JsonSerializer.Deserialize<ChromaCollectionModel>(responseContent); |
| 77 | + |
| 78 | + return collection; |
| 79 | + } |
| 80 | + |
| 81 | + /// <inheritdoc /> |
| 82 | + public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default) |
| 83 | + { |
| 84 | + this._logger.LogDebug("Deleting collection {0}", collectionName); |
| 85 | + |
| 86 | + using var request = DeleteCollectionRequest.Create(collectionName).Build(); |
| 87 | + |
| 88 | + await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc /> |
| 92 | + public async IAsyncEnumerable<string> ListCollectionsAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 93 | + { |
| 94 | + this._logger.LogDebug("Listing collections"); |
| 95 | + |
| 96 | + using var request = ListCollectionsRequest.Create().Build(); |
| 97 | + |
| 98 | + (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 99 | + |
| 100 | + var collections = JsonSerializer.Deserialize<List<ChromaCollectionModel>>(responseContent); |
| 101 | + |
| 102 | + foreach (var collection in collections!) |
| 103 | + { |
| 104 | + yield return collection.Name; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// <inheritdoc /> |
| 109 | + public async Task AddEmbeddingsAsync(string collectionId, string[] ids, float[][] embeddings, object[]? metadatas = null, CancellationToken cancellationToken = default) |
| 110 | + { |
| 111 | + this._logger.LogDebug("Adding embeddings to collection with id: {0}", collectionId); |
| 112 | + |
| 113 | + using var request = AddEmbeddingsRequest.Create(collectionId, ids, embeddings, metadatas).Build(); |
| 114 | + |
| 115 | + await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 116 | + } |
| 117 | + |
| 118 | + /// <inheritdoc /> |
| 119 | + public async Task<ChromaEmbeddingsModel> GetEmbeddingsAsync(string collectionId, string[] ids, string[]? include = null, CancellationToken cancellationToken = default) |
| 120 | + { |
| 121 | + this._logger.LogDebug("Getting embeddings from collection with id: {0}", collectionId); |
| 122 | + |
| 123 | + using var request = GetEmbeddingsRequest.Create(collectionId, ids, include).Build(); |
| 124 | + |
| 125 | + (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 126 | + |
| 127 | + var embeddings = JsonSerializer.Deserialize<ChromaEmbeddingsModel>(responseContent); |
| 128 | + |
| 129 | + return embeddings ?? new ChromaEmbeddingsModel(); |
| 130 | + } |
| 131 | + |
| 132 | + /// <inheritdoc /> |
| 133 | + public async Task DeleteEmbeddingsAsync(string collectionId, string[] ids, CancellationToken cancellationToken = default) |
| 134 | + { |
| 135 | + this._logger.LogDebug("Deleting embeddings from collection with id: {0}", collectionId); |
| 136 | + |
| 137 | + using var request = DeleteEmbeddingsRequest.Create(collectionId, ids).Build(); |
| 138 | + |
| 139 | + await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 140 | + } |
| 141 | + |
| 142 | + /// <inheritdoc /> |
| 143 | + public async Task<ChromaQueryResultModel> QueryEmbeddingsAsync(string collectionId, float[][] queryEmbeddings, int nResults, string[]? include = null, CancellationToken cancellationToken = default) |
| 144 | + { |
| 145 | + this._logger.LogDebug("Query embeddings in collection with id: {0}", collectionId); |
| 146 | + |
| 147 | + using var request = QueryEmbeddingsRequest.Create(collectionId, queryEmbeddings, nResults, include).Build(); |
| 148 | + |
| 149 | + (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 150 | + |
| 151 | + var queryResult = JsonSerializer.Deserialize<ChromaQueryResultModel>(responseContent); |
| 152 | + |
| 153 | + return queryResult ?? new ChromaQueryResultModel(); |
| 154 | + } |
| 155 | + |
| 156 | + #region private ================================================================================ |
| 157 | + |
| 158 | + private const string ApiRoute = "api/v1/"; |
| 159 | + |
| 160 | + private readonly ILogger _logger; |
| 161 | + private readonly HttpClient _httpClient; |
| 162 | + private readonly string? _endpoint = null; |
| 163 | + |
| 164 | + private async Task<(HttpResponseMessage response, string responseContent)> ExecuteHttpRequestAsync( |
| 165 | + HttpRequestMessage request, |
| 166 | + CancellationToken cancellationToken = default) |
| 167 | + { |
| 168 | + string endpoint = this._endpoint ?? this._httpClient.BaseAddress.ToString(); |
| 169 | + endpoint = this.SanitizeEndpoint(endpoint); |
| 170 | + |
| 171 | + string operationName = request.RequestUri.ToString(); |
| 172 | + |
| 173 | + request.RequestUri = new Uri(new Uri(endpoint), operationName); |
| 174 | + |
| 175 | + HttpResponseMessage response = await this._httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 176 | + |
| 177 | + string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + response.EnsureSuccessStatusCode(); |
| 182 | + } |
| 183 | + catch (HttpRequestException e) |
| 184 | + { |
| 185 | + this._logger.LogError(e, "{0} {1} operation failed: {2}, {3}", request.Method.Method, operationName, e.Message, responseContent); |
| 186 | + throw new ChromaClientException($"{request.Method.Method} {operationName} operation failed: {e.Message}, {responseContent}", e); |
| 187 | + } |
| 188 | + |
| 189 | + return (response, responseContent); |
| 190 | + } |
| 191 | + |
| 192 | + private string SanitizeEndpoint(string endpoint) |
| 193 | + { |
| 194 | + StringBuilder builder = new(endpoint); |
| 195 | + |
| 196 | + if (!endpoint.EndsWith("/", StringComparison.Ordinal)) |
| 197 | + { |
| 198 | + builder.Append('/'); |
| 199 | + } |
| 200 | + |
| 201 | + builder.Append(ApiRoute); |
| 202 | + |
| 203 | + return builder.ToString(); |
| 204 | + } |
| 205 | + |
| 206 | + #endregion |
| 207 | +} |
0 commit comments