/
run.go
202 lines (168 loc) · 5.06 KB
/
run.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
package codegen
import (
"context"
"fmt"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
"github.com/go-go-golems/bobatea/pkg/conversation"
"github.com/go-go-golems/geppetto/pkg/cmds"
"github.com/go-go-golems/geppetto/pkg/steps"
"github.com/go-go-golems/geppetto/pkg/steps/ai/chat"
"github.com/go-go-golems/geppetto/pkg/steps/ai/settings"
"github.com/go-go-golems/geppetto/pkg/steps/utils"
"github.com/go-go-golems/glazed/pkg/cli"
"github.com/go-go-golems/glazed/pkg/cmds/layers"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"io"
)
var CodegenTestCmd = &cobra.Command{
Use: "codegen-test",
Short: "Test codegen prompt",
Run: func(cmd *cobra.Command, args []string) {
stepSettings, err := createSettingsFromCobra(cmd)
cobra.CheckErr(err)
c, err := NewTestCodegenCommand()
cobra.CheckErr(err)
c.StepSettings = stepSettings
params := &TestCodegenCommandParameters{
Pretend: "Scientist",
What: "Size of the moon",
Of: "My heart",
Query: []string{"What is the size of the moon?"},
}
ctx := context.Background()
err = c.RunIntoWriter(ctx, params, cmd.OutOrStdout())
cobra.CheckErr(err)
},
}
func createSettingsFromCobra(cmd *cobra.Command) (*settings.StepSettings, error) {
stepSettings := settings.NewStepSettings()
geppettoLayers, err := cmds.CreateGeppettoLayers(stepSettings)
if err != nil {
return nil, err
}
layers_ := layers.NewParameterLayers(layers.WithLayers(geppettoLayers...))
cobraParser, err := cli.NewCobraParserFromLayers(
layers_,
cli.WithCobraMiddlewaresFunc(
cmds.GetCobraCommandGeppettoMiddlewares,
))
cobra.CheckErr(err)
parsedLayers, err := cobraParser.Parse(cmd, nil)
cobra.CheckErr(err)
err = stepSettings.UpdateFromParsedLayers(parsedLayers)
if err != nil {
return nil, err
}
stepSettings.Chat.Stream = true
return stepSettings, nil
}
func printToStdout(s string, w io.Writer) error {
_, err := w.Write([]byte(s))
return err
}
type printer struct {
Name string
w io.Writer
isFirst bool
}
func (p *printer) Print(s string) error {
if p.isFirst {
p.isFirst = false
err := printToStdout(fmt.Sprintf("\n%s: \n", p.Name), p.w)
if err != nil {
return err
}
}
return printToStdout(s, p.w)
}
func NewPrinterFunc(name string, w io.Writer) func(string) error {
p := &printer{
Name: name,
w: w,
isFirst: true,
}
return p.Print
}
var MultiStepCodgenTestCmd = &cobra.Command{
Use: "multi-step",
Short: "Test codegen prompt",
Run: func(cmd *cobra.Command, args []string) {
stepSettings, err := createSettingsFromCobra(cmd)
cobra.CheckErr(err)
scientistCommand, err := NewTestCodegenCommand()
cobra.CheckErr(err)
scientistCommand.StepSettings = stepSettings
scientistParams := &TestCodegenCommandParameters{
Pretend: "Scientist",
What: "Size of the moon",
Of: "My heart",
Query: []string{"What is the size of the moon?"},
}
manager, err := scientistCommand.CreateManager(scientistParams)
cobra.CheckErr(err)
logger := watermill.NewStdLogger(false, false)
pubSub := gochannel.NewGoChannel(gochannel.Config{
// Guarantee that messages are delivered in the order of publishing.
BlockPublishUntilSubscriberAck: true,
}, logger)
router, err := message.NewRouter(message.RouterConfig{}, logger)
cobra.CheckErr(err)
defer func(router *message.Router) {
err := router.Close()
if err != nil {
log.Error().Err(err).Msg("Failed to close router")
}
}(router)
router.AddNoPublisherHandler("scientist",
"scientist",
pubSub,
chat.StepPrinterFunc("Scientist", cmd.OutOrStdout()),
)
router.AddNoPublisherHandler("writer",
"writer",
pubSub,
chat.StepPrinterFunc("Writer", cmd.OutOrStdout()),
)
writerParams := &TestCodegenCommandParameters{
Pretend: "Writer",
What: "Biography of the scientist",
Of: "The previous story",
Query: []string{"Write a beautiful biography."},
}
writerCommand, err := NewTestCodegenCommand()
cobra.CheckErr(err)
writerCommand.StepSettings = stepSettings
writerManager, err := writerCommand.CreateManager(writerParams)
cobra.CheckErr(err)
scientistStep, err := scientistCommand.CreateStep(
chat.WithPublishedTopic(pubSub, "scientist"),
)
cobra.CheckErr(err)
writerStep, err := writerCommand.CreateStep(
chat.WithPublishedTopic(pubSub, "writer"),
)
cobra.CheckErr(err)
mergeStep := utils.NewMergeStep(writerManager, true)
ctx := cmd.Context()
errgrp := errgroup.Group{}
errgrp.Go(func() error {
var scientistResult steps.StepResult[string]
scientistResult, err = scientistStep.Start(ctx, manager.GetConversation())
cobra.CheckErr(err)
mergeResult := steps.Bind[string, conversation.Conversation](ctx, scientistResult, mergeStep)
writerResult := steps.Bind[conversation.Conversation, string](ctx, mergeResult, writerStep)
res := writerResult.Return()
_ = res
return nil
})
errgrp.Go(func() error {
return router.Run(ctx)
})
err = errgrp.Wait()
cobra.CheckErr(err)
},
}