xqdoo00o / ChatGPT-to-API

Scalable unofficial ChatGPT API for production.
648 stars 129 forks source link

这个拿到响应的wss_url,有个优化的想法。 #30

Closed learnLi closed 5 months ago

learnLi commented 5 months ago

https://github.com/xqdoo00o/ChatGPT-to-API/blob/a483cdd1b1bdd9c4c913461e84fb1ab006fc6ed8/internal/chatgpt/request.go#L265

if strings.Contains(firstStr, "\"wss_url\"") { // }

可以通过响应头的content-type来判断是否是流式响应。 if !strings.Contains(response.Header.Get("Content-Type"), "text/event-stream") { isWSS = true conn = connPool[token] var conversationResult chatgpt_types.ChatGPTWSSResponse json.NewDecoder(response.Body).Decode(&conversationResult) respId = conversationResult.ResponseId convId = conversationResult.ConversationId }

这样是否更方便呢?然后就不用处理下面这段了,因为有时候err := json.Unmarshal([]byte(firstStr[6:]), &original_response)有边界问题,有时候开始拿的数据解构不出json服务会报错。 err := json.Unmarshal([]byte(firstStr[6:]), &original_response) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return "", nil } if original_response.Error != nil { c.JSON(500, gin.H{"error": original_response.Error}) return "", nil } convId = original_response.ConversationID

我把它改成了

line, err = reader.ReadString('\n') if err != nil { if err == io.EOF { break } return "", nil }

xqdoo00o commented 5 months ago

确实,已经提交