game-wake-wolf commited on
Commit
c3f457b
·
verified ·
1 Parent(s): 9b6e824

Update api/chat.go

Browse files
Files changed (1) hide show
  1. api/chat.go +15 -1
api/chat.go CHANGED
@@ -186,12 +186,24 @@ func Handler(w http.ResponseWriter, r *http.Request) {
186
  req.Header.Set("Sec-Fetch-Mode", "cors")
187
  req.Header.Set("Sec-Fetch-Dest", "empty")
188
  req.Header.Set("host", "arcane.getmerlin.in")
 
189
  if openAIReq.Stream {
 
 
 
 
 
 
190
  w.Header().Set("Content-Type", "text/event-stream")
191
  w.Header().Set("Cache-Control", "no-cache")
192
  w.Header().Set("Connection", "keep-alive")
193
  w.Header().Set("X-Accel-Buffering", "no")
194
  w.Header().Set("Transfer-Encoding", "chunked")
 
 
 
 
 
195
  } else {
196
  w.Header().Set("Content-Type", "application/json")
197
  }
@@ -257,7 +269,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
257
  return
258
  }
259
 
260
- reader := bufio.NewReaderSize(resp.Body, 256)
261
  for {
262
  line, err := reader.ReadString('\n')
263
  if err != nil {
@@ -297,6 +309,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
297
 
298
  respData, _ := json.Marshal(openAIResp)
299
  fmt.Fprintf(w, "data: %s\n\n", string(respData))
 
300
  }
301
  }
302
  }
@@ -323,6 +336,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
323
  respData, _ := json.Marshal(finalResp)
324
  fmt.Fprintf(w, "data: %s\n\n", string(respData))
325
  fmt.Fprintf(w, "data: [DONE]\n\n")
 
326
  }
327
 
328
  func generateUUID() string {
 
186
  req.Header.Set("Sec-Fetch-Mode", "cors")
187
  req.Header.Set("Sec-Fetch-Dest", "empty")
188
  req.Header.Set("host", "arcane.getmerlin.in")
189
+ var flusher http.Flusher
190
  if openAIReq.Stream {
191
+ var ok bool
192
+ flusher, ok = w.(http.Flusher)
193
+ if !ok {
194
+ http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
195
+ return
196
+ }
197
  w.Header().Set("Content-Type", "text/event-stream")
198
  w.Header().Set("Cache-Control", "no-cache")
199
  w.Header().Set("Connection", "keep-alive")
200
  w.Header().Set("X-Accel-Buffering", "no")
201
  w.Header().Set("Transfer-Encoding", "chunked")
202
+ defer func() {
203
+ if flusher != nil {
204
+ flusher.Flush()
205
+ }
206
+ }()
207
  } else {
208
  w.Header().Set("Content-Type", "application/json")
209
  }
 
269
  return
270
  }
271
 
272
+ reader := bufio.NewReader(resp.Body)
273
  for {
274
  line, err := reader.ReadString('\n')
275
  if err != nil {
 
309
 
310
  respData, _ := json.Marshal(openAIResp)
311
  fmt.Fprintf(w, "data: %s\n\n", string(respData))
312
+ flusher.Flush()
313
  }
314
  }
315
  }
 
336
  respData, _ := json.Marshal(finalResp)
337
  fmt.Fprintf(w, "data: %s\n\n", string(respData))
338
  fmt.Fprintf(w, "data: [DONE]\n\n")
339
+ flusher.Flush()
340
  }
341
 
342
  func generateUUID() string {