1313using System . Linq ;
1414using Qdrant . DotNet . Internal ;
1515
16- namespace Microsoft . SemanticKernel . Skills . Memory . VectorDB ;
16+
17+ namespace Microsoft . SemanticKernel . Connectors . VectorDB ;
1718
1819/// <summary>
1920/// An implementation of <see cref="ILongTermMemoryStore{TEmbedding}"/> which is an embedding of type float
@@ -35,114 +36,137 @@ public async IAsyncEnumerable<string> GetCollectionsAsync(CancellationToken canc
3536 yield return null ;
3637 }
3738
39+
40+
3841 public async Task < DataEntry < VectorRecordData < float > > ? > GetAsync ( string collection , string key , CancellationToken cancel = default )
3942 {
4043 DataEntry < VectorRecordData < float > > ? vectorresult = null ;
41-
42- if ( this . _qdrantdata . ContainsKey ( collection ) && this . _qdrantdata [ collection ] . ContainsKey ( key ) )
44+
45+ if ( this . _qdrantdata . ContainsKey ( collection ) && this . _qdrantdata [ collection ] . ContainsKey ( key ) )
4346 {
4447 vectorresult = this . _qdrantdata [ collection ] [ key ] ;
4548 }
4649 else
4750 {
4851 try
4952 {
50- var vectordata = await this . _qdrantclient . GetVectorDataAsync ( collection , key ) ;
53+ var vectordata = await this . _qdrantclient . GetVectorDataAsync ( collection , key ) ;
5154 if ( vectordata != null )
5255 {
5356 if ( ! this . _qdrantdata . ContainsKey ( collection ) )
5457 {
5558 this . _qdrantdata . TryAdd ( collection , new ConcurrentDictionary < string , DataEntry < VectorRecordData < float > > > ( ) ) ;
56- this . _qdrantdata [ collection ] . TryAdd ( key , vectordata ) ;
59+ this . _qdrantdata [ collection ] . TryAdd ( key , vectordata ) ;
5760 }
5861 else
5962 {
60- this . _qdrantdata [ collection ] . TryAdd ( key , vectordata ) ;
63+ this . _qdrantdata [ collection ] . TryAdd ( key , vectordata ) ;
6164 }
6265
6366 vectorresult = vectordata ;
6467 }
6568 }
66- catch ( Exception ex )
69+ catch ( Exception ex )
6770 {
6871 throw new VectorDbException ( $ "Failed to get vector data from Qdrant { ex . Message } ") ;
6972 }
7073 }
71-
74+
7275 return vectorresult ;
7376 }
7477
75- public async Task < DataEntry < VectorRecordData < float > > > PutAsync ( string collection , DataEntry < VectorRecordData < float > > data ,
76- CancellationToken cancel = default )
78+ public async Task < DataEntry < VectorRecordData < float > > > PutAsync ( string collection , DataEntry < VectorRecordData < float > > data , CancellationToken cancel = default )
7779 {
7880 DataEntry < VectorRecordData < float > > vectordata ; //= new DataEntry<VectorRecordData<float>>(data.Key, data.Value);
79- bool result = await this . _qdrantclient . PutVectorDataAsync < DataEntry < VectorRecordData < float > > > ( collection , data . Key , data , data . Value . Payload ) ;
80- if ( ! result )
81- {
81+ bool result = await this . _qdrantclient . PutVectorDataAsync < DataEntry < VectorRecordData < float > > > ( collection , data . Key , data , data . Value . Payload ) ;
82+ if ( ! result )
83+ {
8284 throw new VectorDbException ( "Failed to put vector data into Qdrant" ) ;
8385 }
84-
85- vectordata = await this . _qdrantclient . GetVectorDataAsync ( collection , data . Key ) ;
86-
87- if ( vectordata == null )
88- {
86+
87+ vectordata = await this . _qdrantclient . GetVectorDataAsync ( collection , data . Key ) ;
88+
89+ if ( vectordata == null )
90+ {
8991 throw new VectorDbException ( "Failed to put and retrieve vector data from Qdrant" ) ;
9092 }
91-
9293 return vectordata ;
94+
9395 }
9496
9597 public Task RemoveAsync ( string collection , string key , CancellationToken cancel = default )
9698 {
97- bool result ;
99+ bool result ;
98100 try
99101 {
100102 result = this . _qdrantclient . DeleteVectorDataAsync ( collection , key ) . Result ;
101103 if ( result )
102104 {
103- if ( this . _qdrantdata . ContainsKey ( collection ) && this . _qdrantdata [ collection ] . ContainsKey ( key ) )
105+ if ( this . _qdrantdata . ContainsKey ( collection ) && this . _qdrantdata [ collection ] . ContainsKey ( key ) )
104106 {
105107 this . _qdrantdata [ collection ] . TryRemove ( key , out _ ) ;
106108 }
107109 }
108110 }
109- catch ( Exception ex )
111+ catch ( Exception ex )
110112 {
111113 throw new VectorDbException ( $ "Failed to remove vector data from Qdrant { ex . Message } ") ;
112114 }
113-
114115 return Task . CompletedTask ;
115116 }
116117
117- public IAsyncEnumerable < ( VectorRecordData < float > , double ) > GetNearestMatchesAsync ( string collection , Embedding < float > embedding , int limit = 1 ,
118- double minRelevanceScore = 0 )
118+ public async IAsyncEnumerable < ( VectorRecordData < float > , double ) > GetNearestMatchesAsync ( string collection , Embedding < float > embedding , int limit = 1 , double minRelevanceScore = 0 )
119119 {
120- throw new System . NotImplementedException ( ) ;
120+
121+ DataEntry < VectorRecordData < float > > vectordata = new DataEntry < VectorRecordData < float > > ( )
122+ {
123+ Value = new VectorRecordData < float > ( embedding , new Dictionary < string , object > ( ) , new List < string > ( ) ) ,
124+ Timestamp = DateTime . Now ,
125+ } ;
126+
127+
128+ var result = await this . _qdrantclient . SearchVectorBySimilarityAsync ( collection , vectordata , minRelevanceScore ) ;
129+ await foreach ( var rd in result . ToAsyncEnumerable ( ) )
130+ {
131+ yield return ( rd . Key . Value ! , rd . Value ) ;
132+ }
121133 }
122134
123- IAsyncEnumerable < ( IEmbeddingWithMetadata < float > , double ) > IEmbeddingIndex < float > . GetNearestMatchesAsync ( string collection , Embedding < float > embedding ,
124- int limit , double minRelevanceScore )
135+ async IAsyncEnumerable < ( IEmbeddingWithMetadata < float > , double ) > IEmbeddingIndex < float > . GetNearestMatchesAsync ( string collection , Embedding < float > embedding , int limit , double minRelevanceScore )
125136 {
126- throw new System . NotImplementedException ( ) ;
137+ DataEntry < VectorRecordData < float > > vectordata = new DataEntry < VectorRecordData < float > > ( )
138+ {
139+ Value = new VectorRecordData < float > ( embedding , new Dictionary < string , object > ( ) , new List < string > ( ) ) ,
140+ Timestamp = DateTime . Now ,
141+ } ;
142+ var result = await this . _qdrantclient . SearchVectorBySimilarityAsync ( collection , vectordata , minRelevanceScore ) ;
143+ await foreach ( var match in result . ToAsyncEnumerable ( ) )
144+ {
145+ yield return ( match . Key . Value ! , match . Value ) ;
146+ }
147+
127148 }
128149
129150 public IAsyncEnumerable < DataEntry < VectorRecordData < float > > > GetAllAsync ( string collection , CancellationToken cancel = default )
130151 {
152+
131153 IAsyncEnumerable < DataEntry < VectorRecordData < float > > > vectorresult ;
132- IVectorDbCollection vectorCollection = this . _qdrantclient . GetCollectionDataAsync ( collection ) . Result ;
154+ IVectorDbCollection vectorCollection = this . _qdrantclient . GetCollectionDataAsync ( collection ) . Result ;
133155
134156 vectorresult = vectorCollection . GetAllVectorsAsync ( ) . Result ! ;
135-
157+
136158 return vectorresult ! ;
159+
137160 }
161+
138162
139- #region private ================================================================================
140163
164+ #region private ================================================================================
141165 private readonly ConcurrentDictionary < string , IVectorDbCollection > _qdrantcollection = new ( ) ;
142166 private readonly ConcurrentDictionary < string , ConcurrentDictionary < string , DataEntry < VectorRecordData < float > > > > _qdrantdata = new ( ) ;
143167 private QdrantVectorDb _qdrantclient ;
144168 private readonly int _defaultvectorsize = 1536 ;
145169 private readonly VectorDistanceType _defaultdistancetype = VectorDistanceType . Cosine ;
146-
147170 #endregion
171+
148172}
0 commit comments