-
Notifications
You must be signed in to change notification settings - Fork 156
feat: add streaming endpoint for inference requests #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
033cf83
feat: add streaming endpoint for inference requests
doringeman a0ddf2e
refactor: combine the requests endpoints
doringeman d61cffb
refactor: on streaming return the same struct as on regular
doringeman 98361c1
OpenAIRecorder: add clarifying comment about modelRecords size assump…
doringeman 78f30f9
OpenAIRecorder: send error event for JSON marshaling failure
doringeman 2fca1f5
OpenAIRecorder: add error handling and logging for SSE response writes
doringeman 296301f
OpenAIRecorder: return an empty list instead of 404
doringeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ import ( | |
| // per model. | ||
| const maximumRecordsPerModel = 10 | ||
|
|
||
| // subscriberChannelBuffer is the buffer size for subscriber channels. | ||
| const subscriberChannelBuffer = 100 | ||
|
|
||
| type responseRecorder struct { | ||
| http.ResponseWriter | ||
| body *bytes.Buffer | ||
|
|
@@ -69,13 +72,18 @@ type OpenAIRecorder struct { | |
| records map[string]*ModelData // key is model ID | ||
| modelManager *models.Manager // for resolving model tags to IDs | ||
| m sync.RWMutex | ||
|
|
||
| // streaming | ||
| subscribers map[string]chan []ModelRecordsResponse | ||
| subMutex sync.RWMutex | ||
| } | ||
|
|
||
| func NewOpenAIRecorder(log logging.Logger, modelManager *models.Manager) *OpenAIRecorder { | ||
| return &OpenAIRecorder{ | ||
| log: log, | ||
| modelManager: modelManager, | ||
| records: make(map[string]*ModelData), | ||
| subscribers: make(map[string]chan []ModelRecordsResponse), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -188,6 +196,18 @@ func (r *OpenAIRecorder) RecordResponse(id, model string, rw http.ResponseWriter | |
| record.Response = response | ||
| record.Error = "" // Ensure Error is empty for successful responses | ||
| } | ||
| // Create ModelRecordsResponse with this single updated record to match | ||
| // what the non-streaming endpoint returns - []ModelRecordsResponse. | ||
| // See getAllRecords and getRecordsByModel. | ||
| modelResponse := []ModelRecordsResponse{{ | ||
| Count: 1, | ||
| Model: model, | ||
| ModelData: ModelData{ | ||
| Config: modelData.Config, | ||
| Records: []*RequestResponsePair{record}, | ||
| }, | ||
| }} | ||
| go r.broadcastToSubscribers(modelResponse) | ||
| return | ||
| } | ||
| } | ||
|
|
@@ -274,36 +294,124 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string { | |
|
|
||
| func (r *OpenAIRecorder) GetRecordsHandler() http.HandlerFunc { | ||
| return func(w http.ResponseWriter, req *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| acceptHeader := req.Header.Get("Accept") | ||
|
|
||
| // Check if client wants Server-Sent Events | ||
| if acceptHeader == "text/event-stream" { | ||
| r.handleStreamingRequests(w, req) | ||
| return | ||
| } | ||
|
|
||
| // Default to JSON response | ||
| r.handleJSONRequests(w, req) | ||
| } | ||
| } | ||
|
|
||
| func (r *OpenAIRecorder) handleJSONRequests(w http.ResponseWriter, req *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
|
|
||
| model := req.URL.Query().Get("model") | ||
|
|
||
| if model == "" { | ||
| // Retrieve all records for all models. | ||
| allRecords := r.getAllRecords() | ||
| if allRecords == nil { | ||
| allRecords = []ModelRecordsResponse{} | ||
| } | ||
| if err := json.NewEncoder(w).Encode(allRecords); err != nil { | ||
| http.Error(w, fmt.Sprintf("Failed to encode all records: %v", err), | ||
| http.StatusInternalServerError) | ||
| return | ||
| } | ||
| } else { | ||
| // Retrieve records for the specified model. | ||
| records := r.getRecordsByModel(model) | ||
| if records == nil { | ||
| records = []ModelRecordsResponse{} | ||
| } | ||
|
doringeman marked this conversation as resolved.
|
||
| if err := json.NewEncoder(w).Encode(records); err != nil { | ||
| http.Error(w, fmt.Sprintf("Failed to encode records for model '%s': %v", model, err), | ||
| http.StatusInternalServerError) | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| model := req.URL.Query().Get("model") | ||
| func (r *OpenAIRecorder) handleStreamingRequests(w http.ResponseWriter, req *http.Request) { | ||
| // Set SSE headers. | ||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.Header().Set("Cache-Control", "no-cache") | ||
| w.Header().Set("Connection", "keep-alive") | ||
|
|
||
| // Create subscriber channel. | ||
| subscriberID := fmt.Sprintf("sub_%d", time.Now().UnixNano()) | ||
| ch := make(chan []ModelRecordsResponse, subscriberChannelBuffer) | ||
|
|
||
| // Register subscriber. | ||
| r.subMutex.Lock() | ||
| r.subscribers[subscriberID] = ch | ||
| r.subMutex.Unlock() | ||
|
|
||
| // Clean up on disconnect. | ||
| defer func() { | ||
| r.subMutex.Lock() | ||
| delete(r.subscribers, subscriberID) | ||
| close(ch) | ||
| r.subMutex.Unlock() | ||
| }() | ||
|
|
||
| // Optional: Send existing records first. | ||
| model := req.URL.Query().Get("model") | ||
| if includeExisting := req.URL.Query().Get("include_existing"); includeExisting == "true" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
| r.sendExistingRecords(w, model) | ||
| } | ||
|
|
||
| if model == "" { | ||
| // Retrieve all records for all models. | ||
| allRecords := r.getAllRecords() | ||
| if allRecords == nil { | ||
| // No records found. | ||
| http.Error(w, "No records found", http.StatusNotFound) | ||
| flusher, ok := w.(http.Flusher) | ||
| if !ok { | ||
| http.Error(w, "Streaming not supported", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| // Send heartbeat to establish connection. | ||
| if _, err := fmt.Fprintf(w, "event: connected\ndata: {\"status\": \"connected\"}\n\n"); err != nil { | ||
| r.log.Errorf("Failed to write connected event to response: %v", err) | ||
| } | ||
| flusher.Flush() | ||
|
|
||
| for { | ||
| select { | ||
| case modelRecords, ok := <-ch: | ||
| if !ok { | ||
| return | ||
| } | ||
| if err := json.NewEncoder(w).Encode(allRecords); err != nil { | ||
| http.Error(w, fmt.Sprintf("Failed to encode all records: %v", err), | ||
| http.StatusInternalServerError) | ||
| return | ||
|
|
||
| // Filter by model if specified. | ||
| // modelRecords is assumed to have size 1 because that's how we call broadcastToSubscribers. | ||
| // We do this so we don't need to query a 2nd time for the model config. | ||
| if model != "" && len(modelRecords) > 0 && modelRecords[0].Model != model { | ||
| continue | ||
|
doringeman marked this conversation as resolved.
|
||
| } | ||
| } else { | ||
| // Retrieve records for the specified model. | ||
| records := r.getRecordsByModel(model) | ||
| if records == nil { | ||
| // No records found for the specified model. | ||
| http.Error(w, fmt.Sprintf("No records found for model '%s'", model), http.StatusNotFound) | ||
| return | ||
|
|
||
| // Send as SSE event. | ||
| jsonData, err := json.Marshal(modelRecords) | ||
| if err != nil { | ||
|
doringeman marked this conversation as resolved.
|
||
| r.log.Errorf("Failed to marshal record for streaming: %v", err) | ||
| errorMsg := fmt.Sprintf(`{"error": "Failed to marshal record: %v"}`, err) | ||
| if _, writeErr := fmt.Fprintf(w, "event: error\ndata: %s\n\n", errorMsg); writeErr != nil { | ||
| r.log.Errorf("Failed to write error event to response: %v", writeErr) | ||
| } | ||
| flusher.Flush() | ||
| continue | ||
| } | ||
| if err := json.NewEncoder(w).Encode(records); err != nil { | ||
| http.Error(w, fmt.Sprintf("Failed to encode records for model '%s': %v", model, err), | ||
| http.StatusInternalServerError) | ||
| return | ||
|
|
||
| if _, err := fmt.Fprintf(w, "event: new_request\ndata: %s\n\n", jsonData); err != nil { | ||
| r.log.Errorf("Failed to write new_request event to response: %v", err) | ||
| } | ||
| flusher.Flush() | ||
|
|
||
| case <-req.Context().Done(): | ||
| // Client disconnected. | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -352,6 +460,60 @@ func (r *OpenAIRecorder) getRecordsByModel(model string) []ModelRecordsResponse | |
| return nil | ||
| } | ||
|
|
||
| func (r *OpenAIRecorder) broadcastToSubscribers(modelResponses []ModelRecordsResponse) { | ||
| r.subMutex.RLock() | ||
| defer r.subMutex.RUnlock() | ||
|
|
||
| for _, ch := range r.subscribers { | ||
| select { | ||
| case ch <- modelResponses: | ||
| default: | ||
| // The channel is full, skip this subscriber. | ||
|
doringeman marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| func (r *OpenAIRecorder) sendExistingRecords(w http.ResponseWriter, model string) { | ||
| var records []ModelRecordsResponse | ||
|
|
||
| if model == "" { | ||
| records = r.getAllRecords() | ||
| } else { | ||
| records = r.getRecordsByModel(model) | ||
| } | ||
|
|
||
| if records != nil { | ||
| // Send each individual request-response pair as a separate event. | ||
| for _, modelRecord := range records { | ||
| for _, requestRecord := range modelRecord.Records { | ||
| // Create a ModelRecordsResponse with a single record to match | ||
| // what the non-streaming endpoint returns - []ModelRecordsResponse. | ||
| // See getAllRecords and getRecordsByModel. | ||
| singleRecord := []ModelRecordsResponse{{ | ||
| Count: 1, | ||
| Model: modelRecord.Model, | ||
| ModelData: ModelData{ | ||
| Config: modelRecord.Config, | ||
| Records: []*RequestResponsePair{requestRecord}, | ||
| }, | ||
| }} | ||
| jsonData, err := json.Marshal(singleRecord) | ||
| if err != nil { | ||
| r.log.Errorf("Failed to marshal existing record for streaming: %v", err) | ||
| errorMsg := fmt.Sprintf(`{"error": "Failed to marshal existing record: %v"}`, err) | ||
| if _, writeErr := fmt.Fprintf(w, "event: error\ndata: %s\n\n", errorMsg); writeErr != nil { | ||
| r.log.Errorf("Failed to write error event to response: %v", writeErr) | ||
| } | ||
| } else { | ||
| if _, writeErr := fmt.Fprintf(w, "event: existing_request\ndata: %s\n\n", jsonData); writeErr != nil { | ||
| r.log.Errorf("Failed to write existing_request event to response: %v", writeErr) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (r *OpenAIRecorder) RemoveModel(model string) { | ||
| modelID := r.modelManager.ResolveModelID(model) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.