Update doc for rerank

This commit is contained in:
yangdx 2025-07-20 00:37:36 +08:00
parent b40fafba73
commit 88bf695de5

View File

@ -22,14 +22,14 @@ from lightrag import LightRAG, QueryParam
from lightrag.rerank import custom_rerank, RerankModel
# Method 1: Using a custom rerank function with all settings included
async def my_rerank_func(query: str, documents: list, top_k: int = None, **kwargs):
async def my_rerank_func(query: str, documents: list, top_n: int = None, **kwargs):
return await custom_rerank(
query=query,
documents=documents,
model="BAAI/bge-reranker-v2-m3",
base_url="https://api.your-provider.com/v1/rerank",
api_key="your_api_key_here",
top_k=top_k or 10, # Handle top_k within the function
top_n=top_n or 10, # Handle top_n within the function
**kwargs
)
@ -95,7 +95,7 @@ result = await custom_rerank(
model="BAAI/bge-reranker-v2-m3",
base_url="https://api.your-provider.com/v1/rerank",
api_key="your_api_key_here",
top_k=10
top_n=10
)
```
@ -109,7 +109,7 @@ result = await jina_rerank(
documents=documents,
model="BAAI/bge-reranker-v2-m3",
api_key="your_jina_api_key",
top_k=10
top_n=10
)
```
@ -123,7 +123,7 @@ result = await cohere_rerank(
documents=documents,
model="rerank-english-v2.0",
api_key="your_cohere_api_key",
top_k=10
top_n=10
)
```
@ -141,7 +141,7 @@ Reranking is automatically applied at these key retrieval stages:
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `enable_rerank` | bool | False | Enable/disable reranking |
| `rerank_model_func` | callable | None | Custom rerank function containing all configurations (model, API keys, top_k, etc.) |
| `rerank_model_func` | callable | None | Custom rerank function containing all configurations (model, API keys, top_n, etc.) |
## Example Usage
@ -154,14 +154,14 @@ from lightrag.llm.openai import gpt_4o_mini_complete, openai_embedding
from lightrag.kg.shared_storage import initialize_pipeline_status
from lightrag.rerank import jina_rerank
async def my_rerank_func(query: str, documents: list, top_k: int = None, **kwargs):
async def my_rerank_func(query: str, documents: list, top_n: int = None, **kwargs):
"""Custom rerank function with all settings included"""
return await jina_rerank(
query=query,
documents=documents,
model="BAAI/bge-reranker-v2-m3",
api_key="your_jina_api_key_here",
top_k=top_k or 10, # Default top_k if not provided
top_n=top_n or 10, # Default top_n if not provided
**kwargs
)
@ -186,7 +186,7 @@ async def main():
# Query with rerank (automatically applied)
result = await rag.aquery(
"Your question here",
param=QueryParam(enable_rerank=True) # This top_k is passed to rerank function
param=QueryParam(enable_rerank=True) # This top_n is passed to rerank function
)
print(result)
@ -212,7 +212,7 @@ async def test_rerank():
model="BAAI/bge-reranker-v2-m3",
base_url="https://api.your-provider.com/v1/rerank",
api_key="your_api_key_here",
top_k=2
top_n=2
)
for doc in reranked:
@ -221,11 +221,11 @@ async def test_rerank():
## Best Practices
1. **Self-Contained Functions**: Include all necessary configurations (API keys, models, top_k handling) within your rerank function
1. **Self-Contained Functions**: Include all necessary configurations (API keys, models, top_n handling) within your rerank function
2. **Performance**: Use reranking selectively for better performance vs. quality tradeoff
3. **API Limits**: Monitor API usage and implement rate limiting within your rerank function
4. **Fallback**: Always handle rerank failures gracefully (returns original results)
5. **Top-k Handling**: Handle top_k parameter appropriately within your rerank function
5. **Top-n Handling**: Handle top_n parameter appropriately within your rerank function
6. **Cost Management**: Consider rerank API costs in your budget planning
## Troubleshooting