Updatex
This commit is contained in:
commit
cfbdfae65d
3 changed files with 87 additions and 0 deletions
46
main.go
Normal file
46
main.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue