Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
handle engines
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Jun 16, 2023
1 parent 45fdad5 commit c4e9b23
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
29 changes: 25 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ import (
)

func openaiHandler(c *gin.Context) {
var authorizations struct {
OpenAI_Email string `json:"openai_email"`
OpenAI_Password string `json:"openai_password"`
Official_API_Key string `json:"official_api_key"`
}
err := c.BindJSON(&authorizations)
if err != nil {
c.JSON(400, gin.H{"error": "JSON invalid"})
}
os.Setenv("OPENAI_EMAIL", authorizations.OpenAI_Email)
os.Setenv("OPENAI_PASSWORD", authorizations.OpenAI_Password)
if authorizations.OpenAI_Email != "" && authorizations.OpenAI_Password != "" {
os.Setenv("OPENAI_EMAIL", authorizations.OpenAI_Email)
os.Setenv("OPENAI_PASSWORD", authorizations.OpenAI_Password)
}
if authorizations.Official_API_Key != "" {
os.Setenv("OFFICIAL_API_KEY", authorizations.Official_API_Key)
}
c.String(200, "OpenAI credentials updated")
}

Expand Down Expand Up @@ -98,7 +108,7 @@ func nightmare(c *gin.Context) {
// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request)

response, err := chatgpt.Send_request(translated_request, token)
response, err := chatgpt.POSTconversation(translated_request, token)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand All @@ -123,7 +133,7 @@ func nightmare(c *gin.Context) {
translated_request.Action = "continue"
translated_request.ConversationID = continue_info.ConversationID
translated_request.ParentMessageID = continue_info.ParentID
response, err = chatgpt.Send_request(translated_request, token)
response, err = chatgpt.POSTconversation(translated_request, token)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand All @@ -142,3 +152,14 @@ func nightmare(c *gin.Context) {
}

}

func engines_handler(c *gin.Context) {
resp, status, err := chatgpt.GETengines()
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
c.JSON(status, resp)
}
16 changes: 15 additions & 1 deletion internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func random_int(min int, max int) int {
return min + rand.Intn(max-min)
}

func Send_request(message chatgpt_types.ChatGPTRequest, access_token string) (*http.Response, error) {
func POSTconversation(message chatgpt_types.ChatGPTRequest, access_token string) (*http.Response, error) {
if http_proxy != "" && len(proxies) == 0 {
client.SetProxy(http_proxy)
}
Expand Down Expand Up @@ -223,3 +223,17 @@ func Handler(c *gin.Context, response *http.Response, token string, translated_r
ParentID: original_response.Message.ID,
}
}

func GETengines() (interface{}, int, error) {
url := "https://api.openai.com/v1/engines"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer "+os.Getenv("OFFICIAL_API_KEY"))
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
var result interface{}
json.NewDecoder(resp.Body).Decode(&result)
return result, resp.StatusCode, nil
}
45 changes: 21 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,29 @@ var HOST string
var PORT string
var ACCESS_TOKENS tokens.AccessToken

var authorizations struct {
OpenAI_Email string `json:"openai_email"`
OpenAI_Password string `json:"openai_password"`
}

func init() {
authorizations.OpenAI_Email = os.Getenv("OPENAI_EMAIL")
authorizations.OpenAI_Password = os.Getenv("OPENAI_PASSWORD")
if authorizations.OpenAI_Email != "" && authorizations.OpenAI_Password != "" {
go func() {
for {
authenticator := auth.NewAuthenticator(authorizations.OpenAI_Email, authorizations.OpenAI_Password, os.Getenv("http_proxy"))
err := authenticator.Begin()
if err != nil {
log.Println(err)
break
}
puid, err := authenticator.GetPUID()
if err != nil {
break
}
os.Setenv("PUID", puid)
println(puid)
go func() {
for {
if os.Getenv("OPENAI_EMAIL") == "" || os.Getenv("OPENAI_PASSWORD") == "" {
time.Sleep(24 * time.Hour * 7)
continue
}
}()
}
authenticator := auth.NewAuthenticator(os.Getenv("OPENAI_EMAIL"), os.Getenv("OPENAI_PASSWORD"), os.Getenv("http_proxy"))
err := authenticator.Begin()
if err != nil {
log.Println(err)
break
}
puid, err := authenticator.GetPUID()
if err != nil {
break
}
os.Setenv("PUID", puid)
println(puid)
time.Sleep(24 * time.Hour * 7)
}
}()

HOST = os.Getenv("SERVER_HOST")
PORT = os.Getenv("SERVER_PORT")
if HOST == "" {
Expand Down Expand Up @@ -103,5 +99,6 @@ func main() {
/// Public routes
router.OPTIONS("/v1/chat/completions", optionsHandler)
router.POST("/v1/chat/completions", Authorization, nightmare)
router.GET("/v1/engines", Authorization, engines_handler)
endless.ListenAndServe(HOST+":"+PORT, router)
}

0 comments on commit c4e9b23

Please sign in to comment.