Use http.ResponseWriter with Flush() to stream LLM responses as Server-Sent Events (SSE) by setting the Content-Type header to text/event-stream and writing data in the `data:
` format.
func streamLLM(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
return
}
response := "Hello from LLM"
for _, chunk := range []string{"Hello", " ", "from", " ", "LLM"} {
fmt.Fprintf(w, "data: %s\n\n", chunk)
flusher.Flush()
time.Sleep(500 * time.Millisecond)
}
}