-
Notifications
You must be signed in to change notification settings - Fork 0
/
steps.go
305 lines (270 loc) · 9.33 KB
/
steps.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/sashabaranov/go-openai"
"github.com/tmc/langchaingo/prompts"
)
func setupSysPrompt(ai *AI, dbs DBs) string {
setupPrompt, _ := dbs.identity.ReadFile("identity/generate")
philosophyTemplate, _ := dbs.identity.ReadFile("identity/philosophy")
t := prompts.NewPromptTemplate(string(philosophyTemplate), nil)
philosophyPrompt, _ := t.Format(map[string]any{"lang": ai.lang})
return string(setupPrompt) + "\nUseful to know:\n" + philosophyPrompt
}
func simpleGen(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// Run the AI on the main prompt and save the results
userPrompt, _ := dbs.input.Get("main_prompt")
messages := ai.Start(
setupSysPrompt(ai, dbs),
userPrompt,
)
// Save the results to workspace
toFiles(messages[len(messages)-1].Content, dbs.workspace)
return messages
}
// Ask the user if they want to clarify anything and save the results to the workspace
func clarify(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// 1. system prompt
qaTemplate, _ := dbs.identity.ReadFile("identity/qa")
t := prompts.NewPromptTemplate(string(qaTemplate), nil)
qaPrompt, _ := t.Format(map[string]any{"lang": ai.lang})
messages := []openai.ChatCompletionMessage{
ai.SystemMessage(qaPrompt),
}
// 2. user specify what want to build
userPrompt, _ := dbs.input.Get("main_prompt")
// 3. ask if anything is unclear, then user clarify, repetitive, util everything is clear
for {
messages = ai.Next(messages, userPrompt)
// everything is clear, break
if strings.ToLower(strings.TrimSpace(messages[len(messages)-1].Content)) == "no" {
break
}
// user clarify to system
fmt.Print("\n(answer in text, or \"q\" to move on)\n")
fmt.Scanln(&userPrompt)
// user input q or nothing, break
if userPrompt == "" || userPrompt == "q" {
break
}
// after clarify ask system if anything is unclear
userPrompt += "\n\nIs anything else unclear? " +
"If yes, only answer in the form:\n" +
"{remaining unclear areas} remaining questions.\n" +
"{Next question}\n" +
"If everything is sufficiently clear, only answer \"no\"."
}
fmt.Println()
return messages
}
func genSpec(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// Generate a spec from the main prompt + clarifications and save the results to the workspace
userPrompt, _ := dbs.input.Get("main_prompt")
messages := []openai.ChatCompletionMessage{
ai.SystemMessage(setupSysPrompt(ai, dbs)),
ai.SystemMessage("Instructions: " + userPrompt),
}
specPrompt, _ := dbs.identity.ReadFile("identity/spec")
messages = ai.Next(messages, string(specPrompt))
if err := dbs.memory.Set("specification", messages[len(messages)-1].Content); err != nil {
panic(err)
}
return messages
}
func respec(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
specMessages, _ := dbs.logs.Get("genSpec")
var messages []openai.ChatCompletionMessage
json.Unmarshal([]byte(specMessages), &messages)
respecPrompt, _ := dbs.identity.ReadFile("identity/respec")
messages = append(messages, ai.SystemMessage(string(respecPrompt)))
messages = ai.Next(messages, "")
messages = ai.Next(messages, "Based on the conversation so far, please reiterate the specification for the program."+
"If there are things that can be improved, please incorporate the improvements."+
"If you are satisfied with the specification, just write out the specification word by word again.")
if err := dbs.memory.Set("specification", messages[len(messages)-1].Content); err != nil {
panic(err)
}
return messages
}
func genUnitTests(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// Generate unit tests based on the specification, that should work.
userPrompt, _ := dbs.input.Get("main_prompt")
specPrompt, _ := dbs.memory.Get("specification")
messages := []openai.ChatCompletionMessage{
ai.SystemMessage(setupSysPrompt(ai, dbs)),
ai.UserMessage("Instructions: " + userPrompt),
ai.UserMessage("Specification: " + specPrompt),
}
unitTestPrompt, _ := dbs.identity.ReadFile("identity/unit_tests")
messages = ai.Next(messages, string(unitTestPrompt))
unitTestContent := messages[len(messages)-1].Content
if err := dbs.memory.Set("unit_tests", unitTestContent); err != nil {
panic(err)
}
toFiles(unitTestContent, dbs.workspace)
return messages
}
func genClarifiedCode(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// get the messages from previous step
clarifyContent, _ := dbs.logs.Get("clarify")
var messages []openai.ChatCompletionMessage
json.Unmarshal([]byte(clarifyContent), &messages)
messages[0] = ai.SystemMessage(setupSysPrompt(ai, dbs))
useQaPrompt, _ := dbs.identity.ReadFile("identity/use_qa")
messages = ai.Next(messages, string(useQaPrompt))
toFiles(messages[len(messages)-1].Content, dbs.workspace)
return messages
}
func genCode(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
// get the messages from previous step
userPrompt, _ := dbs.input.Get("main_prompt")
specPrompt, _ := dbs.memory.Get("specification")
unitTestPrompt, _ := dbs.memory.Get("unit_tests")
messages := []openai.ChatCompletionMessage{
ai.SystemMessage(setupSysPrompt(ai, dbs)),
ai.UserMessage("Instructions:\n\n" + userPrompt),
ai.UserMessage("Specification:\n\n" + specPrompt),
ai.UserMessage("Unit tests:\n\n" + unitTestPrompt),
}
useQaPrompt, _ := dbs.identity.ReadFile("identity/use_qa")
messages = ai.Next(messages, string(useQaPrompt))
toFiles(messages[len(messages)-1].Content, dbs.workspace)
return messages
}
func executeEntrypoint(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
if !dbs.canExecute {
fmt.Printf(`db, %s doesn't allow interactive execution, exiting step`, dbs.dbType)
return []openai.ChatCompletionMessage{}
}
command, _ := dbs.workspace.Get("run.sh")
reader := bufio.NewReader(os.Stdin)
fmt.Println("Do you want to execute this code?")
fmt.Println()
fmt.Println(command)
fmt.Println()
fmt.Println("If yes, press enter, Otherwise, type \"no\"")
fmt.Println()
text, _ := reader.ReadString('\n')
if strings.TrimRight(text, "\n") != "yes" {
fmt.Println("ok, not executing the code")
return []openai.ChatCompletionMessage{}
}
fmt.Println("Executing the code...")
fmt.Println()
cmd := exec.Command("bash", "-c", command)
cmd.Dir = dbs.workspace.Path()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Error executing the code")
fmt.Println(err)
}
return []openai.ChatCompletionMessage{}
}
func genEntrypoint(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
outputTxt, _ := dbs.workspace.Get("all_output.txt")
messages := ai.Start(
`You will get information about a codebase that is currently on disk in the current folder.\n`+
`From this you will answer with code blocks that includes all the necessary unix terminal commands to`+
`a) install dependencies `+
`b) run all necessary parts of the codebase (in parallel if necessary).\n`+
`Do not install globally. `+
`Do not use sudo.\n`+
`Do not explain the code, just give the commands.\n`,
`Information about the codebase:\n\n`+outputTxt,
)
fmt.Println()
regex := regexp.MustCompile("(?s)```\\S*\\n(.+?)```")
if matches := regex.FindAllStringSubmatch(messages[len(messages)-1].Content, -1); matches != nil {
runShContent := make([]string, 0)
for _, match := range matches {
if len(match) > 1 {
runShContent = append(runShContent, match[1])
}
}
if err := dbs.workspace.Set("run.sh", strings.Join(runShContent, "\n")); err != nil {
panic(err)
}
}
return messages
}
func useFeedback(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
userPrompt, _ := dbs.input.Get("main_prompt")
outputTxt, _ := dbs.workspace.Get("all_output.txt")
useFeedbackPrompt, _ := dbs.identity.ReadFile("identity/use_feedback")
messages := []openai.ChatCompletionMessage{
ai.SystemMessage(setupSysPrompt(ai, dbs)),
ai.UserMessage("Instructions: " + userPrompt),
ai.AssistantMessage(outputTxt),
ai.SystemMessage(string(useFeedbackPrompt)),
}
feedback, _ := dbs.memory.Get("feedback")
messages = ai.Next(messages, feedback)
toFiles(messages[len(messages)-1].Content, dbs.workspace)
return messages
}
func fixCode(ai *AI, dbs DBs) []openai.ChatCompletionMessage {
genCodes, _ := dbs.logs.Get("genCode")
var messages []openai.ChatCompletionMessage
json.Unmarshal([]byte(genCodes), &messages)
codeOutput := messages[len(messages)-1].Content
userPrompt, _ := dbs.input.Get("main_prompt")
fixCodePrompt, _ := dbs.identity.ReadFile("identity/fix_code")
messages = []openai.ChatCompletionMessage{
ai.SystemMessage(setupSysPrompt(ai, dbs)),
ai.UserMessage("Instructions: " + userPrompt),
ai.UserMessage(codeOutput),
ai.SystemMessage(string(fixCodePrompt)),
}
messages = ai.Next(messages, "Please fix any errors in the code above.")
toFiles(messages[len(messages)-1].Content, dbs.workspace)
return messages
}
var STEPS = map[string][]func(*AI, DBs) []openai.ChatCompletionMessage{
"default": {
genSpec,
genUnitTests,
genCode,
genEntrypoint,
executeEntrypoint,
},
"benchmark": {
genSpec,
genUnitTests,
genCode,
fixCode,
genEntrypoint,
},
"simple": {
simpleGen,
genEntrypoint,
executeEntrypoint,
},
"clarify": {
clarify,
genClarifiedCode,
genEntrypoint,
executeEntrypoint,
},
"respec": {
genSpec,
respec,
genUnitTests,
genCode,
genEntrypoint,
executeEntrypoint,
},
"execute_only": {
executeEntrypoint,
},
"use_feedback": {
useFeedback,
},
}