-
Notifications
You must be signed in to change notification settings - Fork 24
/
agency.go
53 lines (44 loc) · 1.3 KB
/
agency.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
package agency
import (
"context"
"fmt"
)
// Operation is basic building block.
type Operation struct {
handler OperationHandler
config *OperationConfig
}
// OperationHandler is a function that implements operation's logic.
// It could be thought of as an interface that providers must implement.
type OperationHandler func(context.Context, Message, *OperationConfig) (Message, error)
// OperationConfig represents abstract operation configuration for all possible models.
type OperationConfig struct {
Prompt string
Messages []Message
}
func (p *Operation) Config() *OperationConfig {
return p.config
}
// NewOperation allows to create an operation from a function.
func NewOperation(handler OperationHandler) *Operation {
return &Operation{
handler: handler,
config: &OperationConfig{},
}
}
// Execute executes operation handler with input message and current configuration.
func (p *Operation) Execute(ctx context.Context, input Message) (Message, error) {
output, err := p.handler(ctx, input, p.config)
if err != nil {
return Message{}, err
}
return output, nil
}
func (p *Operation) SetPrompt(prompt string, args ...any) *Operation {
p.config.Prompt = fmt.Sprintf(prompt, args...)
return p
}
func (p *Operation) SetMessages(msgs []Message) *Operation {
p.config.Messages = msgs
return p
}