Skip to content

Commit

Permalink
fix: migrate to the new usage handler architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
namwoam committed Jul 8, 2024
1 parent 159c670 commit 5b0a01b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ai/cohere/v0/config/setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"string"
],
"instillSecret": true,
"instillCredential": false,
"instillCredential": true,
"instillUIOrder": 0,
"title": "API Key",
"type": "string"
Expand Down
45 changes: 41 additions & 4 deletions ai/cohere/v0/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (

type component struct {
base.Component
instillAPIKey string
}

type CohereClient interface {
Expand All @@ -54,14 +55,28 @@ func Init(bc base.Component) *component {

type execution struct {
base.ComponentExecution
execute func(*structpb.Struct) (*structpb.Struct, error)
client CohereClient
execute func(*structpb.Struct) (*structpb.Struct, error)
client CohereClient
usesInstillCredentials bool
}

// WithInstillCredentials loads Instill credentials into the component, which
// can be used to configure it with globally defined parameters instead of with
// user-defined credential values.
func (c *component) WithInstillCredentials(s map[string]any) *component {
c.instillAPIKey = base.ReadFromGlobalConfig(cfgAPIKey, s)
return c
}

func (c *component) CreateExecution(sysVars map[string]any, setup *structpb.Struct, task string) (*base.ExecutionWrapper, error) {
resolvedSetup, resolved, err := c.resolveSetup(setup)
if err != nil {
return nil, err
}
e := &execution{
ComponentExecution: base.ComponentExecution{Component: c, SystemVariables: sysVars, Task: task, Setup: setup},
client: newClient(getAPIKey(setup), c.GetLogger()),
ComponentExecution: base.ComponentExecution{Component: c, SystemVariables: sysVars, Task: task, Setup: resolvedSetup},
client: newClient(getAPIKey(setup), c.GetLogger()),
usesInstillCredentials: resolved,
}
switch task {
case TextGenerationTask:
Expand All @@ -75,6 +90,28 @@ func (c *component) CreateExecution(sysVars map[string]any, setup *structpb.Stru
}
return &base.ExecutionWrapper{Execution: e}, nil
}

// resolveSetup checks whether the component is configured to use the Instill
// credentials injected during initialization and, if so, returns a new setup
// with the secret credential values.
func (c *component) resolveSetup(setup *structpb.Struct) (*structpb.Struct, bool, error) {
apiKey := setup.GetFields()[cfgAPIKey].GetStringValue()
if apiKey != base.SecretKeyword {
return setup, false, nil
}

if c.instillAPIKey == "" {
return nil, false, base.NewUnresolvedCredential(cfgAPIKey)
}

setup.GetFields()[cfgAPIKey] = structpb.NewStringValue(c.instillAPIKey)
return setup, true, nil
}

func (e *execution) UsesInstillCredentials() bool {
return e.usesInstillCredentials
}

func (e *execution) Execute(_ context.Context, inputs []*structpb.Struct) ([]*structpb.Struct, error) {
outputs := make([]*structpb.Struct, len(inputs))

Expand Down

0 comments on commit 5b0a01b

Please sign in to comment.