Spaces:
Runtime error
Runtime error
Update api/common.go
Browse files- api/common.go +29 -21
api/common.go
CHANGED
|
@@ -8,6 +8,7 @@ import (
|
|
| 8 |
|
| 9 |
"encoding/json"
|
| 10 |
"fmt"
|
|
|
|
| 11 |
|
| 12 |
"github.com/gin-gonic/gin"
|
| 13 |
_ "github.com/linweiyuan/go-chatgpt-api/env"
|
|
@@ -78,13 +79,15 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, stri
|
|
| 78 |
oldpart := c.GetString("oldpart")
|
| 79 |
part := ""
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
reader := bufio.NewReader(resp.Body)
|
| 84 |
for {
|
| 85 |
if c.Request.Context().Err() != nil {
|
| 86 |
-
c.Writer.Write([]byte("data: [DONE]" + "\n\n"))
|
| 87 |
-
c.Writer.Flush()
|
| 88 |
break
|
| 89 |
}
|
| 90 |
|
|
@@ -109,25 +112,29 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, stri
|
|
| 109 |
if Status {
|
| 110 |
break
|
| 111 |
}
|
| 112 |
-
} else
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
}
|
|
|
|
|
|
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
|
@@ -166,6 +173,7 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) (bool, stri
|
|
| 166 |
msg_id := message["id"].(string)
|
| 167 |
c.Set("msg_id", msg_id)
|
| 168 |
}
|
|
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
}
|
|
|
|
| 8 |
|
| 9 |
"encoding/json"
|
| 10 |
"fmt"
|
| 11 |
+
"strconv"
|
| 12 |
|
| 13 |
"github.com/gin-gonic/gin"
|
| 14 |
_ "github.com/linweiyuan/go-chatgpt-api/env"
|
|
|
|
| 79 |
oldpart := c.GetString("oldpart")
|
| 80 |
part := ""
|
| 81 |
|
| 82 |
+
if len(oldpart) == 0 {
|
| 83 |
+
c.Writer.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
| 84 |
+
}
|
| 85 |
|
| 86 |
reader := bufio.NewReader(resp.Body)
|
| 87 |
for {
|
| 88 |
if c.Request.Context().Err() != nil {
|
| 89 |
+
// c.Writer.Write([]byte("data: [DONE]" + "\n\n"))
|
| 90 |
+
// c.Writer.Flush()
|
| 91 |
break
|
| 92 |
}
|
| 93 |
|
|
|
|
| 112 |
if Status {
|
| 113 |
break
|
| 114 |
}
|
| 115 |
+
} else {
|
| 116 |
+
if len(oldpart) > 0 {
|
| 117 |
+
var result map[string]interface{}
|
| 118 |
+
err := json.Unmarshal([]byte(line[6:]), &result)
|
| 119 |
+
if err == nil {
|
| 120 |
+
message := result["message"].(map[string]interface{})
|
| 121 |
+
content := message["content"].(map[string]interface{})
|
| 122 |
+
parts := content["parts"].([]interface{})
|
| 123 |
+
part = parts[0].(string)
|
| 124 |
+
|
| 125 |
+
parts[0] = strconv.QuoteToASCII(oldpart + part)
|
| 126 |
+
message["id"] = c.GetString("msg_id")
|
| 127 |
+
metadata := message["metadata"].(map[string]interface{})
|
| 128 |
+
metadata["message_type"] = "next"
|
| 129 |
+
|
| 130 |
+
resultJSON, err2 := json.Marshal(result)
|
| 131 |
+
if err2 == nil {
|
| 132 |
+
line = "data: " + string(resultJSON)
|
| 133 |
+
logger.Info(fmt.Sprintf("HandleConversationResponseAddon: %s", line))
|
| 134 |
+
}
|
| 135 |
}
|
| 136 |
+
} else {
|
| 137 |
+
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
|
|
|
| 173 |
msg_id := message["id"].(string)
|
| 174 |
c.Set("msg_id", msg_id)
|
| 175 |
}
|
| 176 |
+
break
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|