From be9cf554ebb94e1ad6ecb7acac424cd8371e003c Mon Sep 17 00:00:00 2001 From: xiaofei <122727418+xiaozhou26@users.noreply.github.com> Date: Thu, 11 Apr 2024 11:30:30 +0800 Subject: [PATCH] Revert "Add support for ChatGPTUnofficialProxyAPI (#190)" (#193) This reverts commit cf69b79574d6ad66db1245f8b58527b8620c6b69. --- go.mod | 1 - go.sum | 2 - initialize/handlers.go | 165 -------------------------------------- initialize/router.go | 1 - internal/tokens/tokens.go | 4 - 5 files changed, 173 deletions(-) diff --git a/go.mod b/go.mod index 92cf0a24..f0bd1a9f 100644 --- a/go.mod +++ b/go.mod @@ -37,7 +37,6 @@ require ( github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum index af9723fd..f15f2631 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,6 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= -github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/initialize/handlers.go b/initialize/handlers.go index 4d8b9656..6b7381a8 100644 --- a/initialize/handlers.go +++ b/initialize/handlers.go @@ -7,19 +7,11 @@ import ( "aurora/internal/proxys" "aurora/internal/tokens" officialtypes "aurora/typings/official" - chatgpt_types "aurora/typings/chatgpt" - "io" "os" "strings" - "log" - "bufio" - "bytes" - "encoding/json" "github.com/gin-gonic/gin" "github.com/google/uuid" - "database/sql" - _ "github.com/mattn/go-sqlite3" ) type Handler struct { @@ -27,33 +19,6 @@ type Handler struct { token *tokens.AccessToken } -var db *sql.DB - -func init() { - if _, err := os.Stat("aurora.db"); os.IsNotExist(err) { - file, err := os.Create("aurora.db") - if err != nil { - panic(err) - } - file.Close() - } - - var err error - db, err = sql.Open("sqlite3", "aurora.db") - if err != nil { - panic(err) - } - _, err = db.Exec(` - CREATE TABLE IF NOT EXISTS conversation_tokens ( - conversation_id TEXT PRIMARY KEY, - device_id TEXT - ) - `) - if err != nil { - panic(err) - } -} - func NewHandle(proxy *proxys.IProxy, token *tokens.AccessToken) *Handler { return &Handler{proxy: proxy, token: token} } @@ -349,133 +314,3 @@ func (h *Handler) engines(c *gin.Context) { modelS.Data = resModelList c.JSON(status, modelS) } - -func saveConversationToken(conversationID, deviceID string) error { - _, err := db.Exec("INSERT OR REPLACE INTO conversation_tokens (conversation_id, device_id) VALUES (?, ?)", conversationID, deviceID) - if err != nil { - log.Println("error saving ConversationID to database:", err) - return err - } - return nil -} - -func (h *Handler) chatgptConversation(c *gin.Context) { - var original_request chatgpt_types.ChatGPTRequest - 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 - } - if original_request.Messages[0].Author.Role == "" { - original_request.Messages[0].Author.Role = "user" - } - - proxyUrl := h.proxy.GetProxyIP() - conversationID := original_request.ConversationID - - var secret *tokens.Secret - - if conversationID != "" { - row := db.QueryRow("SELECT device_id FROM conversation_tokens WHERE conversation_id = ?", conversationID) - var deviceID string - err := row.Scan(&deviceID) - if err == nil { - secret = h.token.GenerateDeviceId(deviceID) - } else { - secret = h.token.GetSecret() - saveConversationToken(conversationID, secret.Token) - } - } - if secret == nil { - secret = h.token.GetSecret() - } - - client := bogdanfinn.NewStdClient() - turnStile, status, err := chatgpt.InitTurnStile(client, secret, proxyUrl) - if err != nil { - c.JSON(status, gin.H{ - "message": err.Error(), - "type": "InitTurnStile_request_error", - "param": err, - "code": status, - }) - return - } - - response, err := chatgpt.POSTconversation(client, original_request, secret, turnStile, proxyUrl) - 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 - } - - c.Header("Content-Type", response.Header.Get("Content-Type")) - if cacheControl := response.Header.Get("Cache-Control"); cacheControl != "" { - c.Header("Cache-Control", cacheControl) - } - - if conversationID != "" { - _, err := io.Copy(c.Writer, response.Body) - if err != nil { - c.JSON(500, gin.H{"error": "Error sending response"}) - } - return - } - - var buffer bytes.Buffer - - reader := bufio.NewReader(response.Body) - for { - line, _, err := reader.ReadLine() - if err != nil { - if err != io.EOF { - log.Println("Error reading from SSE stream:", err) - } - break - } - - buffer.Write(line) - buffer.WriteString("\n") - - if len(line) < 6 { - continue - } - - dataLine := string(line[6:]) - if !strings.HasPrefix(dataLine, "[DONE]") { - var chatgptResponse chatgpt_types.ChatGPTResponse - err = json.Unmarshal([]byte(dataLine), &chatgptResponse) - if err != nil { - continue - } - if chatgptResponse.ConversationID != "" { - conversationID = chatgptResponse.ConversationID - saveConversationToken(conversationID, secret.Token) - break - } - } - } - - _, err = c.Writer.Write(buffer.Bytes()) - if err != nil { - c.JSON(500, gin.H{"error": "Error sending buffered response"}) - return - } - - _, err = io.Copy(c.Writer, reader) - if err != nil { - c.JSON(500, gin.H{"error": "Error sending remaining response"}) - return - } -} diff --git a/initialize/router.go b/initialize/router.go index 62300248..f2da562f 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -34,6 +34,5 @@ func RegisterRouter() *gin.Engine { authGroup := router.Group("").Use(middlewares.Authorization) authGroup.POST("/v1/chat/completions", handler.nightmare) authGroup.GET("/v1/models", handler.engines) - authGroup.POST("/backend-api/conversation", handler.chatgptConversation) return router } diff --git a/internal/tokens/tokens.go b/internal/tokens/tokens.go index b5922f68..59a499d9 100644 --- a/internal/tokens/tokens.go +++ b/internal/tokens/tokens.go @@ -58,7 +58,3 @@ func (a *AccessToken) UpdateSecret(tokens []*Secret) { func (a *AccessToken) GenerateTempToken(token string) *Secret { return &Secret{Token: token, PUID: "", IsFree: false} } - -func (a *AccessToken) GenerateDeviceId(token string) *Secret { - return &Secret{Token: token, PUID: "", IsFree: true} -}