Skip to content

Commit

Permalink
Merge pull request #40 from tcpiplab/GPT_API_experiment
Browse files Browse the repository at this point in the history
Gpt api experiment
  • Loading branch information
tcpiplab committed May 2, 2023
2 parents e4c06ba + 2f35d39 commit 47176d7
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/workspace.xml
.idea/
.DS_Store
callWordAPI.go
/.idea/dictionaries/*.xml
Expand Down
10 changes: 8 additions & 2 deletions GPT-TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
openaiAPIKey = "your_openai_api_key"
openaiAPIKey := os.Getenv("MY_API_KEY")
openaiAPIURL = "https://api.openai.com/v1/engines/davinci-codex/completions"
)

Expand Down Expand Up @@ -81,4 +81,10 @@ Make sure to replace `your_openai_api_key` with your actual API key from OpenAI.

This example demonstrates a simple way to call the GPT-3 API to rewrite sentences in a more meaningful and coherent way. The code can be easily integrated into your existing golang application.

Please note that using the GPT-3 API might incur costs, depending on your usage. You can find the pricing details on the OpenAI website: https://openai.com/pricing.
Please note that using the GPT-3 API might incur costs, depending on your usage. You can find the pricing details on the OpenAI website: https://openai.com/pricing.

-----------------------------------------

See https://platform.openai.com/docs/api-reference/completions/create

https://platform.openai.com/playground?mode=chat&model=gpt-4
122 changes: 78 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,68 +47,102 @@ Waiting for 60 seconds before clearing the clipboard.
█████████████████████████████████████████████████████████████60
```
# Examples
## Usage
```shell
$ ./passwordgen -h
Usage of ./passwordgen:
-erase
./passwordgen --erase[=false]
(default true)
./passwordgen --erase[=false]
(default true)
-examples
./passwordgen --examples
./passwordgen --examples
-grammatical
./passwordgen --grammatical

./passwordgen --grammatical

-grammatical-ai
./passwordgen --grammatical-ai
(Requires an openai.com GPT-4 API key)
-help
./passwordgen n
Where n is the length of the password.
Length must be the last argument.
./passwordgen n
Where n is the length of the password.
Length must be the last argument.
-hex
./passwordgen --hex
./passwordgen --hex
-interactive
./passwordgen --interactive[=false]
(default true)
./passwordgen --interactive[=false]
(default true)
-memorable
./passwordgen --memorable
./passwordgen --memorable
-mixed
./passwordgen --mixed
./passwordgen --mixed
-passphrases
./passwordgen --passphrases
./passwordgen --passphrases
-random
./passwordgen --random
(default true)
./passwordgen --random
(default true)
-word-chains
./passwordgen --word-chains
./passwordgen --word-chains
```
## Examples
```shell
$ ./passwordgen --examples 20
┌───────────────┬────────────────────────────────────────────────┐
│ --random │ *aoh%lg)ATxs&VL{#t6^ │
├───────────────┼────────────────────────────────────────────────┤
│ --hex │ A2DAC12B4E2EF942D0A6 │
├───────────────┼────────────────────────────────────────────────┤
│ --word-chains │ dancing_blob_angular_unison │
├───────────────┼────────────────────────────────────────────────┤
│ --mixed │ (tannery-shower)randomnessObject)40 │
├───────────────┼────────────────────────────────────────────────┤
│ --passphrases │ monument corporate confidant salutary aerobics │
├───────────────┼────────────────────────────────────────────────┤
│ --memorable │ [Buttonhole&Fatally&371] │
├───────────────┼────────────────────────────────────────────────┤
│ --grammatical │ Don't jump your courageous weakness lively. │
└───────────────┴────────────────────────────────────────────────┘
┌──────────────────┬──────────────────────────────────────┐
│ --random │ tRrLcl}Y{Mv#Tn4wmiDi │
├──────────────────┼──────────────────────────────────────┤
│ --hex │ 67F2B27CE6126C82C55E │
├──────────────────┼──────────────────────────────────────┤
│ --word-chains │ mullets\anatomist\sadness │
├──────────────────┼──────────────────────────────────────┤
│ --mixed │ Necessary/Tone2 │
├──────────────────┼──────────────────────────────────────┤
│ --passphrases │ dial chamomile trailing sheath straw │
├──────────────────┼──────────────────────────────────────┤
│ --memorable │ {Rearrange|Adventurously|1940} │
├──────────────────┼──────────────────────────────────────┤
│ --grammatical │ Don't slug the crazy. │
├──────────────────┼──────────────────────────────────────┤
│ --grammatical-ai │ He is like his grandfather. │
└──────────────────┴──────────────────────────────────────┘
```
## Grammatical and Grammatical-AI
Both `--grammatical` and `grammatical-ai` will generate grammatically correct sentences for use as passphrases. But the AI sentences will make a lot more sense to you. Use of the AI option requires that you have a valid GPT-4 API key in an environment variable named `GPT_API_KEY`.
Also note the usual trailing length integer on the command line. This is required but ignored. It is a bug that needs fixing.
```shell
/passwordgen --grammatical-ai --interactive=false 20
┌───┬─────────────────────────────────────────┐
│ 0 │ Then, they didn't deduce. │
├───┼─────────────────────────────────────────┤
│ 1 │ Is it time to sail?
├───┼─────────────────────────────────────────┤
│ 2 │ Help the willing man. │
├───┼─────────────────────────────────────────┤
│ 3 │ Their impossible series didn't succeed. │
├───┼─────────────────────────────────────────┤
│ 4 │ That is through the window. │
├───┼─────────────────────────────────────────┤
│ 5 │ Undermine my assumption. │
├───┼─────────────────────────────────────────┤
│ 6 │ Don't leave their stupidity. │
├───┼─────────────────────────────────────────┤
│ 7 │ He patted her well-worn bottom. │
├───┼─────────────────────────────────────────┤
│ 8 │ Grill a united dish. │
└───┴─────────────────────────────────────────┘
```
Expand Down Expand Up @@ -255,8 +289,8 @@ passwordgen --hex 4
# Building releases for multiple platforms

```shell
GOOS=darwin GOARCH=arm64 go build -o Release-Binaries/v1.6.x/passwordgen-v1.6.0-darwin-arm64
GOOS=darwin GOARCH=amd64 go build -o Release-Binaries/v1.6.x/passwordgen-v1.6.0-darwin-amd64
GOOS=windows GOARCH=amd64 go build -o Release-Binaries/v1.6.x/passwordgen-v1.6.0-windows-amd64.exe
GOOS=linux GOARCH=amd64 go build -o Release-Binaries/v1.6.x/passwordgen-v1.6.0-linux-amd64
GOOS=darwin GOARCH=arm64 go build -o Release-Binaries/v1.7.x/passwordgen-v1.7.0-darwin-arm64
GOOS=darwin GOARCH=amd64 go build -o Release-Binaries/v1.7.x/passwordgen-v1.7.0-darwin-amd64
GOOS=windows GOARCH=amd64 go build -o Release-Binaries/v1.7.x/passwordgen-v1.7.0-windows-amd64.exe
GOOS=linux GOARCH=amd64 go build -o Release-Binaries/v1.7.x/passwordgen-v1.7.0-linux-amd64
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
Expand Down
14 changes: 10 additions & 4 deletions inputHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func argsHandler() (
randomHex *bool,
examples *bool,
grammatical *bool,
grammaticalAI *bool,
) {
help = flag.Bool(
"help",
Expand All @@ -28,7 +29,7 @@ func argsHandler() (

if *help {
flag.Usage()
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
}

// Interactive mode is the default
Expand Down Expand Up @@ -82,15 +83,20 @@ func argsHandler() (
false,
"./passwordgen --grammatical\n")

grammaticalAI = flag.Bool(
"grammatical-ai",
false,
"./passwordgen --grammatical-ai\n(Requires an openai.com GPT-4 API key)")

flag.Parse()

// For now the length is mandatory and must be the last arg
// TODO: For now the length is mandatory and must be the last arg
if len(os.Args) < 2 {

color.HiRed("\nPlease provide a password length as the last argument\nOr -h for help.\n")
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
}
return interactive, erase, randomPasswords, wordChains, mixedPasswords, nil, passPhrases, memorable, randomHex, examples, grammatical
return interactive, erase, randomPasswords, wordChains, mixedPasswords, nil, passPhrases, memorable, randomHex, examples, grammatical, grammaticalAI
}

func ifInteractive(interactive *bool, rows int) bool {
Expand Down
Binary file modified passwordgen
Binary file not shown.
9 changes: 6 additions & 3 deletions passwordgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {

OS = detectOS()

interactive, erase, randomPasswords, wordChains, mixedPasswords, _, passPhrases, memorable, randomHex, examples, grammatical := argsHandler()
interactive, erase, randomPasswords, wordChains, mixedPasswords, _, passPhrases, memorable, randomHex, examples, grammatical, grammaticalAI := argsHandler()

//if *done {
// return
Expand Down Expand Up @@ -101,18 +101,21 @@ func main() {
if *grammatical {
*randomPasswords = false
}
if *grammaticalAI {
*randomPasswords = false
}

arrayPasswords := make([]string, rows)

if OS == "darwin" || OS == "linux" || OS == "unix" {

// Fill the screen with passwords
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex, *grammatical)
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex, *grammatical, *grammaticalAI)

} else if OS == "windows" {

// Fill the screen with passwords
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex, *grammatical)
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex, *grammatical, *grammaticalAI)
//arrayPasswords = printPasswordTableWindows(
// rows,
// requestedPasswordLength,
Expand Down
Loading

0 comments on commit 47176d7

Please sign in to comment.