在之前的帖子里: https://www.sunp.eu.org/t/909257
我写了个 Go SDK ,包名:github.com/chatgp/gpt3, 对接了 ChatGPT 官方 API 。
今日官方 API 支持了新的对话模型 gpt-3.5-turbo ,我的 SDK 无需做任何改动,只增加了一个测试用例,可以直接使用,推荐给大家~
- 基本使用
package main
import (
"fmt"
"log"
"time"
"github.com/chatgp/gpt3"
)
func main() {
apiKey := "sk-xxx"
// new gpt-3 client
cli, _ := gpt3.NewClient(&gpt3.Options{
ApiKey: apiKey,
Timeout: 30 * time.Second,
Debug: true,
})
// request api
uri := "/v1/chat/completions"
params := map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]interface{}{
{"role": "user", "content": "hello 10 times"},
},
}
res, err := cli.Post(uri, params)
if err != nil {
log.Fatalf("request api failed: %v", err)
}
fmt.Printf("message is: %s", res.Get("choices.0.message.content").String())
// Output: xxx
}
- 测试用例:

- 参考:
ChatGPT 新模型 API 文档: https://platform.openai.com/docs/guides/chat
- 我的应用已经对接上最新版 API 了,欢迎大家体验~
