|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Globalization; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.SemanticKernel.Memory; |
| 13 | +using Microsoft.SemanticKernel.Memory.Storage; |
| 14 | +using Qdrant.DotNet; |
| 15 | +using Qdrant.DotNet.Internal.Diagnostics; |
| 16 | + |
| 17 | +namespace Microsoft.SemanticKernel.Skills.Memory.VectorDB; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// /// An implementation of <see cref="IDataStore{TValue}"/> backed by a Qdrant Vector database. |
| 21 | +/// </summary> |
| 22 | +/// <remarks>The vector data is saved to Qdrant Vector Database run locally or in cloud, specified in the constructor. |
| 23 | +/// The data persists between subsequent instances. </remarks> |
| 24 | +/// <typeparam name="TValue">The type of data to be stored in this data store.</typeparam> |
| 25 | +public class QdrantDataStore<TValue> : IDataStore<TValue> |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Connect a Qdrant Vector database |
| 29 | + /// </summary> |
| 30 | + /// <param name="endpoint">IP Address or URL endpoint to Qdrant Vectore Database.</param> |
| 31 | + /// <param name="port">Port to be used for Qdrant Vector DB</param> |
| 32 | + public static QdrantDataStore<TValue> ConnectVectorDB(string endpoint, int port) |
| 33 | + { |
| 34 | + QdrantDb vectorDBConnection = QdrantVectorDB.CreateVectorDBConnection(endpoint, port); |
| 35 | + return new QdrantDataStore<QdrantDb>(vectorDBConnection); |
| 36 | + } |
| 37 | + |
| 38 | + /// <inheritdoc/> |
| 39 | + public IAsyncEnumerable<string> GetContainersAsync() |
| 40 | + { |
| 41 | + //Go get all collection in DB of them everytime |
| 42 | + return this._qdrantstore.Keys.ToAsyncEnumerable(); |
| 43 | + } |
| 44 | + |
| 45 | + /// <inheritdoc/> |
| 46 | + public IAsyncEnumerable<DataEntry<TValue>> GetAllAsync(string container, CancellationToken cancel = default) |
| 47 | + { |
| 48 | + |
| 49 | + IVectorDbCollection vdbCollection = this._qdrantstore.GetOrNull(container); |
| 50 | + if (vdbCollection != null) {return vdbCollection; } |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + vdbCollection = await this._qdrantdbClient.GetCollection(container); |
| 55 | + this._qdrantstore.AddOrUpdate(container, vdbCollection); |
| 56 | + } |
| 57 | + catch(VectorDBException ex) |
| 58 | + { |
| 59 | + throw new VectorDbException($"Error while getting collection {container} from Qdrant Vector DB", ex); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + /// <inheritdoc/> |
| 65 | + public Task<DataEntry<TValue>?> GetAsync(string container, string key, CancellationToken cancel = default) |
| 66 | + { |
| 67 | + IVectorDbCollection vdbCollection = this._qdrantstore.GetOrNull(container); |
| 68 | + |
| 69 | + return Task.FromResult<DataEntry<TValue>?>(null); |
| 70 | + } |
| 71 | + |
| 72 | + /// <inheritdoc/> |
| 73 | + Task<DataEntry<TValue>> PutAsync(string container, DataEntry<TValue> data, CancellationToken cancel = default){} |
| 74 | + |
| 75 | + /// <inheritdoc/> |
| 76 | + Task RemoveAsync(string container, string key, CancellationToken cancel = default); |
| 77 | + |
| 78 | + #region private ================================================================================ |
| 79 | + |
| 80 | + private readonly QdrantDb _qdrantdbClient; |
| 81 | + private readonly ConcurrentDictionary<string, IVectorDbCollection> _qdrantstore = new(); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Constructor |
| 85 | + /// </summary> |
| 86 | + /// <param name="qdrantdbClient">Qdrant VectorDB Client</param> |
| 87 | + private QdrantDataStore(QdrantDb qdrantdbClient) |
| 88 | + { |
| 89 | + this._qdrantdbClient = qdrantdbClient; |
| 90 | + } |
| 91 | + |
| 92 | + private static string? ValueToString(TValue? value) |
| 93 | + { |
| 94 | + if (value != null) |
| 95 | + { |
| 96 | + if (typeof(TValue) == typeof(string)) |
| 97 | + { |
| 98 | + return value.ToString(); |
| 99 | + } |
| 100 | + |
| 101 | + return JsonSerializer.Serialize(value); |
| 102 | + } |
| 103 | + |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + private static string? ToTimestampString(DateTimeOffset? timestamp) |
| 108 | + { |
| 109 | + return timestamp?.ToString("u", CultureInfo.InvariantCulture); |
| 110 | + } |
| 111 | + |
| 112 | + private static DateTimeOffset? ParseTimestamp(string? str) |
| 113 | + { |
| 114 | + if (!string.IsNullOrEmpty(str) |
| 115 | + && DateTimeOffset.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out DateTimeOffset timestamp)) |
| 116 | + { |
| 117 | + return timestamp; |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + #endregion |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
0 commit comments