openai gpt-3 sdk, written by golang.
login the openai official website, and get your own API_KEY.
- install gpt3 sdk
go get -u github.com/chatgp/gpt3
- request api with gpt-3 client
package main
import (
"fmt"
"log"
"time"
"github.com/chatgp/gpt3"
)
func main() {
apiKey := "xxx"
// new gpt-3 client
cli, _ := gpt3.NewClient(&gpt3.Options{
ApiKey: apiKey,
Timeout: 30 * time.Second,
Debug: true,
})
// request api
uri := "/v1/models"
res, err := cli.Get(uri)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
for _, v := range res.Get("data").Array() {
fmt.Printf("model id: %s\n", v.Get("id").String())
}
}
see available apis in OpenAI documents
there are some examples under the examples
folder, check and see how to request other apis.
cli := getClient()
uri := "/v1/chat/completions"
params := map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]interface{}{
{"role": "user", "content": "hello"},
},
}
res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
message := res.Get("choices.0.message.content").String()
fmt.Printf("message is: %s", message)
// Output: xxx
uri := "/v1/completions"
params := map[string]interface{}{
"model": "text-davinci-003",
"prompt": "say hello three times",
"max_tokens": 2048,
"temperature": 0.9,
"n": 1,
"stream": false,
}
res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
fmt.Println(res.GetString("choices.0.text"))
uri := "/v1/edits"
params := map[string]interface{}{
"model": "text-davinci-edit-001",
"input": "Are you hapy today?",
"instruction": "fix mistake",
"temperature": 0.9,
"n": 1,
}
res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
fmt.Println(res.GetString("choices.0.text"))
uri := "/v1/images/generations"
params := map[string]interface{}{
"prompt": "a beautiful girl with big eyes",
"n": 1,
"size": "256x256",
"response_format": "url",
}
res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
fmt.Println(res.GetString("data.0.url"))
-
Telegram Group: ChatGPT Creators
-
Discord Server: ChatGPT Creators
-
Wechat Group: ChatGPT Creators