-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorstore_query.go
89 lines (70 loc) · 1.99 KB
/
vectorstore_query.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
package vectorstore
import (
"context"
"fmt"
"github.com/RussellLuo/orchestrator"
"github.com/go-aie/llmflow"
)
const TypeVectorStoreQuery = "vectorstore_query"
func init() {
MustRegisterVectorStoreQuery(orchestrator.GlobalRegistry)
}
func MustRegisterVectorStoreQuery(r *orchestrator.Registry) {
r.MustRegister(&orchestrator.TaskFactory{
Type: TypeVectorStoreQuery,
New: func() orchestrator.Task { return new(VectorStoreQuery) },
})
}
type VectorStoreQuery struct {
orchestrator.TaskHeader
Input struct {
Vendor string `json:"vendor"`
Config *Config `json:"config"`
Vector orchestrator.Expr[llmflow.Vector] `json:"vector"`
TopK int `json:"top_k"`
MinScore float64 `json:"min_score"`
} `json:"input"`
store VectorStore
}
func (vs *VectorStoreQuery) Init(r *orchestrator.Registry) error {
if vs.Input.TopK == 0 {
vs.Input.TopK = 3 // defaults to 3
}
store, err := New(vs.Input.Vendor, vs.Input.Config)
if err != nil {
return err
}
vs.store = store
return nil
}
func (vs *VectorStoreQuery) String() string {
return fmt.Sprintf("%s(name:%s)", vs.Type, vs.Name)
}
func (vs *VectorStoreQuery) Execute(ctx context.Context, input orchestrator.Input) (orchestrator.Output, error) {
vector, err := vs.Input.Vector.EvaluateX(input)
if err != nil {
return nil, err
}
similarities, err := vs.store.Query(ctx, vector, vs.Input.TopK, vs.Input.MinScore)
if err != nil {
return nil, err
}
sims, err := orchestrator.DefaultCodec.Encode(similarities)
if err != nil {
return nil, err
}
return orchestrator.Output{"similarities": sims}, nil
}
type VectorStoreQueryBuilder struct {
task *VectorStoreQuery
}
func NewVectorStoreQuery(name string) *VectorStoreQueryBuilder {
task := &VectorStoreQuery{
TaskHeader: orchestrator.TaskHeader{
Name: name,
Type: TypeVectorStoreQuery,
},
}
return &VectorStoreQueryBuilder{task: task}
}
func (b *VectorStoreQueryBuilder) Build() orchestrator.Task { return b.task }