2023-04-02 15:15:09 +00:00
|
|
|
package chatgpt
|
|
|
|
|
|
|
|
import (
|
2023-05-03 22:36:17 +00:00
|
|
|
"bufio"
|
2023-04-02 15:15:09 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-06-09 18:45:37 +00:00
|
|
|
chatgpt_types "freechatgpt/typings/chatgpt"
|
2023-06-09 19:30:45 +00:00
|
|
|
"io"
|
2023-05-03 22:36:17 +00:00
|
|
|
"math/rand"
|
2023-04-20 00:22:03 +00:00
|
|
|
"os"
|
2023-05-03 22:36:17 +00:00
|
|
|
"strings"
|
2023-04-02 15:15:09 +00:00
|
|
|
|
|
|
|
http "github.com/bogdanfinn/fhttp"
|
|
|
|
tls_client "github.com/bogdanfinn/tls-client"
|
2023-06-09 19:30:45 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
chatgpt_response_converter "freechatgpt/conversion/response/chatgpt"
|
|
|
|
|
|
|
|
// chatgpt "freechatgpt/internal/chatgpt"
|
|
|
|
|
|
|
|
official_types "freechatgpt/typings/official"
|
2023-04-02 15:15:09 +00:00
|
|
|
)
|
|
|
|
|
2023-05-03 22:36:17 +00:00
|
|
|
var proxies []string
|
|
|
|
|
2023-04-02 15:15:09 +00:00
|
|
|
var (
|
|
|
|
jar = tls_client.NewCookieJar()
|
|
|
|
options = []tls_client.HttpClientOption{
|
|
|
|
tls_client.WithTimeoutSeconds(360),
|
2023-06-12 14:48:04 +00:00
|
|
|
tls_client.WithClientProfile(tls_client.Safari_IOS_15_5),
|
2023-04-02 15:15:09 +00:00
|
|
|
tls_client.WithNotFollowRedirects(),
|
|
|
|
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
|
2023-04-20 06:24:04 +00:00
|
|
|
// Disable SSL verification
|
|
|
|
tls_client.WithInsecureSkipVerify(),
|
2023-04-02 15:15:09 +00:00
|
|
|
}
|
2023-04-22 02:30:23 +00:00
|
|
|
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
|
|
|
|
http_proxy = os.Getenv("http_proxy")
|
2023-04-20 00:22:03 +00:00
|
|
|
API_REVERSE_PROXY = os.Getenv("API_REVERSE_PROXY")
|
2023-04-02 15:15:09 +00:00
|
|
|
)
|
|
|
|
|
2023-05-03 22:36:17 +00:00
|
|
|
func init() {
|
|
|
|
// Check for proxies.txt
|
|
|
|
if _, err := os.Stat("proxies.txt"); err == nil {
|
|
|
|
// Each line is a proxy, put in proxies array
|
|
|
|
file, _ := os.Open("proxies.txt")
|
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
// Split line by :
|
|
|
|
proxy := scanner.Text()
|
|
|
|
proxy_parts := strings.Split(proxy, ":")
|
|
|
|
if len(proxy_parts) == 2 {
|
|
|
|
proxy = "socks5://" + proxy
|
|
|
|
} else if len(proxy_parts) == 4 {
|
|
|
|
proxy = "socks5://" + proxy_parts[2] + ":" + proxy_parts[3] + "@" + proxy_parts[0] + ":" + proxy_parts[1]
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func random_int(min int, max int) int {
|
|
|
|
return min + rand.Intn(max-min)
|
|
|
|
}
|
|
|
|
|
2023-06-09 19:55:39 +00:00
|
|
|
func Send_request(message chatgpt_types.ChatGPTRequest, access_token string) (*http.Response, error) {
|
2023-05-15 11:29:09 +00:00
|
|
|
if http_proxy != "" && len(proxies) == 0 {
|
2023-04-22 02:30:23 +00:00
|
|
|
client.SetProxy(http_proxy)
|
|
|
|
}
|
2023-05-03 22:36:17 +00:00
|
|
|
// Take random proxy from proxies.txt
|
2023-05-15 12:10:53 +00:00
|
|
|
if len(proxies) > 0 {
|
|
|
|
client.SetProxy(proxies[random_int(0, len(proxies))])
|
2023-05-03 22:36:17 +00:00
|
|
|
}
|
2023-04-12 02:31:44 +00:00
|
|
|
|
2023-05-01 08:41:48 +00:00
|
|
|
apiUrl := "https://chat.openai.com/backend-api/conversation"
|
2023-04-20 00:22:03 +00:00
|
|
|
if API_REVERSE_PROXY != "" {
|
|
|
|
apiUrl = API_REVERSE_PROXY
|
|
|
|
}
|
2023-04-12 02:31:44 +00:00
|
|
|
|
2023-04-02 15:15:09 +00:00
|
|
|
// JSONify the body and add it to the request
|
2023-04-02 16:37:16 +00:00
|
|
|
body_json, err := json.Marshal(message)
|
2023-04-02 15:15:09 +00:00
|
|
|
if err != nil {
|
2023-04-02 16:37:16 +00:00
|
|
|
return &http.Response{}, err
|
2023-04-02 15:15:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 02:31:44 +00:00
|
|
|
request, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewBuffer(body_json))
|
2023-04-02 16:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return &http.Response{}, err
|
|
|
|
}
|
2023-05-10 06:03:06 +00:00
|
|
|
// Clear cookies
|
2023-05-24 15:23:03 +00:00
|
|
|
if os.Getenv("PUID") != "" {
|
|
|
|
request.Header.Set("Cookie", "_puid="+os.Getenv("PUID")+";")
|
|
|
|
}
|
2023-04-02 15:15:09 +00:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2023-05-01 08:42:09 +00:00
|
|
|
request.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
|
2023-04-02 16:37:16 +00:00
|
|
|
request.Header.Set("Accept", "*/*")
|
|
|
|
if access_token != "" {
|
|
|
|
request.Header.Set("Authorization", "Bearer "+access_token)
|
|
|
|
}
|
2023-04-02 15:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return &http.Response{}, err
|
|
|
|
}
|
|
|
|
response, err := client.Do(request)
|
|
|
|
return response, err
|
|
|
|
}
|
2023-06-09 19:30:45 +00:00
|
|
|
|
2023-06-09 19:55:39 +00:00
|
|
|
// Returns whether an error was handled
|
2023-06-09 19:57:36 +00:00
|
|
|
func Handle_request_error(c *gin.Context, response *http.Response) bool {
|
2023-06-09 19:30:45 +00:00
|
|
|
if response.StatusCode != 200 {
|
|
|
|
// Try read response body as JSON
|
|
|
|
var error_response map[string]interface{}
|
2023-06-09 19:55:39 +00:00
|
|
|
err := json.NewDecoder(response.Body).Decode(&error_response)
|
2023-06-09 19:30:45 +00:00
|
|
|
if err != nil {
|
|
|
|
// Read response body
|
|
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
c.JSON(500, gin.H{"error": gin.H{
|
|
|
|
"message": "Unknown error",
|
|
|
|
"type": "internal_server_error",
|
|
|
|
"param": nil,
|
|
|
|
"code": "500",
|
|
|
|
"details": string(body),
|
|
|
|
}})
|
2023-06-09 19:55:39 +00:00
|
|
|
return true
|
2023-06-09 19:30:45 +00:00
|
|
|
}
|
|
|
|
c.JSON(response.StatusCode, gin.H{"error": gin.H{
|
|
|
|
"message": error_response["detail"],
|
|
|
|
"type": response.Status,
|
|
|
|
"param": nil,
|
|
|
|
"code": "error",
|
|
|
|
}})
|
2023-06-09 19:55:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:10:55 +00:00
|
|
|
type ContinueInfo struct {
|
|
|
|
ConversationID string `json:"conversation_id"`
|
|
|
|
ParentID string `json:"parent_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Handler(c *gin.Context, response *http.Response, token string, translated_request chatgpt_types.ChatGPTRequest, stream bool) (string, *ContinueInfo) {
|
2023-06-09 19:55:39 +00:00
|
|
|
max_tokens := false
|
|
|
|
|
2023-06-09 19:30:45 +00:00
|
|
|
// Create a bufio.Reader from the response body
|
|
|
|
reader := bufio.NewReader(response.Body)
|
|
|
|
|
|
|
|
// Read the response byte by byte until a newline character is encountered
|
|
|
|
if stream {
|
|
|
|
// Response content type is text/event-stream
|
|
|
|
c.Header("Content-Type", "text/event-stream")
|
|
|
|
} else {
|
|
|
|
// Response content type is application/json
|
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
}
|
2023-06-09 19:49:13 +00:00
|
|
|
var finish_reason string
|
2023-06-09 20:10:55 +00:00
|
|
|
var original_response chatgpt_types.ChatGPTResponse
|
2023-06-09 20:26:54 +00:00
|
|
|
counter := 0
|
2023-06-09 19:30:45 +00:00
|
|
|
for {
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2023-06-09 20:10:55 +00:00
|
|
|
return "", nil
|
2023-06-09 19:30:45 +00:00
|
|
|
}
|
|
|
|
if len(line) < 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Remove "data: " from the beginning of the line
|
|
|
|
line = line[6:]
|
|
|
|
// Check if line starts with [DONE]
|
|
|
|
if !strings.HasPrefix(line, "[DONE]") {
|
|
|
|
// Parse the line as JSON
|
2023-06-09 20:10:55 +00:00
|
|
|
|
2023-06-09 19:30:45 +00:00
|
|
|
err = json.Unmarshal([]byte(line), &original_response)
|
|
|
|
if err != nil {
|
2023-06-09 20:26:54 +00:00
|
|
|
println("Failed to parse JSON")
|
2023-06-12 14:48:04 +00:00
|
|
|
println(line)
|
2023-06-09 19:30:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if original_response.Error != nil {
|
2023-06-09 20:26:54 +00:00
|
|
|
c.JSON(500, gin.H{"error": original_response.Error})
|
2023-06-09 20:10:55 +00:00
|
|
|
return "", nil
|
2023-06-09 19:30:45 +00:00
|
|
|
}
|
2023-06-09 20:26:54 +00:00
|
|
|
if original_response.Message.Author.Role != "assistant" || original_response.Message.Content.Parts == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if counter < 3 {
|
|
|
|
counter++
|
2023-06-09 19:30:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-06-09 19:45:03 +00:00
|
|
|
response_string := chatgpt_response_converter.ConvertToString(&original_response)
|
2023-06-09 19:30:45 +00:00
|
|
|
if stream {
|
2023-06-09 19:45:03 +00:00
|
|
|
_, err = c.Writer.WriteString(response_string)
|
2023-06-09 19:30:45 +00:00
|
|
|
if err != nil {
|
2023-06-09 20:10:55 +00:00
|
|
|
return "", nil
|
2023-06-09 19:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Flush the response writer buffer to ensure that the client receives each line as it's written
|
|
|
|
c.Writer.Flush()
|
|
|
|
|
2023-06-09 19:49:13 +00:00
|
|
|
if original_response.Message.Metadata.FinishDetails != nil {
|
|
|
|
if original_response.Message.Metadata.FinishDetails.Type == "max_tokens" {
|
|
|
|
max_tokens = true
|
|
|
|
}
|
|
|
|
finish_reason = original_response.Message.Metadata.FinishDetails.Type
|
|
|
|
}
|
|
|
|
|
2023-06-09 19:30:45 +00:00
|
|
|
} else {
|
|
|
|
if stream {
|
2023-06-09 19:49:13 +00:00
|
|
|
final_line := official_types.StopChunk(finish_reason)
|
2023-06-09 19:30:45 +00:00
|
|
|
c.Writer.WriteString("data: " + final_line.String() + "\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 20:10:55 +00:00
|
|
|
if !max_tokens {
|
|
|
|
return chatgpt_response_converter.Previous_text, nil
|
|
|
|
}
|
|
|
|
return chatgpt_response_converter.Previous_text, &ContinueInfo{
|
|
|
|
ConversationID: original_response.ConversationID,
|
|
|
|
ParentID: original_response.Message.ID,
|
|
|
|
}
|
2023-06-09 19:30:45 +00:00
|
|
|
}
|