From 76e0c8e141462d3afeb7925ce856dbaa2c4f2a6c Mon Sep 17 00:00:00 2001 From: Abhinav R Jha Date: Sun, 27 Apr 2025 02:13:19 +0530 Subject: [PATCH] Update api.go Improved comments to be more descriptive and professional (helpful for team/code reviewers). Made small optimizations like: Inline assignment if idx := ... Using switch in backendModeForRequest instead of chained if-else, for better scalability if more cases are added. Minor formatting/spacing fixes for easier reading. Added a // 10 MB comment next to the constant for instant clarity. --- pkg/inference/scheduling/api.go | 41 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/pkg/inference/scheduling/api.go b/pkg/inference/scheduling/api.go index 492ed7d..8d7625a 100644 --- a/pkg/inference/scheduling/api.go +++ b/pkg/inference/scheduling/api.go @@ -7,38 +7,39 @@ import ( ) const ( - // maximumOpenAIInferenceRequestSize is the maximum OpenAI API embedding or - // completion request size that Scheduler will allow. This should be large - // enough to encompass any real-world request but also small enough to avoid - // DoS attacks. - maximumOpenAIInferenceRequestSize = 10 * 1024 * 1024 + // maximumOpenAIInferenceRequestSize defines the maximum size (in bytes) + // allowed for an OpenAI API embedding or completion request. + // It should be large enough for real-world usage but small enough + // to mitigate DoS risks. + maximumOpenAIInferenceRequestSize = 10 * 1024 * 1024 // 10 MB ) -// trimRequestPathToOpenAIRoot trims a request path to start at the first -// instance of /v1/ to appear in the path. +// trimRequestPathToOpenAIRoot returns the substring of path starting from +// the first occurrence of "/v1/". If not found, it returns the original path. func trimRequestPathToOpenAIRoot(path string) string { - index := strings.Index(path, "/v1/") - if index == -1 { - return path + if idx := strings.Index(path, "/v1/"); idx != -1 { + return path[idx:] } - return path[index:] + return path } -// backendModeForRequest determines the backend operation mode to handle an -// OpenAI inference request. Its second parameter is true if and only if a valid -// mode could be determined. +// backendModeForRequest maps an OpenAI API path to the appropriate +// inference backend mode. Returns the mode and true if a valid mode is determined, +// otherwise returns false. func backendModeForRequest(path string) (inference.BackendMode, bool) { - if strings.HasSuffix(path, "/v1/chat/completions") || strings.HasSuffix(path, "/v1/completions") { + switch { + case strings.HasSuffix(path, "/v1/chat/completions"), strings.HasSuffix(path, "/v1/completions"): return inference.BackendModeCompletion, true - } else if strings.HasSuffix(path, "/v1/embeddings") { + case strings.HasSuffix(path, "/v1/embeddings"): return inference.BackendModeEmbedding, true + default: + return inference.BackendMode(0), false } - return inference.BackendMode(0), false } -// OpenAIInferenceRequest is used to extract the model specification from either -// a chat completion or embedding request in the OpenAI API. +// OpenAIInferenceRequest represents the model information extracted from +// a chat completion or embedding request payload to the OpenAI API. type OpenAIInferenceRequest struct { - // Model is the requested model name. + // Model specifies the model name requested. Model string `json:"model"` }