2024-10-09 15:30:22 +08:00
# DRAFT Python API Reference
2024-10-14 20:48:23 +08:00
**THE API REFERENCES BELOW ARE STILL UNDER DEVELOPMENT.**
2024-10-19 19:46:13 +08:00
---
2024-10-09 15:30:22 +08:00
:::tip NOTE
2024-10-18 20:56:33 +08:00
Dataset Management
2024-10-09 15:30:22 +08:00
:::
2024-10-19 19:46:13 +08:00
---
2024-10-18 20:56:33 +08:00
## Create dataset
2024-10-09 15:30:22 +08:00
```python
RAGFlow.create_dataset(
name: str,
avatar: str = "",
description: str = "",
language: str = "English",
permission: str = "me",
document_count: int = 0,
chunk_count: int = 0,
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
### Parameters
2024-10-14 20:03:33 +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:
- Permitted characters include:
- English letters (a-z, A-Z)
- Digits (0-9)
- "_" (underscore)
- Must begin with an English letter or underscore.
- Maximum 65,535 characters.
- Case-insensitive.
2024-10-14 20:03:33 +08:00
#### avatar: `str`
2024-10-09 15:30:22 +08:00
2024-10-11 09:55:27 +08:00
Base64 encoding of the avatar. Defaults to `""`
2024-10-09 15:30:22 +08:00
2024-10-14 20:03:33 +08:00
#### description: `str`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
A brief description of the dataset to create. Defaults to `""` .
2024-10-09 15:30:22 +08:00
2024-10-14 20:03:33 +08:00
#### language: `str`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The language setting of the dataset to create. Available options:
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
- `"English"` (Default)
- `"Chinese"`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
#### permission
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
Specifies who can access the dataset to create. You can set it only to `"me"` for now.
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +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
- `"one"` :One
- `"knowledge_graph"` : Knowledge Graph
- `"email"` : Email
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
#### parser_config
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
The parser configuration of the dataset. A `ParserConfig` object contains the following attributes:
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
- `chunk_token_count` : Defaults to `128` .
- `layout_recognize` : Defaults to `True` .
2024-10-19 19:46:13 +08:00
- `delimiter` : Defaults to `"\n!?。;!?"` .
2024-10-14 20:48:23 +08:00
- `task_page_size` : Defaults to `12` .
2024-10-09 15:30:22 +08:00
### Returns
2024-10-14 20:48:23 +08:00
- Success: A `dataset` object.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
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-10-18 20:56:33 +08:00
## Delete datasets
2024-10-09 15:30:22 +08:00
```python
2024-10-14 20:48:23 +08:00
RAGFlow.delete_datasets(ids: list[str] = None)
2024-10-09 15:30:22 +08:00
```
2024-10-19 19:46:13 +08:00
Deletes specified datasets or all datasets in the system.
2024-10-14 20:48:23 +08:00
### Parameters
2024-10-14 20:03:33 +08:00
2024-10-19 19:46:13 +08:00
#### ids: `list[str]`
2024-10-11 09:55:27 +08:00
2024-10-19 19:46:13 +08:00
The IDs of the datasets to delete. Defaults to `None` . If not specified, all datasets in the system will be deleted.
2024-10-11 09:55:27 +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
### Examples
2024-10-12 20:07:21 +08:00
```python
2024-10-19 19:46:13 +08:00
rag_object.delete_datasets(ids=["id_1","id_2"])
2024-10-09 15:30:22 +08:00
```
---
2024-10-18 20:56:33 +08:00
## List datasets
2024-10-09 15:30:22 +08:00
```python
RAGFlow.list_datasets(
page: int = 1,
page_size: int = 1024,
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-18 20:56:33 +08:00
Retrieves a list of datasets.
2024-10-09 15:30:22 +08:00
### Parameters
2024-10-14 20:03:33 +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-10-14 20:03:33 +08:00
#### page_size: `int`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The number of datasets on each page. Defaults to `1024` .
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +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-10-14 20:03:33 +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-10-14 20:03:33 +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-10-14 20:03:33 +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
### Returns
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-10-14 20:03:33 +08:00
### Examples
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +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-10-18 20:56:33 +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-10-18 20:56:33 +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
### Parameters
#### update_message: `dict[str, str|int]`, *Required*
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with the following keys:
2024-10-18 20:56:33 +08:00
- `"name"` : `str` The name of the dataset to update.
2024-10-19 19:46:13 +08:00
- `"embedding_model"` : `str` The embedding model name to update.
2024-10-14 20:48:23 +08:00
- Ensure that `"chunk_count"` is `0` before updating `"embedding_model"` .
2024-10-19 19:46:13 +08:00
- `"chunk_method"` : `str` The chunking method for the dataset. Available options:
2024-10-14 20:48:23 +08:00
- `"naive"` : General
- `"manual` : Manual
- `"qa"` : Q& A
- `"table"` : Table
- `"paper"` : Paper
- `"book"` : Book
- `"laws"` : Laws
- `"presentation"` : Presentation
- `"picture"` : Picture
- `"one"` :One
- `"knowledge_graph"` : Knowledge Graph
- `"email"` : Email
2024-10-09 15:30:22 +08:00
### Returns
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
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")
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
---
:::tip API GROUPING
2024-10-18 20:56:33 +08:00
File Management within Dataset
2024-10-09 15:30:22 +08:00
:::
2024-10-18 20:56:33 +08:00
---
2024-10-17 18:19:17 +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-10-09 15:30:22 +08:00
### Parameters
2024-10-19 19:46:13 +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-10-16 18:41:24 +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
### Examples
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-10-16 18:41:24 +08:00
## Update document
```python
Document.update(update_message:dict)
```
2024-10-17 18:19:17 +08:00
Updates configurations for the current document.
2024-10-16 18:41:24 +08:00
### Parameters
2024-10-18 20:56:33 +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-18 20:56:33 +08:00
- `"name"` : `str` The name of the document to update.
- `"parser_config"` : `dict[str, Any]` The parsing configuration for the document:
- `"chunk_token_count"` : Defaults to `128` .
- `"layout_recognize"` : Defaults to `True` .
- `"delimiter"` : Defaults to `'\n!?。;!?'` .
- `"task_page_size"` : Defaults to `12` .
- `"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
- `"knowledge_graph"` : Knowledge Graph
- `"email"` : Email
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
2024-10-16 18:41:24 +08:00
```python
from ragflow import RAGFlow
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-10-16 18:41:24 +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-10-16 18:41:24 +08:00
### Returns
2024-10-18 20:56:33 +08:00
The downloaded document in bytes.
2024-10-16 18:41:24 +08:00
### Examples
```python
from ragflow import RAGFlow
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)
```
---
## List documents
```python
2024-10-16 20:38:19 +08:00
Dataset.list_documents(id:str =None, keywords: str=None, offset: int=0, limit:int = 1024,order_by:str = "create_time", desc: bool = True) -> list[Document]
2024-10-09 15:30:22 +08:00
```
2024-10-18 20:56:33 +08:00
Retrieves a list of documents from the current dataset.
2024-10-09 15:30:22 +08:00
### Parameters
2024-10-19 19:46:13 +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-10-19 19:46:13 +08:00
#### keywords: `str`
2024-10-16 18:41:24 +08:00
2024-10-18 20:56:33 +08:00
The keywords to match document titles. Defaults to `None` .
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
#### offset: `int`
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
The starting index for the documents to retrieve. Typically used in confunction with `limit` . Defaults to `0` .
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
#### limit: `int`
2024-10-16 18:41:24 +08:00
2024-10-19 19:46:13 +08:00
The maximum number of documents to retrieve. Defaults to `1024` . A value of `-1` indicates that all documents should be returned.
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +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-10-19 19:46:13 +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-10-09 15:30:22 +08:00
### Returns
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` .
- `knowledgebase_id` : The dataset ID associated with the document. Defaults to `None` .
- `chunk_method` The chunk method name. Defaults to `""` . ?????naive??????
- `parser_config` : `ParserConfig` Configuration object for the parser. Defaults to `{"pages": [[1, 1000000]]}` .
- `source_type` : The source type of the document. Defaults to `"local"` .
- `type` : Type or category of the document???????????. Defaults to `""` .
- `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` .
- `chunk_count` : `int` The number of chunks that the document is split into. Defaults to `0` .
- `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` .
- `process_duation` : `float` Duration of the processing in seconds or minutes.??????? Defaults to `0.0` .
- `run` : `str` ?????????????????? Defaults to `"0"` .
- `status` : `str` ??????????????????? Defaults to `"1"` .
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-17 18:19:17 +08:00
dataset = rag.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}])
for doc in dataset.list_documents(keywords="rag", offset=0, limit=12):
print(doc)
2024-10-09 15:30:22 +08:00
```
---
## Delete documents
```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.
### Parameters
#### ids: `list[list]`
The IDs of the documents to delete. Defaults to `None` . If not specified, all documents in the dataset will be deleted.
2024-10-17 19:52:35 +08:00
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
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-10-18 20:56:33 +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
```
### Parameters
2024-10-19 19:46:13 +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
### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```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-16 18:41:24 +08:00
{'name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
{'name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
{'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-10-18 20:56:33 +08:00
## Stop parsing documents
```python
DataSet.async_cancel_parse_documents(document_ids:list[str])-> None
```
### Parameters
2024-10-19 19:46:13 +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
### Returns
- Success: No value is returned.
- Failure: `Exception`
### Examples
```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 = [
{'name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
{'name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
{'name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
]
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-10-16 18:41:24 +08:00
## List chunks
2024-10-17 18:19:17 +08:00
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
Document.list_chunks(keywords: str = None, offset: int = 0, limit: int = -1, id : str = None) -> list[Chunk]
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
Retrieves a list of document chunks.
2024-10-09 15:30:22 +08:00
### Parameters
2024-10-19 19:46:13 +08:00
#### keywords: `str`
2024-10-17 19:52:35 +08:00
List chunks whose name has the given keywords. Defaults to `None`
2024-10-19 19:46:13 +08:00
#### offset: `int`
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The starting index for the chunks to retrieve. Defaults to `1`
2024-10-09 15:30:22 +08:00
2024-10-17 19:52:35 +08:00
#### limit
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The maximum number of chunks to retrieve. Default: `30`
2024-10-17 19:52:35 +08:00
#### id
The ID of the chunk to retrieve. Default: `None`
2024-10-17 18:19:17 +08:00
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 19:52:35 +08:00
2024-10-19 19:46:13 +08:00
- Success: A list of `Chunk` objects.
- Failure: `Exception` .
2024-10-09 15:30:22 +08:00
2024-10-16 18:41:24 +08:00
### Examples
2024-10-17 19:52:35 +08:00
2024-10-16 18:41:24 +08:00
```python
from ragflow 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("123")
dataset = dataset[0]
dataset.async_parse_documents(["wdfxb5t547d"])
for chunk in doc.list_chunks(keywords="rag", offset=0, limit=12):
print(chunk)
2024-10-16 18:41:24 +08:00
```
2024-10-17 18:19:17 +08:00
2024-10-09 15:30:22 +08:00
## Add chunk
```python
Document.add_chunk(content:str) -> Chunk
```
### Parameters
2024-10-17 19:52:35 +08:00
#### content: *Required*
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
The text content of the chunk.
2024-10-17 18:19:17 +08:00
2024-10-16 20:38:19 +08:00
#### important_keywords :`list[str]`
2024-10-17 18:19:17 +08:00
2024-10-17 19:52:35 +08:00
List the key terms or phrases that are significant or central to the chunk's content.
2024-10-09 15:30:22 +08:00
### Returns
chunk
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-17 19:52:35 +08:00
dataset = rag.list_datasets(id="123")
dtaset = 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")
```
---
## Delete chunk
```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-10-16 18:41:24 +08:00
### Parameters
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
#### chunk_ids: `list[str]`
2024-10-17 18:19:17 +08:00
2024-10-19 19:46:13 +08:00
The IDs of the chunks to delete. Defaults to `None` . If not specified, all chunks of the current document will be deleted.
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-16 18:41:24 +08:00
ds = rag.list_datasets(id="123")
ds = ds[0]
doc = ds.list_documents(id="wdfxb5t547d")
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-10-16 18:41:24 +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-10-16 18:41:24 +08:00
### Parameters
2024-10-18 20:56:33 +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-18 20:56:33 +08:00
- `"content"` : `str` Content of the chunk.
- `"important_keywords"` : `list[str]` A list of key terms to attach to the chunk.
2024-10-19 19:46:13 +08:00
- `"available"` : `int` The chunk's availability status in the dataset. Value options:
2024-10-18 20:56:33 +08:00
- `0` : Unavailable
- `1` : Available
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 18:19:17 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
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-10-18 20:56:33 +08:00
## Retrieve chunks
2024-10-09 15:30:22 +08:00
```python
2024-10-16 20:38:19 +08:00
RAGFlow.retrieve(question:str="", datasets:list[str]=None, document=list[str]=None, offset:int=1, limit:int=30, similarity_threshold:float=0.2, vector_similarity_weight:float=0.3, top_k:int=1024,rerank_id:str=None,keyword:bool=False,higlight:bool=False) -> list[Chunk]
2024-10-09 15:30:22 +08:00
```
### Parameters
2024-10-18 20:56:33 +08:00
#### question: `str` *Required*
2024-10-09 15:30:22 +08:00
The user query or query keywords. Defaults to `""` .
2024-10-18 20:56:33 +08:00
#### datasets: `list[str]`, *Required*
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The datasets to search from.
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
#### document: `list[str]`
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The documents to search from. `None` means no limitation. Defaults to `None` .
2024-10-09 15:30:22 +08:00
#### offset: `int`
2024-10-19 19:46:13 +08:00
The starting index for the documents to retrieve. Defaults to `0` ??????.
2024-10-09 15:30:22 +08:00
#### limit: `int`
2024-10-19 19:46:13 +08:00
The maximum number of chunks to retrieve. Defaults to `6` .
2024-10-09 15:30:22 +08:00
#### Similarity_threshold: `float`
The minimum similarity score. Defaults to `0.2` .
#### similarity_threshold_weight: `float`
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
#### top_k: `int`
2024-10-18 20:56:33 +08:00
The number of chunks engaged in vector cosine computaton. Defaults to `1024` .
#### rerank_id
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
The ID of the rerank model. Defaults to `None` .
2024-10-16 18:41:24 +08:00
2024-10-18 20:56:33 +08:00
#### keyword
Indicates whether keyword-based matching is enabled:
- `True` : Enabled.
- `False` : Disabled.
2024-10-16 18:41:24 +08:00
#### highlight:`bool`
Specifying whether to enable highlighting of matched terms in the results (True) or not (False).
2024-10-18 20:56:33 +08:00
2024-10-09 15:30:22 +08:00
### Returns
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
### Examples
```python
from ragflow import RAGFlow
2024-10-18 20:56:33 +08:00
rag_object = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
ds = rag_object.list_datasets(name="ragflow")
2024-10-16 18:41:24 +08:00
ds = ds[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-10-18 20:56:33 +08:00
rag_object.create_document(ds, name=name, blob=open(path, "rb").read())
2024-10-16 18:41:24 +08:00
doc = ds.list_documents(name=name)
doc = doc[0]
ds.async_parse_documents([doc.id])
2024-10-18 20:56:33 +08:00
for c in rag_object.retrieve(question="What's ragflow?",
2024-10-16 18:41:24 +08:00
datasets=[ds.id], documents=[doc.id],
offset=1, limit=30, similarity_threshold=0.2,
2024-10-09 15:30:22 +08:00
vector_similarity_weight=0.3,
top_k=1024
):
print(c)
```
---
:::tip API GROUPING
2024-10-17 18:19:17 +08:00
Chat Assistant Management
2024-10-09 15:30:22 +08:00
:::
2024-10-19 19:46:13 +08:00
---
2024-10-16 20:38:19 +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 = "",
knowledgebases: 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-10-18 20:56:33 +08:00
### Parameters
2024-10-09 15:30:22 +08:00
2024-10-17 19:52:35 +08:00
The following shows the attributes of a `Chat` object:
2024-10-18 20:56:33 +08:00
#### name: *Required*
The name of the chat assistant. Defaults to `"assistant"` .
#### avatar
Base64 encoding of the avatar. Defaults to `""` .
#### knowledgebases: `list[str]`
The IDs of the associated datasets. Defaults to `[""]` .
#### llm
The llm of the created chat. 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:
- `model_name` , `str`
The chat model name. If it is `None` , the user's default chat model will be returned.
- `temperature` , `float`
Controls the randomness of the model's predictions. A lower temperature increases the model's conficence in its responses; a higher temperature increases creativity and diversity. Defaults to `0.1` .
- `top_p` , `float`
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`
- `presence_penalty` , `float`
This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2` .
- `frequency penalty` , `float`
Similar to the presence penalty, this reduces the model’ s tendency to repeat the same words frequently. Defaults to `0.7` .
- `max_token` , `int`
This sets the maximum length of the model’ s output, measured in the number of tokens (words or pieces of words). Defaults to `512` .
#### Prompt
Instructions for the LLM to follow. A `Prompt` object contains the following attributes:
- `"similarity_threshold"` : `float` A similarity score to evaluate distance between two lines of text. It's weighted keywords similarity and vector cosine similarity. If the similarity between query and chunk is less than this threshold, the chunk will be filtered out. Defaults to `0.2` .
- `"keywords_similarity_weight"` : `float` It's weighted keywords similarity and vector cosine similarity or rerank score (0~1). Defaults to `0.7` .
- `"top_n"` : `int` Not all the chunks whose similarity score is above the 'similarity threshold' will be feed to LLMs. LLM can only see these 'Top N' chunks. Defaults to `8` .
- `"variables"` : `list[dict[]]` If you use dialog APIs, the variables might help you chat with your clients with different strategies. The variables are used to fill in the 'System' part in prompt in order to give LLM a hint. The 'knowledge' is a very special variable which will be filled-in with the retrieved chunks. All the variables in 'System' should be curly bracketed. Defaults to `[{"key": "knowledge", "optional": True}]`
- `"rerank_model"` : `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""` .
- `"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-18 20:56:33 +08:00
- `"prompt"` : `str` The prompt content. Defaults to `You are an intelligent assistant. Please summarize the content of the dataset to answer the question. Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the knowledge base!" Answers need to consider chat history.
2024-10-09 15:30:22 +08:00
Here is the knowledge base:
{knowledge}
2024-10-16 20:38:19 +08:00
The above is the knowledge base.`.
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
### Returns
- Success: A `Chat` object representing the chat assistant.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-14 20:48:23 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-18 20:56:33 +08:00
kbs = rag.list_datasets(name="kb_1")
list_kb=[]
for kb in kbs:
list_kb.append(kb.id)
assi = rag.create_chat("Miss R", knowledgebases=list_kb)
2024-10-09 15:30:22 +08:00
```
---
2024-10-12 13:48:43 +08:00
## Update chat
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-10-14 20:48:23 +08:00
### Parameters
2024-10-19 19:46:13 +08:00
#### update_message: `dict[str, str|list[str]|dict[]]`, *Required*
A dictionary representing the attributes to update, with the following keys:
2024-10-14 20:48:23 +08:00
- `"name"` : `str` The name of the chat assistant to update.
- `"avatar"` : `str` Base64 encoding of the avatar. Defaults to `""`
2024-10-19 19:46:13 +08:00
- `"knowledgebases"` : `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.
- `"temperature"` , `float` Controls the randomness of the model's predictions.
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.
2024-10-14 20:48:23 +08:00
- `"max_token"` , `int` This sets the maximum length of the model’ s output, measured in the number of tokens (words or pieces of words).
2024-10-16 20:38:19 +08:00
- `"prompt"` : Instructions for the LLM to follow.
- `"similarity_threshold"` : `float` A score to evaluate distance between two lines of text. It's weighted keywords similarity and vector cosine similarity. If the similarity between query and chunk is less than this threshold, the chunk will be filtered out. Defaults to `0.2` .
- `"keywords_similarity_weight"` : `float` It's weighted keywords similarity and vector cosine similarity or rerank score (0~1). Defaults to `0.7` .
- `"top_n"` : `int` Not all the chunks whose similarity score is above the 'similarity threshold' will be feed to LLMs. LLM can only see these 'Top N' chunks. Defaults to `8` .
- `"variables"` : `list[dict[]]` If you use dialog APIs, the variables might help you chat with your clients with different strategies. The variables are used to fill in the 'System' part in prompt in order to give LLM a hint. The 'knowledge' is a very special variable which will be filled-in with the retrieved chunks. All the variables in 'System' should be curly bracketed. Defaults to `[{"key": "knowledge", "optional": True}]`
- `"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` .
- `"prompt"` : `str` The prompt content. Defaults to `You are an intelligent assistant. Please summarize the content of the knowledge base to answer the question. Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the knowledge base!" Answers need to consider chat history.
Here is the knowledge base:
{knowledge}
The above is the knowledge base.`.
2024-10-14 20:48:23 +08:00
2024-10-09 15:30:22 +08:00
### Returns
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-14 20:48:23 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
knowledge_base = rag.list_datasets(name="kb_1")
assistant = rag.create_chat("Miss R", knowledgebases=knowledge_base)
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-10-12 13:48:43 +08:00
## Delete chats
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-10-14 20:48:23 +08:00
### Parameters
2024-10-12 13:48:43 +08:00
2024-10-19 19:46:13 +08:00
#### ids: `list[str]`
2024-10-12 13:48:43 +08:00
2024-10-19 19:46:13 +08:00
The IDs of the chat assistants to delete. Defaults to `None` . If not specified, all chat assistants in the system will be deleted.
2024-10-09 15:30:22 +08:00
### Returns
2024-10-14 20:48:23 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-14 20:48:23 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
2024-10-12 13:48:43 +08:00
rag.delete_chats(ids=["id_1","id_2"])
2024-10-09 15:30:22 +08:00
```
---
2024-10-12 13:48:43 +08:00
## List chats
2024-10-09 15:30:22 +08:00
```python
2024-10-12 13:48:43 +08:00
RAGFlow.list_chats(
page: int = 1,
page_size: int = 1024,
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-17 19:52:35 +08:00
Retrieves a list of chat assistants.
2024-10-09 15:30:22 +08:00
### Parameters
2024-10-14 20:48:23 +08:00
#### page
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-10-14 20:48:23 +08:00
#### page_size
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The number of chat assistants on each page. Defaults to `1024` .
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
#### order_by
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
The attribute by which the results are sorted. Defaults to `"create_time"` .
2024-10-09 15:30:22 +08:00
2024-10-14 20:48:23 +08:00
#### desc
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-10-12 13:48:43 +08:00
#### id: `string`
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
The ID of the chat to retrieve. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-10-12 13:48:43 +08:00
#### name: `string`
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
The name of the chat to retrieve. Defaults to `None` .
2024-10-14 20:48:23 +08:00
2024-10-09 15:30:22 +08:00
### Returns
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
### Examples
```python
from ragflow import RAGFlow
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
```
---
:::tip API GROUPING
Chat-session APIs
:::
2024-10-19 19:46:13 +08:00
---
2024-10-09 15:30:22 +08:00
## Create session
```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-10-16 20:38:19 +08:00
Creates a chat session.
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
### Parameters
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
#### name
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-10-16 20:38:19 +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.
- `message` : `list[Message]` The messages of the created session assistant. Default: `[{"role": "assistant", "content": "Hi! I am your assistant, can I help you?"}]`
- `chat_id` : `str` The ID of the associated chat assistant.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag.list_chats(name="Miss R")
assistant = assistant[0]
session = assistant.create_session()
2024-10-09 15:30:22 +08:00
```
2024-10-12 19:35:19 +08:00
## Update 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-10-19 19:46:13 +08:00
Updates the current session name.
2024-10-16 20:38:19 +08:00
### Parameters
#### update_message: `dict[str, Any]`, *Required*
2024-10-19 19:46:13 +08:00
A dictionary representing the attributes to update, with only one key:
2024-10-16 20:38:19 +08:00
- `"name"` : `str` The name of the session to update.
2024-10-09 15:30:22 +08:00
### Returns
2024-10-16 20:38:19 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag.list_chats(name="Miss R")
assistant = assistant[0]
session = assistant.create_session("session_name")
session.update({"name": "updated_name"})
2024-10-09 15:30:22 +08:00
```
---
## Chat
```python
2024-10-12 19:35:19 +08:00
Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
2024-10-09 15:30:22 +08:00
```
2024-10-17 18:19:17 +08:00
Asks a question to start a conversation.
2024-10-09 15:30:22 +08:00
### Parameters
2024-10-16 20:38:19 +08:00
#### question *Required*
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
The question to start an AI chat. Defaults to `None` .
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
#### stream
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
Indicates whether to output responses in a streaming way:
- `True` : Enable streaming.
- `False` : (Default) Disable streaming.
2024-10-09 15:30:22 +08:00
### Returns
2024-10-17 18:19:17 +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`
2024-10-16 20:38:19 +08:00
2024-10-17 18:19:17 +08:00
The following shows the attributes of a `Message` object:
2024-10-09 15:30:22 +08:00
#### id: `str`
2024-10-17 18:19:17 +08:00
The auto-generated message ID.
2024-10-09 15:30:22 +08:00
#### content: `str`
The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"` .
2024-10-16 20:38:19 +08:00
#### reference: `list[Chunk]`
2024-10-09 15:30:22 +08:00
2024-10-17 18:19:17 +08:00
A list of `Chunk` objects representing references to the message, each containing the following attributes:
2024-10-09 15:30:22 +08:00
2024-10-18 20:56:33 +08:00
- `id` `str`
2024-10-17 18:19:17 +08:00
The chunk ID.
2024-10-18 20:56:33 +08:00
- `content` `str`
2024-10-17 18:19:17 +08:00
The content of the chunk.
2024-10-18 20:56:33 +08:00
- `image_id` `str`
2024-10-17 18:19:17 +08:00
The ID of the snapshot of the chunk.
2024-10-18 20:56:33 +08:00
- `document_id` `str`
2024-10-17 18:19:17 +08:00
The ID of the referenced document.
2024-10-18 20:56:33 +08:00
- `document_name` `str`
2024-10-17 18:19:17 +08:00
The name of the referenced document.
2024-10-18 20:56:33 +08:00
- `position` `list[str]`
2024-10-17 18:19:17 +08:00
The location information of the chunk within the referenced document.
2024-10-18 20:56:33 +08:00
- `knowledgebase_id` `str`
The ID of the dataset to which the referenced document belongs.
- `similarity` `float`
2024-10-17 18:19:17 +08:00
A composite similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity.
2024-10-18 20:56:33 +08:00
- `vector_similarity` `float`
2024-10-17 18:19:17 +08:00
A vector similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between vector embeddings.
2024-10-18 20:56:33 +08:00
- `term_similarity` `float`
2024-10-17 18:19:17 +08:00
A keyword similarity score of the chunk ranging from `0` to `1` , with a higher value indicating greater similarity between keywords.
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag.list_chats(name="Miss R")
assistant = assistant[0]
2024-10-17 18:19:17 +08:00
session = assistant.create_session()
2024-10-09 15:30:22 +08:00
print("\n==================== Miss R =====================\n")
2024-10-16 20:38:19 +08:00
print(assistant.get_prologue())
2024-10-09 15:30:22 +08:00
while True:
question = input("\n==================== User =====================\n> ")
print("\n==================== Miss R =====================\n")
cont = ""
2024-10-17 18:19:17 +08:00
for ans in session.ask(question, stream=True):
print(answer.content[len(cont):], end='', flush=True)
cont = answer.content
2024-10-09 15:30:22 +08:00
```
---
## List sessions
```python
2024-10-12 19:35:19 +08:00
Chat.list_sessions(
page: int = 1,
page_size: int = 1024,
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-10-16 20:38:19 +08:00
### Parameters
2024-10-09 15:30:22 +08:00
2024-10-16 20:38:19 +08:00
#### page
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-10-16 20:38:19 +08:00
#### page_size
2024-10-09 15:30:22 +08:00
2024-10-19 19:46:13 +08:00
The number of sessions on each page. Defaults to `1024` .
2024-10-12 19:35:19 +08:00
2024-10-16 20:38:19 +08:00
#### orderby
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-10-16 20:38:19 +08:00
#### desc
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-10-16 20:38:19 +08:00
#### id
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-10-16 20:38:19 +08:00
#### name
2024-10-12 19:35:19 +08:00
2024-10-16 20:38:19 +08:00
The name of the chat to retrieve. Defaults to `None` .
2024-10-12 19:35:19 +08:00
2024-10-16 20:38:19 +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-10-16 20:38:19 +08:00
### Examples
```python
from ragflow import RAGFlow
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-10-16 20:38:19 +08:00
## Delete 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-10-19 19:46:13 +08:00
Deletes sessions by ID.
2024-10-16 20:38:19 +08:00
### Parameters
2024-10-19 19:46:13 +08:00
#### ids: `list[str]`
2024-10-16 20:38:19 +08:00
2024-10-19 19:46:13 +08:00
The IDs of the sessions to delete. Defaults to `None` . If not specified, all sessions associated with the current chat assistant will be deleted.
2024-10-16 20:38:19 +08:00
2024-10-09 15:30:22 +08:00
### Returns
2024-10-16 20:38:19 +08:00
- Success: No value is returned.
- Failure: `Exception`
2024-10-09 15:30:22 +08:00
### Examples
```python
from ragflow import RAGFlow
2024-10-16 20:38:19 +08:00
rag = RAGFlow(api_key="< YOUR_API_KEY > ", base_url="http://< YOUR_BASE_URL > :9380")
assistant = rag.list_chats(name="Miss R")
assistant = assistant[0]
assistant.delete_sessions(ids=["id_1","id_2"])
```