Skip to content

Commit 81f5047

Browse files
authored
Include initial prompt when interpolating {{input}} in prompt.yaml files (#47)
2 parents 2f31074 + 17303c3 commit 81f5047

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

cmd/run/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,12 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
270270
if isPipe(os.Stdin) {
271271
promptFromPipe, _ := io.ReadAll(os.Stdin)
272272
if len(promptFromPipe) > 0 {
273+
singleShot = true
273274
pipedContent = strings.TrimSpace(string(promptFromPipe))
274-
if pf == nil {
275+
if initialPrompt != "" {
275276
initialPrompt = initialPrompt + "\n" + pipedContent
276-
singleShot = true
277+
} else {
278+
initialPrompt = pipedContent
277279
}
278280
}
279281
}
@@ -291,8 +293,8 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
291293
if pf != nil {
292294
for _, m := range pf.Messages {
293295
content := m.Content
294-
if pipedContent != "" && strings.ToLower(m.Role) == "user" {
295-
content = strings.ReplaceAll(content, "{{input}}", pipedContent)
296+
if strings.ToLower(m.Role) == "user" {
297+
content = strings.ReplaceAll(content, "{{input}}", initialPrompt)
296298
}
297299
switch strings.ToLower(m.Role) {
298300
case "system":

cmd/run/run_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ messages:
149149
require.Contains(t, out.String(), reply) // response streamed to output
150150
})
151151

152-
t.Run("--file with {{input}} placeholder is substituted with stdin", func(t *testing.T) {
152+
t.Run("--file with {{input}} placeholder is substituted with initial prompt and stdin", func(t *testing.T) {
153153
const yamlBody = `
154154
name: Summarizer
155155
description: Summarizes input text
@@ -208,18 +208,21 @@ messages:
208208

209209
out := new(bytes.Buffer)
210210
cfg := command.NewConfig(out, out, client, true, 100)
211+
212+
initialPrompt := "Please summarize the provided text."
211213
runCmd := NewRunCommand(cfg)
212214
runCmd.SetArgs([]string{
213215
"--file", tmp.Name(),
214216
azuremodels.FormatIdentifier("openai", "test-model"),
217+
initialPrompt,
215218
})
216219

217220
_, err = runCmd.ExecuteC()
218221
require.NoError(t, err)
219222

220223
require.Len(t, capturedReq.Messages, 3)
221224
require.Equal(t, "You are a text summarizer.", *capturedReq.Messages[0].Content)
222-
require.Equal(t, piped, *capturedReq.Messages[1].Content) // {{input}} -> "Hello there!"
225+
require.Equal(t, initialPrompt+"\n"+piped, *capturedReq.Messages[1].Content) // {{input}} -> "Please summarize the provided text.\nHello there!"
223226

224227
require.Contains(t, out.String(), reply)
225228
})

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/spf13/pflag v1.0.5
1616
github.com/stretchr/testify v1.10.0
1717
golang.org/x/text v0.23.0
18+
gopkg.in/yaml.v3 v3.0.1
1819
)
1920

2021
require (
@@ -49,5 +50,4 @@ require (
4950
golang.org/x/net v0.38.0 // indirect
5051
golang.org/x/sys v0.31.0 // indirect
5152
golang.org/x/term v0.30.0 // indirect
52-
gopkg.in/yaml.v3 v3.0.1 // indirect
5353
)

0 commit comments

Comments
 (0)