-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
86 lines (68 loc) · 1.68 KB
/
agent.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
package agent
import (
"context"
)
type Agent struct {
completionFunc CompletionFunc
messages []*Message
}
type Option func(a *Agent)
func WithMiddleware(m MiddlewareFunc) Option {
return func(a *Agent) {
a.completionFunc = m(a.completionFunc)
}
}
func New(c CompletionFunc, opts ...Option) *Agent {
a := &Agent{
completionFunc: c,
messages: make([]*Message, 0),
}
for _, o := range opts {
o(a)
}
return a
}
// NewFromAgent creates a new agent based on an existing agent.
//
// The new agent will have the same capabilities and history as the previous
// agent, but any changes will not be propogated to the original.
func NewFromAgent(a *Agent) *Agent {
na := &Agent{
completionFunc: a.completionFunc,
messages: make([]*Message, 0, len(a.messages)),
}
for _, m := range a.messages {
na.messages = append(na.messages, NewMessageFromMessage(m))
}
return na
}
func (a *Agent) Add(role Role, content string) *Agent {
msg := newMessage()
msg.Role = role
msg.content = content
a.messages = append(a.messages, msg)
return a
}
func (a *Agent) AddMessage(m *Message) *Agent {
a.messages = append(a.messages, m)
return a
}
func (a *Agent) Messages() []*Message {
msgs := make([]*Message, len(a.messages))
copy(msgs, a.messages)
return msgs
}
func (a *Agent) Step(ctx context.Context) (*Message, error) {
nextMsg, err := a.completionFunc(ctx, a.messages, nil)
if err != nil {
return nil, err
}
// An empty step is oke. This would be possible if there is some
// internal state, like a sub-assistant, that hasn't yet resulted in a
// message.
if nextMsg == nil {
return nil, nil
}
a.messages = append(a.messages, nextMsg)
return nextMsg, nil
}