feat: Add Automatic Text Truncation Support for Embedding Functions#2523
Merged
danielaskdd merged 5 commits intoHKUDS:mainfrom Dec 22, 2025
Merged
feat: Add Automatic Text Truncation Support for Embedding Functions#2523danielaskdd merged 5 commits intoHKUDS:mainfrom
danielaskdd merged 5 commits intoHKUDS:mainfrom
Conversation
- Auto-inject max_token_size in wrapper - Implement OpenAI client-side truncation - Update Gemini/Ollama embed signatures - Relax summary token warning threshold - Update server startup logging
- Set cache env var before import - Support raw encoding names - Add cl100k_base to default list - Improve cache path resolution
* Fix typo in log message * Add missing closing parenthesis
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Add Automatic Text Truncation Support for Embedding Functions
Summary
This PR enhances the
EmbeddingFuncwrapper to automatically injectmax_token_sizeparameter to underlying embedding functions that support it. This enables automatic text truncation for embedding operations, preventing API errors caused by texts exceeding model token limits.Changes
Core Enhancement:
lightrag/utils.pyimport inspectfor function signature introspectionmax_token_sizeinjection logic inEmbeddingFunc.__call__:inspect.signature()to check if the underlying function supportsmax_token_sizeTypeErrorfor unsupported functions)OpenAI Embedding:
lightrag/llm/openai.pyimport tiktokenfor tokenization_TIKTOKEN_ENCODING_CACHEmodule-level cache and_get_tiktoken_encoding_for_model()helpermax_token_sizeparameter toopenai_embed()functionGemini Embedding:
lightrag/llm/gemini.pymax_token_sizeparameter togemini_embed()functionautoTruncate=Trueby default)Ollama Embedding:
lightrag/llm/ollama.pymax_token_sizeparameter toollama_embed()functionnum_ctxsettingMinor Updates
lightrag/api/lightrag_server.py: Updated log message for claritylightrag/operate.py: Changed token threshold from 90% to 100% and improved warning messageTruncation Strategies by Provider
How It Works
Backward Compatibility
max_token_sizeparameter continue to work (signature check prevents injection)