forked from gravitational/teleport
/
embeddingprocessor.go
331 lines (285 loc) · 10.3 KB
/
embeddingprocessor.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ai
import (
"context"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
embeddingpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/embedding/v1"
"github.com/gravitational/teleport/api/internalutils/stream"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/utils/retryutils"
embeddinglib "github.com/gravitational/teleport/lib/ai/embedding"
"github.com/gravitational/teleport/lib/services"
streamutils "github.com/gravitational/teleport/lib/utils/stream"
)
// maxEmbeddingAPISize is the maximum number of entities that can be embedded in a single API call.
const maxEmbeddingAPISize = 1000
// Embeddings implements the minimal interface used by the Embedding processor.
type Embeddings interface {
// GetAllEmbeddings returns all embeddings.
GetAllEmbeddings(ctx context.Context) stream.Stream[*embeddinglib.Embedding]
// UpsertEmbedding creates or update a single ai.Embedding in the backend.
UpsertEmbedding(ctx context.Context, embedding *embeddinglib.Embedding) (*embeddinglib.Embedding, error)
}
// MarshalEmbedding marshals the ai.Embedding resource to binary ProtoBuf.
func MarshalEmbedding(embedding *embeddinglib.Embedding) ([]byte, error) {
data, err := proto.Marshal((*embeddingpb.Embedding)(embedding))
if err != nil {
return nil, trace.Wrap(err)
}
return data, nil
}
// UnmarshalEmbedding unmarshals binary ProtoBuf into an ai.Embedding resource.
func UnmarshalEmbedding(bytes []byte) (*embeddinglib.Embedding, error) {
if len(bytes) == 0 {
return nil, trace.BadParameter("missing embedding data")
}
var embedding embeddingpb.Embedding
err := proto.Unmarshal(bytes, &embedding)
if err != nil {
return nil, trace.Wrap(err)
}
return (*embeddinglib.Embedding)(&embedding), nil
}
// EmbeddingHashMatches returns true if the hash of the embedding matches the
// given hash.
func EmbeddingHashMatches(embedding *embeddinglib.Embedding, hash embeddinglib.Sha256Hash) bool {
if len(embedding.EmbeddedHash) != 32 {
return false
}
return *(*embeddinglib.Sha256Hash)(embedding.EmbeddedHash) == hash
}
// BatchReducer is a helper that processes data in batches.
type BatchReducer[T, V any] struct {
data []T
batchSize int
processFn func(ctx context.Context, data []T) (V, error)
}
// NewBatchReducer is a BatchReducer constructor.
func NewBatchReducer[T, V any](processFn func(ctx context.Context, data []T) (V, error), batchSize int) *BatchReducer[T, V] {
return &BatchReducer[T, V]{
data: make([]T, 0),
batchSize: batchSize,
processFn: processFn,
}
}
// Add adds a new item to the batch. If the batch is full, it will be processed
// and the result will be returned. Otherwise, a zero value will be returned.
// Finalize must be called to process the remaining data in the batch.
func (b *BatchReducer[T, V]) Add(ctx context.Context, data T) (V, error) {
b.data = append(b.data, data)
if len(b.data) >= b.batchSize {
val, err := b.processFn(ctx, b.data)
b.data = b.data[:0]
return val, trace.Wrap(err)
}
var def V
return def, nil
}
// Finalize processes the remaining data in the batch and returns the result.
func (b *BatchReducer[T, V]) Finalize(ctx context.Context) (V, error) {
if len(b.data) > 0 {
val, err := b.processFn(ctx, b.data)
b.data = b.data[:0]
return val, trace.Wrap(err)
}
var def V
return def, nil
}
// EmbeddingProcessorConfig is the configuration for EmbeddingProcessor.
type EmbeddingProcessorConfig struct {
AIClient embeddinglib.Embedder
EmbeddingSrv Embeddings
EmbeddingsRetriever *SimpleRetriever
NodeSrv *services.UnifiedResourceCache
Log logrus.FieldLogger
Jitter retryutils.Jitter
}
// EmbeddingProcessor is responsible for processing nodes, generating embeddings
// and storing their embeddings in the backend.
type EmbeddingProcessor struct {
aiClient embeddinglib.Embedder
embeddingSrv Embeddings
embeddingsRetriever *SimpleRetriever
nodeSrv *services.UnifiedResourceCache
log logrus.FieldLogger
jitter retryutils.Jitter
}
// NewEmbeddingProcessor returns a new EmbeddingProcessor.
func NewEmbeddingProcessor(cfg *EmbeddingProcessorConfig) *EmbeddingProcessor {
return &EmbeddingProcessor{
aiClient: cfg.AIClient,
embeddingSrv: cfg.EmbeddingSrv,
embeddingsRetriever: cfg.EmbeddingsRetriever,
nodeSrv: cfg.NodeSrv,
log: cfg.Log,
jitter: cfg.Jitter,
}
}
// resourceStringPair is a helper struct that pairs a resource with a data string.
type resourceStringPair struct {
resource types.Resource
data string
}
// mapProcessFn is a helper function that maps a slice of resourceStringPair,
// compute embeddings and return them as a slice.
func (e *EmbeddingProcessor) mapProcessFn(ctx context.Context, data []*resourceStringPair) ([]*embeddinglib.Embedding, error) {
dataBatch := make([]string, 0, len(data))
for _, pair := range data {
dataBatch = append(dataBatch, pair.data)
}
embeddings, err := e.aiClient.ComputeEmbeddings(ctx, dataBatch)
if err != nil {
return nil, trace.Wrap(err)
}
results := make([]*embeddinglib.Embedding, 0, len(embeddings))
for i, embedding := range embeddings {
emb := embeddinglib.NewEmbedding(data[i].resource.GetKind(),
data[i].resource.GetName(), embedding,
embeddinglib.EmbeddingHash([]byte(data[i].data)),
)
results = append(results, emb)
}
return results, nil
}
// Run runs the EmbeddingProcessor.
func (e *EmbeddingProcessor) Run(ctx context.Context, initialDelay, period time.Duration) error {
initTimer := time.NewTimer(initialDelay)
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-initTimer.C:
// Stop the timer after the initial delay.
initTimer.Stop()
e.process(ctx)
case <-time.After(e.jitter(period)):
e.process(ctx)
}
}
}
// process updates embeddings for all resources once.
func (e *EmbeddingProcessor) process(ctx context.Context) {
batch := NewBatchReducer(e.mapProcessFn,
maxEmbeddingAPISize, // Max batch size allowed by OpenAI API,
)
e.log.Debugf("embedding processor started")
defer e.log.Debugf("embedding processor finished")
embeddingsStream := e.embeddingSrv.GetAllEmbeddings(ctx)
unifiedResources, err := e.nodeSrv.GetUnifiedResources(ctx)
if err != nil {
e.log.Debugf("embedding processor failed with error: %v", err)
return
}
resources := make([]types.Resource, len(unifiedResources))
for i, unifiedResource := range unifiedResources {
resources[i] = unifiedResource
unifiedResources[i] = nil
}
resourceStream := stream.Slice(resources)
s := streamutils.NewZipStreams(
resourceStream,
embeddingsStream,
// On new resource callback. Add the resource to the batch.
func(resource types.Resource) error {
resourceData, err := embeddinglib.SerializeResource(resource)
if err != nil {
return trace.Wrap(err)
}
vectors, err := batch.Add(ctx, &resourceStringPair{resource, string(resourceData)})
if err != nil {
return trace.Wrap(err)
}
if err := e.upsertEmbeddings(ctx, vectors); err != nil {
return trace.Wrap(err)
}
return nil
},
// On equal resource callback. Check if the resource's embedding hash matches
// the one in the backend. If not, add the resource to the batch.
func(resource types.Resource, embedding *embeddinglib.Embedding) error {
resourceData, err := embeddinglib.SerializeResource(resource)
if err != nil {
return trace.Wrap(err)
}
resourceHash := embeddinglib.EmbeddingHash(resourceData)
if !EmbeddingHashMatches(embedding, resourceHash) {
vectors, err := batch.Add(ctx, &resourceStringPair{resource, string(resourceData)})
if err != nil {
return trace.Wrap(err)
}
if err := e.upsertEmbeddings(ctx, vectors); err != nil {
return trace.Wrap(err)
}
}
return nil
},
// On compare keys callback. Compare the keys for iteration.
func(resource types.Resource, embeddings *embeddinglib.Embedding) int {
return strings.Compare(resource.GetName(), embeddings.GetEmbeddedID())
},
)
if err := s.Process(); err != nil {
e.log.Warnf("Failed to generate nodes embedding: %v", err)
}
// Process the remaining resources in the batch
vectors, err := batch.Finalize(ctx)
if err != nil {
e.log.Warnf("Failed to add node to batch: %v", err)
return
}
if err := e.upsertEmbeddings(ctx, vectors); err != nil {
e.log.Warnf("Failed to upsert embeddings: %v", err)
}
if err := e.updateMemIndex(ctx); err != nil {
e.log.Warnf("Failed to update memory index: %v", err)
}
}
// updateMemIndex is a helper function that updates the in-memory index with the
// latest embeddings. The new index is created and then swapped with the old one.
func (e *EmbeddingProcessor) updateMemIndex(ctx context.Context) error {
embeddingsIndex := NewSimpleRetriever()
embeddingsStream := e.embeddingSrv.GetAllEmbeddings(ctx)
for embeddingsStream.Next() {
embedding := embeddingsStream.Item()
if !embeddingsIndex.Insert(embedding.GetEmbeddedID(), embedding) {
e.log.Warnf("Embeddings index is full, some resources can be missing")
break
}
}
if err := embeddingsStream.Done(); err != nil {
return trace.Wrap(err)
}
e.embeddingsRetriever.Swap(embeddingsIndex)
return nil
}
// upsertEmbeddings is a helper function that upserts the embeddings into the backend.
func (e *EmbeddingProcessor) upsertEmbeddings(ctx context.Context, rawEmbeddings []*embeddinglib.Embedding) error {
// Store the new embeddings into the backend
for _, embedding := range rawEmbeddings {
_, err := e.embeddingSrv.UpsertEmbedding(ctx, embedding)
if err != nil {
return trace.Wrap(err)
}
}
return nil
}