Skip to content
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

feat!: provide input via stdin and as an argument #140

Closed
Closed
Show file tree
Hide file tree
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
32 changes: 27 additions & 5 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
return err
}

var promptBuilder strings.Builder
var prompt, input string
mode := "txt"

Expand All @@ -178,12 +179,33 @@
slog.Debug("No input via pipe provided")
return ErrMissingInput
}
prompt = input
// mode is provided via command line args
if len(args) == 1 {
slog.Debug("Mode provided via command line args")
mode = args[0]
_, err = promptBuilder.WriteString(input)
if err != nil {
return err
}

Check warning on line 185 in cli/root.go

View check run for this annotation

Codecov / codecov/patch

cli/root.go#L184-L185

Added lines #L184 - L185 were not covered by tests

// check if args are provided
if len(args) == 0 {
// no mode or prompt provided via command line args
slog.Debug("No mode or additional prompt provided via command line args")

} else if len(args) == 1 {
// additional prompt was provided via command line args
slog.Debug("Additional prompt provided via command line args")
_, err = promptBuilder.WriteString("\n\n" + args[0])
if err != nil {
return err
}

Check warning on line 198 in cli/root.go

View check run for this annotation

Codecov / codecov/patch

cli/root.go#L197-L198

Added lines #L197 - L198 were not covered by tests

} else {
// mode and additional prompt were provided via command line args
mode = strings.ToLower(args[0])
_, err = promptBuilder.WriteString("\n\n" + args[1])
if err != nil {
return err
}

Check warning on line 206 in cli/root.go

View check run for this annotation

Codecov / codecov/patch

cli/root.go#L205-L206

Added lines #L205 - L206 were not covered by tests
}
prompt = promptBuilder.String()

} else {
// input is provided via command line args
Expand Down
124 changes: 124 additions & 0 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,130 @@ func TestRootCmd_SimplePromptViaPipedShell(t *testing.T) {
wg.Wait()
}

func TestRootCmd_SimplePromptViaPipedShell_AdditionalArgPrompt(t *testing.T) {
stdinPrompt := "Say the following: "
argPrompt := "Hello World!"
expectedPrompt := stdinPrompt + "\n\n" + argPrompt
expectedResponse := "Hello World!\n"

mem := &exitMemento{}
var wg sync.WaitGroup
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()

config := createTestConfig(t)

root := newRootCmd(mem.Exit, config, mockIsPipedShell(true, nil), api.MockClient(strings.Clone(expectedResponse), nil))
root.cmd.SetIn(stdinReader)
root.cmd.SetOut(stdoutWriter)

wg.Add(1)
go func() {
defer wg.Done()
_, errWrite := stdinWriter.Write([]byte(stdinPrompt))
require.NoError(t, stdinWriter.Close())
require.NoError(t, errWrite)
}()

wg.Add(1)
go func() {
defer wg.Done()
var buf bytes.Buffer
_, err := io.Copy(&buf, stdoutReader)
require.NoError(t, err)
require.NoError(t, stdoutReader.Close())
require.Equal(t, expectedResponse, buf.String())
}()

chatName := "test_chat"
root.Execute([]string{argPrompt, "--chat", chatName})
require.Equal(t, 0, mem.code)
require.NoError(t, stdinReader.Close())
require.NoError(t, stdoutWriter.Close())

manager, err := chat.NewFilesystemChatSessionManager(config)
require.NoError(t, err)

var messages []openai.ChatCompletionMessage
messages, err = manager.GetSession(chatName)
require.NoError(t, err)
require.Len(t, messages, 2)

// Check if the prompt was added
require.Equal(t, openai.ChatMessageRoleUser, messages[0].Role)
require.Equal(t, expectedPrompt, messages[0].Content)

// Check if the response was added
require.Equal(t, openai.ChatMessageRoleAssistant, messages[1].Role)
require.Equal(t, strings.TrimSpace(expectedResponse), messages[1].Content)

wg.Wait()
}

func TestRootCmd_SimplePromptViaPipedShell_AdditionalModeAndArg(t *testing.T) {
stdinPrompt := "Say the following: "
argPrompt := "Hello World!"
expectedPrompt := stdinPrompt + "\n\n" + argPrompt
expectedResponse := "Hello World!\n"

mem := &exitMemento{}
var wg sync.WaitGroup
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()

config := createTestConfig(t)

root := newRootCmd(mem.Exit, config, mockIsPipedShell(true, nil), api.MockClient(strings.Clone(expectedResponse), nil))
root.cmd.SetIn(stdinReader)
root.cmd.SetOut(stdoutWriter)

wg.Add(1)
go func() {
defer wg.Done()
_, errWrite := stdinWriter.Write([]byte(stdinPrompt))
require.NoError(t, stdinWriter.Close())
require.NoError(t, errWrite)
}()

wg.Add(1)
go func() {
defer wg.Done()
var buf bytes.Buffer
_, err := io.Copy(&buf, stdoutReader)
require.NoError(t, err)
require.NoError(t, stdoutReader.Close())
require.Equal(t, expectedResponse, buf.String())
}()

chatName := "test_chat"
root.Execute([]string{"sh", argPrompt, "--chat", chatName})
require.Equal(t, 0, mem.code)
require.NoError(t, stdinReader.Close())
require.NoError(t, stdoutWriter.Close())

manager, err := chat.NewFilesystemChatSessionManager(config)
require.NoError(t, err)

var messages []openai.ChatCompletionMessage
messages, err = manager.GetSession(chatName)
require.NoError(t, err)
require.Len(t, messages, 3)

// Check if the modifier was added
require.Equal(t, openai.ChatMessageRoleSystem, messages[0].Role)
require.Contains(t, messages[0].Content, "command translation engine")

// Check if the prompt was added
require.Equal(t, openai.ChatMessageRoleUser, messages[1].Role)
require.Equal(t, expectedPrompt, messages[1].Content)

// Check if the response was added
require.Equal(t, openai.ChatMessageRoleAssistant, messages[2].Role)
require.Equal(t, strings.TrimSpace(expectedResponse), messages[2].Content)

wg.Wait()
}

func TestRootCmd_PipedShell_NoInput(t *testing.T) {
mem := &exitMemento{}
var wg sync.WaitGroup
Expand Down
8 changes: 5 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ $ sgpt "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.
```

You can also pass prompts to SGPT using pipes:
In addition, you can pass a file via pipes and add your prompt to ask a question about the file or have it summarized
for you.

```shell
$ echo -n "mass of sun" | sgpt
The mass of the sun is approximately 1.989 x 10^30 kilograms.
cat yourfile.txt | sgpt "summarize everything above"
```

The prompt passed as an argument is appended to the data that is piped in.

## Code Generation Capabilities

By adding the `code` command to your prompt, you can generate code based on given instructions by using the
Expand Down
13 changes: 8 additions & 5 deletions docs/usage/query-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ $ sgpt "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.
```

You can also pass prompts to SGPT using pipes:
You can also use the `--clipboard` flag to write the answer to the clipboard:

```shell
$ echo -n "mass of sun" | sgpt
$ sgpt --clipboard "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.
```

You can also use the `--clipboard` flag to write the answer to the clipboard:
In addition, you can pass a file via pipes and add your prompt to ask a question about the file or have it summarized
for you.

```shell
$ sgpt --clipboard "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.
cat yourfile.txt | sgpt "summarize everything above"
```

The prompt passed as an argument is appended to the data that is piped in. Keep in mind: Depending on the model you are
using, there are different limits to the number of tokens you can use in your prompt.

## Generate Code

SGPT can efficiently generate code based on given instructions. For instance, to solve the classic FizzBuzz problem
Expand Down
2 changes: 1 addition & 1 deletion modifiers/prompts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
messages:
- role: system
text: |-
Act as a natural language to {{ with .SHELL -}}{{ . }}{{ end -}} command translation engine on {{.OS}}.
Act as a natural language to {{ with .SHELL -}}{{ . }} {{ end -}} command translation engine on {{.OS}}.
You are an expert {{if .SHELL -}}in {{ .SHELL }} on {{.OS}} {{ else -}} in {{.OS}} {{ end -}}and translate the question at the end to valid syntax.
Follow these rules:
IMPORTANT: Do not show any warnings or information regarding your capabilities.
Expand Down