-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
194 lines (162 loc) · 4.25 KB
/
workflow.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
"github.com/expr-lang/expr"
"gopkg.in/yaml.v3"
)
type Tasks struct {
IterationValuesIn string `yaml:"iterator_script"`
IterationValues []interface{}
Tasks []Task `yaml:"tasks"`
}
type Task struct {
Name string `yaml:"name"`
Prompt string `yaml:"prompt"`
Role string `yaml:"role"`
Plugin string `yaml:"plugin"`
Model string `yaml:"model"`
Temperature string `yaml:"temperature"`
PreScript string `yaml:"pre_script"`
PostScript string `yaml:"post_script"`
Tools []Tool `yaml:"tools,omitempty"`
}
func getAbsolutePath(path string) (string, error) {
workflowDir := filepath.Dir(appCfg.WorkflowPath)
joinedPath := filepath.Join(workflowDir, path)
return filepath.Abs(joinedPath)
}
func workflowChain(path string, p string) (string, error) {
absPath, err := getAbsolutePath(path)
if err != nil {
return "", fmt.Errorf("error loading workflow, check filepath: %v", err)
}
res, err := exec.Command("assembllm", "--raw", "-w", absPath, p).Output()
if err != nil {
return "", fmt.Errorf("error loading workflow: %v\n%v\n%v", absPath, string(res), err)
}
return string(res), nil
}
func generateResponseForTasks(tasks Tasks) (string, error) {
var out string
for _, task := range tasks.Tasks {
if task.PreScript != "" {
s, err := runExpr(task.Prompt, task.PreScript)
if err != nil {
return "", err
}
task.Prompt = task.Prompt + s
}
var res string
if task.Plugin != "" {
pluginCfg, err := getPluginConfig(task.Plugin, getConfigPath())
if err != nil {
return "", err
}
if task.Temperature != "" {
pluginCfg.Temperature = task.Temperature
}
pluginCfg.Role = task.Role
pluginCfg.Model = task.Model
prompt := out + task.Prompt
if task.Tools != nil {
res, err = pluginCfg.generateResponseWithTools(prompt, task.Tools)
if err != nil {
return "", err
}
} else {
res, err = pluginCfg.generateResponse(prompt, true)
if err != nil {
return "", err
}
}
}
if task.PostScript != "" {
s, err := runExpr(res, task.PostScript)
if err != nil {
return "", err
}
res = s
}
out = res
}
if !appCfg.Raw {
return glamour.Render(out, "dark")
}
return out, nil
}
func handleTasks(prompt string) error {
tasksCfg, err := os.ReadFile(appCfg.WorkflowPath)
if err != nil {
return err
}
var tasks Tasks
err = yaml.Unmarshal(tasksCfg, &tasks)
if err != nil {
return err
}
if tasks.IterationValuesIn == "" {
tasks.IterationValues = []interface{}{nil}
} else {
env := map[string]interface{}{
"input": prompt,
"Get": httpGet,
"AppendFile": appendFile,
"ReadFile": readfile,
"Extism": callExtismPlugin,
"Resend": resend,
}
program, err := expr.Compile(tasks.IterationValuesIn, expr.Env(env), expr.AsKind(reflect.Slice))
if err != nil {
return err
}
output, err := expr.Run(program, env)
if err != nil {
return err
}
tasks.IterationValues = output.([]interface{})
}
var res string
for i := range tasks.IterationValues {
appCfg.CurrentIterationValue = tasks.IterationValues[i]
if len(tasks.Tasks) > 0 {
if prompt != "" {
tasks.Tasks[0].Prompt = prompt + " " + tasks.Tasks[0].Prompt
}
}
action := func(tasks Tasks) {
res, err = generateResponseForTasks(tasks)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
_ = spinner.New().
Title("Generating...").
TitleStyle(lipgloss.NewStyle().Faint(true)).
Action(func() { action(tasks) }).
Run()
fmt.Print(res)
}
if appCfg.Feedback {
var rerun bool
huh.NewConfirm().Title("Would you like to provide feedback and rerun the workflow?").Value(&rerun).Run()
if rerun {
var feedback string
huh.NewInput().Title("Provide your feedback or follow-up question:").Value(&feedback).Run()
return handleTasks("you were prompted with " + prompt + "and responded with " + res + " the user provided this feedback: " + feedback)
}
}
return nil
}
func executeWorkflow(args []string) error {
prompt := generatePrompt(args, false)
return handleTasks(prompt)
}