This commit is contained in:
2023-04-08 11:35:28 -05:00
parent f5a22be60c
commit af881d80b0
5 changed files with 122 additions and 35 deletions

View File

@@ -2,38 +2,12 @@ package controller
import (
"context"
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
openai "github.com/sashabaranov/go-openai"
)
var (
// Declare global variables
apiKey string
config *configStruct
)
type configStruct struct {
apiKey string `json : "API_KEY"`
}
func init() {
// Load values for global variables from environment variables
err := godotenv.Load(".env")
if err != nil {
fmt.Println(err.Error())
//return err
}
apiKey = os.Getenv("API_KEY")
}
// GeneralOpenAI godoc
//
// @Summary Gerneral ChatGPT
@@ -47,7 +21,7 @@ func init() {
func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
message := ctx.Query("message")
result, err := createChatCompletion(message)
result, err := createChatCompletion(c, message)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
}
@@ -68,7 +42,7 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
message := "want you to act as a travel guide. I will give you my location and you will give me suggestions " + ctx.Query("message")
result, err := createChatCompletion(message)
result, err := createChatCompletion(c, message)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
}
@@ -76,9 +50,9 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
ctx.String(http.StatusOK, result)
}
func createChatCompletion(message string) (string, error) {
func createChatCompletion(c *Controller, message string) (string, error) {
var client = openai.NewClient(apiKey)
var client = c.openaiClient
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{