2023-04-05 10:07:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-09 18:45:37 +00:00
|
|
|
chatgpt_request_converter "freechatgpt/conversion/requests/chatgpt"
|
2023-06-09 19:30:45 +00:00
|
|
|
chatgpt "freechatgpt/internal/chatgpt"
|
2023-04-05 10:07:07 +00:00
|
|
|
"freechatgpt/internal/tokens"
|
2023-06-09 18:45:37 +00:00
|
|
|
official_types "freechatgpt/typings/official"
|
2023-04-14 03:52:31 +00:00
|
|
|
"os"
|
2023-04-05 10:07:07 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-05-30 05:46:20 +00:00
|
|
|
func openaiHandler(c *gin.Context) {
|
|
|
|
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)
|
2023-06-04 12:45:48 +00:00
|
|
|
c.String(200, "OpenAI credentials updated")
|
2023-05-30 05:46:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 10:07:07 +00:00
|
|
|
func passwordHandler(c *gin.Context) {
|
|
|
|
// Get the password from the request (json) and update the password
|
|
|
|
type password_struct struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
var password password_struct
|
|
|
|
err := c.BindJSON(&password)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, "password not provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ADMIN_PASSWORD = password.Password
|
2023-04-14 03:52:31 +00:00
|
|
|
// Set environment variable
|
|
|
|
os.Setenv("ADMIN_PASSWORD", ADMIN_PASSWORD)
|
2023-04-05 10:07:07 +00:00
|
|
|
c.String(200, "password updated")
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:19:54 +00:00
|
|
|
func puidHandler(c *gin.Context) {
|
|
|
|
// Get the password from the request (json) and update the password
|
|
|
|
type puid_struct struct {
|
|
|
|
PUID string `json:"puid"`
|
|
|
|
}
|
|
|
|
var puid puid_struct
|
|
|
|
err := c.BindJSON(&puid)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, "puid not provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Set environment variable
|
|
|
|
os.Setenv("PUID", puid.PUID)
|
|
|
|
c.String(200, "puid updated")
|
|
|
|
}
|
|
|
|
|
2023-04-05 10:07:07 +00:00
|
|
|
func tokensHandler(c *gin.Context) {
|
|
|
|
// Get the request_tokens from the request (json) and update the request_tokens
|
|
|
|
var request_tokens []string
|
|
|
|
err := c.BindJSON(&request_tokens)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, "tokens not provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ACCESS_TOKENS = tokens.NewAccessToken(request_tokens)
|
|
|
|
c.String(200, "tokens updated")
|
|
|
|
}
|
|
|
|
func optionsHandler(c *gin.Context) {
|
|
|
|
// Set headers for CORS
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
|
|
c.Header("Access-Control-Allow-Methods", "POST")
|
|
|
|
c.Header("Access-Control-Allow-Headers", "*")
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"message": "pong",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func nightmare(c *gin.Context) {
|
2023-06-09 18:45:37 +00:00
|
|
|
var original_request official_types.APIRequest
|
2023-04-08 12:01:00 +00:00
|
|
|
err := c.BindJSON(&original_request)
|
2023-05-17 13:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, gin.H{"error": gin.H{
|
|
|
|
"message": "Request must be proper JSON",
|
|
|
|
"type": "invalid_request_error",
|
|
|
|
"param": nil,
|
|
|
|
"code": err.Error(),
|
|
|
|
}})
|
|
|
|
}
|
2023-04-12 02:31:44 +00:00
|
|
|
|
2023-05-28 12:12:52 +00:00
|
|
|
authHeader := c.GetHeader("Authorization")
|
2023-04-12 02:31:44 +00:00
|
|
|
token := ACCESS_TOKENS.GetToken()
|
2023-05-28 12:12:52 +00:00
|
|
|
if authHeader != "" {
|
|
|
|
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
|
|
|
|
// Check if customAccessToken starts with sk-
|
|
|
|
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1UaEVOVUpHTkVNMVFURTRNMEZCTWpkQ05UZzVNRFUxUlRVd1FVSkRNRU13UmtGRVFrRXpSZyJ9") {
|
|
|
|
token = customAccessToken
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 20:10:55 +00:00
|
|
|
// Convert the chat request to a ChatGPT request
|
|
|
|
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request)
|
2023-04-12 02:31:44 +00:00
|
|
|
|
2023-06-09 19:57:36 +00:00
|
|
|
response, err := chatgpt.Send_request(translated_request, token)
|
|
|
|
if err != nil {
|
2023-06-13 09:39:21 +00:00
|
|
|
c.JSON(500, gin.H{
|
|
|
|
"error": "error sending request",
|
2023-06-09 19:57:36 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if chatgpt.Handle_request_error(c, response) {
|
|
|
|
return
|
|
|
|
}
|
2023-06-09 20:10:55 +00:00
|
|
|
var full_response string
|
2023-06-13 10:10:37 +00:00
|
|
|
for i := 2; i > 0; i-- {
|
2023-06-09 20:10:55 +00:00
|
|
|
var continue_info *chatgpt.ContinueInfo
|
|
|
|
var response_part string
|
|
|
|
response_part, continue_info = chatgpt.Handler(c, response, token, translated_request, original_request.Stream)
|
|
|
|
full_response += response_part
|
|
|
|
if continue_info == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
println("Continuing conversation")
|
|
|
|
translated_request.Messages = nil
|
|
|
|
translated_request.Action = "continue"
|
|
|
|
translated_request.ConversationID = continue_info.ConversationID
|
|
|
|
translated_request.ParentMessageID = continue_info.ParentID
|
|
|
|
response, err = chatgpt.Send_request(translated_request, token)
|
|
|
|
if err != nil {
|
2023-06-13 10:10:27 +00:00
|
|
|
c.JSON(500, gin.H{
|
|
|
|
"error": "error sending request",
|
2023-06-09 20:10:55 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if chatgpt.Handle_request_error(c, response) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 19:45:03 +00:00
|
|
|
if !original_request.Stream {
|
|
|
|
c.JSON(200, official_types.NewChatCompletion(full_response))
|
2023-06-09 20:29:01 +00:00
|
|
|
} else {
|
|
|
|
c.String(200, "data: [DONE]\n\n")
|
2023-06-09 19:45:03 +00:00
|
|
|
}
|
2023-04-05 10:07:07 +00:00
|
|
|
|
|
|
|
}
|