a working golang gpt-3 example

This commit is contained in:
Jeff Carr 2022-12-03 19:22:34 -06:00
parent cee5e5fffd
commit 17a8798b1b
5 changed files with 78 additions and 0 deletions

1
.gitignore vendored
View File

@ -28,3 +28,4 @@ example-ui-table/example-ui-table
autobuild/autobuild
example-flag/example-flag
example-gpt-3/example-gpt-3

9
example-gpt-3/Makefile Normal file
View File

@ -0,0 +1,9 @@
build:
# reset
GO111MODULE="off" go build -v -x
deps:
go get github.com/openai/gpt-3
get:
GO111MODULE="off" go get -v -u .

View File

@ -0,0 +1,18 @@
#!/bin/bash
#
export OPENAI_API_KEY=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
curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "Convert this text to a programmatic command:\n\nExample: Ask Constance if we need some bread\nOutput: send-msg `find constance` Do we need some bread?\n\nReach out to the ski store and figure out if I can get my skis fixed before I leave on Thursday",
"temperature": 0,
"max_tokens": 100,
"top_p": 1.0,
"frequency_penalty": 0.2,
"presence_penalty": 0.0,
"stop": ["\n"]
}'

View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"github.com/openai/gpt-3"
)
func main() {
client := gpt3.NewClient("API_KEY")
res, err := client.Evaluate("Hello, world!")
if err != nil {
panic(err)
}
fmt.Println(res.Text)
}

35
example-gpt-3/main.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/PullRequestInc/go-gpt3"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
apiKey := os.Getenv("OPENAI_API_KEY")
apiKey = "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"
if apiKey == "" {
log.Fatalln("Missing API KEY")
}
ctx := context.Background()
client := gpt3.NewClient(apiKey)
resp, err := client.Completion(ctx, gpt3.CompletionRequest{
Prompt: []string{"The first thing you should know about javascript is"},
MaxTokens: gpt3.IntPtr(30),
Stop: []string{"."},
Echo: true,
})
if err != nil {
log.Fatalln(err)
}
fmt.Println(resp.Choices[0].Text)
}