-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PostgresVectorCollection.cs
126 lines (114 loc) · 4.02 KB
/
PostgresVectorCollection.cs
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
namespace LangChain.Databases.Postgres;
/// <summary>
/// Postgres vector store (using <see href="https://github.com/pgvector/pgvector"/>)
/// <remarks>
/// required: CREATE EXTENSION IF NOT EXISTS vector
/// </remarks>
/// </summary>
public class PostgresVectorCollection(
PostgresDbClient client,
string name = VectorCollection.DefaultName,
string? id = null)
: VectorCollection(name, id), IVectorCollection
{
/// <inheritdoc />
public async Task<IReadOnlyCollection<string>> AddAsync(
IReadOnlyCollection<Vector> items,
CancellationToken cancellationToken = default)
{
items = items ?? throw new ArgumentNullException(nameof(items));
foreach (var item in items)
{
await client.UpsertAsync(
tableName: Name,
id: item.Id,
content: item.Text,
metadata: item.Metadata,
embedding: item.Embedding,
timestamp: DateTime.UtcNow,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}
return items
.Select(i => i.Id)
.ToArray();
}
/// <inheritdoc />
public async Task<Vector?> GetAsync(string id, CancellationToken cancellationToken = default)
{
var record = await client.GetRecordByIdAsync(Name, id, withEmbeddings: false, cancellationToken).ConfigureAwait(false);
return record != null
? new Vector
{
Text = record.Content,
Metadata = record.Metadata,
}
: null;
}
/// <inheritdoc />
public async Task<bool> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default)
{
await client
.DeleteBatchAsync(Name, ids.ToList(), cancellationToken)
.ConfigureAwait(false);
return true;
}
/// <inheritdoc />
public async Task<VectorSearchResponse> SearchAsync(
VectorSearchRequest request,
VectorSearchSettings? settings = default,
CancellationToken cancellationToken = default)
{
request = request ?? throw new ArgumentNullException(nameof(request));
settings ??= new VectorSearchSettings();
var records = await client
.GetWithDistanceAsync(
Name,
request.Embeddings.First(),
settings.DistanceStrategy,
limit: settings.NumberOfResults,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
// MMR
// TODO: implement maximal_marginal_relevance method or call python?
// var embeddingArray = embedding.ToArray();
// var results = await _postgresDbClient
// .GetWithDistanceAsync(
// _collectionName,
// embeddingArray,
// _distanceStrategy,
// limit: k,
// withEmbeddings: true,
// cancellationToken: cancellationToken)
// .ConfigureAwait(false);
//
// var mmrSelected = maximal_marginal_relevance(
// embeddingArray,
// results.Select(r => r.Item1.Embedding!),
// k: k,
// lambdaMult: lambdaMult);
//
// var resultDocs = results
// .Select((r, i) => (Record: r.Item1, Index: i))
// .Where(v => mmrSelected.Contains(v.Index))
// .Select(v => new Document(v.Record.Content, v.Record.Metadata));
//
// return resultDocs;
return new VectorSearchResponse
{
Items = records
.Select(r => new Vector
{
Text = r.Item1.Content,
Metadata = r.Item1.Metadata,
Distance = r.Item2,
})
.ToArray(),
};
}
/// <inheritdoc />
public Task<bool> IsEmptyAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}