-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorCollection.cs
More file actions
226 lines (183 loc) · 6.02 KB
/
Copy pathVectorCollection.cs
File metadata and controls
226 lines (183 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System.Collections;
using System.Linq.Expressions;
using Google.Protobuf;
using Google.Protobuf.Collections;
using Jigen.Client.BaseTypes;
using Jigen.Client.Filtering;
using Jigen.Proto;
namespace Jigen.Client;
public class VectorCollection<T>(Context store, VectorCollectionOptions<T> options = null) :
IDictionary<VectorKey, VectorEntry<T>>
where T : class, new()
{
private readonly VectorCollectionOptions<T> _options = options ?? new VectorCollectionOptions<T>();
public IEnumerator<KeyValuePair<VectorKey, VectorEntry<T>>> GetEnumerator()
{
foreach (var k in Keys)
{
if (TryGetValue(k, out var value))
yield return new KeyValuePair<VectorKey, VectorEntry<T>>(k, value);
}
yield break;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<VectorKey, VectorEntry<T>> item)
{
Add(item.Key, item.Value);
}
public void Clear()
{
store.ServiceClient.Clear(CollectionKey);
}
public bool Contains(KeyValuePair<VectorKey, VectorEntry<T>> item)
{
return ContainsKey(item.Key);
}
public void CopyTo(KeyValuePair<VectorKey, VectorEntry<T>>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<VectorKey, VectorEntry<T>> item)
{
return Remove(item.Key);
}
public int Count =>
store.ServiceClient.Count(CollectionKey).Count;
public bool IsReadOnly { get; } = false;
public void Add(VectorKey key, VectorEntry<T> value)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(key.Value);
ArgumentNullException.ThrowIfNull(value.Content);
var v = new Vector()
{
Database = GetDatabaseName(),
Collection = GetCollectionName(),
Key = ByteString.CopyFrom(key.Value),
Content = ByteString.CopyFrom(_options.DocumentSerializer.Serialize(value.Content).Span),
};
if (value.Embedding != null)
v.Embeddings.AddRange(value.Embedding);
store.ServiceClient.SetVector(v);
}
public void Add(VectorKey key, T content, string sentence)
{
ArgumentNullException.ThrowIfNull(key.Value);
ArgumentNullException.ThrowIfNull(sentence);
ArgumentNullException.ThrowIfNull(content);
store.ServiceClient.SetDocument(new Document()
{
Database = GetDatabaseName(),
Collection = GetCollectionName(),
Key = ByteString.CopyFrom(key.Value),
Content = ByteString.CopyFrom(_options.DocumentSerializer.Serialize(content).Span),
Sentence = sentence
});
}
public void Add(VectorKey key, T content, float[] embeddings)
{
this.Add(key, new VectorEntry<T>()
{
Content = content, Embedding = embeddings
});
}
public bool ContainsKey(VectorKey key)
{
return store.ServiceClient.Contains(ToItemKey(key)).Success;
}
public List<VectorSearchResult<T>> Search(float[] embeddings, int top = 10)
{
if (embeddings == null || embeddings.Length == 0)
return [];
var request = new SearchVectorRequest
{
Database = GetDatabaseName(),
Collection = GetCollectionName(),
Top = top
};
request.Embeddings.AddRange(embeddings);
var result = store.ServiceClient.SearchVector(request);
return result.Results.Select(i => new VectorSearchResult<T>
{
Key = i.Key.ToByteArray(),
Content = _options.DocumentSerializer.Deserialize<T>(i.Content.Memory),
Score = i.Score
}).ToList();
}
public List<VectorSearchResult<T>> Search(string sentence, int top = 10)
{
return Search(sentence, predicate: null, top: top);
}
public List<VectorSearchResult<T>> Search(string sentence, Expression<Func<T, bool>> predicate, int top = 10)
{
if (string.IsNullOrWhiteSpace(sentence))
return [];
var request = new SearchDocumentRequest
{
Database = GetDatabaseName(),
Collection = GetCollectionName(),
Sentence = sentence,
Top = top
};
if (predicate != null)
request.Filter = ProtoExpressionTranslator.Translate(predicate);
var result = store.ServiceClient.SearchDocument(request);
return result.Results.Select(i => new VectorSearchResult<T>
{
Key = i.Key.ToByteArray(),
Content = _options.DocumentSerializer.Deserialize<T>(i.Content.Memory),
Score = i.Score
}).ToList();
}
public bool Remove(VectorKey key)
{
return store.ServiceClient.DeleteVector(ToItemKey(key)).Success;
}
public bool TryGetValue(VectorKey key, out VectorEntry<T> value)
{
value = null;
var result = store.ServiceClient.GetContent(ToItemKey(key));
if (!result.Content.IsEmpty)
value = new VectorEntry<T>()
{
Key = key,
Content = _options.DocumentSerializer.Deserialize<T>(result.Content.Memory)
};
return !result.Content.IsEmpty;
}
public VectorEntry<T> this[VectorKey key]
{
get => TryGetValue(key, out var result) ? result : null;
set => Add(key, value);
}
public ICollection<VectorKey> Keys => store.ServiceClient.GetAllKeys(CollectionKey).Keys.Select(k => (VectorKey)k.Span).ToList();
public ICollection<VectorEntry<T>> Values =>
store.ServiceClient.GetAllKeys(CollectionKey).Keys.Select(k => TryGetValue(k.Span, out var value) ? value : null).ToList();
private ItemKey ToItemKey(VectorKey key) => new()
{
Database = GetDatabaseName(),
Collection = GetCollectionName(),
Key = ByteString.CopyFrom(key.Value)
};
private CollectionKey CollectionKey => new()
{
Database = GetDatabaseName(), Collection = GetCollectionName()
};
private string GetDatabaseName()
{
if (string.IsNullOrWhiteSpace(store.Options.DatabaseName))
throw new InvalidOperationException(
"ConnectionOptions.DatabaseName is required and cannot be null or empty.");
return store.Options.DatabaseName;
}
private string GetCollectionName()
{
if (string.IsNullOrWhiteSpace(_options.Name))
throw new InvalidOperationException(
"VectorCollectionOptions.Name is required and cannot be null or empty.");
return _options.Name;
}
}