@@ -13,34 +13,25 @@ import (
13
13
"fmt"
14
14
"os"
15
15
16
- "github.com/fogfish/golem/pipe"
16
+ "github.com/fogfish/golem/pipe/v2 "
17
17
"github.com/fogfish/golem/pure/monoid"
18
18
"github.com/kshard/chatter"
19
19
"github.com/kshard/chatter/llm/bedrock"
20
20
"github.com/kshard/thinker"
21
21
"github.com/kshard/thinker/agent"
22
- "github.com/kshard/thinker/codec"
23
22
"github.com/kshard/thinker/command"
24
- "github.com/kshard/thinker/memory"
25
- "github.com/kshard/thinker/reasoner"
26
23
)
27
24
28
25
//------------------------------------------------------------------------------
29
26
30
27
// The agent creates short-stoty, see Hello World example for details
31
28
type AgentA struct {
32
- * agent.Automata [ string , string ]
29
+ * agent.Prompter [ string ]
33
30
}
34
31
35
32
func NewAgentA (llm chatter.Chatter ) * AgentA {
36
33
agt := & AgentA {}
37
- agt .Automata = agent .NewAutomata (llm ,
38
- memory .NewVoid ("" ),
39
- reasoner .NewVoid [string ](),
40
- codec .FromEncoder (agt .story ),
41
- codec .DecoderID ,
42
- )
43
-
34
+ agt .Prompter = agent .NewPrompter (llm , agt .story )
44
35
return agt
45
36
}
46
37
@@ -53,26 +44,15 @@ func (AgentA) story(subj string) (prompt chatter.Prompt, err error) {
53
44
54
45
// The agent creates workflow to process local files, see Script example for details
55
46
type AgentB struct {
56
- * agent.Automata [string , thinker.CmdOut ]
57
- registry * command.Registry
47
+ * agent.Worker [string ]
58
48
}
59
49
60
50
func NewAgentB (llm chatter.Chatter ) * AgentB {
61
- agt := & AgentB {}
51
+ registry := command .NewRegistry ()
52
+ registry .Register (command .Bash ("MacOS" , "/tmp/script" ))
62
53
63
- agt .registry = command .NewRegistry ()
64
- agt .registry .Register (command .Bash ("MacOS" , "/tmp/script" ))
65
- agt .registry .Register (command .Return ())
66
-
67
- agt .Automata = agent .NewAutomata (llm ,
68
- memory .NewStream (memory .INFINITE , `
69
- You are automomous agent who uses tools to perform required tasks.
70
- You are using and remember context from earlier chat history to execute the task.
71
- ` ),
72
- reasoner .NewEpoch (4 , agt ),
73
- agt ,
74
- agt .registry ,
75
- )
54
+ agt := & AgentB {}
55
+ agt .Worker = agent .NewWorker (llm , 4 , thinker.Encoder [string ](agt ), registry )
76
56
77
57
return agt
78
58
}
@@ -84,41 +64,9 @@ func (agt AgentB) Encode(string) (prompt chatter.Prompt, err error) {
84
64
(2) Analyse file content and answer the question: Who is main character in the story? Remember the answer in your context.
85
65
(3) Return all Remembered answers as comma separated string.` )
86
66
87
- // Inject tools
88
- agt .registry .Harden (& prompt )
89
67
return
90
68
}
91
69
92
- func (AgentB ) Deduct (state thinker.State [thinker.CmdOut ]) (thinker.Phase , chatter.Prompt , error ) {
93
- // the registry has failed to execute command, we have to supply the feedback to LLM
94
- if state .Feedback != nil && state .Confidence < 1.0 {
95
- var prompt chatter.Prompt
96
- prompt .WithTask ("Refine the previous workflow step using the feedback below." )
97
- prompt .With (state .Feedback )
98
-
99
- return thinker .AGENT_REFINE , prompt , nil
100
- }
101
-
102
- // the workflow has successfully completed
103
- // Note: pseudo-command return is executed
104
- if state .Reply .Cmd == command .RETURN {
105
- return thinker .AGENT_RETURN , chatter.Prompt {}, nil
106
- }
107
-
108
- // the workflow step is completed
109
- if state .Reply .Cmd == command .BASH {
110
- var prompt chatter.Prompt
111
- prompt .WithTask ("Continue the workflow execution." )
112
- prompt .With (
113
- chatter .Blob ("The command has returned:\n " , state .Reply .Output ),
114
- )
115
-
116
- return thinker .AGENT_ASK , prompt , nil
117
- }
118
-
119
- return thinker .AGENT_ABORT , chatter.Prompt {}, fmt .Errorf ("unknown state" )
120
- }
121
-
122
70
//------------------------------------------------------------------------------
123
71
124
72
func main () {
@@ -145,29 +93,19 @@ func main() {
145
93
who := pipe .Seq ("Cat" , "Dog" , "Cow" , "Pig" )
146
94
147
95
// Use agent to transform input into story
148
- story := pipe .StdErr (pipe .Map (ctx , who ,
149
- func (x string ) (string , error ) {
150
- return agtA .Prompt (context .Background (), x )
151
- },
152
- ))
96
+ story := pipe .StdErr (pipe .Map (ctx , who , pipe .Lift (agtA .Seek )))
153
97
154
98
// Write stories into file system
155
- file := pipe .StdErr (pipe .Map (ctx , story , txt2file ))
99
+ file := pipe .StdErr (pipe .Map (ctx , story , pipe . Lift ( txt2file ) ))
156
100
157
101
// Wait until all files are written
158
102
syn := pipe .Fold (ctx , file , mString )
159
103
160
104
// Use agent to conduct analysis of local files
161
- act := pipe .StdErr (pipe .Map (ctx , syn ,
162
- func (x string ) (thinker.CmdOut , error ) {
163
- return agtB .Prompt (context .Background (), x )
164
- },
165
- ))
105
+ act := pipe .StdErr (pipe .Map (ctx , syn , pipe .Lift (agtB .Echo )))
166
106
167
107
// Output the result of the pipeline
168
- <- pipe .ForEach (ctx , act ,
169
- func (x thinker.CmdOut ) { fmt .Printf ("==> %s\n " , x .Output ) },
170
- )
108
+ <- pipe .ForEach (ctx , act , pipe .Pure (stdout ))
171
109
172
110
close ()
173
111
}
@@ -186,3 +124,8 @@ func txt2file(x string) (string, error) {
186
124
187
125
// naive string monoid
188
126
var mString = monoid .FromOp ("" , func (a string , b string ) string { return a + " " + b })
127
+
128
+ func stdout (x thinker.CmdOut ) thinker.CmdOut {
129
+ fmt .Printf ("==> %s\n " , x .Output )
130
+ return x
131
+ }
0 commit comments