-
Notifications
You must be signed in to change notification settings - Fork 0
Made conversation more fluent with AI calls #26
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
Changes from all commits
d1f0084
f192b10
f6b4e96
4638623
7c9c100
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "regexp" | ||
| "runtime" | ||
| "sort" | ||
| "strings" | ||
|
|
@@ -18,8 +19,9 @@ import ( | |
| const systemPrompt = `You are an assistant providing terminal commands based on user's questions. | ||
| You will be given a question about how to do something in the CLI environment. | ||
| You will then find out what command to execute and provide the command. | ||
| Do not provide any additional information, explanation or context, just the linux command. | ||
| For example, if question is about listing all files in a directory for linux, respond with "ls".` | ||
| Make sure to enclose the actual command inside *** marker. | ||
| For example, if question is about listing all files in a directory for linux, respond with "***ls***". | ||
| If no question is asked by user, continue the conversation. If they want to exit, respond with "***exit***".` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively the prompt could be to return a JSON with specific key values. That way Gromit could infer how the AI interpreted the original question, etc.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea that is a great point! I will refactor to make it that way. |
||
|
|
||
| type Gromit struct { | ||
| cli.Command | ||
|
|
@@ -131,10 +133,6 @@ func WithAskForConfirmation(confirm bool) ConfigurationModifier { | |
| func (g *Gromit) actionGromit(ctx context.Context, command *cli.Command) error { | ||
| commandArgs := command.Args().Slice() | ||
| query := strings.Join(commandArgs, " ") | ||
| if query == "" { | ||
| g.print("Please run ./gromit --help to see usage") | ||
| return nil | ||
| } | ||
| prompt := g.String("systemPrompt") | ||
| if prompt == "" { | ||
| prompt = systemPrompt | ||
|
|
@@ -147,51 +145,74 @@ func (g *Gromit) actionGromit(ctx context.Context, command *cli.Command) error { | |
| model: g.String("model"), | ||
| systemPrompt: prompt, | ||
| } | ||
| err := g.handleUserQuery(ctx, query) | ||
| terminalCommand, err := g.extractCommandForQuery(ctx, query) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if terminalCommand != "" { | ||
| err = g.handleTerminalCommand(ctx, terminalCommand) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| for ctx.Err() == nil { | ||
| confirmation, err := g.askConfirmation("Can I help you with anything else?") | ||
| //read the user input, pass it to AI | ||
| reader := bufio.NewReader(g.Reader) | ||
| query, err := reader.ReadString('\n') | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if confirmation.confirmed { | ||
| g.print("How can I help?") | ||
| reader := bufio.NewReader(os.Stdin) | ||
| query, err := reader.ReadString('\n') | ||
| terminalCommand, err := g.extractCommandForQuery(ctx, query) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if terminalCommand == "exit" { | ||
| break | ||
| } | ||
| if terminalCommand != "" { | ||
| err = g.handleTerminalCommand(ctx, terminalCommand) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err = g.handleUserQuery(ctx, query); err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (g *Gromit) handleUserQuery(ctx context.Context, query string) error { | ||
| func (g *Gromit) extractCommandForQuery(ctx context.Context, query string) (string, error) { | ||
| assister, err := g.AssisterCreator.GetAssister(g.configuration.AiParameters) | ||
| if err != nil { | ||
| return err | ||
| return "", err | ||
| } | ||
| exeCommand, err := assister.GetTerminalCommand(ctx, query) | ||
| if query == "" { | ||
| query = "Can you please introduce yourself or continue the conversation?" | ||
| } | ||
| response, err := assister.GetTerminalCommand(ctx, query) | ||
| if err != nil { | ||
| return err | ||
| return "", err | ||
| } | ||
| g.print("In order to do that, you need to run:") | ||
| g.print(exeCommand) | ||
| //command is enclosed in *** marker | ||
| regexp := regexp.MustCompile(`\*\*\*(.*?)\*\*\*`) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could compile this once as a package level var and reuse it. (also see my earlier suggestion about asking AI to return structured response. That way regex would be redundant I think. |
||
| commands := regexp.FindStringSubmatch(response) | ||
| var command string | ||
| if len(commands) > 0 { | ||
| command = commands[1] | ||
| } else { | ||
| g.print(response) | ||
| } | ||
| return command, nil | ||
| } | ||
|
|
||
| func (g *Gromit) handleTerminalCommand(ctx context.Context, terminalCommand string) error { | ||
| g.print("In order to do that, you need to run:") | ||
| g.print(terminalCommand) | ||
| confirmation, err := g.askConfirmation("Would you like to run this command?") | ||
| if err != nil { | ||
| g.print("Error reading your response") | ||
| return err | ||
| } | ||
| if confirmation.confirmed { | ||
| err = g.executeCommand(exeCommand) | ||
| err = g.executeCommand(terminalCommand) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty argument also calls the AI to generate some text for introduction. Therefore removed this check to not call AI on each build.