Update rerank doc in README

This commit is contained in:
yangdx 2025-08-23 23:06:10 +08:00
parent 3d5e6226a9
commit 49ea9a79a7
4 changed files with 89 additions and 55 deletions

View file

@ -181,7 +181,7 @@ python examples/lightrag_openai_demo.py
## 使用LightRAG Core进行编程 ## 使用LightRAG Core进行编程
> 如果您希望将LightRAG集成到您的项目中建议您使用LightRAG Server提供的REST API。LightRAG Core通常用于嵌入式应用或供希望进行研究与评估的学者使用。 > ⚠️ **如果您希望将LightRAG集成到您的项目中建议您使用LightRAG Server提供的REST API**。LightRAG Core通常用于嵌入式应用或供希望进行研究与评估的学者使用。
### 一个简单程序 ### 一个简单程序
@ -594,31 +594,15 @@ if __name__ == "__main__":
</details> </details>
### 对话历史 ### Rerank函数注入
LightRAG现在通过对话历史功能支持多轮对话。以下是使用方法 为了提高检索质量,可以根据更有效的相关性评分模型对文档进行重排序。`rerank.py`文件提供了三个Reranker提供商的驱动函数
```python * **Cohere / vLLM**: `cohere_rerank`
# 创建对话历史 * **Jina AI**: `jina_rerank`
conversation_history = [ * **Aliyun阿里云**: `ali_rerank`
{"role": "user", "content": "主角对圣诞节的态度是什么?"},
{"role": "assistant", "content": "在故事开始时,埃比尼泽·斯克鲁奇对圣诞节持非常消极的态度..."},
{"role": "user", "content": "他的态度是如何改变的?"}
]
# 创建带有对话历史的查询参数 您可以将这些函数之一注入到LightRAG对象的`rerank_model_func`属性中。这将使LightRAG的查询功能能够使用注入的函数对检索到的文本块进行重新排序。有关详细用法请参阅`examples/rerank_example.py`文件。
query_param = QueryParam(
mode="mix", # 或其他模式:"local"、"global"、"hybrid"
conversation_history=conversation_history, # 添加对话历史
history_turns=3 # 考虑最近的对话轮数
)
# 进行考虑对话历史的查询
response = rag.query(
"是什么导致了他性格的这种变化?",
param=query_param
)
```
### 用户提示词 vs. 查询内容 ### 用户提示词 vs. 查询内容

View file

@ -179,11 +179,12 @@ For a streaming response implementation example, please see `examples/lightrag_o
## Programing with LightRAG Core ## Programing with LightRAG Core
> If you would like to integrate LightRAG into your project, we recommend utilizing the REST API provided by the LightRAG Server. LightRAG Core is typically intended for embedded applications or for researchers who wish to conduct studies and evaluations. > ⚠️ **If you would like to integrate LightRAG into your project, we recommend utilizing the REST API provided by the LightRAG Server**. LightRAG Core is typically intended for embedded applications or for researchers who wish to conduct studies and evaluations.
### ⚠️ Important: Initialization Requirements ### ⚠️ Important: Initialization Requirements
**LightRAG requires explicit initialization before use.** You must call both `await rag.initialize_storages()` and `await initialize_pipeline_status()` after creating a LightRAG instance, otherwise you will encounter errors like: **LightRAG requires explicit initialization before use.** You must call both `await rag.initialize_storages()` and `await initialize_pipeline_status()` after creating a LightRAG instance, otherwise you will encounter errors like:
- `AttributeError: __aenter__` - if storages are not initialized - `AttributeError: __aenter__` - if storages are not initialized
- `KeyError: 'history_messages'` - if pipeline status is not initialized - `KeyError: 'history_messages'` - if pipeline status is not initialized
@ -596,36 +597,15 @@ if __name__ == "__main__":
</details> </details>
### Conversation History Support ### Rerank Function Injection
To enhance retrieval quality, documents can be re-ranked based on a more effective relevance scoring model. The `rerank.py` file provides three Reranker provider driver functions:
LightRAG now supports multi-turn dialogue through the conversation history feature. Here's how to use it: * **Cohere / vLLM**: `cohere_rerank`
* **Jina AI**: `jina_rerank`
* **Aliyun**: `ali_rerank`
<details> You can inject one of these functions into the `rerank_model_func` attribute of the LightRAG object. This will enable LightRAG's query function to re-order retrieved text blocks using the injected function. For detailed usage, please refer to the `examples/rerank_example.py` file.
<summary> <b> Usage Example </b></summary>
```python
# Create conversation history
conversation_history = [
{"role": "user", "content": "What is the main character's attitude towards Christmas?"},
{"role": "assistant", "content": "At the beginning of the story, Ebenezer Scrooge has a very negative attitude towards Christmas..."},
{"role": "user", "content": "How does his attitude change?"}
]
# Create query parameters with conversation history
query_param = QueryParam(
mode="mix", # or any other mode: "local", "global", "hybrid"
conversation_history=conversation_history, # Add the conversation history
)
# Make a query that takes into account the conversation history
response = rag.query(
"What causes this change in his character?",
param=query_param
)
```
</details>
### User Prompt vs. Query ### User Prompt vs. Query
@ -646,8 +626,6 @@ response_default = rag.query(
print(response_default) print(response_default)
``` ```
### Insert ### Insert
<details> <details>

View file

@ -421,6 +421,44 @@ LIGHTRAG_DOC_STATUS_STORAGE=PGDocStatusStorage
| --embedding-binding | ollama | 嵌入绑定类型lollms、ollama、openai、azure_openai、aws_bedrock | | --embedding-binding | ollama | 嵌入绑定类型lollms、ollama、openai、azure_openai、aws_bedrock |
| auto-scan-at-startup | - | 扫描输入目录中的新文件并开始索引 | | auto-scan-at-startup | - | 扫描输入目录中的新文件并开始索引 |
### Reranking 配置
Reranking 查询召回的块可以显著提高检索质量它通过基于优化的相关性评分模型对文档重新排序。LightRAG 目前支持以下 rerank 提供商:
- **Cohere / vLLM**:提供与 Cohere AI 的 `v2/rerank` 端点的完整 API 集成。由于 vLLM 提供了与 Cohere 兼容的 reranker API因此也支持所有通过 vLLM 部署的 reranker 模型。
- **Jina AI**:提供与所有 Jina rerank 模型的完全实现兼容性。
- **阿里云**:具有旨在支持阿里云 rerank API 格式的自定义实现。
Rerank 提供商通过 `.env` 文件进行配置。以下是使用 vLLM 本地部署的 rerank 模型的示例配置:
```
RERANK_BINDING=cohere
RERANK_MODEL=BAAI/bge-reranker-v2-m3
RERANK_BINDING_HOST=http://localhost:8000/v1/rerank
RERANK_BINDING_API_KEY=your_rerank_api_key_here
```
以下是使用阿里云提供的 Reranker 服务的示例配置:
```
RERANK_BINDING=aliyun
RERANK_MODEL=gte-rerank-v2
RERANK_BINDING_HOST=https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank
RERANK_BINDING_API_KEY=your_rerank_api_key_here
```
有关完整的 reranker 配置示例,请参阅 `env.example` 文件。
### 启用 Reranking
可以按查询启用或禁用 Reranking。
`/query``/query/stream` API 端点包含一个 `enable_rerank` 参数,默认设置为 `true`,用于控制当前查询是否激活 reranking。要将 `enable_rerank` 参数的默认值更改为 `false`,请设置以下环境变量:
```
RERANK_BY_DEFAULT=False
```
### .env 文件示例 ### .env 文件示例
```bash ```bash

View file

@ -422,9 +422,43 @@ You cannot change storage implementation selection after adding documents to Lig
| --embedding-binding | ollama | Embedding binding type (lollms, ollama, openai, azure_openai, aws_bedrock) | | --embedding-binding | ollama | Embedding binding type (lollms, ollama, openai, azure_openai, aws_bedrock) |
| --auto-scan-at-startup| - | Scan input directory for new files and start indexing | | --auto-scan-at-startup| - | Scan input directory for new files and start indexing |
### Additional Ollama Binding Options ### Reranking Configuration
When using `--llm-binding ollama` or `--embedding-binding ollama`, additional Ollama-specific configuration options are available. To see all available Ollama binding options, add `--help` to the command line when starting the server. These additional options allow for fine-tuning of Ollama model parameters and connection settings. Reranking query-recalled chunks can significantly enhance retrieval quality by re-ordering documents based on an optimized relevance scoring model. LightRAG currently supports the following rerank providers:
- **Cohere / vLLM**: Offers full API integration with Cohere AI's `v2/rerank` endpoint. As vLLM provides a Cohere-compatible reranker API, all reranker models deployed via vLLM are also supported.
- **Jina AI**: Provides complete implementation compatibility with all Jina rerank models.
- **Aliyun**: Features a custom implementation designed to support Aliyun's rerank API format.
The rerank provider is configured via the `.env` file. Below is an example configuration for a rerank model deployed locally using vLLM:
```
RERANK_BINDING=cohere
RERANK_MODEL=BAAI/bge-reranker-v2-m3
RERANK_BINDING_HOST=http://localhost:8000/v1/rerank
RERANK_BINDING_API_KEY=your_rerank_api_key_here
```
Here is an example configuration for utilizing the Reranker service provided by Aliyun:
```
RERANK_BINDING=aliyun
RERANK_MODEL=gte-rerank-v2
RERANK_BINDING_HOST=https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank
RERANK_BINDING_API_KEY=your_rerank_api_key_here
```
For comprehensive reranker configuration examples, please refer to the `env.example` file.
### Enable Reranking
Reranking can be enabled or disabled on a per-query basis.
The `/query` and `/query/stream` API endpoints include an `enable_rerank` parameter, which is set to `true` by default, controlling whether reranking is active for the current query. To change the default value of the `enable_rerank` parameter to `false`, set the following environment variable:
```
RERANK_BY_DEFAULT=False
```
### .env Examples ### .env Examples