Skip to content

Commit

Permalink
Add anthropic support
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Jul 21, 2023
1 parent 17ca6a4 commit fb84260
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 162 deletions.
11 changes: 10 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ env:
before:
hooks:
- go mod tidy
- ./scripts/completions.sh
builds:
- env:
- CGO_ENABLED=0
Expand Down Expand Up @@ -35,6 +36,7 @@ archives:
files:
- README.md
- LICENSE
- completions/*
checksum:
name_template: '{{ .ProjectName }}_checksums.txt'
changelog:
Expand All @@ -55,4 +57,11 @@ nfpms:
formats:
- apk
- deb
- rpm
- rpm
contents:
- src: ./completions/genie.bash
dst: /etc/bash_completion.d/genie
- src: ./completions/genie.fish
dst: /usr/share/fish/completions/genie.fish
- src: ./completions/genie.zsh
dst: /usr/local/share/zsh/site-functions/_genie
62 changes: 54 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,79 @@ Run the following command in the checked-out repository:
```bash
make docker-build

docker run -it --rm -v $PWD/dist:/dist -e OPENAI_API_KEY=$OPENAI_API_KEY genie -p "create a golang hello world"
docker run -it --rm -v $PWD/dist:/dist -e OPENAI_API_KEY=$OPENAI_API_KEY genie openai -p "create a golang hello world"
```

## How to use
Genie is a versatile code generation tool that simplifies the process of creating applications, APIs, and more. It offers an array of commands and features to cater to various code generation needs.

```text
Usage:
genie [command]
Available Commands:
anthropic Run codegen provided by anthropic
completion Generate the autocompletion script for the specified shell
help Help about any command
openai Run codegen provided by openAI
Flags:
-h, --help help for genie
-o, --outdir string outdir to use (default "dist")
-p, --prompt string prompt to use (required)
-v, --version version for genie
Use "genie [command] --help" for more information about a command.
```

### OpenAI
```text
Usage:
genie [flags]
genie openai [flags]
Examples:
genie -p "Create a python hello world"
genie -p prompt.txt
genie openai -p "Create a python hello world"
genie openai -p prompt.txt
Flags:
--api-key string openAI api key
-h, --help help for genie
-h, --help help for openai
--max-tokens int max tokens to use (default -1)
-m, --model string model to use (default "gpt-3.5-turbo")
-o, --outdir string outdir to use (default "dist")
-p, --prompt string prompt to use (required)
-t, --temperature float32 temperature to use (default 0.4)
-v, --version version for genie
Global Flags:
-o, --outdir string outdir to use (default "dist")
-p, --prompt string prompt to use (required)
```

### Anthropic
```text
Usage:
genie anthropic [flags]
Examples:
genie anthropic -p "Create a python hello world"
genie anthropic -p prompt.txt
Flags:
--api-key string anthropic api key
-h, --help help for anthropic
--max-tokens int max tokens to use (default -1)
-m, --model string model to use (default "claude-v1")
-t, --temperature float temperature to use (default 0.4)
Global Flags:
-o, --outdir string outdir to use (default "dist")
-p, --prompt string prompt to use (required)
```

## Environment Variables
The following environment variables are supported by this project:

| Variable Name | Description |
|-------------------|-------------------------------------------------------------------------------------------------------|
| ANTHROPIC_API_KEY | The API key required for accessing the Anthropic service. |
| OPENAI_API_KEY | The API key required for accessing the OpenAI service. |

## Example
Expand Down
59 changes: 59 additions & 0 deletions cmd/anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"context"
"os"

"github.com/hupe1980/genie/pkg/codegen"
"github.com/spf13/cobra"
)

// anthropicOptions holds the command-line flags for the anthropic command.
type anthropicOptions struct {
apiKey string
model string
temperature float64
maxTokens int
}

// newAnthropicCmd creates a Cobra command for running codegen provided by Anthropc.
func newAnthropicCmd(globalOpts *globalOptions) *cobra.Command {
opts := &anthropicOptions{}

cmd := &cobra.Command{
Use: "anthropic",
Short: "Run codegen provided by anthropic",
SilenceUsage: true,
SilenceErrors: true,
Example: `genie anthropic -p "Create a python hello world"
genie anthropic -p prompt.txt`,
RunE: func(cmd *cobra.Command, args []string) error {
apiKey := opts.apiKey
if apiKey == "" {
apiKey = os.Getenv("ANTHROPIC_API_KEY")
}

cg, err := codegen.NewAnthropic(apiKey, func(o *codegen.AnthropicOptions) {
o.ModelName = opts.model
o.Temperature = opts.temperature
o.MaxTokens = opts.maxTokens
})
if err != nil {
return err
}

if err := run(context.Background(), globalOpts, cg); err != nil {
return err
}

return nil
},
}

cmd.Flags().StringVarP(&opts.apiKey, "api-key", "", "", "anthropic api key")
cmd.Flags().StringVarP(&opts.model, "model", "m", codegen.DefaultAnthropicModelName, "model to use")
cmd.Flags().IntVarP(&opts.maxTokens, "max-tokens", "", codegen.DefaultOpenAIMaxTokens, "max tokens to use")
cmd.Flags().Float64VarP(&opts.temperature, "temperature", "t", codegen.DefaultAnthropicTemperature, "temperature to use")

return cmd
}
63 changes: 63 additions & 0 deletions cmd/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/hupe1980/genie/pkg/codegen"
"github.com/spf13/cobra"
)

// openAIOptions holds the command-line flags for the openAI command.
type openAIOptions struct {
apiKey string
model string
temperature float32
maxTokens int
}

// newOpenAICmd creates a Cobra command for running codegen provided by OpenAI.
func newOpenAICmd(globalOpts *globalOptions) *cobra.Command {
opts := &openAIOptions{}

cmd := &cobra.Command{
Use: "openai",
Short: "Run codegen provided by openAI",
SilenceUsage: true,
SilenceErrors: true,
Example: `genie openai -p "Create a python hello world"
genie openai -p prompt.txt`,
RunE: func(cmd *cobra.Command, args []string) error {
apiKey := opts.apiKey
if apiKey == "" {
apiKey = os.Getenv("OPENAI_API_KEY")
}

cg, err := codegen.NewOpenAI(apiKey, func(o *codegen.OpenAIOptions) {
o.ModelName = opts.model
o.Temperature = opts.temperature
o.MaxTokens = opts.maxTokens
})
if err != nil {
return err
}

if err := run(context.Background(), globalOpts, cg); err != nil {
return err
}

fmt.Println()
fmt.Println(cg.Info())

return nil
},
}

cmd.Flags().StringVarP(&opts.apiKey, "api-key", "", "", "openAI api key")
cmd.Flags().StringVarP(&opts.model, "model", "m", codegen.DefaultOpenAIModelName, "model to use")
cmd.Flags().IntVarP(&opts.maxTokens, "max-tokens", "", codegen.DefaultOpenAIMaxTokens, "max tokens to use")
cmd.Flags().Float32VarP(&opts.temperature, "temperature", "t", codegen.DefaultOpenAITemperature, "temperature to use")

return cmd
}
Loading

0 comments on commit fb84260

Please sign in to comment.