1
0
This commit is contained in:
Antonio 2023-06-10 02:45:37 +08:00
parent af5758365f
commit 53f9c7a2e8
7 changed files with 53 additions and 48 deletions

View File

@ -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" {

View File

@ -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")

View File

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

View File

@ -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"`
}

View File

@ -1,4 +1,4 @@
package types
package official
type APIRequest struct {
Messages []api_message `json:"messages"`

View File

@ -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"`