From 53f9c7a2e8ee0a3141ca6c59bfee4280902a58e4 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sat, 10 Jun 2023 02:45:37 +0800 Subject: [PATCH] refactor --- .../requests}/chatgpt/convert.go | 7 ++-- handlers.go | 17 ++++----- internal/chatgpt/request.go | 3 +- .../typings.go => typings/chatgpt/request.go | 0 typings/chatgpt/response.go | 36 +++++++++++++++++++ .../api.go => typings/official/request.go | 2 +- .../official/response.go | 36 +------------------ 7 files changed, 53 insertions(+), 48 deletions(-) rename {internal => conversion/requests}/chatgpt/convert.go (70%) rename internal/chatgpt/typings.go => typings/chatgpt/request.go (100%) create mode 100644 typings/chatgpt/response.go rename internal/typings/api.go => typings/official/request.go (93%) rename internal/typings/responses/typings.go => typings/official/response.go (69%) diff --git a/internal/chatgpt/convert.go b/conversion/requests/chatgpt/convert.go similarity index 70% rename from internal/chatgpt/convert.go rename to conversion/requests/chatgpt/convert.go index 7cfb6e1..41b54ff 100644 --- a/internal/chatgpt/convert.go +++ b/conversion/requests/chatgpt/convert.go @@ -1,12 +1,13 @@ package chatgpt import ( - typings "freechatgpt/internal/typings" + chatgpt_types "freechatgpt/typings/chatgpt" + official_types "freechatgpt/typings/official" "strings" ) -func ConvertAPIRequest(api_request typings.APIRequest) ChatGPTRequest { - chatgpt_request := NewChatGPTRequest() +func ConvertAPIRequest(api_request official_types.APIRequest) chatgpt_types.ChatGPTRequest { + chatgpt_request := chatgpt_types.NewChatGPTRequest() if strings.HasPrefix(api_request.Model, "gpt-4") { chatgpt_request.Model = "gpt-4" if api_request.Model == "gpt-4-browsing" || api_request.Model == "gpt-4-plugins" || api_request.Model == "gpt-4-mobile" || api_request.Model == "gpt-4-code-interpreter" { diff --git a/handlers.go b/handlers.go index eef1016..19ec9f9 100644 --- a/handlers.go +++ b/handlers.go @@ -3,10 +3,11 @@ package main import ( "bufio" "encoding/json" + chatgpt_request_converter "freechatgpt/conversion/requests/chatgpt" "freechatgpt/internal/chatgpt" "freechatgpt/internal/tokens" - typings "freechatgpt/internal/typings" - "freechatgpt/internal/typings/responses" + chatgpt_types "freechatgpt/typings/chatgpt" + official_types "freechatgpt/typings/official" "io" "os" "strings" @@ -78,7 +79,7 @@ func optionsHandler(c *gin.Context) { }) } func nightmare(c *gin.Context) { - var original_request typings.APIRequest + var original_request official_types.APIRequest err := c.BindJSON(&original_request) if err != nil { c.JSON(400, gin.H{"error": gin.H{ @@ -89,7 +90,7 @@ func nightmare(c *gin.Context) { }}) } // Convert the chat request to a ChatGPT request - translated_request := chatgpt.ConvertAPIRequest(original_request) + translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request) authHeader := c.GetHeader("Authorization") token := ACCESS_TOKENS.GetToken() @@ -163,7 +164,7 @@ func nightmare(c *gin.Context) { // Check if line starts with [DONE] if !strings.HasPrefix(line, "[DONE]") { // Parse the line as JSON - var original_response responses.Data + var original_response chatgpt_types.ChatGPTResponse err = json.Unmarshal([]byte(line), &original_response) if err != nil { continue @@ -182,7 +183,7 @@ func nightmare(c *gin.Context) { } tmp_fulltext := original_response.Message.Content.Parts[0] original_response.Message.Content.Parts[0] = strings.ReplaceAll(original_response.Message.Content.Parts[0], fulltext, "") - translated_response := responses.NewChatCompletionChunk(original_response.Message.Content.Parts[0]) + translated_response := official_types.NewChatCompletionChunk(original_response.Message.Content.Parts[0]) // Stream the response to the client response_string := translated_response.String() @@ -198,14 +199,14 @@ func nightmare(c *gin.Context) { fulltext = tmp_fulltext } else { if !original_request.Stream { - full_response := responses.NewChatCompletion(fulltext) + full_response := official_types.NewChatCompletion(fulltext) if err != nil { return } c.JSON(200, full_response) return } - final_line := responses.StopChunk() + final_line := official_types.StopChunk() c.Writer.WriteString("data: " + final_line.String() + "\n\n") c.String(200, "data: [DONE]\n\n") diff --git a/internal/chatgpt/request.go b/internal/chatgpt/request.go index 88ef836..acd9e45 100644 --- a/internal/chatgpt/request.go +++ b/internal/chatgpt/request.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "encoding/json" + chatgpt_types "freechatgpt/typings/chatgpt" "math/rand" "os" "strings" @@ -56,7 +57,7 @@ func random_int(min int, max int) int { return min + rand.Intn(max-min) } -func SendRequest(message ChatGPTRequest, access_token string) (*http.Response, error) { +func SendRequest(message chatgpt_types.ChatGPTRequest, access_token string) (*http.Response, error) { if http_proxy != "" && len(proxies) == 0 { client.SetProxy(http_proxy) } diff --git a/internal/chatgpt/typings.go b/typings/chatgpt/request.go similarity index 100% rename from internal/chatgpt/typings.go rename to typings/chatgpt/request.go diff --git a/typings/chatgpt/response.go b/typings/chatgpt/response.go new file mode 100644 index 0000000..5d550f5 --- /dev/null +++ b/typings/chatgpt/response.go @@ -0,0 +1,36 @@ +package chatgpt + +type ChatGPTResponse struct { + Message Message `json:"message"` + ConversationID string `json:"conversation_id"` + Error interface{} `json:"error"` +} + +type Message struct { + ID string `json:"id"` + Author Author `json:"author"` + CreateTime float64 `json:"create_time"` + UpdateTime interface{} `json:"update_time"` + Content Content `json:"content"` + EndTurn interface{} `json:"end_turn"` + Weight float64 `json:"weight"` + Metadata Metadata `json:"metadata"` + Recipient string `json:"recipient"` +} + +type Content struct { + ContentType string `json:"content_type"` + Parts []string `json:"parts"` +} + +type Author struct { + Role string `json:"role"` + Name interface{} `json:"name"` + Metadata map[string]interface{} `json:"metadata"` +} + +type Metadata struct { + Timestamp string `json:"timestamp_"` + MessageType interface{} `json:"message_type"` + FinishDetails interface{} `json:"finish_details"` +} diff --git a/internal/typings/api.go b/typings/official/request.go similarity index 93% rename from internal/typings/api.go rename to typings/official/request.go index 597c4f6..56b5e70 100644 --- a/internal/typings/api.go +++ b/typings/official/request.go @@ -1,4 +1,4 @@ -package types +package official type APIRequest struct { Messages []api_message `json:"messages"` diff --git a/internal/typings/responses/typings.go b/typings/official/response.go similarity index 69% rename from internal/typings/responses/typings.go rename to typings/official/response.go index 3c21c70..746de88 100644 --- a/internal/typings/responses/typings.go +++ b/typings/official/response.go @@ -1,41 +1,7 @@ -package responses +package official import "encoding/json" -type Message struct { - ID string `json:"id"` - Author Author `json:"author"` - CreateTime float64 `json:"create_time"` - UpdateTime interface{} `json:"update_time"` - Content Content `json:"content"` - EndTurn interface{} `json:"end_turn"` - Weight float64 `json:"weight"` - Metadata Metadata `json:"metadata"` - Recipient string `json:"recipient"` -} - -type Author struct { - Role string `json:"role"` - Name interface{} `json:"name"` - Metadata map[string]interface{} `json:"metadata"` -} - -type Content struct { - ContentType string `json:"content_type"` - Parts []string `json:"parts"` -} - -type Metadata struct { - Timestamp string `json:"timestamp_"` - MessageType interface{} `json:"message_type"` - FinishDetails interface{} `json:"finish_details"` -} - -type Data struct { - Message Message `json:"message"` - ConversationID string `json:"conversation_id"` - Error interface{} `json:"error"` -} type ChatCompletionChunk struct { ID string `json:"id"` Object string `json:"object"`