-
Notifications
You must be signed in to change notification settings - Fork 452
[API] add support for embeddings api #1208
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
Draft
gcalmettes
wants to merge
10
commits into
vllm-project:main
Choose a base branch
from
gcalmettes:feat/support-embeddings-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+378
−59
Draft
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cbbbf37
feat: validate request body for embeddings requests
gcalmettes 8e85c03
feat: add v1/embeddings in created httpRoute
gcalmettes ef82afa
feat: dynamically generate httpRouteMatch based on parsed labels
gcalmettes 49978b4
feat: document max embeddings input array size
gcalmettes cecb382
feat: explicitely logs unknown embeddings types
gcalmettes c165cc0
feat: explicitely set default route option
gcalmettes fadfadf
feat: introduce openai-specific types for request type matching
gcalmettes 61f1a18
feat: switch validation of request body based on request type
gcalmettes 2c86645
feat: process response body differentially based on request type
gcalmettes 10b946b
feat: re-used defined variables from openai utils in modelrouter
gcalmettes 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
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,16 +18,22 @@ | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
configPb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" | ||
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" | ||
envoyTypePb "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
"github.com/openai/openai-go" | ||
"github.com/openai/openai-go/packages/param" | ||
"github.com/vllm-project/aibrix/pkg/utils" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// OpenAI has a 2048 size limits for embeddings array inputs | ||
// see https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-input | ||
var maxEmbeddingInputArraySize = 2048 | ||
|
||
// validateRequestBody validates input by unmarshaling request body into respective openai-golang struct based on requestpath. | ||
// nolint:nakedret | ||
func validateRequestBody(requestID, requestPath string, requestBody []byte, user utils.User) (model, message string, stream bool, errRes *extProcPb.ProcessingResponse) { | ||
|
@@ -69,6 +75,24 @@ | |
} | ||
model = completionObj.Model | ||
message = completionObj.Prompt | ||
} else if requestPath == "/v1/embeddings" { | ||
message = "" // prefix_cache algorithms are not relevant for embeddings | ||
var jsonMap map[string]json.RawMessage | ||
if err := json.Unmarshal(requestBody, &jsonMap); err != nil { | ||
klog.ErrorS(err, "error to unmarshal request body", "requestID", requestID, "requestBody", string(requestBody)) | ||
errRes = buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "error processing request body", HeaderErrorRequestBodyProcessing, "true") | ||
return | ||
gcalmettes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
embeddingObj := openai.EmbeddingNewParams{} | ||
if err := json.Unmarshal(requestBody, &embeddingObj); err != nil { | ||
klog.ErrorS(err, "error to unmarshal embeddings object", "requestID", requestID, "requestBody", string(requestBody)) | ||
errRes = buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "error processing request body", HeaderErrorRequestBodyProcessing, "true") | ||
return | ||
} | ||
model = embeddingObj.Model | ||
if errRes = checkEmbeddingInputSequenceLen(requestID, embeddingObj); errRes != nil { | ||
return | ||
} | ||
} else { | ||
errRes = buildErrorResponse(envoyTypePb.StatusCode_NotImplemented, "unknown request path", HeaderErrorRequestBodyProcessing, "true") | ||
return | ||
|
@@ -142,6 +166,57 @@ | |
return builder.String(), nil | ||
} | ||
|
||
// getEmbeddingsInputLen returns the len of the embeddings object | ||
func checkEmbeddingInputSequenceLen(requestID string, embeddingObj openai.EmbeddingNewParams) *extProcPb.ProcessingResponse { | ||
inputParam := embeddingObj.Input | ||
var size int | ||
isArrayType := false | ||
switch input := embeddingNewParamsInputUnionAsAny(&inputParam).(type) { | ||
case *string: | ||
size = len(*input) | ||
case *[]string: | ||
size = len(*input) | ||
isArrayType = true | ||
case *[]int64: | ||
size = len(*input) | ||
case *[][]int64: | ||
size = len(*input) | ||
isArrayType = true | ||
default: | ||
// Should never happend, but if input is of an unexpected non-nil type, let's explicitly error log it. | ||
// Size will be 0 in this case, which is then handled by the check below. | ||
if input != nil { | ||
klog.ErrorS(nil, "unhandled embedding input type", "requestID", requestID, "inputType", fmt.Sprintf("%T", input)) | ||
} | ||
} | ||
|
||
if size == 0 { | ||
klog.ErrorS(nil, "no input in the request body", "requestID", requestID) | ||
return buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "no messages in the request body", HeaderErrorRequestBodyProcessing, "true") | ||
} | ||
|
||
if isArrayType && size > maxEmbeddingInputArraySize { | ||
klog.ErrorS(nil, "embeddings content is too large", "requestID", requestID, "size", size) | ||
return buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "embeddings content is too large", HeaderErrorRequestBodyProcessing, "true") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// TODO: make asAny method publicly available on OpenAI go | ||
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. |
||
func embeddingNewParamsInputUnionAsAny(u *openai.EmbeddingNewParamsInputUnion) any { | ||
if !param.IsOmitted(u.OfString) { | ||
return &u.OfString.Value | ||
} else if !param.IsOmitted(u.OfArrayOfStrings) { | ||
return &u.OfArrayOfStrings | ||
} else if !param.IsOmitted(u.OfArrayOfTokens) { | ||
return &u.OfArrayOfTokens | ||
} else if !param.IsOmitted(u.OfArrayOfTokenArrays) { | ||
return &u.OfArrayOfTokenArrays | ||
} | ||
return nil | ||
} | ||
|
||
// generateErrorResponse construct envoy proxy error response | ||
// deprecated: use buildErrorResponse | ||
func generateErrorResponse(statusCode envoyTypePb.StatusCode, headers []*configPb.HeaderValueOption, body string) *extProcPb.ProcessingResponse { | ||
|
Oops, something went wrong.
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.