Skip to content

Include initial prompt when interpolating {{input}} in prompt.yaml files #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
@@ -270,10 +270,12 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
if isPipe(os.Stdin) {
promptFromPipe, _ := io.ReadAll(os.Stdin)
if len(promptFromPipe) > 0 {
singleShot = true
pipedContent = strings.TrimSpace(string(promptFromPipe))
if pf == nil {
if initialPrompt != "" {
initialPrompt = initialPrompt + "\n" + pipedContent
singleShot = true
} else {
initialPrompt = pipedContent
}
}
}
@@ -291,8 +293,8 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
if pf != nil {
for _, m := range pf.Messages {
content := m.Content
if pipedContent != "" && strings.ToLower(m.Role) == "user" {
content = strings.ReplaceAll(content, "{{input}}", pipedContent)
if strings.ToLower(m.Role) == "user" {
content = strings.ReplaceAll(content, "{{input}}", initialPrompt)
}
switch strings.ToLower(m.Role) {
case "system":
7 changes: 5 additions & 2 deletions cmd/run/run_test.go
Original file line number Diff line number Diff line change
@@ -149,7 +149,7 @@ messages:
require.Contains(t, out.String(), reply) // response streamed to output
})

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

out := new(bytes.Buffer)
cfg := command.NewConfig(out, out, client, true, 100)

initialPrompt := "Please summarize the provided text."
runCmd := NewRunCommand(cfg)
runCmd.SetArgs([]string{
"--file", tmp.Name(),
azuremodels.FormatIdentifier("openai", "test-model"),
initialPrompt,
})

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

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

require.Contains(t, out.String(), reply)
})
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.10.0
golang.org/x/text v0.23.0
gopkg.in/yaml.v3 v3.0.1
)

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