Skip to content

Commit

Permalink
add Go and Python execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Daulet Zhanguzin committed May 5, 2024
1 parent 9a2273a commit b980647
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Binary file added .github/ball.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/ball_web.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Cmd is a versatile command-line tool that leverages AI to understand natural lan
## run code
If you'd like to run the generated shell command or script, use `-run`:
If you'd like to run the generated shell command or code, use `-run`:
```bash
$ cmd -run print third last commit hash
To print the third last commit hash, you can use the following Git command:
Expand All @@ -20,6 +20,7 @@ git log --pretty=format:"%H" -n 1 --skip 2
This command will display the commit hash of the commit that is two commits before the most recent one. The `--pretty=format:"%H"` option specifies that you want to display the commit hash in the output, and the `-n 1` option limits the output to only one commit. The `--skip 2` option skips the two most recent commits and displays the hash of the third last commit.
a200e6d429e2888344d7254ac02a00618ab432a2
```
Supported languages include Go, Bash, Python and HTML. The language is assumed from identifier immediately following backticks of fenced code blocks, hence it could be error prone if no language is specified, or if code is broken down into multiple blocks (common for HTML).

## execute code

Expand Down Expand Up @@ -60,3 +61,23 @@ You can check current configuration using `cmd -config`, and to change it use:
```bash
$ cmd -connectors web-search
```

# Cool use cases

## Read PDF document

Pipe with xpdf (`brew install xpdf`) or similar:
```bash
$ pdftotext -nopgbrk -eol unix -q document.pdf - | cmd tldr
```

## Use web search

Seemingly unrelated tasks could benefit from web search, e.g. here is a result of `cmd -run html for a bouncing ball`:

![gif of a bouncing ball](./.github/ball.gif)

and here is the result with web search enabled (`cmd -connectors web-search`):

![gif of a bouncing ball](./.github/ball_web.gif)

23 changes: 22 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
listModels = flag.Bool("list-models", false, "List available models.")
setModel = flag.String("model", "", "Set model to use.")
listConnectors = flag.Bool("list-connectors", false, "List available connectors.")
setConnectors = flag.String("connectors", "", "Set connectors to use.")
setConnectors = flag.String("connectors", "", "Set comma delimited list of connectors to use.")
setTemp = flag.Float64("temperature", 0.0, "Set temperature value.")
setTopP = flag.Float64("top-p", 0.0, "Set top-p value.")
setTopK = flag.Int("top-k", 0, "Set top-k value.")
Expand Down Expand Up @@ -121,6 +121,19 @@ func generate(

func runBlock(block *parser.CodeBlock) error {
switch block.Lang {
case parser.Go:
code := block.Code
// TODO remove when prompt engineering is there to add "make it runnable"
if !strings.HasPrefix(block.Code, "package") {
code = fmt.Sprintf("package main\n\n%s", block.Code)
}
path := fmt.Sprintf("%smain.go", os.TempDir())
if err := os.WriteFile(path, []byte(code), 0644); err != nil {
return err
}
if err := runCmd("go", "run", path); err != nil {
return err
}
case parser.Bash:
if err := runCmd("bash", "-c", block.Code); err != nil {
return err
Expand All @@ -133,6 +146,14 @@ func runBlock(block *parser.CodeBlock) error {
if err := runCmd("open", fmt.Sprintf("file://%s", path)); err != nil {
return err
}
case parser.Python:
path := fmt.Sprintf("%smain.py", os.TempDir())
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
return err
}
if err := runCmd("python", path); err != nil {
return err
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion parser/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func language(s string) Language {
return Bash
case "html":
return HTML
case "python":
case "python", "python3":
return Python
default:
return Unknown
Expand Down

0 comments on commit b980647

Please sign in to comment.