forked from aurora-develop/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
188 lines (173 loc) · 4.74 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
chatgpt_request_converter "aurora/conversion/requests/chatgpt"
"aurora/internal/chatgpt"
official_types "aurora/typings/official"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
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) {
var original_request official_types.APIRequest
err := c.BindJSON(&original_request)
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(),
}})
return
}
proxy_url := ProxyIP.GetProxyIP()
secret := ACCESS_TOKENS.GetSecret()
authHeader := c.GetHeader("Authorization")
if authHeader != "" {
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiI") {
secret = ACCESS_TOKENS.GenerateTempToken(customAccessToken)
}
}
if secret == nil {
c.JSON(400, gin.H{"error": "Not Account Found."})
c.Abort()
return
}
uid := uuid.NewString()
turnStile, status, err := chatgpt.InitTurnStile(secret, proxy_url)
if err != nil {
c.JSON(status, gin.H{
"message": err.Error(),
"type": "InitTurnStile_request_error",
"param": err,
"code": status,
})
return
}
if !secret.IsFree {
err = chatgpt.InitWSConn(secret.Token, uid, proxy_url)
if err != nil {
c.JSON(500, gin.H{"error": "unable to create ws tunnel"})
return
}
}
// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request, secret, turnStile.Arkose, proxy_url)
response, err := chatgpt.POSTconversation(translated_request, secret, turnStile, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
defer response.Body.Close()
if chatgpt.Handle_request_error(c, response) {
return
}
var full_response string
for i := 3; i > 0; i-- {
var continue_info *chatgpt.ContinueInfo
var response_part string
response_part, continue_info = chatgpt.Handler(c, response, secret, uid, translated_request, original_request.Stream)
full_response += response_part
if continue_info == nil {
break
}
translated_request.Messages = nil
translated_request.Action = "continue"
translated_request.ConversationID = continue_info.ConversationID
translated_request.ParentMessageID = continue_info.ParentID
if turnStile.Arkose {
chatgpt_request_converter.RenewTokenForRequest(&translated_request, secret.PUID, proxy_url)
}
response, err = chatgpt.POSTconversation(translated_request, secret, turnStile, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
defer response.Body.Close()
if chatgpt.Handle_request_error(c, response) {
return
}
}
if c.Writer.Status() != 200 {
return
}
if !original_request.Stream {
c.JSON(200, official_types.NewChatCompletion(full_response))
} else {
c.String(200, "data: [DONE]\n\n")
}
chatgpt.UnlockSpecConn(secret.Token, uid)
}
func engines_handler(c *gin.Context) {
proxy_url := ProxyIP.GetProxyIP()
secret := ACCESS_TOKENS.GetSecret()
authHeader := c.GetHeader("Authorization")
if authHeader != "" {
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
// Check if customAccessToken starts with sk-
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiI") {
secret = ACCESS_TOKENS.GenerateTempToken(customAccessToken)
}
}
if secret.Token == "" || secret == nil {
c.JSON(400, gin.H{"error": "Not Account Found."})
return
}
resp, status, err := chatgpt.GETengines(secret, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
type ResData struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
OwnedBy string `json:"owned_by"`
}
type JSONData struct {
Object string `json:"object"`
Data []ResData `json:"data"`
}
modelS := JSONData{
Object: "list",
}
var resModelList []ResData
if len(resp.Models) > 2 {
res_data := ResData{
ID: "gpt-4-mobile",
Object: "model",
Created: 1685474247,
OwnedBy: "openai",
}
resModelList = append(resModelList, res_data)
}
for _, model := range resp.Models {
res_data := ResData{
ID: model.Slug,
Object: "model",
Created: 1685474247,
OwnedBy: "openai",
}
if model.Slug == "text-davinci-002-render-sha" {
res_data.ID = "gpt-3.5-turbo"
}
resModelList = append(resModelList, res_data)
}
modelS.Data = resModelList
c.JSON(status, modelS)
}