-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.go
50 lines (42 loc) · 1.01 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"io"
"net/http"
"github.com/rs/zerolog/log"
)
func (sf *StarFleet) handleGenerate(w http.ResponseWriter, r *http.Request) {
id := r.Header.Get("X-Request-ID")
payload, err := io.ReadAll(r.Body)
if err != nil {
LogHttpErr(w, id, "Failed to read request body", err, http.StatusBadRequest)
return
}
ctx := r.Context()
job := NewJob(ctx, id, payload)
//defer job.Close()
log.Info().Str("request id", id).Msg("Beginning generation job")
if err := sf.workerPool.Enlist(job); err != nil {
LogHttpErr(w, id, "Could not connect to LLM", err, http.StatusServiceUnavailable)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
LogHttpErr(w, id, "Streaming not supported by connection", nil, http.StatusBadRequest)
return
}
for {
select {
case <-ctx.Done():
return
case <-job.Ctx.Done():
return
case token := <-job.Output:
fmt.Fprint(w, token)
flusher.Flush()
case err := <-job.Err:
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}