-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvectorstore_upsert.go
More file actions
92 lines (72 loc) · 2.04 KB
/
Copy pathvectorstore_upsert.go
File metadata and controls
92 lines (72 loc) · 2.04 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
package vectorstore
import (
"context"
"fmt"
"github.com/RussellLuo/orchestrator"
"github.com/go-aie/llmflow"
)
const TypeVectorStoreUpsert = "vectorstore_upsert"
func init() {
MustRegisterVectorStoreUpsert(orchestrator.GlobalRegistry)
}
func MustRegisterVectorStoreUpsert(r *orchestrator.Registry) {
r.MustRegister(&orchestrator.TaskFactory{
Type: TypeVectorStoreUpsert,
New: func() orchestrator.Task { return new(VectorStoreUpsert) },
})
}
type VectorStoreUpsert struct {
orchestrator.TaskHeader
Input struct {
Vendor string `json:"vendor"`
Config *Config `json:"config"`
Vectors orchestrator.Expr[[]llmflow.Vector] `json:"vectors"`
Documents orchestrator.Expr[[]*llmflow.Document] `json:"documents"`
} `json:"input"`
store VectorStore
}
func (vs *VectorStoreUpsert) Init(r *orchestrator.Registry) error {
store, err := New(vs.Input.Vendor, vs.Input.Config)
if err != nil {
return err
}
vs.store = store
return nil
}
func (vs *VectorStoreUpsert) String() string {
return fmt.Sprintf("%s(name:%s)", vs.Type, vs.Name)
}
func (vs *VectorStoreUpsert) Execute(ctx context.Context, input orchestrator.Input) (orchestrator.Output, error) {
vectors, err := vs.Input.Vectors.EvaluateX(input)
if err != nil {
return nil, err
}
documents, err := vs.Input.Documents.EvaluateX(input)
if err != nil {
return nil, err
}
if len(vectors) != len(documents) {
return nil, fmt.Errorf("len(vectors) does not equal len(documents)")
}
// Attach vectors to documents.
for i, vector := range vectors {
documents[i].Vector = vector
}
if err := vs.store.Upsert(ctx, documents); err != nil {
return nil, err
}
return orchestrator.Output{}, nil
}
type VectorStoreUpsertBuilder struct {
task *VectorStoreUpsert
}
func NewVectorStoreUpsert(name string) *VectorStoreUpsertBuilder {
task := &VectorStoreUpsert{
TaskHeader: orchestrator.TaskHeader{
Name: name,
Type: TypeVectorStoreUpsert,
},
}
return &VectorStoreUpsertBuilder{task: task}
}
func (b *VectorStoreUpsertBuilder) Build() orchestrator.Task { return b.task }