使用Golang接入ChatGPT的方法
官方文档
官方文档链接https://platform.openai.com/docs/api-reference/introduction
官方收集的社区爱好者提供的SDK:https://platform.openai.com/docs/libraries/community-libraries
接入方法
注册好openai账号后,创建好API Keys,创建页面https://platform.openai.com/account/api-keys
API调用是有调用速率限制的,详细写可查看https://platform.openai.com/docs/guides/rate-limits/overview
有18美元的免费调用额度,超过后会收费

实现Demo
package main
import (
"context"
"fmt"
gogpt "github.com/sashabaranov/go-gpt3"
)
func main() {
c := gogpt.NewClient("填写你的API Keys")
ctx := context.Background()
req := gogpt.CompletionRequest{
Model: gogpt.GPT3TextDavinci003,
MaxTokens: 2048,
N: 1,
Stop: nil,
Temperature: 0.5,
Prompt: "使用Go实现二分查找算法",
}
resp, err := c.CreateCompletion(ctx, req)
if err != nil {
return
}
fmt.Println(resp.Choices[0].Text)
}参数说明
https://platform.openai.com/docs/api-reference/completions/create