Skip to content

Commit

Permalink
refactor(openai.go): delete cmp command functions & make functions mo…
Browse files Browse the repository at this point in the history
…re generic
  • Loading branch information
serhhatsari committed Sep 16, 2023
1 parent 6b0e7a5 commit 27f584b
Showing 1 changed file with 10 additions and 60 deletions.
70 changes: 10 additions & 60 deletions pkg/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openai

import (
"bytes"
"github.com/serhhatsari/askgpt/internal/utils"
"io"
"net/http"
"os"
Expand All @@ -10,85 +11,34 @@ import (
)

const (
gptUrl = "https://api.openai.com/v1/chat/completions"
completionsUrl = "https://api.openai.com/v1/completions"
imageUrl = "https://api.openai.com/v1/images/generations"
gptUrl = "https://api.openai.com/v1/chat/completions"
imageUrl = "https://api.openai.com/v1/images/generations"
)

var openaiApiKey string

func SendRequestToChatGPT(jsonBody []byte) []byte {
setEnv()

req := createGPTRequest(jsonBody)

req = addHeaders(req)

utils.CheckToken()
req := createRequest(jsonBody, gptUrl)
return sendRequest(req)
}

func SendRequestToCompletions(jsonBody []byte) []byte {
setEnv()
req := createCompletionsRequest(jsonBody)
req = addHeaders(req)
pterm.Info.Println("Request sent to OpenAI. Waiting for response...")
res := sendRequest(req)
pterm.Info.Println("Response received from OpenAI:")
return res
}

func SendRequestToDallE(jsonBody []byte) []byte {
setEnv()
req := createDallERequest(jsonBody)
req = addHeaders(req)
utils.CheckToken()
req := createRequest(jsonBody, imageUrl)
pterm.Info.Println("Request sent to OpenAI. Waiting for response...")
res := sendRequest(req)
pterm.Info.Println("Response received from OpenAI:")
return res
}

func setEnv() {
openaiApiKey = os.Getenv("OPENAI_API_KEY")
if openaiApiKey == "" {
pterm.Error.Println("Please set the OPENAI_API_KEY environment variable.")
os.Exit(1)
}
}

func createGPTRequest(jsonBody []byte) *http.Request {

// Create the HTTP request
req, err := http.NewRequest("POST", gptUrl, bytes.NewBuffer(jsonBody))
if err != nil {
pterm.Error.Println("Error creating request for ChatGPT")
os.Exit(1)
}
return req
}

func createCompletionsRequest(jsonBody []byte) *http.Request {

// Create the HTTP request
req, err := http.NewRequest("POST", completionsUrl, bytes.NewBuffer(jsonBody))
if err != nil {
pterm.Error.Println("Error creating request for completions")
os.Exit(1)
}
return req
}

func createDallERequest(jsonBody []byte) *http.Request {
func createRequest(jsonBody []byte, url string) *http.Request {
// Create the HTTP request
req, err := http.NewRequest("POST", imageUrl, bytes.NewBuffer(jsonBody))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
pterm.Error.Println("Error creating request for Dall-E")
pterm.Error.Println("Error creating request")
os.Exit(1)
}
return req
}

func addHeaders(req *http.Request) *http.Request {
// Set the headers
req.Header.Set("Authorization", "Bearer "+openaiApiKey)
req.Header.Set("Content-Type", "application/json")
return req
Expand Down

0 comments on commit 27f584b

Please sign in to comment.