feat: Add task-aware embedding support #2560
Open
+458
−29
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.
Description
Modern embedding models (e.g., Gemini and FRIDA) support asymmetric embeddings through task-specific prefixes, which significantly improve retrieval accuracy by generating different embeddings for queries versus documents. This PR implements this capability in LightRAG.
Related Issues
N/A
Changes Made
Configuration & Documentation
EMBEDDING_DOCUMENT_PREFIXandEMBEDDING_QUERY_PREFIXenvironment variables tolightrag/api/config.pydocs/DockerDeployment.mdandenv.examplewith new configuration optionsexamples/unofficial-sample/lightrag_embedding_prefixes.pyCore Infrastructure
EmbeddingFuncwrapper inlightrag/utils.pywithsupports_contextparameterwrap_embedding_func_with_attrsdecorator to support context-aware functionslightrag/operate.pyto passcontextVector Storage Backends
Updated all storage implementations to use context parameter:
lightrag/kg/faiss_impl.pylightrag/kg/milvus_impl.pylightrag/kg/mongo_impl.pylightrag/kg/nano_vector_db_impl.pylightrag/kg/postgres_impl.pylightrag/kg/qdrant_impl.pyLLM Provider Bindings
Updated embedding functions with context support:
lightrag/llm/openai.py- Prefix supportlightrag/llm/ollama.py- Prefix supportlightrag/llm/gemini.py- Automatic task_type selectionlightrag/llm/jina.py- Automatic task selectionlightrag/llm/hf.py- Prefix supportBinding Options
GeminiEmbeddingOptionsto support automatic task_type selectionAPI Server
lightrag/api/lightrag_server.pylightrag/api/utils_api.pysplash screen to display prefix settingsChecklist
Additional Notes
Backward Compatibility
EMBEDDING_DOCUMENT_PREFIXorEMBEDDING_QUERY_PREFIXenvironment variables are set