Skip to content

Commit b331608

Browse files
authored
Merge pull request #139 from ericcurtin/stdin
Allow stdin and multiple prompt args
2 parents 6ece744 + ee3965d commit b331608

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

commands/run.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"io"
78
"os"
89
"strings"
910

@@ -102,13 +103,29 @@ func newRunCmd() *cobra.Command {
102103

103104
model := args[0]
104105
prompt := ""
105-
if len(args) == 1 {
106-
if debug {
107-
cmd.Printf("Running model %s\n", model)
106+
args_len := len(args)
107+
if args_len > 1 {
108+
prompt = strings.Join(args[1:], " ")
109+
}
110+
111+
fi, err := os.Stdin.Stat()
112+
if err == nil && (fi.Mode()&os.ModeCharDevice) == 0 {
113+
// Read all from stdin
114+
reader := bufio.NewReader(os.Stdin)
115+
input, err := io.ReadAll(reader)
116+
if err == nil {
117+
if prompt != "" {
118+
prompt += "\n\n"
119+
}
120+
121+
prompt += string(input)
108122
}
109-
} else {
110-
prompt = args[1]
111-
if debug {
123+
}
124+
125+
if debug {
126+
if prompt == "" {
127+
cmd.Printf("Running model %s\n", model)
128+
} else {
112129
cmd.Printf("Running model %s with prompt %s\n", model, prompt)
113130
}
114131
}
@@ -180,9 +197,7 @@ func newRunCmd() *cobra.Command {
180197
"See 'docker model run --help' for more information",
181198
)
182199
}
183-
if len(args) > 2 {
184-
return fmt.Errorf("too many arguments, expected " + cmdArgs)
185-
}
200+
186201
return nil
187202
}
188203

0 commit comments

Comments
 (0)