-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (37 loc) · 861 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"chatgpt/api"
"chatgpt/config"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type Message struct {
Msg string `json:"msg"`
}
func ChatGPTPost(c *gin.Context) {
m := Message{}
_ = c.BindJSON(&m)
retMsg, _ := api.Completions(m.Msg)
fmt.Printf("\nmsg: %s\nretMsg: %s\n", m.Msg, retMsg)
c.JSON(http.StatusOK, gin.H{"msg": m.Msg, "retMsg": retMsg})
}
func ChatGPTGet(c *gin.Context) {
msg := c.Query("msg")
if msg == "" {
c.String(http.StatusOK, "问题不能为空哦")
}
retMsg, _ := api.Completions(msg)
fmt.Printf("\nmsg: %s\nretMsg: %s\n", msg, retMsg)
c.String(http.StatusOK, retMsg)
}
func main() {
gin.DisableConsoleColor()
gin.SetMode(gin.ReleaseMode)
app := gin.New()
app.POST("/", ChatGPTPost)
app.GET("/", ChatGPTGet)
port := config.Get("PORT")
fmt.Printf("Port: %s\n", port)
_ = app.Run(port)
}