fix: enforce embedding model max token limit via proper truncation#13178
Draft
timon0305 wants to merge 1 commit intoinfiniflow:mainfrom
Draft
fix: enforce embedding model max token limit via proper truncation#13178timon0305 wants to merge 1 commit intoinfiniflow:mainfrom
timon0305 wants to merge 1 commit intoinfiniflow:mainfrom
Conversation
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.
Summary
Fixes the embedding model max token limit not being enforced, causing API errors when input text exceeds the model's context window. This was reported across multiple providers (Ollama, vLLM, HuggingFace TEI) with models like bge-large (512 tokens), multilingual-e5-large-instruct (512 tokens), etc.
Root causes fixed
LLMBundle.encode()truncated by character count instead of token count —text[:target_len]slices by character index, butmax_lengthis in tokens. For multi-byte text (Chinese/Japanese), this still exceeds the token limit. Fixed to usetruncate()which properly tokenizes and truncates at the token level.LLMBundle.encode_queries()had no truncation at all — User search queries passed directly to embedding API without any length check. Added token-level truncation using the configuredmax_length.12 embedding provider classes lacked input truncation — Added
truncate()calls to bothencode()andencode_queries()for:OllamaEmbed,XinferenceEmbed,LocalAIEmbed,HuggingFaceEmbed,CoHereEmbed,SILICONFLOWEmbed,ReplicateEmbed,BaiduYiyanEmbed,VoyageEmbed,VolcEngineEmbed,ZhipuEmbed(encode_queries),QWenEmbed(encode_queries character slice → token truncate).Misleading tooltip for max_tokens setting — The UI tooltip said "Defaults to 512" but the actual default is 8192. Updated en/zh locales to accurately describe the field as "maximum input context length" with correct default.
Changes
api/db/services/llm_service.py— FixedLLMBundle.encode()to use token-leveltruncate()instead of character slicing; added truncation toLLMBundle.encode_queries()rag/llm/embedding_model.py— Addedtruncate()to 12 provider classes missing input truncationweb/src/locales/en.ts— Fixed maxTokensTip tooltip text and default valueweb/src/locales/zh.ts— Fixed maxTokensTip tooltip text and default value (Chinese)Closes #4683