How to quickly access chatgpt? [Introducing go-gpt3]
-
- The above introduces how to use chatgpt on the web page?
- V1. Introduce how to use chatgpt in golang?
-
- 1. Check the chatgpt projects recommended by the official website
- 2.Access go-gpt3
- 3. Use and run it in your own project... (because the example is difficult to understand, so the properties are configured one by one)
- 4. The running results are as shown in the figure...
- 5. Compare the running results of the web version...
- V2. Gin/Html has been integrated to create a web version...
- Complete code
The above introduces how to use chatgpt on the web page?
ChatGPT-OpenAI registration and use nanny-level tutorial? [Available out of the box] [Free to use]
V1. Introduce how to use chatgpt in golang?
1. Check the chatgpt projects recommended by the official website
First look at the projects recommended by the official website
2.Access go-gpt3
3. Use and run it in your own project... (because the example is difficult to understand, so the properties are configured one by one)
3.1 Installation project
First, go get github.com/sashabaranov/go-gpt3
has many parameters, which is not much different from the web version...
3.2 Replace with your own code
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)
}
3.3 Change to your own key
4. The running results are as shown in the figure...
5. Compare the running results of the web version...
V2. Gin/Html has been integrated to create a web version...
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
}