Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- api/chatgpt/api.go +7 -7
- api/common.go +23 -5
api/chatgpt/api.go
CHANGED
@@ -35,7 +35,7 @@ func CreateConversation(c *gin.Context) {
|
|
35 |
c.AbortWithStatusJSON(http.StatusBadRequest, api.ReturnMessage(parseJsonErrorMessage))
|
36 |
return
|
37 |
}
|
38 |
-
|
39 |
if request.ConversationID == nil || *request.ConversationID == "" {
|
40 |
request.ConversationID = nil
|
41 |
}
|
@@ -81,15 +81,15 @@ func CreateConversation(c *gin.Context) {
|
|
81 |
return
|
82 |
}
|
83 |
|
84 |
-
Status, ConversationID := api.HandleConversationResponse(c, resp)
|
85 |
if Status {
|
86 |
-
ContinueConversation(c, ConversationID, request.ParentMessageID, request.Model)
|
87 |
}
|
88 |
}
|
89 |
|
90 |
-
func ContinueConversation(c *gin.Context, conversationID string, parentMessageID string, model string) {
|
91 |
var request ContinueConversationRequest
|
92 |
-
|
93 |
request.ConversationID = &conversationID
|
94 |
request.ParentMessageID = parentMessageID
|
95 |
request.Model = model
|
@@ -133,9 +133,9 @@ func ContinueConversation(c *gin.Context, conversationID string, parentMessageID
|
|
133 |
return
|
134 |
}
|
135 |
|
136 |
-
Status, ConversationID := api.HandleConversationResponse(c, resp)
|
137 |
if Status {
|
138 |
-
ContinueConversation(c, ConversationID, request.ParentMessageID, request.Model)
|
139 |
}
|
140 |
}
|
141 |
|
|
|
35 |
c.AbortWithStatusJSON(http.StatusBadRequest, api.ReturnMessage(parseJsonErrorMessage))
|
36 |
return
|
37 |
}
|
38 |
+
|
39 |
if request.ConversationID == nil || *request.ConversationID == "" {
|
40 |
request.ConversationID = nil
|
41 |
}
|
|
|
81 |
return
|
82 |
}
|
83 |
|
84 |
+
Status, ConversationID, part := api.HandleConversationResponse(c, resp, "")
|
85 |
if Status {
|
86 |
+
ContinueConversation(c, ConversationID, request.ParentMessageID, request.Model, part)
|
87 |
}
|
88 |
}
|
89 |
|
90 |
+
func ContinueConversation(c *gin.Context, conversationID string, parentMessageID string, model string, oldpart string) {
|
91 |
var request ContinueConversationRequest
|
92 |
+
|
93 |
request.ConversationID = &conversationID
|
94 |
request.ParentMessageID = parentMessageID
|
95 |
request.Model = model
|
|
|
133 |
return
|
134 |
}
|
135 |
|
136 |
+
Status, ConversationID, part := api.HandleConversationResponse(c, resp, oldpart)
|
137 |
if Status {
|
138 |
+
ContinueConversation(c, ConversationID, request.ParentMessageID, request.Model, part)
|
139 |
}
|
140 |
}
|
141 |
|
api/common.go
CHANGED
@@ -72,19 +72,24 @@ func GetAccessToken(accessToken string) string {
|
|
72 |
}
|
73 |
|
74 |
//goland:noinspection GoUnhandledErrorResult
|
75 |
-
func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, string) {
|
76 |
Status := false
|
77 |
ConversationID := ""
|
|
|
78 |
c.Writer.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
79 |
|
80 |
reader := bufio.NewReader(resp.Body)
|
81 |
for {
|
82 |
if c.Request.Context().Err() != nil {
|
|
|
|
|
83 |
break
|
84 |
}
|
85 |
|
86 |
line, err := reader.ReadString('\n')
|
87 |
if err != nil {
|
|
|
|
|
88 |
break
|
89 |
}
|
90 |
|
@@ -96,15 +101,28 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, stri
|
|
96 |
continue
|
97 |
}
|
98 |
|
99 |
-
if strings.HasPrefix(line, "[DONE]") ||
|
100 |
-
Status {
|
101 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
}
|
103 |
|
104 |
c.Writer.Write([]byte(line + "\n\n"))
|
105 |
c.Writer.Flush()
|
106 |
if strings.HasPrefix(line, "[DONE]") {
|
107 |
-
|
108 |
}
|
109 |
|
110 |
data := line[6:]
|
@@ -128,7 +146,7 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, stri
|
|
128 |
}
|
129 |
}
|
130 |
}
|
131 |
-
return Status, ConversationID
|
132 |
}
|
133 |
|
134 |
//goland:noinspection GoUnhandledErrorResult,SpellCheckingInspection
|
|
|
72 |
}
|
73 |
|
74 |
//goland:noinspection GoUnhandledErrorResult
|
75 |
+
func HandleConversationResponse(c *gin.Context, resp *http.Response, text string) (bool, string, string) {
|
76 |
Status := false
|
77 |
ConversationID := ""
|
78 |
+
part := ""
|
79 |
c.Writer.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
80 |
|
81 |
reader := bufio.NewReader(resp.Body)
|
82 |
for {
|
83 |
if c.Request.Context().Err() != nil {
|
84 |
+
c.Writer.Write([]byte("data: " + "\n\n"))
|
85 |
+
c.Writer.Flush()
|
86 |
break
|
87 |
}
|
88 |
|
89 |
line, err := reader.ReadString('\n')
|
90 |
if err != nil {
|
91 |
+
c.Writer.Write([]byte("data: " + "\n\n"))
|
92 |
+
c.Writer.Flush()
|
93 |
break
|
94 |
}
|
95 |
|
|
|
101 |
continue
|
102 |
}
|
103 |
|
104 |
+
if strings.HasPrefix(line, "[DONE]") || Status {
|
|
|
105 |
break
|
106 |
+
} else if text == "" {
|
107 |
+
var result map[string]interface{}
|
108 |
+
err := json.Unmarshal([]byte(line[6:]), &result)
|
109 |
+
if err == nil {
|
110 |
+
message := result["message"].(map[string]interface{})
|
111 |
+
content := message["content"].(map[string]interface{})
|
112 |
+
parts := content["parts"].([]interface{})
|
113 |
+
part = parts[0].(string)
|
114 |
+
parts[0] = text + part
|
115 |
+
resultJSON, err2 := json.Marshal(result)
|
116 |
+
if err2 == nil {
|
117 |
+
line = "data: " + string(resultJSON)
|
118 |
+
}
|
119 |
+
}
|
120 |
}
|
121 |
|
122 |
c.Writer.Write([]byte(line + "\n\n"))
|
123 |
c.Writer.Flush()
|
124 |
if strings.HasPrefix(line, "[DONE]") {
|
125 |
+
break
|
126 |
}
|
127 |
|
128 |
data := line[6:]
|
|
|
146 |
}
|
147 |
}
|
148 |
}
|
149 |
+
return Status, ConversationID, part
|
150 |
}
|
151 |
|
152 |
//goland:noinspection GoUnhandledErrorResult,SpellCheckingInspection
|