Skip to content

Commit

Permalink
Revert "Add support for ChatGPTUnofficialProxyAPI (aurora-develop#190)…
Browse files Browse the repository at this point in the history
…" (aurora-develop#193)

This reverts commit cf69b79.
  • Loading branch information
xiaozhou26 authored Apr 11, 2024
1 parent 04435a2 commit be9cf55
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 173 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
165 changes: 0 additions & 165 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,18 @@ 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 {
proxy *proxys.IProxy
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}
}
Expand Down Expand Up @@ -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
}
}
1 change: 0 additions & 1 deletion initialize/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 0 additions & 4 deletions internal/tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

0 comments on commit be9cf55

Please sign in to comment.