Skip to content

Commit fae9b4d

Browse files
committed
feat: document max embeddings input array size
1 parent a28193b commit fae9b4d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pkg/plugins/gateway/util.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
"k8s.io/klog/v2"
3030
)
3131

32+
// OpenAI has a 2048 size limits for embeddings array inputs
33+
// see https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-input
34+
var maxEmbeddingInputArraySize = 2048
35+
3236
// validateRequestBody validates input by unmarshaling request body into respective openai-golang struct based on requestpath.
3337
// nolint:nakedret
3438
func validateRequestBody(requestID, requestPath string, requestBody []byte, user utils.User) (model, message string, stream bool, errRes *extProcPb.ProcessingResponse) {
@@ -165,23 +169,27 @@ func getChatCompletionsMessage(requestID string, chatCompletionObj openai.ChatCo
165169
func checkEmbeddingInputSequenceLen(requestID string, embeddingObj openai.EmbeddingNewParams) *extProcPb.ProcessingResponse {
166170
inputParam := embeddingObj.Input
167171
var size int
172+
isArrayType := false
168173
switch input := embeddingNewParamsInputUnionAsAny(&inputParam).(type) {
169174
case *string:
170175
size = len(*input)
171176
case *[]string:
172177
size = len(*input)
178+
isArrayType = true
173179
case *[]int64:
174180
size = len(*input)
175181
case *[][]int64:
176182
size = len(*input)
183+
isArrayType = true
177184
default:
178185
}
179186

180187
if size == 0 {
181188
klog.ErrorS(nil, "no input in the request body", "requestID", requestID)
182189
return buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "no messages in the request body", HeaderErrorRequestBodyProcessing, "true")
183190
}
184-
if size > 1024 {
191+
192+
if isArrayType && size > maxEmbeddingInputArraySize {
185193
klog.ErrorS(nil, "embeddings content is too large", "requestID", requestID, "size", size)
186194
return buildErrorResponse(envoyTypePb.StatusCode_BadRequest, "embeddings content is too large", HeaderErrorRequestBodyProcessing, "true")
187195
}

0 commit comments

Comments
 (0)