-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
197 lines (159 loc) · 4.72 KB
/
main.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
//go:generate compogen readme --connector ./config ./README.mdx
package stabilityai
import (
"context"
_ "embed"
"fmt"
"sync"
"google.golang.org/protobuf/types/known/structpb"
"github.com/instill-ai/component/pkg/base"
"github.com/instill-ai/x/errmsg"
)
const (
host = "https://api.stability.ai"
TextToImageTask = "TASK_TEXT_TO_IMAGE"
ImageToImageTask = "TASK_IMAGE_TO_IMAGE"
cfgAPIKey = "api_key"
)
var (
//go:embed config/definition.json
definitionJSON []byte
//go:embed config/tasks.json
tasksJSON []byte
//go:embed config/stabilityai.json
stabilityaiJSON []byte
once sync.Once
con *Connector
)
// Connector executes queries against StabilityAI.
type Connector struct {
base.Connector
usageHandlerCreator base.UsageHandlerCreator
secretAPIKey string
}
// Init returns an initialized StabilityAI connector.
func Init(bc base.Connector) *Connector {
once.Do(func() {
con = &Connector{Connector: bc}
err := con.LoadConnectorDefinition(definitionJSON, tasksJSON, map[string][]byte{"stabilityai.json": stabilityaiJSON})
if err != nil {
panic(err)
}
})
return con
}
// WithSecrets loads secrets into the connector, which can be used to configure
// it with globaly defined parameters.
func (c *Connector) WithSecrets(s map[string]any) *Connector {
c.secretAPIKey = base.ReadFromSecrets(cfgAPIKey, s)
return c
}
// WithUsageHandlerCreator overrides the UsageHandlerCreator method.
func (c *Connector) WithUsageHandlerCreator(newUH base.UsageHandlerCreator) *Connector {
c.usageHandlerCreator = newUH
return c
}
// UsageHandlerCreator returns a function to initialize a UsageHandler.
func (c *Connector) UsageHandlerCreator() base.UsageHandlerCreator {
if c.usageHandlerCreator == nil {
return c.Connector.UsageHandlerCreator()
}
return c.usageHandlerCreator
}
// resolveSecrets looks for references to a global secret in the connection
// and replaces them by the global secret injected during initialization.
func (c *Connector) resolveSecrets(conn *structpb.Struct) (*structpb.Struct, bool, error) {
apiKey := conn.GetFields()[cfgAPIKey].GetStringValue()
if apiKey != base.SecretKeyword {
return conn, false, nil
}
if c.secretAPIKey == "" {
return nil, false, base.NewUnresolvedSecret(cfgAPIKey)
}
conn.GetFields()[cfgAPIKey] = structpb.NewStringValue(c.secretAPIKey)
return conn, true, nil
}
// CreateExecution initializes a connector executor that can be used in a
// pipeline trigger.
func (c *Connector) CreateExecution(sysVars map[string]any, connection *structpb.Struct, task string) (*base.ExecutionWrapper, error) {
resolvedConnection, resolved, err := c.resolveSecrets(connection)
if err != nil {
return nil, err
}
return &base.ExecutionWrapper{Execution: &execution{
ConnectorExecution: base.ConnectorExecution{
Connector: c,
SystemVariables: sysVars,
Connection: resolvedConnection,
Task: task,
},
usesSecret: resolved,
}}, nil
}
type execution struct {
base.ConnectorExecution
usesSecret bool
}
func (e *execution) UsesSecret() bool {
return e.usesSecret
}
func (e *execution) Execute(_ context.Context, inputs []*structpb.Struct) ([]*structpb.Struct, error) {
client := newClient(e.Connection, e.GetLogger())
outputs := []*structpb.Struct{}
for _, input := range inputs {
switch e.Task {
case TextToImageTask:
params, err := parseTextToImageReq(input)
if err != nil {
return inputs, err
}
resp := ImageTaskRes{}
req := client.R().SetResult(&resp).SetBody(params)
if _, err := req.Post(params.path); err != nil {
return inputs, err
}
output, err := textToImageOutput(resp)
if err != nil {
return nil, err
}
outputs = append(outputs, output)
case ImageToImageTask:
params, err := parseImageToImageReq(input)
if err != nil {
return inputs, err
}
data, ct, err := params.getBytes()
if err != nil {
return inputs, err
}
resp := ImageTaskRes{}
req := client.R().SetBody(data).SetResult(&resp).SetHeader("Content-Type", ct)
if _, err := req.Post(params.path); err != nil {
return inputs, err
}
output, err := imageToImageOutput(resp)
if err != nil {
return nil, err
}
outputs = append(outputs, output)
default:
return nil, errmsg.AddMessage(
fmt.Errorf("not supported task: %s", e.Task),
fmt.Sprintf("%s task is not supported.", e.Task),
)
}
}
return outputs, nil
}
// Test checks the connector state.
func (c *Connector) Test(sysVars map[string]any, connection *structpb.Struct) error {
var engines []Engine
req := newClient(connection, c.Logger).R().SetResult(&engines)
if _, err := req.Get(listEnginesPath); err != nil {
return err
}
if len(engines) == 0 {
return fmt.Errorf("no engines")
}
return nil
}