Skip to content

Commit

Permalink
Add support for ChatGPTUnofficialProxyAPI (aurora-develop#195)
Browse files Browse the repository at this point in the history
* Add support for ChatGPTUnofficialProxyAPI

Add support for ChatGPTUnofficialProxyAPI.

添加 /backend-api/conversation 路由,该路求接收网页版 ChatGPT 格式的请求,返回网页版 ChatGPT 格式的响应。

当环境变量 USE_DB=true 时使用数据库储存 conversation_id 和 oai-device-id 的关系,默认不使用。

支持通过请求头
Authorization: Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
传入 oai-device-id。

支持同时添加自定义 Authorization 以及 OpenAI accessToken 或 oai-device-id,中间通过空格分隔,例:
Authorization: Bearer MySecretWord xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

* Remove database-related functionality.

移除和数据库有关的功能。

向 /backend-api/conversation 接口发起请求时需在 Authorization 请求头携带和对话 conversation_id 相关联的 accessToken 或 oai-device-id。
  • Loading branch information
yjmp14 authored Apr 14, 2024
1 parent be9cf55 commit aa9598b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
78 changes: 78 additions & 0 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"aurora/internal/proxys"
"aurora/internal/tokens"
officialtypes "aurora/typings/official"
chatgpt_types "aurora/typings/chatgpt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -314,3 +316,79 @@ func (h *Handler) engines(c *gin.Context) {
modelS.Data = resModelList
c.JSON(status, modelS)
}

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()

var secret *tokens.Secret

isUUID := func(str string) bool {
_, err := uuid.Parse(str)
return err == nil
}

authHeader := c.GetHeader("Authorization")
if authHeader != "" {
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiI") {
secret = h.token.GenerateTempToken(customAccessToken)
}
if isUUID(customAccessToken) {
secret = h.token.GenerateDeviceId(customAccessToken)
}
}

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)
}

_, err = io.Copy(c.Writer, response.Body)
if err != nil {
c.JSON(500, gin.H{"error": "Error sending response"})
}
}
1 change: 1 addition & 0 deletions initialize/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ 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: 4 additions & 0 deletions internal/tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ 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}
}
7 changes: 6 additions & 1 deletion middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ func Authorization(c *gin.Context) {
c.Abort()
return
}
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
tokenParts := strings.Split(strings.Replace(authHeader, "Bearer ", "", 1)," ")
customAccessToken := tokenParts[0]
if customer_key != customAccessToken {
c.JSON(401, gin.H{"error": "Unauthorized"})
c.Abort()
return
}
if len(tokenParts) > 1 {
openaiAccessToken := tokenParts[1]
c.Request.Header.Set("Authorization", "Bearer " + openaiAccessToken)
}
}
c.Next()
}

0 comments on commit aa9598b

Please sign in to comment.