This commit is contained in:
Kare-Udon 2023-03-04 23:50:11 +08:00
commit cfbdfae65d
3 changed files with 87 additions and 0 deletions

46
main.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
"os"
)
func main() {
// get token
token := os.Getenv("OPEN_AI_KEY")
r := gin.Default()
// /getAns?msg=foobar
r.GET("/getAns", func(c *gin.Context) {
msg := c.Query("msg")
ans := GetChatGPTAns(token, msg)
c.JSON(200, gin.H{
"answer": ans,
})
})
err := r.Run()
if err != nil {
return
}
}
func GetChatGPTAns(token string, msg string) string {
c := openai.NewClient(token)
resp, err := c.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: msg,
},
},
},
)
if err != nil {
return ""
}
return resp.Choices[0].Message.Content
}