a working golang gpt-3 example
This commit is contained in:
parent
cee5e5fffd
commit
17a8798b1b
|
@ -28,3 +28,4 @@ example-ui-table/example-ui-table
|
||||||
|
|
||||||
autobuild/autobuild
|
autobuild/autobuild
|
||||||
example-flag/example-flag
|
example-flag/example-flag
|
||||||
|
example-gpt-3/example-gpt-3
|
||||||
|
|
|
@ -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 .
|
|
@ -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"]
|
||||||
|
}'
|
|
@ -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)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue