[Feature] Implement /v1/embeddings endpoint for OpenAI-compatible API#4550
Open
ZhijunLStudio wants to merge 1 commit intoInternLM:mainfrom
Open
[Feature] Implement /v1/embeddings endpoint for OpenAI-compatible API#4550ZhijunLStudio wants to merge 1 commit intoInternLM:mainfrom
/v1/embeddings endpoint for OpenAI-compatible API#4550ZhijunLStudio wants to merge 1 commit intoInternLM:mainfrom
Conversation
Add support for the standard OpenAI embeddings endpoint that extracts last hidden states from the model and applies mean pooling. This enables downstream tools (LangChain, LlamaIndex, RAG pipelines) to use lmdeploy for text embedding generation. Changes: - Replace stub /v1/embeddings with full implementation supporting float and base64 encoding formats - Thread last_hidden_states through the PyTorch engine pipeline (BatchedOutputs -> InferOutput -> EngineOutput) - Capture full-sequence hidden states before postprocessing slices to last token, and mean pool per-sequence in the engine - Add output_last_hidden_state to SamplingParam with validation - Tested end-to-end with Qwen3-8B: cosine similarity ordering is correct (0.9754 > 0.9478)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
The
/v1/embeddingsendpoint is a standard OpenAI API supported by vLLM, SGLang, and TGI. Many downstream tools (LangChain, LlamaIndex, RAG pipelines) depend on it to generate text embeddings. Currently lmdeploy's/v1/embeddingsis a stub that returnsUnsupported by turbomind.The infrastructure to pass
last_hidden_statethrough the pipeline already exists at the high level (Response,EngineOutput,GenOutall have the field), but the PyTorch engine's internal pipeline never populates it.Modification
API layer
lmdeploy/serve/openai/protocol.py: Addencoding_formatfield toEmbeddingsRequest(supportsfloatandbase64)lmdeploy/serve/openai/api_server.py: Replace stub with full implementation that calls engine withmax_new_tokens=1+output_last_hidden_state='all', applies mean pooling across input sequence, and returnsEmbeddingsResponsePyTorch engine pipeline (threading hidden states from model forward to API response)
lmdeploy/pytorch/messages.py: Addoutput_last_hidden_statefield toSamplingParam, addreturn_last_hidden_statesproperty toSchedulerSequence, replace unsupported warning with validationlmdeploy/pytorch/engine/inputs_maker.py: Add__need_hidden_statescheck and passreturn_last_hidden_statesflaglmdeploy/pytorch/engine/model_agent/agent.py: Addlast_hidden_statestoBatchedOutputs, capture full-sequence hidden states in_async_model_forwardbefore postprocessing slices to last token, mean pool per-sequencelmdeploy/pytorch/engine/engine.py: Addlast_hidden_statesfield toInferOutputlmdeploy/pytorch/engine/engine_loop.py: Thread hidden states through_send_respand_make_infer_outputslmdeploy/pytorch/engine/engine_instance.py: Passlast_hidden_statetoEngineOutputTested with
BC-breaking
No. The new endpoint is additive. Existing TurboMind
output_last_hidden_statesupport is unchanged.Use cases
Checklist