2024-12-25 20:21:38 +08:00
---
2025-05-23 14:18:14 +08:00
sidebar_position: 5
2024-10-30 19:40:39 +08:00
slug: /python_api_reference
---
2025-01-06 16:54:22 +08:00
# Python API
2024-10-09 15:30:22 +08:00
2025-03-03 15:42:39 +08:00
A complete reference for RAGFlow's Python APIs. Before proceeding, please ensure you [have your RAGFlow API key ready for authentication ](../guides/models/llm_api_key_setup.md ).
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
:::tip NOTE
Run the following command to download the Python SDK:
2024-10-19 19:46:13 +08:00
2024-12-18 19:01:05 +08:00
```bash
pip install ragflow-sdk
```
2025-02-26 15:52:26 +08:00
2024-10-09 15:30:22 +08:00
:::
2024-10-19 19:46:13 +08:00
---
2024-12-13 10:25:52 +08:00
2025-03-26 09:03:18 +08:00
## ERROR CODES
---
| Code | Message | Description |
|------|----------------------|-----------------------------|
| 400 | Bad Request | Invalid request parameters |
| 401 | Unauthorized | Unauthorized access |
| 403 | Forbidden | Access denied |
| 404 | Not Found | Resource not found |
| 500 | Internal Server Error| Server internal error |
| 1001 | Invalid Chunk ID | Invalid Chunk ID |
| 1002 | Chunk Update Failed | Chunk update failed |
---
2025-02-26 15:52:26 +08:00
## OpenAI-Compatible API
---
### Create chat completion
Creates a model response for the given historical chat conversation via OpenAI's API.
#### Parameters
##### model: `str`, *Required*
The model used to generate the response. The server will parse this automatically, so you can set it to any value for now.
##### messages: `list[object]`, *Required*
A list of historical chat messages used to generate the response. This must contain at least one message with the `user` role.
##### stream: `boolean`
Whether to receive the response as a stream. Set this to `false` explicitly if you prefer to receive the entire response in one go instead of as a stream.
#### Returns
2025-02-28 16:09:40 +08:00
- Success: Response [message ](https://platform.openai.com/docs/api-reference/chat/create ) like OpenAI
2025-02-26 15:52:26 +08:00
- Failure: `Exception`
#### Examples
```python
from openai import OpenAI
model = "model"
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/< chat_id > ")
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"},
],
stream=True
)
stream = True
if stream:
for chunk in completion:
print(chunk)
else:
print(completion.choices[0].message.content)
```
2024-12-18 19:01:05 +08:00
## DATASET MANAGEMENT
2024-12-13 10:25:52 +08:00
2024-12-18 19:01:05 +08:00
---
2024-10-19 19:46:13 +08:00
2024-12-18 19:01:05 +08:00
### Create dataset
2024-10-09 15:30:22 +08:00
```python
RAGFlow.create_dataset(
name: str,
2025-04-29 16:53:57 +08:00
avatar: Optional[str] = None,
description: Optional[str] = None,
embedding_model: Optional[str] = "BAAI/bge-large-zh-v1.5@BAAI ",
2024-10-09 15:30:22 +08:00
permission: str = "me",
2024-10-18 20:56:33 +08:00
chunk_method: str = "naive",
2024-10-09 15:30:22 +08:00
parser_config: DataSet.ParserConfig = None
) -> DataSet
```
2024-10-18 20:56:33 +08:00
Creates a dataset.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`, *Required*
2024-10-09 15:30:22 +08:00
The unique name of the dataset to create. It must adhere to the following requirements:
2025-04-29 16:53:57 +08:00
- Maximum 128 characters.
2024-10-09 15:30:22 +08:00
- Case-insensitive.
2024-12-18 19:01:05 +08:00
##### avatar: `str`
2024-10-09 15:30:22 +08:00
2025-04-29 16:53:57 +08:00
Base64 encoding of the avatar. Defaults to `None`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### description: `str`
2024-10-09 15:30:22 +08:00
2025-04-29 16:53:57 +08:00
A brief description of the dataset to create. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### permission
2024-10-09 15:30:22 +08:00
2024-11-14 18:44:37 +08:00
Specifies who can access the dataset to create. Available options:
- `"me"` : (Default) Only you can manage the dataset.
- `"team"` : All team members can manage the dataset.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### chunk_method, `str`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The chunking method of the dataset to create. Available options:
- `"naive"` : General (default)
- `"manual` : Manual
- `"qa"` : Q& A
- `"table"` : Table
- `"paper"` : Paper
- `"book"` : Book
- `"laws"` : Laws
- `"presentation"` : Presentation
- `"picture"` : Picture
2024-10-25 17:11:58 +08:00
- `"one"` : One
2024-10-30 17:59:23 +08:00
- `"email"` : Email
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### parser_config
2024-10-09 15:30:22 +08:00
2024-10-30 17:59:23 +08:00
The parser configuration of the dataset. A `ParserConfig` object's attributes vary based on the selected `chunk_method` :
2024-10-30 15:33:36 +08:00
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"naive"` :
2025-04-15 17:45:52 +08:00
`{"chunk_token_num":128,"delimiter":"\\n","html4excel":False,"layout_recognize":True,"raptor":{"use_raptor":False}}` .
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"qa"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"manuel"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"table"` :
`None`
- `chunk_method` =`"paper"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"book"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"laws"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"picture"` :
`None`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"presentation"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"one"` :
`None`
- `chunk_method` =`"knowledge-graph"` :
2025-04-09 19:32:25 +08:00
`{"chunk_token_num":128,"delimiter":"\\n","entity_types":["organization","person","location","event","time"]}`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"email"` :
`None`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-14 20:48:23 +08:00
- Success: A `dataset` object.
- Failure: `Exception`
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-19 19:46:13 +08:00
dataset = rag_object.create_dataset(name="kb_1")
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Delete datasets
2024-10-09 15:30:22 +08:00
```python
2025-05-16 10:16:43 +08:00
RAGFlow.delete_datasets(ids: list[str] | None = None)
2024-10-09 15:30:22 +08:00
```
2024-10-24 20:04:50 +08:00
Deletes datasets by ID.
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-14 20:03:33 +08:00
2025-05-16 10:16:43 +08:00
##### ids: `list[str]` or `None`, *Required*
2024-10-11 09:55:27 +08:00
2025-05-16 10:16:43 +08:00
The IDs of the datasets to delete. Defaults to `None` .
- If `None` , all datasets will be deleted.
- If an array of IDs, only the specified datasets will be deleted.
- If an empty array, no datasets will be deleted.
2024-10-11 09:55:27 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
2024-10-12 20:07:21 +08:00
```python
2025-05-16 10:16:43 +08:00
rag_object.delete_datasets(ids=["d94a8dc02c9711f0930f7fbc369eab6d","e94a8dc02c9711f0930f7fbc369eab6e"])
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### List datasets
2024-10-09 15:30:22 +08:00
```python
RAGFlow.list_datasets(
page: int = 1,
2024-11-05 15:21:37 +08:00
page_size: int = 30,
2024-10-09 15:30:22 +08:00
orderby: str = "create_time",
2024-10-11 09:55:27 +08:00
desc: bool = True,
id: str = None,
name: str = None
2024-10-16 20:38:19 +08:00
) -> list[DataSet]
2024-10-09 15:30:22 +08:00
```
2024-10-22 17:10:23 +08:00
Lists datasets.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
Specifies the page on which the datasets will be displayed. Defaults to `1` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-09 15:30:22 +08:00
2024-11-05 15:21:37 +08:00
The number of datasets on each page. Defaults to `30` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The field by which datasets should be sorted. Available options:
- `"create_time"` (default)
- `"update_time"`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
Indicates whether the retrieved datasets should be sorted in descending order. Defaults to `True` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The ID of the dataset to retrieve. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The name of the dataset to retrieve. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
- Success: A list of `DataSet` objects.
2024-10-14 20:48:23 +08:00
- Failure: `Exception` .
2024-10-12 20:07:21 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### List all datasets
2024-10-14 20:03:33 +08:00
2024-10-14 20:48:23 +08:00
```python
2024-10-19 19:46:13 +08:00
for dataset in rag_object.list_datasets():
print(dataset)
2024-10-09 15:30:22 +08:00
```
2024-12-18 19:01:05 +08:00
##### Retrieve a dataset by ID
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
```python
dataset = rag_object.list_datasets(id = "id_1")
print(dataset[0])
```
---
2024-10-14 20:03:33 +08:00
2024-12-18 19:01:05 +08:00
### Update dataset
2024-10-09 15:30:22 +08:00
```python
2024-10-11 09:55:27 +08:00
DataSet.update(update_message: dict)
2024-10-09 15:30:22 +08:00
```
2024-10-19 19:46:13 +08:00
Updates configurations for the current dataset.
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
##### update_message: `dict[str, str|int]`, *Required*
2024-10-14 20:48:23 +08:00
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with the following keys:
2024-10-25 17:11:58 +08:00
- `"name"` : `str` The revised name of the dataset.
2025-05-09 19:17:08 +08:00
- Basic Multilingual Plane (BMP) only
- Maximum 128 characters
- Case-insensitive
- `"avatar"` : (*Body parameter*), `string`
The updated base64 encoding of the avatar.
- Maximum 65535 characters
- `"embedding_model"` : (*Body parameter*), `string`
The updated embedding model name.
2024-10-14 20:48:23 +08:00
- Ensure that `"chunk_count"` is `0` before updating `"embedding_model"` .
2025-05-09 19:17:08 +08:00
- Maximum 255 characters
- Must follow `model_name@model_factory` format
- `"permission"` : (*Body parameter*), `string`
The updated dataset permission. Available options:
- `"me"` : (Default) Only you can manage the dataset.
- `"team"` : All team members can manage the dataset.
- `"pagerank"` : (*Body parameter*), `int`
refer to [Set page rank ](https://ragflow.io/docs/dev/set_page_rank )
- Default: `0`
- Minimum: `0`
- Maximum: `100`
- `"chunk_method"` : (*Body parameter*), `enum<string>`
The chunking method for the dataset. Available options:
- `"naive"` : General (default)
2024-10-14 20:48:23 +08:00
- `"book"` : Book
2025-05-09 19:17:08 +08:00
- `"email"` : Email
2024-10-14 20:48:23 +08:00
- `"laws"` : Laws
2025-05-09 19:17:08 +08:00
- `"manual"` : Manual
2024-10-25 17:11:58 +08:00
- `"one"` : One
2025-05-09 19:17:08 +08:00
- `"paper"` : Paper
- `"picture"` : Picture
- `"presentation"` : Presentation
- `"qa"` : Q& A
- `"table"` : Table
- `"tag"` : Tag
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(name="kb_name")
2025-03-26 17:30:09 +08:00
dataset = dataset[0]
2024-10-18 20:56:33 +08:00
dataset.update({"embedding_model":"BAAI/bge-zh-v1.5", "chunk_method":"manual"})
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-09 15:30:22 +08:00
---
2024-12-18 19:01:05 +08:00
## FILE MANAGEMENT WITHIN DATASET
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
---
2024-12-18 19:01:05 +08:00
### Upload documents
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
DataSet.upload_documents(document_list: list[dict])
2024-10-09 15:30:22 +08:00
```
2024-10-18 20:56:33 +08:00
Uploads documents to the current dataset.
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### document_list: `list[dict]`, *Required*
2024-10-17 18:19:17 +08:00
A list of dictionaries representing the documents to upload, each containing the following keys:
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
- `"display_name"` : (Optional) The file name to display in the dataset.
- `"blob"` : (Optional) The binary content of the file to upload.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-16 18:41:24 +08:00
2024-10-17 18:19:17 +08:00
```python
2024-10-18 20:56:33 +08:00
dataset = rag_object.create_dataset(name="kb_name")
dataset.upload_documents([{"display_name": "1.txt", "blob": "< BINARY_CONTENT_OF_THE_DOC > "}, {"display_name": "2.pdf", "blob": "< BINARY_CONTENT_OF_THE_DOC > "}])
2024-10-16 18:41:24 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-16 18:41:24 +08:00
---
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
### Update document
2024-10-16 18:41:24 +08:00
```python
Document.update(update_message:dict)
```
2024-10-17 18:19:17 +08:00
Updates configurations for the current document.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### update_message: `dict[str, str|dict[]]`, *Required*
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with the following keys:
2024-10-22 17:10:23 +08:00
- `"display_name"` : `str` The name of the document to update.
2025-03-04 15:43:09 +08:00
- `"meta_fields"` : `dict[str, Any]` The meta fields of the document.
2024-10-18 20:56:33 +08:00
- `"chunk_method"` : `str` The parsing method to apply to the document.
- `"naive"` : General
- `"manual` : Manual
- `"qa"` : Q& A
- `"table"` : Table
- `"paper"` : Paper
- `"book"` : Book
- `"laws"` : Laws
- `"presentation"` : Presentation
- `"picture"` : Picture
- `"one"` : One
2024-10-30 17:59:23 +08:00
- `"email"` : Email
2024-10-30 15:33:36 +08:00
- `"parser_config"` : `dict[str, Any]` The parsing configuration for the document. Its attributes vary based on the selected `"chunk_method"` :
- `"chunk_method"` =`"naive"` :
2025-04-15 17:45:52 +08:00
`{"chunk_token_num":128,"delimiter":"\\n","html4excel":False,"layout_recognize":True,"raptor":{"use_raptor":False}}` .
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"qa"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"manuel"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"table"` :
`None`
- `chunk_method` =`"paper"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"book"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"laws"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"presentation"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"picture"` :
`None`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"one"` :
`None`
- `chunk_method` =`"knowledge-graph"` :
2025-04-09 19:32:25 +08:00
`{"chunk_token_num":128,"delimiter":"\\n","entity_types":["organization","person","location","event","time"]}`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"email"` :
`None`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
2024-10-16 18:41:24 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(id='id')
dataset = dataset[0]
2024-10-17 18:19:17 +08:00
doc = dataset.list_documents(id="wdfxb5t547d")
2024-10-16 18:41:24 +08:00
doc = doc[0]
2024-10-18 20:56:33 +08:00
doc.update([{"parser_config": {"chunk_token_count": 256}}, {"chunk_method": "manual"}])
2024-10-16 18:41:24 +08:00
```
2024-10-09 15:30:22 +08:00
---
2024-12-18 19:01:05 +08:00
### Download document
2024-10-09 15:30:22 +08:00
```python
2024-10-16 18:41:24 +08:00
Document.download() -> bytes
```
2024-10-19 19:46:13 +08:00
Downloads the current document.
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-16 18:41:24 +08:00
2024-10-18 20:56:33 +08:00
The downloaded document in bytes.
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-16 18:41:24 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-16 18:41:24 +08:00
2024-10-18 20:56:33 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(id="id")
dataset = dataset[0]
doc = dataset.list_documents(id="wdfxb5t547d")
2024-10-16 18:41:24 +08:00
doc = doc[0]
open("~/ragflow.txt", "wb+").write(doc.download())
print(doc)
```
---
2024-12-18 19:01:05 +08:00
### List documents
2024-10-16 18:41:24 +08:00
```python
2024-11-05 15:21:37 +08:00
Dataset.list_documents(id:str =None, keywords: str=None, page: int=1, page_size:int = 30, order_by:str = "create_time", desc: bool = True) -> list[Document]
2024-10-09 15:30:22 +08:00
```
2024-10-22 17:10:23 +08:00
Lists documents in the current dataset.
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The ID of the document to retrieve. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### keywords: `str`
2024-10-16 18:41:24 +08:00
2024-10-21 09:47:59 +08:00
The keywords used to match document titles. Defaults to `None` .
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-16 18:41:24 +08:00
2024-11-04 20:03:14 +08:00
Specifies the page on which the documents will be displayed. Defaults to `1` .
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-16 18:41:24 +08:00
2024-11-05 15:21:37 +08:00
The maximum number of documents on each page. Defaults to `30` .
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The field by which documents should be sorted. Available options:
2024-10-18 20:56:33 +08:00
2024-10-19 19:46:13 +08:00
- `"create_time"` (default)
2024-10-18 20:56:33 +08:00
- `"update_time"`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-10-17 18:19:17 +08:00
2024-10-18 20:56:33 +08:00
Indicates whether the retrieved documents should be sorted in descending order. Defaults to `True` .
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-17 19:52:35 +08:00
- Success: A list of `Document` objects.
- Failure: `Exception` .
2024-10-09 15:30:22 +08:00
2024-10-17 19:52:35 +08:00
A `Document` object contains the following attributes:
2024-10-19 19:46:13 +08:00
- `id` : The document ID. Defaults to `""` .
- `name` : The document name. Defaults to `""` .
- `thumbnail` : The thumbnail image of the document. Defaults to `None` .
2024-10-24 16:14:07 +08:00
- `dataset_id` : The dataset ID associated with the document. Defaults to `None` .
2025-04-27 11:44:08 +08:00
- `chunk_method` The chunking method name. Defaults to `"naive"` .
2024-10-19 19:46:13 +08:00
- `source_type` : The source type of the document. Defaults to `"local"` .
2024-10-21 19:50:45 +08:00
- `type` : Type or category of the document. Defaults to `""` . Reserved for future use.
2024-10-19 19:46:13 +08:00
- `created_by` : `str` The creator of the document. Defaults to `""` .
- `size` : `int` The document size in bytes. Defaults to `0` .
- `token_count` : `int` The number of tokens in the document. Defaults to `0` .
2024-10-21 09:47:59 +08:00
- `chunk_count` : `int` The number of chunks in the document. Defaults to `0` .
2024-10-19 19:46:13 +08:00
- `progress` : `float` The current processing progress as a percentage. Defaults to `0.0` .
- `progress_msg` : `str` A message indicating the current progress status. Defaults to `""` .
- `process_begin_at` : `datetime` The start time of document processing. Defaults to `None` .
2024-10-21 19:50:45 +08:00
- `process_duation` : `float` Duration of the processing in seconds. Defaults to `0.0` .
- `run` : `str` The document's processing status:
2024-10-23 11:00:35 +08:00
- `"UNSTART"` (default)
- `"RUNNING"`
- `"CANCEL"`
- `"DONE"`
- `"FAIL"`
2024-10-21 19:50:45 +08:00
- `status` : `str` Reserved for future use.
2024-10-30 15:33:36 +08:00
- `parser_config` : `ParserConfig` Configuration object for the parser. Its attributes vary based on the selected `chunk_method` :
- `chunk_method` =`"naive"` :
2025-04-15 17:45:52 +08:00
`{"chunk_token_num":128,"delimiter":"\\n","html4excel":False,"layout_recognize":True,"raptor":{"use_raptor":False}}` .
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"qa"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"manuel"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"table"` :
`None`
- `chunk_method` =`"paper"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"book"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"laws"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"presentation"` :
2025-04-15 17:45:52 +08:00
`{"raptor": {"use_raptor": False}}`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"picure"` :
`None`
2024-10-30 15:33:36 +08:00
- `chunk_method` =`"one"` :
`None`
2024-10-30 17:59:23 +08:00
- `chunk_method` =`"email"` :
`None`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.create_dataset(name="kb_1")
2024-10-09 15:30:22 +08:00
filename1 = "~/ragflow.txt"
2024-10-19 19:46:13 +08:00
blob = open(filename1 , "rb").read()
dataset.upload_documents([{"name":filename1,"blob":blob}])
2024-11-04 20:03:14 +08:00
for doc in dataset.list_documents(keywords="rag", page=0, page_size=12):
2024-10-19 19:46:13 +08:00
print(doc)
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Delete documents
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
DataSet.delete_documents(ids: list[str] = None)
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
Deletes documents by ID.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-19 19:46:13 +08:00
2024-12-18 19:01:05 +08:00
##### ids: `list[list]`
2024-10-19 19:46:13 +08:00
2024-10-24 20:04:50 +08:00
The IDs of the documents to delete. Defaults to `None` . If it is not specified, all documents in the dataset will be deleted.
2024-10-17 19:52:35 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(name="kb_1")
dataset = dataset[0]
dataset.delete_documents(ids=["id_1","id_2"])
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Parse documents
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
DataSet.async_parse_documents(document_ids:list[str]) -> None
2024-10-09 15:30:22 +08:00
```
2024-10-21 09:47:59 +08:00
Parses documents in the current dataset.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### document_ids: `list[str]`, *Required*
2024-10-17 18:19:17 +08:00
2024-10-17 19:52:35 +08:00
The IDs of the documents to parse.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-17 18:19:17 +08:00
2024-10-21 19:50:45 +08:00
- Success: No value is returned.
2024-10-17 18:19:17 +08:00
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.create_dataset(name="dataset_name")
2024-10-09 15:30:22 +08:00
documents = [
2024-10-22 17:10:23 +08:00
{'display_name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
{'display_name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
{'display_name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
2024-10-09 15:30:22 +08:00
]
2024-10-19 19:46:13 +08:00
dataset.upload_documents(documents)
documents = dataset.list_documents(keywords="test")
ids = []
2024-10-16 18:41:24 +08:00
for document in documents:
ids.append(document.id)
2024-10-19 19:46:13 +08:00
dataset.async_parse_documents(ids)
print("Async bulk parsing initiated.")
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
---
2024-12-18 19:01:05 +08:00
### Stop parsing documents
2024-10-18 20:56:33 +08:00
```python
DataSet.async_cancel_parse_documents(document_ids:list[str])-> None
```
2024-10-21 09:47:59 +08:00
Stops parsing specified documents.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
##### document_ids: `list[str]`, *Required*
2024-10-18 20:56:33 +08:00
2024-10-19 19:46:13 +08:00
The IDs of the documents for which parsing should be stopped.
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-18 20:56:33 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-18 20:56:33 +08:00
```python
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.create_dataset(name="dataset_name")
2024-10-18 20:56:33 +08:00
documents = [
2024-10-22 17:10:23 +08:00
{'display_name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
{'display_name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
{'display_name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
2024-10-18 20:56:33 +08:00
]
2024-10-19 19:46:13 +08:00
dataset.upload_documents(documents)
documents = dataset.list_documents(keywords="test")
ids = []
2024-10-18 20:56:33 +08:00
for document in documents:
ids.append(document.id)
2024-10-19 19:46:13 +08:00
dataset.async_parse_documents(ids)
print("Async bulk parsing initiated.")
dataset.async_cancel_parse_documents(ids)
print("Async bulk parsing cancelled.")
2024-10-18 20:56:33 +08:00
```
---
2024-12-25 20:21:38 +08:00
## CHUNK MANAGEMENT WITHIN DATASET
---
2024-12-18 19:01:05 +08:00
### Add chunk
2024-10-17 18:19:17 +08:00
2024-10-09 15:30:22 +08:00
```python
2024-10-22 17:10:23 +08:00
Document.add_chunk(content:str, important_keywords:list[str] = []) -> Chunk
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-21 09:47:59 +08:00
Adds a chunk to the current document.
2024-10-19 19:46:13 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### content: `str`, *Required*
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
The text content of the chunk.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### important_keywords: `list[str]`
2024-10-17 19:52:35 +08:00
2024-10-21 09:47:59 +08:00
The key terms or phrases to tag with the chunk.
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-17 19:52:35 +08:00
2024-10-21 09:47:59 +08:00
- Success: A `Chunk` object.
2024-10-19 19:46:13 +08:00
- Failure: `Exception` .
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
A `Chunk` object contains the following attributes:
2024-10-23 20:07:47 +08:00
- `id` : `str` : The chunk ID.
- `content` : `str` The text content of the chunk.
- `important_keywords` : `list[str]` A list of key terms or phrases tagged with the chunk.
2024-10-21 09:47:59 +08:00
- `create_time` : `str` The time when the chunk was created (added to the document).
- `create_timestamp` : `float` The timestamp representing the creation time of the chunk, expressed in seconds since January 1, 1970.
2024-10-24 16:14:07 +08:00
- `dataset_id` : `str` The ID of the associated dataset.
2024-10-21 09:47:59 +08:00
- `document_name` : `str` The name of the associated document.
- `document_id` : `str` The ID of the associated document.
2024-10-22 17:10:23 +08:00
- `available` : `bool` The chunk's availability status in the dataset. Value options:
- `False` : Unavailable
2024-10-24 20:04:50 +08:00
- `True` : Available (default)
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-17 19:52:35 +08:00
2024-10-16 18:41:24 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2025-01-27 15:45:16 +08:00
datasets = rag_object.list_datasets(id="123")
dataset = datasets[0]
2024-10-21 09:47:59 +08:00
doc = dataset.list_documents(id="wdfxb5t547d")
doc = doc[0]
chunk = doc.add_chunk(content="xxxxxxx")
2024-10-16 18:41:24 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-21 09:47:59 +08:00
---
2024-12-18 19:01:05 +08:00
### List chunks
2024-10-09 15:30:22 +08:00
```python
2024-11-05 15:21:37 +08:00
Document.list_chunks(keywords: str = None, page: int = 1, page_size: int = 30, id : str = None) -> list[Chunk]
2024-10-09 15:30:22 +08:00
```
2024-10-22 17:10:23 +08:00
Lists chunks in the current document.
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### keywords: `str`
2024-10-23 11:00:35 +08:00
2024-10-21 09:47:59 +08:00
The keywords used to match chunk content. Defaults to `None`
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-21 09:47:59 +08:00
2024-11-04 20:03:14 +08:00
Specifies the page on which the chunks will be displayed. Defaults to `1` .
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-17 18:19:17 +08:00
2024-11-05 15:21:37 +08:00
The maximum number of chunks on each page. Defaults to `30` .
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-21 09:47:59 +08:00
The ID of the chunk to retrieve. Default: `None`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
- Success: A list of `Chunk` objects.
- Failure: `Exception` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets("123")
dataset = dataset[0]
2024-12-27 14:40:00 +08:00
docs = dataset.list_documents(keywords="test", page=1, page_size=12)
for chunk in docs[0].list_chunks(keywords="rag", page=0, page_size=12):
2024-10-21 09:47:59 +08:00
print(chunk)
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Delete chunks
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
Document.delete_chunks(chunk_ids: list[str])
2024-10-09 15:30:22 +08:00
```
2024-10-17 19:52:35 +08:00
2024-10-19 19:46:13 +08:00
Deletes chunks by ID.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-17 18:19:17 +08:00
2024-12-18 19:01:05 +08:00
##### chunk_ids: `list[str]`
2024-10-17 18:19:17 +08:00
2024-10-24 20:04:50 +08:00
The IDs of the chunks to delete. Defaults to `None` . If it is not specified, all chunks of the current document will be deleted.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(id="123")
dataset = dataset[0]
doc = dataset.list_documents(id="wdfxb5t547d")
2024-10-16 18:41:24 +08:00
doc = doc[0]
2024-10-09 15:30:22 +08:00
chunk = doc.add_chunk(content="xxxxxxx")
2024-10-16 18:41:24 +08:00
doc.delete_chunks(["id_1","id_2"])
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Update chunk
2024-10-09 15:30:22 +08:00
```python
2024-10-16 18:41:24 +08:00
Chunk.update(update_message: dict)
2024-10-09 15:30:22 +08:00
```
2024-10-18 20:56:33 +08:00
2024-10-19 19:46:13 +08:00
Updates content or configurations for the current chunk.
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### update_message: `dict[str, str|list[str]|int]` *Required*
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with the following keys:
2024-10-23 20:07:47 +08:00
- `"content"` : `str` The text content of the chunk.
2024-10-21 09:47:59 +08:00
- `"important_keywords"` : `list[str]` A list of key terms or phrases to tag with the chunk.
2024-10-22 17:10:23 +08:00
- `"available"` : `bool` The chunk's availability status in the dataset. Value options:
- `False` : Unavailable
2024-10-24 20:04:50 +08:00
- `True` : Available (default)
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
dataset = rag_object.list_datasets(id="123")
2024-10-17 19:52:35 +08:00
dataset = dataset[0]
doc = dataset.list_documents(id="wdfxb5t547d")
2024-10-16 18:41:24 +08:00
doc = doc[0]
2024-10-09 15:30:22 +08:00
chunk = doc.add_chunk(content="xxxxxxx")
2024-10-17 19:52:35 +08:00
chunk.update({"content":"sdfx..."})
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Retrieve chunks
2024-10-09 15:30:22 +08:00
```python
2025-04-16 10:31:10 +08:00
RAGFlow.retrieve(question:str="", dataset_ids:list[str]=None, document_ids=list[str]=None, page:int=1, page_size:int=30, similarity_threshold:float=0.2, vector_similarity_weight:float=0.3, top_k:int=1024,rerank_id:str=None,keyword:bool=False,highlight:bool=False) -> list[Chunk]
2024-10-09 15:30:22 +08:00
```
2024-10-22 17:10:23 +08:00
Retrieves chunks from specified datasets.
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### question: `str`, *Required*
2024-10-09 15:30:22 +08:00
The user query or query keywords. Defaults to `""` .
2024-12-18 19:01:05 +08:00
##### dataset_ids: `list[str]`, *Required*
2024-10-09 15:30:22 +08:00
2025-03-28 16:03:37 +08:00
The IDs of the datasets to search. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### document_ids: `list[str]`
2024-10-09 15:30:22 +08:00
2025-03-28 16:03:37 +08:00
The IDs of the documents to search. Defaults to `None` . You must ensure all selected documents use the same embedding model. Otherwise, an error will occur.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-09 15:30:22 +08:00
2024-10-22 17:10:23 +08:00
The starting index for the documents to retrieve. Defaults to `1` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-09 15:30:22 +08:00
2024-11-05 15:21:37 +08:00
The maximum number of chunks to retrieve. Defaults to `30` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### Similarity_threshold: `float`
2024-10-09 15:30:22 +08:00
The minimum similarity score. Defaults to `0.2` .
2024-12-18 19:01:05 +08:00
##### vector_similarity_weight: `float`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The weight of vector cosine similarity. Defaults to `0.3` . If x represents the vector cosine similarity, then (1 - x) is the term similarity weight.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### top_k: `int`
2024-10-09 15:30:22 +08:00
2025-01-27 15:45:16 +08:00
The number of chunks engaged in vector cosine computation. Defaults to `1024` .
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
##### rerank_id: `str`
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
The ID of the rerank model. Defaults to `None` .
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### keyword: `bool`
2024-10-18 20:56:33 +08:00
2024-10-22 17:10:23 +08:00
Indicates whether to enable keyword-based matching:
2024-10-18 20:56:33 +08:00
2024-10-22 17:10:23 +08:00
- `True` : Enable keyword-based matching.
- `False` : Disable keyword-based matching (default).
2024-10-16 18:41:24 +08:00
2024-12-18 19:01:05 +08:00
##### highlight: `bool`
2024-10-16 18:41:24 +08:00
2024-10-23 20:07:47 +08:00
Specifies whether to enable highlighting of matched terms in the results:
2024-10-22 17:10:23 +08:00
- `True` : Enable highlighting of matched terms.
- `False` : Disable highlighting of matched terms (default).
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
- Success: A list of `Chunk` objects representing the document chunks.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-21 09:47:59 +08:00
dataset = rag_object.list_datasets(name="ragflow")
dataset = dataset[0]
2024-10-09 15:30:22 +08:00
name = 'ragflow_test.txt'
2024-10-16 18:41:24 +08:00
path = './test_data/ragflow_test.txt'
2024-12-31 17:25:24 +08:00
documents =[{"display_name":"test_retrieve_chunks.txt","blob":open(path, "rb").read()}]
2024-12-27 14:49:43 +08:00
docs = dataset.upload_documents(documents)
doc = docs[0]
doc.add_chunk(content="This is a chunk addition test")
for c in rag_object.retrieve(dataset_ids=[dataset.id],document_ids=[doc.id]):
print(c)
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
## CHAT ASSISTANT MANAGEMENT
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
---
2024-12-18 19:01:05 +08:00
### Create chat assistant
2024-10-14 20:48:23 +08:00
2024-10-09 15:30:22 +08:00
```python
2024-10-12 13:48:43 +08:00
RAGFlow.create_chat(
2024-10-18 20:56:33 +08:00
name: str,
avatar: str = "",
2024-10-24 16:14:07 +08:00
dataset_ids: list[str] = [],
2024-10-12 13:48:43 +08:00
llm: Chat.LLM = None,
prompt: Chat.Prompt = None
) -> Chat
2024-10-09 15:30:22 +08:00
```
2024-10-16 20:38:19 +08:00
Creates a chat assistant.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`, *Required*
2024-10-18 20:56:33 +08:00
2024-10-23 20:07:47 +08:00
The name of the chat assistant.
2024-10-18 20:56:33 +08:00
2024-12-18 19:01:05 +08:00
##### avatar: `str`
2024-10-18 20:56:33 +08:00
Base64 encoding of the avatar. Defaults to `""` .
2024-12-18 19:01:05 +08:00
##### dataset_ids: `list[str]`
2024-10-18 20:56:33 +08:00
The IDs of the associated datasets. Defaults to `[""]` .
2024-12-18 19:01:05 +08:00
##### llm: `Chat.LLM`
2024-10-18 20:56:33 +08:00
2024-10-23 20:07:47 +08:00
The LLM settings for the chat assistant to create. Defaults to `None` . When the value is `None` , a dictionary with the following values will be generated as the default. An `LLM` object contains the following attributes:
2024-10-18 20:56:33 +08:00
2024-10-23 20:07:47 +08:00
- `model_name` : `str`
2024-10-24 20:04:50 +08:00
The chat model name. If it is `None` , the user's default chat model will be used.
2024-10-23 20:07:47 +08:00
- `temperature` : `float`
2024-12-18 15:46:31 +08:00
Controls the randomness of the model's predictions. A lower temperature results in more conservative responses, while a higher temperature yields more creative and diverse responses. Defaults to `0.1` .
2024-10-23 20:07:47 +08:00
- `top_p` : `float`
2024-10-18 20:56:33 +08:00
Also known as “nucleus sampling”, this parameter sets a threshold to select a smaller set of words to sample from. It focuses on the most likely words, cutting off the less probable ones. Defaults to `0.3`
2024-10-23 20:07:47 +08:00
- `presence_penalty` : `float`
2024-10-18 20:56:33 +08:00
This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2` .
2024-10-23 20:07:47 +08:00
- `frequency penalty` : `float`
2024-10-18 20:56:33 +08:00
Similar to the presence penalty, this reduces the model’ s tendency to repeat the same words frequently. Defaults to `0.7` .
2024-12-18 19:01:05 +08:00
##### prompt: `Chat.Prompt`
2024-10-18 20:56:33 +08:00
Instructions for the LLM to follow. A `Prompt` object contains the following attributes:
2024-11-15 15:17:34 +08:00
- `similarity_threshold` : `float` RAGFlow employs either a combination of weighted keyword similarity and weighted vector cosine similarity, or a combination of weighted keyword similarity and weighted reranking score during retrieval. If a similarity score falls below this threshold, the corresponding chunk will be excluded from the results. The default value is `0.2` .
2024-10-23 20:07:47 +08:00
- `keywords_similarity_weight` : `float` This argument sets the weight of keyword similarity in the hybrid similarity score with vector cosine similarity or reranking model similarity. By adjusting this weight, you can control the influence of keyword similarity in relation to other similarity measures. The default value is `0.7` .
- `top_n` : `int` This argument specifies the number of top chunks with similarity scores above the `similarity_threshold` that are fed to the LLM. The LLM will *only* access these 'top N' chunks. The default value is `8` .
- `variables` : `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations** . Note that:
2024-10-24 20:04:50 +08:00
- `knowledge` is a reserved variable, which represents the retrieved chunks.
- All the variables in 'System' should be curly bracketed.
- The default value is `[{"key": "knowledge", "optional": True}]` .
2024-10-23 20:07:47 +08:00
- `rerank_model` : `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""` .
2024-12-30 19:22:57 +08:00
- `top_k` : `int` Refers to the process of reordering or selecting the top-k items from a list or set based on a specific ranking criterion. Default to 1024.
2024-10-23 20:07:47 +08:00
- `empty_response` : `str` If nothing is retrieved in the dataset for the user's question, this will be used as the response. To allow the LLM to improvise when nothing is found, leave this blank. Defaults to `None` .
- `opener` : `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"` .
- `show_quote` : `bool` Indicates whether the source of text should be displayed. Defaults to `True` .
2024-10-31 10:37:13 +08:00
- `prompt` : `str` The prompt content.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-18 20:56:33 +08:00
- Success: A `Chat` object representing the chat assistant.
- Failure: `Exception`
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
datasets = rag_object.list_datasets(name="kb_1")
dataset_ids = []
for dataset in datasets:
dataset_ids.append(dataset.id)
2024-10-24 16:14:07 +08:00
assistant = rag_object.create_chat("Miss R", dataset_ids=dataset_ids)
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Update chat assistant
2024-10-09 15:30:22 +08:00
```python
2024-10-12 13:48:43 +08:00
Chat.update(update_message: dict)
2024-10-09 15:30:22 +08:00
```
2024-10-19 19:46:13 +08:00
Updates configurations for the current chat assistant.
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
##### update_message: `dict[str, str|list[str]|dict[]]`, *Required*
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with the following keys:
2024-10-14 20:48:23 +08:00
2024-10-25 17:11:58 +08:00
- `"name"` : `str` The revised name of the chat assistant.
2024-10-14 20:48:23 +08:00
- `"avatar"` : `str` Base64 encoding of the avatar. Defaults to `""`
2024-10-24 16:14:07 +08:00
- `"dataset_ids"` : `list[str]` The datasets to update.
2024-10-16 20:38:19 +08:00
- `"llm"` : `dict` The LLM settings:
- `"model_name"` , `str` The chat model name.
2024-12-18 15:46:31 +08:00
- `"temperature"` , `float` Controls the randomness of the model's predictions. A lower temperature results in more conservative responses, while a higher temperature yields more creative and diverse responses.
2024-10-14 20:48:23 +08:00
- `"top_p"` , `float` Also known as “nucleus sampling”, this parameter sets a threshold to select a smaller set of words to sample from.
2024-10-16 20:38:19 +08:00
- `"presence_penalty"` , `float` This discourages the model from repeating the same information by penalizing words that have appeared in the conversation.
- `"frequency penalty"` , `float` Similar to presence penalty, this reduces the model’ s tendency to repeat the same words.
- `"prompt"` : Instructions for the LLM to follow.
2024-11-15 15:17:34 +08:00
- `"similarity_threshold"` : `float` RAGFlow employs either a combination of weighted keyword similarity and weighted vector cosine similarity, or a combination of weighted keyword similarity and weighted rerank score during retrieval. This argument sets the threshold for similarities between the user query and chunks. If a similarity score falls below this threshold, the corresponding chunk will be excluded from the results. The default value is `0.2` .
2024-10-23 20:07:47 +08:00
- `"keywords_similarity_weight"` : `float` This argument sets the weight of keyword similarity in the hybrid similarity score with vector cosine similarity or reranking model similarity. By adjusting this weight, you can control the influence of keyword similarity in relation to other similarity measures. The default value is `0.7` .
- `"top_n"` : `int` This argument specifies the number of top chunks with similarity scores above the `similarity_threshold` that are fed to the LLM. The LLM will *only* access these 'top N' chunks. The default value is `8` .
2024-10-24 20:04:50 +08:00
- `"variables"` : `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations** . Note that:
- `knowledge` is a reserved variable, which represents the retrieved chunks.
- All the variables in 'System' should be curly bracketed.
- The default value is `[{"key": "knowledge", "optional": True}]` .
2024-10-16 20:38:19 +08:00
- `"rerank_model"` : `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""` .
2024-10-18 20:56:33 +08:00
- `"empty_response"` : `str` If nothing is retrieved in the dataset for the user's question, this will be used as the response. To allow the LLM to improvise when nothing is retrieved, leave this blank. Defaults to `None` .
2024-10-16 20:38:19 +08:00
- `"opener"` : `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"` .
- `"show_quote` : `bool` Indicates whether the source of text should be displayed Defaults to `True` .
2024-10-31 10:37:13 +08:00
- `"prompt"` : `str` The prompt content.
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
datasets = rag_object.list_datasets(name="kb_1")
2024-10-24 16:14:07 +08:00
dataset_id = datasets[0].id
assistant = rag_object.create_chat("Miss R", dataset_ids=[dataset_id])
2024-10-16 20:38:19 +08:00
assistant.update({"name": "Stefan", "llm": {"temperature": 0.8}, "prompt": {"top_n": 8}})
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Delete chat assistants
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
RAGFlow.delete_chats(ids: list[str] = None)
2024-10-09 15:30:22 +08:00
```
2024-10-12 13:48:43 +08:00
2024-10-19 19:46:13 +08:00
Deletes chat assistants by ID.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-12 13:48:43 +08:00
2024-12-18 19:01:05 +08:00
##### ids: `list[str]`
2024-10-12 13:48:43 +08:00
2024-11-18 17:38:17 +08:00
The IDs of the chat assistants to delete. Defaults to `None` . If it is empty or not specified, all chat assistants in the system will be deleted.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
rag_object.delete_chats(ids=["id_1","id_2"])
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### List chat assistants
2024-10-09 15:30:22 +08:00
```python
2024-10-12 13:48:43 +08:00
RAGFlow.list_chats(
page: int = 1,
2024-11-05 15:21:37 +08:00
page_size: int = 30,
2024-10-12 13:48:43 +08:00
orderby: str = "create_time",
desc: bool = True,
id: str = None,
name: str = None
2024-10-16 20:38:19 +08:00
) -> list[Chat]
2024-10-09 15:30:22 +08:00
```
2024-10-22 17:10:23 +08:00
Lists chat assistants.
2024-10-17 19:52:35 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
Specifies the page on which the chat assistants will be displayed. Defaults to `1` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-09 15:30:22 +08:00
2024-11-05 15:21:37 +08:00
The number of chat assistants on each page. Defaults to `30` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
The attribute by which the results are sorted. Available options:
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
- `"create_time"` (default)
- `"update_time"`
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
Indicates whether the retrieved chat assistants should be sorted in descending order. Defaults to `True` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
The ID of the chat assistant to retrieve. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
The name of the chat assistant to retrieve. Defaults to `None` .
2024-10-14 20:48:23 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
- Success: A list of `Chat` objects.
2024-10-14 20:48:23 +08:00
- Failure: `Exception` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
for assistant in rag_object.list_chats():
2024-10-14 20:48:23 +08:00
print(assistant)
2024-10-09 15:30:22 +08:00
```
---
2024-12-19 14:36:51 +08:00
## SESSION MANAGEMENT
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
---
2024-12-18 19:01:05 +08:00
### Create session with chat assistant
2024-10-09 15:30:22 +08:00
```python
2024-10-12 19:35:19 +08:00
Chat.create_session(name: str = "New session") -> Session
2024-10-09 15:30:22 +08:00
```
2024-11-14 18:44:37 +08:00
Creates a session with the current chat assistant.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
The name of the chat session to create.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
- Success: A `Session` object containing the following attributes:
- `id` : `str` The auto-generated unique identifier of the created session.
- `name` : `str` The name of the created session.
2025-06-05 03:29:07 +02:00
- `message` : `list[Message]` The opening message of the created session. Default: `[{"role": "assistant", "content": "Hi! I am your assistant, can I help you?"}]`
2024-10-16 20:38:19 +08:00
- `chat_id` : `str` The ID of the associated chat assistant.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag_object.list_chats(name="Miss R")
2024-10-16 20:38:19 +08:00
assistant = assistant[0]
session = assistant.create_session()
2024-10-09 15:30:22 +08:00
```
2024-10-21 09:47:59 +08:00
---
2024-12-18 19:01:05 +08:00
### Update chat assistant's session
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
Session.update(update_message: dict)
2024-10-09 15:30:22 +08:00
```
2024-11-14 18:44:37 +08:00
Updates the current session of the current chat assistant.
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
##### update_message: `dict[str, Any]`, *Required*
2024-10-16 20:38:19 +08:00
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with only one key:
2024-10-25 17:11:58 +08:00
- `"name"` : `str` The revised name of the session.
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag_object.list_chats(name="Miss R")
2024-10-16 20:38:19 +08:00
assistant = assistant[0]
session = assistant.create_session("session_name")
session.update({"name": "updated_name"})
2024-10-09 15:30:22 +08:00
```
---
2024-12-18 19:01:05 +08:00
### List chat assistant's sessions
2024-10-09 15:30:22 +08:00
```python
2024-10-12 19:35:19 +08:00
Chat.list_sessions(
page: int = 1,
2024-11-05 15:21:37 +08:00
page_size: int = 30,
2024-10-12 19:35:19 +08:00
orderby: str = "create_time",
desc: bool = True,
id: str = None,
name: str = None
2024-10-16 20:38:19 +08:00
) -> list[Session]
2024-10-09 15:30:22 +08:00
```
2024-10-16 20:38:19 +08:00
Lists sessions associated with the current chat assistant.
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
Specifies the page on which the sessions will be displayed. Defaults to `1` .
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-10-09 15:30:22 +08:00
2024-11-05 15:21:37 +08:00
The number of sessions on each page. Defaults to `30` .
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-10-12 19:35:19 +08:00
2024-10-19 19:46:13 +08:00
The field by which sessions should be sorted. Available options:
2024-10-18 20:56:33 +08:00
2024-10-19 19:46:13 +08:00
- `"create_time"` (default)
2024-10-18 20:56:33 +08:00
- `"update_time"`
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-10-12 19:35:19 +08:00
2024-10-18 20:56:33 +08:00
Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True` .
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-12 19:35:19 +08:00
2024-10-16 20:38:19 +08:00
The ID of the chat session to retrieve. Defaults to `None` .
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
##### name: `str`
2024-10-12 19:35:19 +08:00
2024-10-21 09:47:59 +08:00
The name of the chat session to retrieve. Defaults to `None` .
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-12 19:35:19 +08:00
2024-10-16 20:38:19 +08:00
- Success: A list of `Session` objects associated with the current chat assistant.
- Failure: `Exception` .
2024-10-12 19:35:19 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-16 20:38:19 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-16 20:38:19 +08:00
2024-10-19 19:46:13 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag_object.list_chats(name="Miss R")
2024-10-16 20:38:19 +08:00
assistant = assistant[0]
for session in assistant.list_sessions():
print(session)
```
2024-10-12 19:35:19 +08:00
2024-10-09 15:30:22 +08:00
---
2024-12-18 19:01:05 +08:00
### Delete chat assistant's sessions
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
Chat.delete_sessions(ids:list[str] = None)
2024-10-09 15:30:22 +08:00
```
2024-11-14 18:44:37 +08:00
Deletes sessions of the current chat assistant by ID.
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
##### ids: `list[str]`
2024-10-16 20:38:19 +08:00
2024-10-24 20:04:50 +08:00
The IDs of the sessions to delete. Defaults to `None` . If it is not specified, all sessions associated with the current chat assistant will be deleted.
2024-10-16 20:38:19 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-09 15:30:22 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-09 15:30:22 +08:00
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag_object.list_chats(name="Miss R")
2024-10-16 20:38:19 +08:00
assistant = assistant[0]
assistant.delete_sessions(ids=["id_1","id_2"])
2024-10-21 09:47:59 +08:00
```
---
2024-12-18 19:01:05 +08:00
### Converse with chat assistant
2024-10-21 09:47:59 +08:00
```python
2024-12-20 17:34:16 +08:00
Session.ask(question: str = "", stream: bool = False, **kwargs) -> Optional[Message, iter[Message]]
2024-10-21 09:47:59 +08:00
```
2024-11-14 18:44:37 +08:00
Asks a specified chat assistant a question to start an AI-powered conversation.
:::tip NOTE
In streaming mode, not all responses include a reference, as this depends on the system's judgement.
:::
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
##### question: `str`, *Required*
2024-10-21 09:47:59 +08:00
2025-01-27 15:45:16 +08:00
The question to start an AI-powered conversation. Default to `""`
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
##### stream: `bool`
2024-10-21 09:47:59 +08:00
Indicates whether to output responses in a streaming way:
2024-11-22 11:11:06 +08:00
- `True` : Enable streaming (default).
- `False` : Disable streaming.
2024-10-21 09:47:59 +08:00
2024-12-20 17:34:16 +08:00
##### **kwargs
The parameters in prompt(system).
2024-12-18 19:01:05 +08:00
#### Returns
2024-10-21 09:47:59 +08:00
2024-12-18 19:01:05 +08:00
- A `Message` object containing the response to the question if `stream` is set to `False` .
2024-10-21 09:47:59 +08:00
- An iterator containing multiple `message` objects (`iter[Message]` ) if `stream` is set to `True`
The following shows the attributes of a `Message` object:
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-10-21 09:47:59 +08:00
The auto-generated message ID.
2024-12-18 19:01:05 +08:00
##### content: `str`
2024-10-21 09:47:59 +08:00
The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"` .
2024-12-18 19:01:05 +08:00
##### reference: `list[Chunk]`
2024-10-21 09:47:59 +08:00
A list of `Chunk` objects representing references to the message, each containing the following attributes:
- `id` `str`
The chunk ID.
- `content` `str`
The content of the chunk.
2024-11-12 14:59:41 +08:00
- `img_id` `str`
2024-10-29 19:56:46 +08:00
The ID of the snapshot of the chunk. Applicable only when the source of the chunk is an image, PPT, PPTX, or PDF file.
2024-10-21 09:47:59 +08:00
- `document_id` `str`
The ID of the referenced document.
- `document_name` `str`
The name of the referenced document.
- `position` `list[str]`
The location information of the chunk within the referenced document.
2024-10-24 16:14:07 +08:00
- `dataset_id` `str`
2024-10-21 09:47:59 +08:00
The ID of the dataset to which the referenced document belongs.
2024-10-29 19:56:46 +08:00
- `similarity` `float`
A composite similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity. It is the weighted sum of `vector_similarity` and `term_similarity` .
2024-10-21 09:47:59 +08:00
- `vector_similarity` `float`
A vector similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between vector embeddings.
- `term_similarity` `float`
A keyword similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between keywords.
2024-12-18 19:01:05 +08:00
#### Examples
2024-10-21 09:47:59 +08:00
```python
2024-12-11 12:38:57 +08:00
from ragflow_sdk import RAGFlow
2024-10-21 09:47:59 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag_object.list_chats(name="Miss R")
assistant = assistant[0]
session = assistant.create_session()
print("\n==================== Miss R =====================\n")
2024-11-12 17:14:33 +08:00
print("Hello. What can I do for you?")
2024-10-21 09:47:59 +08:00
while True:
question = input("\n==================== User =====================\n> ")
print("\n==================== Miss R =====================\n")
cont = ""
for ans in session.ask(question, stream=True):
2024-11-06 18:03:45 +08:00
print(ans.content[len(cont):], end='', flush=True)
cont = ans.content
```
2024-11-14 18:44:37 +08:00
2024-11-06 18:03:45 +08:00
---
2024-12-18 19:01:05 +08:00
### Create session with agent
2024-11-06 18:03:45 +08:00
```python
2025-03-03 17:15:16 +08:00
Agent.create_session(**kwargs) -> Session
2024-11-06 18:03:45 +08:00
```
2024-12-18 19:01:05 +08:00
Creates a session with the current agent.
2024-11-06 18:03:45 +08:00
2024-12-20 17:34:16 +08:00
#### Parameters
##### **kwargs
The parameters in `begin` component.
2024-12-18 19:01:05 +08:00
#### Returns
2024-11-06 18:03:45 +08:00
- Success: A `Session` object containing the following attributes:
- `id` : `str` The auto-generated unique identifier of the created session.
2025-06-05 03:29:07 +02:00
- `message` : `list[Message]` The messages of the created session assistant. Default: `[{"role": "assistant", "content": "Hi! I am your assistant, can I help you?"}]`
2024-12-19 18:19:56 +08:00
- `agent_id` : `str` The ID of the associated agent.
2024-11-06 18:03:45 +08:00
- Failure: `Exception`
2024-12-18 19:01:05 +08:00
#### Examples
2024-11-06 18:03:45 +08:00
```python
2024-12-25 20:21:38 +08:00
from ragflow_sdk import RAGFlow, Agent
2024-11-06 18:03:45 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2025-03-18 14:20:19 +08:00
agent_id = "AGENT_ID"
agent = rag_object.list_agents(id = agent_id)[0]
2025-03-03 17:15:16 +08:00
session = agent.create_session()
2024-11-06 18:03:45 +08:00
```
2024-11-14 18:44:37 +08:00
2024-11-06 18:03:45 +08:00
---
2024-12-18 19:01:05 +08:00
### Converse with agent
2024-11-06 18:03:45 +08:00
```python
2024-12-20 17:34:16 +08:00
Session.ask(question: str="", stream: bool = False) -> Optional[Message, iter[Message]]
2024-11-06 18:03:45 +08:00
```
2024-11-14 18:44:37 +08:00
Asks a specified agent a question to start an AI-powered conversation.
:::tip NOTE
In streaming mode, not all responses include a reference, as this depends on the system's judgement.
:::
2024-11-06 18:03:45 +08:00
2024-12-18 19:01:05 +08:00
#### Parameters
2024-11-06 18:03:45 +08:00
2024-12-20 17:34:16 +08:00
##### question: `str`
2024-11-06 18:03:45 +08:00
2025-02-18 19:29:40 +08:00
The question to start an AI-powered conversation. Ifthe **Begin** component takes parameters, a question is not required.
2024-11-06 18:03:45 +08:00
2024-12-18 19:01:05 +08:00
##### stream: `bool`
2024-11-06 18:03:45 +08:00
Indicates whether to output responses in a streaming way:
2024-11-22 11:11:06 +08:00
- `True` : Enable streaming (default).
- `False` : Disable streaming.
2024-11-06 18:03:45 +08:00
2024-12-18 19:01:05 +08:00
#### Returns
2024-11-06 18:03:45 +08:00
- A `Message` object containing the response to the question if `stream` is set to `False`
- An iterator containing multiple `message` objects (`iter[Message]` ) if `stream` is set to `True`
The following shows the attributes of a `Message` object:
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-11-06 18:03:45 +08:00
The auto-generated message ID.
2024-12-18 19:01:05 +08:00
##### content: `str`
2024-11-06 18:03:45 +08:00
The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"` .
2024-12-18 19:01:05 +08:00
##### reference: `list[Chunk]`
2024-11-06 18:03:45 +08:00
A list of `Chunk` objects representing references to the message, each containing the following attributes:
- `id` `str`
The chunk ID.
- `content` `str`
The content of the chunk.
- `image_id` `str`
The ID of the snapshot of the chunk. Applicable only when the source of the chunk is an image, PPT, PPTX, or PDF file.
- `document_id` `str`
The ID of the referenced document.
- `document_name` `str`
The name of the referenced document.
- `position` `list[str]`
The location information of the chunk within the referenced document.
- `dataset_id` `str`
The ID of the dataset to which the referenced document belongs.
- `similarity` `float`
A composite similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity. It is the weighted sum of `vector_similarity` and `term_similarity` .
- `vector_similarity` `float`
A vector similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between vector embeddings.
- `term_similarity` `float`
A keyword similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between keywords.
2024-12-18 19:01:05 +08:00
#### Examples
2024-11-06 18:03:45 +08:00
```python
2024-12-25 20:21:38 +08:00
from ragflow_sdk import RAGFlow, Agent
2024-11-06 18:03:45 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
AGENT_id = "AGENT_ID"
2025-03-03 17:15:16 +08:00
agent = rag_object.list_agents(id = AGENT_id)[0]
session = agent.create_session()
2024-11-06 18:03:45 +08:00
2024-11-14 18:44:37 +08:00
print("\n===== Miss R ====\n")
2024-11-06 18:03:45 +08:00
print("Hello. What can I do for you?")
while True:
2024-11-14 18:44:37 +08:00
question = input("\n===== User ====\n> ")
print("\n==== Miss R ====\n")
2024-11-06 18:03:45 +08:00
cont = ""
for ans in session.ask(question, stream=True):
print(ans.content[len(cont):], end='', flush=True)
cont = ans.content
2024-12-04 16:23:22 +08:00
```
2024-12-18 19:01:05 +08:00
2024-12-04 16:23:22 +08:00
---
2024-12-18 19:01:05 +08:00
### List agent sessions
2024-12-04 16:23:22 +08:00
```python
Agent.list_sessions(
page: int = 1,
page_size: int = 30,
orderby: str = "update_time",
desc: bool = True,
id: str = None
) -> List[Session]
```
Lists sessions associated with the current agent.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-12-04 16:23:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-12-04 16:23:22 +08:00
Specifies the page on which the sessions will be displayed. Defaults to `1` .
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-12-04 16:23:22 +08:00
The number of sessions on each page. Defaults to `30` .
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-12-04 16:23:22 +08:00
The field by which sessions should be sorted. Available options:
- `"create_time"`
- `"update_time"` (default)
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-12-04 16:23:22 +08:00
Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True` .
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-12-04 16:23:22 +08:00
The ID of the agent session to retrieve. Defaults to `None` .
2024-12-18 19:01:05 +08:00
#### Returns
2024-12-04 16:23:22 +08:00
- Success: A list of `Session` objects associated with the current agent.
- Failure: `Exception` .
2024-12-18 19:01:05 +08:00
#### Examples
2024-12-04 16:23:22 +08:00
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2025-03-03 17:15:16 +08:00
AGENT_id = "AGENT_ID"
agent = rag_object.list_agents(id = AGENT_id)[0]
sessons = agent.list_sessions()
2024-12-04 16:23:22 +08:00
for session in sessions:
print(session)
```
2025-03-03 17:15:16 +08:00
---
### Delete agent's sessions
```python
Agent.delete_sessions(ids: list[str] = None)
```
Deletes sessions of a agent by ID.
#### Parameters
##### ids: `list[str]`
The IDs of the sessions to delete. Defaults to `None` . If it is not specified, all sessions associated with the agent will be deleted.
#### Returns
- Success: No value is returned.
- Failure: `Exception`
#### Examples
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
AGENT_id = "AGENT_ID"
agent = rag_object.list_agents(id = AGENT_id)[0]
agent.delete_sessions(ids=["id_1","id_2"])
```
2024-12-04 16:23:22 +08:00
---
2024-12-18 19:01:05 +08:00
2024-12-19 14:36:51 +08:00
## AGENT MANAGEMENT
---
2024-12-18 19:01:05 +08:00
### List agents
2024-12-04 16:23:22 +08:00
```python
RAGFlow.list_agents(
page: int = 1,
page_size: int = 30,
orderby: str = "create_time",
desc: bool = True,
id: str = None,
title: str = None
) -> List[Agent]
```
Lists agents.
2024-12-18 19:01:05 +08:00
#### Parameters
2024-12-04 16:23:22 +08:00
2024-12-18 19:01:05 +08:00
##### page: `int`
2024-12-04 16:23:22 +08:00
Specifies the page on which the agents will be displayed. Defaults to `1` .
2024-12-18 19:01:05 +08:00
##### page_size: `int`
2024-12-04 16:23:22 +08:00
The number of agents on each page. Defaults to `30` .
2024-12-18 19:01:05 +08:00
##### orderby: `str`
2024-12-04 16:23:22 +08:00
The attribute by which the results are sorted. Available options:
- `"create_time"` (default)
- `"update_time"`
2024-12-18 19:01:05 +08:00
##### desc: `bool`
2024-12-04 16:23:22 +08:00
Indicates whether the retrieved agents should be sorted in descending order. Defaults to `True` .
2024-12-18 19:01:05 +08:00
##### id: `str`
2024-12-04 16:23:22 +08:00
The ID of the agent to retrieve. Defaults to `None` .
2024-12-18 19:01:05 +08:00
##### name: `str`
2024-12-04 16:23:22 +08:00
The name of the agent to retrieve. Defaults to `None` .
2024-12-18 19:01:05 +08:00
#### Returns
2024-12-04 16:23:22 +08:00
- Success: A list of `Agent` objects.
- Failure: `Exception` .
2024-12-18 19:01:05 +08:00
#### Examples
2024-12-04 16:23:22 +08:00
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
for agent in rag_object.list_agents():
print(agent)
2024-12-18 19:01:05 +08:00
```
2025-03-13 19:06:50 +08:00
---
2025-05-12 17:59:53 +08:00
### Create agent
2025-03-13 19:06:50 +08:00
2025-05-12 17:59:53 +08:00
```python
RAGFlow.create_agent(
title: str,
dsl: dict,
description: str | None = None
) -> None
```
Create an agent.
#### Parameters
##### title: `str`
Specifies the title of the agent.
##### dsl: `dict`
Specifies the canvas DSL of the agent.
##### description: `str`
The description of the agent. Defaults to `None` .
#### Returns
- Success: Nothing.
- Failure: `Exception` .
#### Examples
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
rag_object.create_agent(
title="Test Agent",
description="A test agent",
dsl={
# ... canvas DSL here ...
}
)
```
---
### Update agent
```python
RAGFlow.update_agent(
agent_id: str,
title: str | None = None,
description: str | None = None,
dsl: dict | None = None
) -> None
```
Update an agent.
#### Parameters
##### agent_id: `str`
Specifies the id of the agent to be updated.
##### title: `str`
Specifies the new title of the agent. `None` if you do not want to update this.
##### dsl: `dict`
Specifies the new canvas DSL of the agent. `None` if you do not want to update this.
##### description: `str`
The new description of the agent. `None` if you do not want to update this.
#### Returns
- Success: Nothing.
- Failure: `Exception` .
#### Examples
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
rag_object.update_agent(
agent_id="58af890a2a8911f0a71a11b922ed82d6",
title="Test Agent",
description="A test agent",
dsl={
# ... canvas DSL here ...
}
)
```
---
### Delete agent
```python
RAGFlow.delete_agent(
agent_id: str
) -> None
```
Delete an agent.
#### Parameters
##### agent_id: `str`
Specifies the id of the agent to be deleted.
#### Returns
- Success: Nothing.
- Failure: `Exception` .
#### Examples
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
rag_object.delete_agent("58af890a2a8911f0a71a11b922ed82d6")
```
---