-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathai_builder.go
More file actions
71 lines (63 loc) · 1.6 KB
/
Copy pathai_builder.go
File metadata and controls
71 lines (63 loc) · 1.6 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
package bootstrap
import (
"context"
"fmt"
v1 "github.com/tx7do/go-wind-bootstrap/conf/gen/go/bootstrap/v1"
)
// resolveAi 检查 Ai 配置中每个 optional 字段,
// 对已设置的 AI 模型类型分别调用对应 builder。
// 返回按类型名索引的 AI 客户端实例映射和统一的 cleanup 函数。
func resolveAi(ctx context.Context, cfg *v1.Ai) (map[string]any, func(), error) {
type field struct {
name string
builder AiBuilder
}
var fields []field
if cfg.GetOpenai() != nil {
b, err := getAiBuilder(AiTypeOpenAI)
if err != nil {
return nil, nil, err
}
fields = append(fields, field{name: AiTypeOpenAI, builder: b})
}
if cfg.GetLangchaingo() != nil {
b, err := getAiBuilder(AiTypeLangChainGo)
if err != nil {
return nil, nil, err
}
fields = append(fields, field{name: AiTypeLangChainGo, builder: b})
}
if cfg.GetEino() != nil {
b, err := getAiBuilder(AiTypeEino)
if err != nil {
return nil, nil, err
}
fields = append(fields, field{name: AiTypeEino, builder: b})
}
if len(fields) == 0 {
return nil, nil, fmt.Errorf("bootstrap: no ai specified")
}
instances := make(map[string]any)
var cleanups []func()
for _, f := range fields {
inst, cleanup, err := f.builder(ctx, cfg)
if err != nil {
for _, c := range cleanups {
c()
}
return nil, nil, fmt.Errorf("bootstrap: build ai %q: %w", f.name, err)
}
if inst != nil {
instances[f.name] = inst
}
if cleanup != nil {
cleanups = append(cleanups, cleanup)
}
}
finalCleanup := func() {
for _, c := range cleanups {
c()
}
}
return instances, finalCleanup, nil
}