gzdaniel commited on
Commit
15a45e2
·
1 Parent(s): 940fee0

Update doc for rerank

Browse files
Files changed (1) hide show
  1. docs/rerank_integration.md +12 -12
docs/rerank_integration.md CHANGED
@@ -22,14 +22,14 @@ from lightrag import LightRAG, QueryParam
22
  from lightrag.rerank import custom_rerank, RerankModel
23
 
24
  # Method 1: Using a custom rerank function with all settings included
25
- async def my_rerank_func(query: str, documents: list, top_k: int = None, **kwargs):
26
  return await custom_rerank(
27
  query=query,
28
  documents=documents,
29
  model="BAAI/bge-reranker-v2-m3",
30
  base_url="https://api.your-provider.com/v1/rerank",
31
  api_key="your_api_key_here",
32
- top_k=top_k or 10, # Handle top_k within the function
33
  **kwargs
34
  )
35
 
@@ -95,7 +95,7 @@ result = await custom_rerank(
95
  model="BAAI/bge-reranker-v2-m3",
96
  base_url="https://api.your-provider.com/v1/rerank",
97
  api_key="your_api_key_here",
98
- top_k=10
99
  )
100
  ```
101
 
@@ -109,7 +109,7 @@ result = await jina_rerank(
109
  documents=documents,
110
  model="BAAI/bge-reranker-v2-m3",
111
  api_key="your_jina_api_key",
112
- top_k=10
113
  )
114
  ```
115
 
@@ -123,7 +123,7 @@ result = await cohere_rerank(
123
  documents=documents,
124
  model="rerank-english-v2.0",
125
  api_key="your_cohere_api_key",
126
- top_k=10
127
  )
128
  ```
129
 
@@ -141,7 +141,7 @@ Reranking is automatically applied at these key retrieval stages:
141
  | Parameter | Type | Default | Description |
142
  |-----------|------|---------|-------------|
143
  | `enable_rerank` | bool | False | Enable/disable reranking |
144
- | `rerank_model_func` | callable | None | Custom rerank function containing all configurations (model, API keys, top_k, etc.) |
145
 
146
  ## Example Usage
147
 
@@ -154,14 +154,14 @@ from lightrag.llm.openai import gpt_4o_mini_complete, openai_embedding
154
  from lightrag.kg.shared_storage import initialize_pipeline_status
155
  from lightrag.rerank import jina_rerank
156
 
157
- async def my_rerank_func(query: str, documents: list, top_k: int = None, **kwargs):
158
  """Custom rerank function with all settings included"""
159
  return await jina_rerank(
160
  query=query,
161
  documents=documents,
162
  model="BAAI/bge-reranker-v2-m3",
163
  api_key="your_jina_api_key_here",
164
- top_k=top_k or 10, # Default top_k if not provided
165
  **kwargs
166
  )
167
 
@@ -186,7 +186,7 @@ async def main():
186
  # Query with rerank (automatically applied)
187
  result = await rag.aquery(
188
  "Your question here",
189
- param=QueryParam(enable_rerank=True) # This top_k is passed to rerank function
190
  )
191
 
192
  print(result)
@@ -212,7 +212,7 @@ async def test_rerank():
212
  model="BAAI/bge-reranker-v2-m3",
213
  base_url="https://api.your-provider.com/v1/rerank",
214
  api_key="your_api_key_here",
215
- top_k=2
216
  )
217
 
218
  for doc in reranked:
@@ -221,11 +221,11 @@ async def test_rerank():
221
 
222
  ## Best Practices
223
 
224
- 1. **Self-Contained Functions**: Include all necessary configurations (API keys, models, top_k handling) within your rerank function
225
  2. **Performance**: Use reranking selectively for better performance vs. quality tradeoff
226
  3. **API Limits**: Monitor API usage and implement rate limiting within your rerank function
227
  4. **Fallback**: Always handle rerank failures gracefully (returns original results)
228
- 5. **Top-k Handling**: Handle top_k parameter appropriately within your rerank function
229
  6. **Cost Management**: Consider rerank API costs in your budget planning
230
 
231
  ## Troubleshooting
 
22
  from lightrag.rerank import custom_rerank, RerankModel
23
 
24
  # Method 1: Using a custom rerank function with all settings included
25
+ async def my_rerank_func(query: str, documents: list, top_n: int = None, **kwargs):
26
  return await custom_rerank(
27
  query=query,
28
  documents=documents,
29
  model="BAAI/bge-reranker-v2-m3",
30
  base_url="https://api.your-provider.com/v1/rerank",
31
  api_key="your_api_key_here",
32
+ top_n=top_n or 10, # Handle top_n within the function
33
  **kwargs
34
  )
35
 
 
95
  model="BAAI/bge-reranker-v2-m3",
96
  base_url="https://api.your-provider.com/v1/rerank",
97
  api_key="your_api_key_here",
98
+ top_n=10
99
  )
100
  ```
101
 
 
109
  documents=documents,
110
  model="BAAI/bge-reranker-v2-m3",
111
  api_key="your_jina_api_key",
112
+ top_n=10
113
  )
114
  ```
115
 
 
123
  documents=documents,
124
  model="rerank-english-v2.0",
125
  api_key="your_cohere_api_key",
126
+ top_n=10
127
  )
128
  ```
129
 
 
141
  | Parameter | Type | Default | Description |
142
  |-----------|------|---------|-------------|
143
  | `enable_rerank` | bool | False | Enable/disable reranking |
144
+ | `rerank_model_func` | callable | None | Custom rerank function containing all configurations (model, API keys, top_n, etc.) |
145
 
146
  ## Example Usage
147
 
 
154
  from lightrag.kg.shared_storage import initialize_pipeline_status
155
  from lightrag.rerank import jina_rerank
156
 
157
+ async def my_rerank_func(query: str, documents: list, top_n: int = None, **kwargs):
158
  """Custom rerank function with all settings included"""
159
  return await jina_rerank(
160
  query=query,
161
  documents=documents,
162
  model="BAAI/bge-reranker-v2-m3",
163
  api_key="your_jina_api_key_here",
164
+ top_n=top_n or 10, # Default top_n if not provided
165
  **kwargs
166
  )
167
 
 
186
  # Query with rerank (automatically applied)
187
  result = await rag.aquery(
188
  "Your question here",
189
+ param=QueryParam(enable_rerank=True) # This top_n is passed to rerank function
190
  )
191
 
192
  print(result)
 
212
  model="BAAI/bge-reranker-v2-m3",
213
  base_url="https://api.your-provider.com/v1/rerank",
214
  api_key="your_api_key_here",
215
+ top_n=2
216
  )
217
 
218
  for doc in reranked:
 
221
 
222
  ## Best Practices
223
 
224
+ 1. **Self-Contained Functions**: Include all necessary configurations (API keys, models, top_n handling) within your rerank function
225
  2. **Performance**: Use reranking selectively for better performance vs. quality tradeoff
226
  3. **API Limits**: Monitor API usage and implement rate limiting within your rerank function
227
  4. **Fallback**: Always handle rerank failures gracefully (returns original results)
228
+ 5. **Top-n Handling**: Handle top_n parameter appropriately within your rerank function
229
  6. **Cost Management**: Consider rerank API costs in your budget planning
230
 
231
  ## Troubleshooting