Spaces:
Runtime error
Runtime error
Update api/common.go
Browse files- api/common.go +22 -2
api/common.go
CHANGED
|
@@ -6,7 +6,8 @@ import (
|
|
| 6 |
"os"
|
| 7 |
"strings"
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
"github.com/linweiyuan/go-chatgpt-api/util/logger"
|
| 11 |
"github.com/gin-gonic/gin"
|
| 12 |
_ "github.com/linweiyuan/go-chatgpt-api/env"
|
|
@@ -85,7 +86,8 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) {
|
|
| 85 |
}
|
| 86 |
|
| 87 |
line = strings.TrimSpace(line)
|
| 88 |
-
|
|
|
|
| 89 |
if strings.HasPrefix(line, "event") ||
|
| 90 |
strings.HasPrefix(line, "data: 20") ||
|
| 91 |
line == "" {
|
|
@@ -94,6 +96,24 @@ func HandleConversationResponse(c *gin.Context, resp *http.Response) {
|
|
| 94 |
|
| 95 |
c.Writer.Write([]byte(line + "\n\n"))
|
| 96 |
c.Writer.Flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
|
|
|
| 6 |
"os"
|
| 7 |
"strings"
|
| 8 |
|
| 9 |
+
"fmt"
|
| 10 |
+
"encoding/json"
|
| 11 |
"github.com/linweiyuan/go-chatgpt-api/util/logger"
|
| 12 |
"github.com/gin-gonic/gin"
|
| 13 |
_ "github.com/linweiyuan/go-chatgpt-api/env"
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
line = strings.TrimSpace(line)
|
| 89 |
+
|
| 90 |
+
// logger.Info(fmt.Sprintf("HandleConversationResponse: %s", line))
|
| 91 |
if strings.HasPrefix(line, "event") ||
|
| 92 |
strings.HasPrefix(line, "data: 20") ||
|
| 93 |
line == "" {
|
|
|
|
| 96 |
|
| 97 |
c.Writer.Write([]byte(line + "\n\n"))
|
| 98 |
c.Writer.Flush()
|
| 99 |
+
|
| 100 |
+
data := line[6:]
|
| 101 |
+
var result map[string]interface{}
|
| 102 |
+
err := json.Unmarshal([]byte(data), &result)
|
| 103 |
+
if err != nil {
|
| 104 |
+
continue
|
| 105 |
+
}
|
| 106 |
+
message := result["message"].(map[string]interface{})
|
| 107 |
+
status := message["status"].(string)
|
| 108 |
+
|
| 109 |
+
logger.Info(fmt.Sprintf("status的值: %s", status))
|
| 110 |
+
if(status == "finished_successfully") {
|
| 111 |
+
finishDetails := message["metadata"].(map[string]interface{})["finish_details"].(map[string]interface{})
|
| 112 |
+
finishType := finishDetails["type"].(string)
|
| 113 |
+
if(finishType == "max_tokens") {
|
| 114 |
+
logger.Info(fmt.Sprintf("finish_details中type的值: %s", finishType))
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|