How to Stream LLM Responses in Go (Server-Sent Events)

Web
Stream LLM responses in Go using Server-Sent Events by setting the text/event-stream header and flushing the response writer after each chunk.

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)
    }
}