赞
踩
ChatGPT-OpenAI注册使用保姆级教程?[开箱可用][免费使用]
先是go get github.com/sashabaranov/go-gpt3
参数很多,跟网页版的区别不大…
package main
import (
"context"
"fmt"
gogpt "github.com/sashabaranov/go-gpt3"
)
func main() {
token := "Your Key"
ask := "Please write a article About how to live in the china?"
example(token, ask)
}
func example(token string, prompt string) {
c := gogpt.NewClient(token)
ctx := context.Background()
req := gogpt.CompletionRequest{
Model: gogpt.GPT3TextDavinci001,
Temperature: 0.4,
MaxTokens: 1000,
TopP: 1,
FrequencyPenalty: 0,
PresencePenalty: 0,
BestOf: 1,
Prompt: prompt,
}
resp, err := c.CreateCompletion(ctx, req)
if err != nil {
return
}
fmt.Println(resp.Choices[0].Text)
}
main.go
package main
import (
"embed"
"github.com/gin-gonic/gin"
"html/template"
"io/ioutil"
"net/http"
"strings"
)
//go:embed static/* templates/*
var f embed.FS
func main() {
router := gin.Default()
// asset加载html
templates, err := loadTemplate()
if err != nil {
panic(err)
}
// 配置模板
router.SetHTMLTemplate(templates)
// 配置静态文件夹路径 第一个参数是api,第二个是文件夹路径
router.StaticFS("/static/", http.FS(f))
rootGroup := router.Group("/")
{
rootGroup.GET("/", Index)
rootGroup.GET("/index", Index)
rootGroup.GET("/test/", Test)
rootGroup.POST("/chatgpt", ChatGPT)
}
router.Run(":9090")
}
// 执行命令: go-assets-builder templates -o assets.go -p main
func loadTemplate() (*template.Template, error) {
t := template.New("")
for name, file := range Assets.Files {
// 可以用.tmpl .html
if file.IsDir() || !strings.HasSuffix(name, ".html") {
continue
}
h, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
t, err = t.New(name).Parse(string(h))
if err != nil {
return nil, err
}
}
return t, nil
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。