You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_api_reference.md 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. ---
  2. sidebar_position: 2
  3. slug: /python_api_reference
  4. ---
  5. # Python API Reference
  6. A complete reference for RAGFlow's Python APIs. Before proceeding, please ensure you [have your RAGFlow API key ready for authentication](https://ragflow.io/docs/dev/acquire_ragflow_api_key).
  7. :::tip NOTE
  8. Run the following command to download the Python SDK:
  9. ```bash
  10. pip install ragflow-sdk
  11. ```
  12. :::
  13. ---
  14. ## DATASET MANAGEMENT
  15. ---
  16. ### Create dataset
  17. ```python
  18. RAGFlow.create_dataset(
  19. name: str,
  20. avatar: str = "",
  21. description: str = "",
  22. embedding_model: str = "BAAI/bge-zh-v1.5",
  23. language: str = "English",
  24. permission: str = "me",
  25. chunk_method: str = "naive",
  26. parser_config: DataSet.ParserConfig = None
  27. ) -> DataSet
  28. ```
  29. Creates a dataset.
  30. #### Parameters
  31. ##### name: `str`, *Required*
  32. The unique name of the dataset to create. It must adhere to the following requirements:
  33. - Permitted characters include:
  34. - English letters (a-z, A-Z)
  35. - Digits (0-9)
  36. - "_" (underscore)
  37. - Must begin with an English letter or underscore.
  38. - Maximum 65,535 characters.
  39. - Case-insensitive.
  40. ##### avatar: `str`
  41. Base64 encoding of the avatar. Defaults to `""`
  42. ##### description: `str`
  43. A brief description of the dataset to create. Defaults to `""`.
  44. ##### language: `str`
  45. The language setting of the dataset to create. Available options:
  46. - `"English"` (default)
  47. - `"Chinese"`
  48. ##### permission
  49. Specifies who can access the dataset to create. Available options:
  50. - `"me"`: (Default) Only you can manage the dataset.
  51. - `"team"`: All team members can manage the dataset.
  52. ##### chunk_method, `str`
  53. The chunking method of the dataset to create. Available options:
  54. - `"naive"`: General (default)
  55. - `"manual`: Manual
  56. - `"qa"`: Q&A
  57. - `"table"`: Table
  58. - `"paper"`: Paper
  59. - `"book"`: Book
  60. - `"laws"`: Laws
  61. - `"presentation"`: Presentation
  62. - `"picture"`: Picture
  63. - `"one"`: One
  64. - `"knowledge_graph"`: Knowledge Graph
  65. Ensure your LLM is properly configured on the **Settings** page before selecting this. Please also note that Knowledge Graph consumes a large number of Tokens!
  66. - `"email"`: Email
  67. ##### parser_config
  68. The parser configuration of the dataset. A `ParserConfig` object's attributes vary based on the selected `chunk_method`:
  69. - `chunk_method`=`"naive"`:
  70. `{"chunk_token_num":128,"delimiter":"\\n!?;。;!?","html4excel":False,"layout_recognize":True,"raptor":{"user_raptor":False}}`.
  71. - `chunk_method`=`"qa"`:
  72. `{"raptor": {"user_raptor": False}}`
  73. - `chunk_method`=`"manuel"`:
  74. `{"raptor": {"user_raptor": False}}`
  75. - `chunk_method`=`"table"`:
  76. `None`
  77. - `chunk_method`=`"paper"`:
  78. `{"raptor": {"user_raptor": False}}`
  79. - `chunk_method`=`"book"`:
  80. `{"raptor": {"user_raptor": False}}`
  81. - `chunk_method`=`"laws"`:
  82. `{"raptor": {"user_raptor": False}}`
  83. - `chunk_method`=`"picture"`:
  84. `None`
  85. - `chunk_method`=`"presentation"`:
  86. `{"raptor": {"user_raptor": False}}`
  87. - `chunk_method`=`"one"`:
  88. `None`
  89. - `chunk_method`=`"knowledge-graph"`:
  90. `{"chunk_token_num":128,"delimiter":"\\n!?;。;!?","entity_types":["organization","person","location","event","time"]}`
  91. - `chunk_method`=`"email"`:
  92. `None`
  93. #### Returns
  94. - Success: A `dataset` object.
  95. - Failure: `Exception`
  96. #### Examples
  97. ```python
  98. from ragflow_sdk import RAGFlow
  99. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  100. dataset = rag_object.create_dataset(name="kb_1")
  101. ```
  102. ---
  103. ### Delete datasets
  104. ```python
  105. RAGFlow.delete_datasets(ids: list[str] = None)
  106. ```
  107. Deletes datasets by ID.
  108. #### Parameters
  109. ##### ids: `list[str]`, *Required*
  110. The IDs of the datasets to delete. Defaults to `None`. If it is not specified, all datasets will be deleted.
  111. #### Returns
  112. - Success: No value is returned.
  113. - Failure: `Exception`
  114. #### Examples
  115. ```python
  116. rag_object.delete_datasets(ids=["id_1","id_2"])
  117. ```
  118. ---
  119. ### List datasets
  120. ```python
  121. RAGFlow.list_datasets(
  122. page: int = 1,
  123. page_size: int = 30,
  124. orderby: str = "create_time",
  125. desc: bool = True,
  126. id: str = None,
  127. name: str = None
  128. ) -> list[DataSet]
  129. ```
  130. Lists datasets.
  131. #### Parameters
  132. ##### page: `int`
  133. Specifies the page on which the datasets will be displayed. Defaults to `1`.
  134. ##### page_size: `int`
  135. The number of datasets on each page. Defaults to `30`.
  136. ##### orderby: `str`
  137. The field by which datasets should be sorted. Available options:
  138. - `"create_time"` (default)
  139. - `"update_time"`
  140. ##### desc: `bool`
  141. Indicates whether the retrieved datasets should be sorted in descending order. Defaults to `True`.
  142. ##### id: `str`
  143. The ID of the dataset to retrieve. Defaults to `None`.
  144. ##### name: `str`
  145. The name of the dataset to retrieve. Defaults to `None`.
  146. #### Returns
  147. - Success: A list of `DataSet` objects.
  148. - Failure: `Exception`.
  149. #### Examples
  150. ##### List all datasets
  151. ```python
  152. for dataset in rag_object.list_datasets():
  153. print(dataset)
  154. ```
  155. ##### Retrieve a dataset by ID
  156. ```python
  157. dataset = rag_object.list_datasets(id = "id_1")
  158. print(dataset[0])
  159. ```
  160. ---
  161. ### Update dataset
  162. ```python
  163. DataSet.update(update_message: dict)
  164. ```
  165. Updates configurations for the current dataset.
  166. #### Parameters
  167. ##### update_message: `dict[str, str|int]`, *Required*
  168. A dictionary representing the attributes to update, with the following keys:
  169. - `"name"`: `str` The revised name of the dataset.
  170. - `"embedding_model"`: `str` The updated embedding model name.
  171. - Ensure that `"chunk_count"` is `0` before updating `"embedding_model"`.
  172. - `"chunk_method"`: `str` The chunking method for the dataset. Available options:
  173. - `"naive"`: General
  174. - `"manual`: Manual
  175. - `"qa"`: Q&A
  176. - `"table"`: Table
  177. - `"paper"`: Paper
  178. - `"book"`: Book
  179. - `"laws"`: Laws
  180. - `"presentation"`: Presentation
  181. - `"picture"`: Picture
  182. - `"one"`: One
  183. - `"email"`: Email
  184. - `"knowledge_graph"`: Knowledge Graph
  185. Ensure your LLM is properly configured on the **Settings** page before selecting this. Please also note that Knowledge Graph consumes a large number of Tokens!
  186. #### Returns
  187. - Success: No value is returned.
  188. - Failure: `Exception`
  189. #### Examples
  190. ```python
  191. from ragflow_sdk import RAGFlow
  192. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  193. dataset = rag_object.list_datasets(name="kb_name")
  194. dataset.update({"embedding_model":"BAAI/bge-zh-v1.5", "chunk_method":"manual"})
  195. ```
  196. ---
  197. ## FILE MANAGEMENT WITHIN DATASET
  198. ---
  199. ### Upload documents
  200. ```python
  201. DataSet.upload_documents(document_list: list[dict])
  202. ```
  203. Uploads documents to the current dataset.
  204. #### Parameters
  205. ##### document_list: `list[dict]`, *Required*
  206. A list of dictionaries representing the documents to upload, each containing the following keys:
  207. - `"display_name"`: (Optional) The file name to display in the dataset.
  208. - `"blob"`: (Optional) The binary content of the file to upload.
  209. #### Returns
  210. - Success: No value is returned.
  211. - Failure: `Exception`
  212. #### Examples
  213. ```python
  214. dataset = rag_object.create_dataset(name="kb_name")
  215. dataset.upload_documents([{"display_name": "1.txt", "blob": "<BINARY_CONTENT_OF_THE_DOC>"}, {"display_name": "2.pdf", "blob": "<BINARY_CONTENT_OF_THE_DOC>"}])
  216. ```
  217. ---
  218. ### Update document
  219. ```python
  220. Document.update(update_message:dict)
  221. ```
  222. Updates configurations for the current document.
  223. #### Parameters
  224. ##### update_message: `dict[str, str|dict[]]`, *Required*
  225. A dictionary representing the attributes to update, with the following keys:
  226. - `"display_name"`: `str` The name of the document to update.
  227. - `"chunk_method"`: `str` The parsing method to apply to the document.
  228. - `"naive"`: General
  229. - `"manual`: Manual
  230. - `"qa"`: Q&A
  231. - `"table"`: Table
  232. - `"paper"`: Paper
  233. - `"book"`: Book
  234. - `"laws"`: Laws
  235. - `"presentation"`: Presentation
  236. - `"picture"`: Picture
  237. - `"one"`: One
  238. - `"knowledge_graph"`: Knowledge Graph
  239. Ensure your LLM is properly configured on the **Settings** page before selecting this. Please also note that Knowledge Graph consumes a large number of Tokens!
  240. - `"email"`: Email
  241. - `"parser_config"`: `dict[str, Any]` The parsing configuration for the document. Its attributes vary based on the selected `"chunk_method"`:
  242. - `"chunk_method"`=`"naive"`:
  243. `{"chunk_token_num":128,"delimiter":"\\n!?;。;!?","html4excel":False,"layout_recognize":True,"raptor":{"user_raptor":False}}`.
  244. - `chunk_method`=`"qa"`:
  245. `{"raptor": {"user_raptor": False}}`
  246. - `chunk_method`=`"manuel"`:
  247. `{"raptor": {"user_raptor": False}}`
  248. - `chunk_method`=`"table"`:
  249. `None`
  250. - `chunk_method`=`"paper"`:
  251. `{"raptor": {"user_raptor": False}}`
  252. - `chunk_method`=`"book"`:
  253. `{"raptor": {"user_raptor": False}}`
  254. - `chunk_method`=`"laws"`:
  255. `{"raptor": {"user_raptor": False}}`
  256. - `chunk_method`=`"presentation"`:
  257. `{"raptor": {"user_raptor": False}}`
  258. - `chunk_method`=`"picture"`:
  259. `None`
  260. - `chunk_method`=`"one"`:
  261. `None`
  262. - `chunk_method`=`"knowledge-graph"`:
  263. `{"chunk_token_num":128,"delimiter":"\\n!?;。;!?","entity_types":["organization","person","location","event","time"]}`
  264. - `chunk_method`=`"email"`:
  265. `None`
  266. #### Returns
  267. - Success: No value is returned.
  268. - Failure: `Exception`
  269. #### Examples
  270. ```python
  271. from ragflow_sdk import RAGFlow
  272. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  273. dataset = rag_object.list_datasets(id='id')
  274. dataset = dataset[0]
  275. doc = dataset.list_documents(id="wdfxb5t547d")
  276. doc = doc[0]
  277. doc.update([{"parser_config": {"chunk_token_count": 256}}, {"chunk_method": "manual"}])
  278. ```
  279. ---
  280. ### Download document
  281. ```python
  282. Document.download() -> bytes
  283. ```
  284. Downloads the current document.
  285. #### Returns
  286. The downloaded document in bytes.
  287. #### Examples
  288. ```python
  289. from ragflow_sdk import RAGFlow
  290. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  291. dataset = rag_object.list_datasets(id="id")
  292. dataset = dataset[0]
  293. doc = dataset.list_documents(id="wdfxb5t547d")
  294. doc = doc[0]
  295. open("~/ragflow.txt", "wb+").write(doc.download())
  296. print(doc)
  297. ```
  298. ---
  299. ### List documents
  300. ```python
  301. 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]
  302. ```
  303. Lists documents in the current dataset.
  304. #### Parameters
  305. ##### id: `str`
  306. The ID of the document to retrieve. Defaults to `None`.
  307. ##### keywords: `str`
  308. The keywords used to match document titles. Defaults to `None`.
  309. ##### page: `int`
  310. Specifies the page on which the documents will be displayed. Defaults to `1`.
  311. ##### page_size: `int`
  312. The maximum number of documents on each page. Defaults to `30`.
  313. ##### orderby: `str`
  314. The field by which documents should be sorted. Available options:
  315. - `"create_time"` (default)
  316. - `"update_time"`
  317. ##### desc: `bool`
  318. Indicates whether the retrieved documents should be sorted in descending order. Defaults to `True`.
  319. #### Returns
  320. - Success: A list of `Document` objects.
  321. - Failure: `Exception`.
  322. A `Document` object contains the following attributes:
  323. - `id`: The document ID. Defaults to `""`.
  324. - `name`: The document name. Defaults to `""`.
  325. - `thumbnail`: The thumbnail image of the document. Defaults to `None`.
  326. - `dataset_id`: The dataset ID associated with the document. Defaults to `None`.
  327. - `chunk_method` The chunk method name. Defaults to `"naive"`.
  328. - `source_type`: The source type of the document. Defaults to `"local"`.
  329. - `type`: Type or category of the document. Defaults to `""`. Reserved for future use.
  330. - `created_by`: `str` The creator of the document. Defaults to `""`.
  331. - `size`: `int` The document size in bytes. Defaults to `0`.
  332. - `token_count`: `int` The number of tokens in the document. Defaults to `0`.
  333. - `chunk_count`: `int` The number of chunks in the document. Defaults to `0`.
  334. - `progress`: `float` The current processing progress as a percentage. Defaults to `0.0`.
  335. - `progress_msg`: `str` A message indicating the current progress status. Defaults to `""`.
  336. - `process_begin_at`: `datetime` The start time of document processing. Defaults to `None`.
  337. - `process_duation`: `float` Duration of the processing in seconds. Defaults to `0.0`.
  338. - `run`: `str` The document's processing status:
  339. - `"UNSTART"` (default)
  340. - `"RUNNING"`
  341. - `"CANCEL"`
  342. - `"DONE"`
  343. - `"FAIL"`
  344. - `status`: `str` Reserved for future use.
  345. - `parser_config`: `ParserConfig` Configuration object for the parser. Its attributes vary based on the selected `chunk_method`:
  346. - `chunk_method`=`"naive"`:
  347. `{"chunk_token_num":128,"delimiter":"\\n!?;。;!?","html4excel":False,"layout_recognize":True,"raptor":{"user_raptor":False}}`.
  348. - `chunk_method`=`"qa"`:
  349. `{"raptor": {"user_raptor": False}}`
  350. - `chunk_method`=`"manuel"`:
  351. `{"raptor": {"user_raptor": False}}`
  352. - `chunk_method`=`"table"`:
  353. `None`
  354. - `chunk_method`=`"paper"`:
  355. `{"raptor": {"user_raptor": False}}`
  356. - `chunk_method`=`"book"`:
  357. `{"raptor": {"user_raptor": False}}`
  358. - `chunk_method`=`"laws"`:
  359. `{"raptor": {"user_raptor": False}}`
  360. - `chunk_method`=`"presentation"`:
  361. `{"raptor": {"user_raptor": False}}`
  362. - `chunk_method`=`"picure"`:
  363. `None`
  364. - `chunk_method`=`"one"`:
  365. `None`
  366. - `chunk_method`=`"knowledge-graph"`:
  367. `{"chunk_token_num":128,"delimiter": "\\n!?;。;!?","entity_types":["organization","person","location","event","time"]}`
  368. - `chunk_method`=`"email"`:
  369. `None`
  370. #### Examples
  371. ```python
  372. from ragflow_sdk import RAGFlow
  373. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  374. dataset = rag_object.create_dataset(name="kb_1")
  375. filename1 = "~/ragflow.txt"
  376. blob = open(filename1 , "rb").read()
  377. dataset.upload_documents([{"name":filename1,"blob":blob}])
  378. for doc in dataset.list_documents(keywords="rag", page=0, page_size=12):
  379. print(doc)
  380. ```
  381. ---
  382. ### Delete documents
  383. ```python
  384. DataSet.delete_documents(ids: list[str] = None)
  385. ```
  386. Deletes documents by ID.
  387. #### Parameters
  388. ##### ids: `list[list]`
  389. The IDs of the documents to delete. Defaults to `None`. If it is not specified, all documents in the dataset will be deleted.
  390. #### Returns
  391. - Success: No value is returned.
  392. - Failure: `Exception`
  393. #### Examples
  394. ```python
  395. from ragflow_sdk import RAGFlow
  396. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  397. dataset = rag_object.list_datasets(name="kb_1")
  398. dataset = dataset[0]
  399. dataset.delete_documents(ids=["id_1","id_2"])
  400. ```
  401. ---
  402. ### Parse documents
  403. ```python
  404. DataSet.async_parse_documents(document_ids:list[str]) -> None
  405. ```
  406. Parses documents in the current dataset.
  407. #### Parameters
  408. ##### document_ids: `list[str]`, *Required*
  409. The IDs of the documents to parse.
  410. #### Returns
  411. - Success: No value is returned.
  412. - Failure: `Exception`
  413. #### Examples
  414. ```python
  415. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  416. dataset = rag_object.create_dataset(name="dataset_name")
  417. documents = [
  418. {'display_name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
  419. {'display_name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
  420. {'display_name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
  421. ]
  422. dataset.upload_documents(documents)
  423. documents = dataset.list_documents(keywords="test")
  424. ids = []
  425. for document in documents:
  426. ids.append(document.id)
  427. dataset.async_parse_documents(ids)
  428. print("Async bulk parsing initiated.")
  429. ```
  430. ---
  431. ### Stop parsing documents
  432. ```python
  433. DataSet.async_cancel_parse_documents(document_ids:list[str])-> None
  434. ```
  435. Stops parsing specified documents.
  436. #### Parameters
  437. ##### document_ids: `list[str]`, *Required*
  438. The IDs of the documents for which parsing should be stopped.
  439. #### Returns
  440. - Success: No value is returned.
  441. - Failure: `Exception`
  442. #### Examples
  443. ```python
  444. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  445. dataset = rag_object.create_dataset(name="dataset_name")
  446. documents = [
  447. {'display_name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
  448. {'display_name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
  449. {'display_name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
  450. ]
  451. dataset.upload_documents(documents)
  452. documents = dataset.list_documents(keywords="test")
  453. ids = []
  454. for document in documents:
  455. ids.append(document.id)
  456. dataset.async_parse_documents(ids)
  457. print("Async bulk parsing initiated.")
  458. dataset.async_cancel_parse_documents(ids)
  459. print("Async bulk parsing cancelled.")
  460. ```
  461. ---
  462. ### Add chunk
  463. ```python
  464. Document.add_chunk(content:str, important_keywords:list[str] = []) -> Chunk
  465. ```
  466. Adds a chunk to the current document.
  467. #### Parameters
  468. ##### content: `str`, *Required*
  469. The text content of the chunk.
  470. ##### important_keywords: `list[str]`
  471. The key terms or phrases to tag with the chunk.
  472. #### Returns
  473. - Success: A `Chunk` object.
  474. - Failure: `Exception`.
  475. A `Chunk` object contains the following attributes:
  476. - `id`: `str`: The chunk ID.
  477. - `content`: `str` The text content of the chunk.
  478. - `important_keywords`: `list[str]` A list of key terms or phrases tagged with the chunk.
  479. - `create_time`: `str` The time when the chunk was created (added to the document).
  480. - `create_timestamp`: `float` The timestamp representing the creation time of the chunk, expressed in seconds since January 1, 1970.
  481. - `dataset_id`: `str` The ID of the associated dataset.
  482. - `document_name`: `str` The name of the associated document.
  483. - `document_id`: `str` The ID of the associated document.
  484. - `available`: `bool` The chunk's availability status in the dataset. Value options:
  485. - `False`: Unavailable
  486. - `True`: Available (default)
  487. #### Examples
  488. ```python
  489. from ragflow_sdk import RAGFlow
  490. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  491. dataset = rag_object.list_datasets(id="123")
  492. dtaset = dataset[0]
  493. doc = dataset.list_documents(id="wdfxb5t547d")
  494. doc = doc[0]
  495. chunk = doc.add_chunk(content="xxxxxxx")
  496. ```
  497. ---
  498. ### List chunks
  499. ```python
  500. Document.list_chunks(keywords: str = None, page: int = 1, page_size: int = 30, id : str = None) -> list[Chunk]
  501. ```
  502. Lists chunks in the current document.
  503. #### Parameters
  504. ##### keywords: `str`
  505. The keywords used to match chunk content. Defaults to `None`
  506. ##### page: `int`
  507. Specifies the page on which the chunks will be displayed. Defaults to `1`.
  508. ##### page_size: `int`
  509. The maximum number of chunks on each page. Defaults to `30`.
  510. ##### id: `str`
  511. The ID of the chunk to retrieve. Default: `None`
  512. #### Returns
  513. - Success: A list of `Chunk` objects.
  514. - Failure: `Exception`.
  515. #### Examples
  516. ```python
  517. from ragflow_sdk import RAGFlow
  518. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  519. dataset = rag_object.list_datasets("123")
  520. dataset = dataset[0]
  521. dataset.async_parse_documents(["wdfxb5t547d"])
  522. for chunk in doc.list_chunks(keywords="rag", page=0, page_size=12):
  523. print(chunk)
  524. ```
  525. ---
  526. ### Delete chunks
  527. ```python
  528. Document.delete_chunks(chunk_ids: list[str])
  529. ```
  530. Deletes chunks by ID.
  531. #### Parameters
  532. ##### chunk_ids: `list[str]`
  533. The IDs of the chunks to delete. Defaults to `None`. If it is not specified, all chunks of the current document will be deleted.
  534. #### Returns
  535. - Success: No value is returned.
  536. - Failure: `Exception`
  537. #### Examples
  538. ```python
  539. from ragflow_sdk import RAGFlow
  540. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  541. dataset = rag_object.list_datasets(id="123")
  542. dataset = dataset[0]
  543. doc = dataset.list_documents(id="wdfxb5t547d")
  544. doc = doc[0]
  545. chunk = doc.add_chunk(content="xxxxxxx")
  546. doc.delete_chunks(["id_1","id_2"])
  547. ```
  548. ---
  549. ### Update chunk
  550. ```python
  551. Chunk.update(update_message: dict)
  552. ```
  553. Updates content or configurations for the current chunk.
  554. #### Parameters
  555. ##### update_message: `dict[str, str|list[str]|int]` *Required*
  556. A dictionary representing the attributes to update, with the following keys:
  557. - `"content"`: `str` The text content of the chunk.
  558. - `"important_keywords"`: `list[str]` A list of key terms or phrases to tag with the chunk.
  559. - `"available"`: `bool` The chunk's availability status in the dataset. Value options:
  560. - `False`: Unavailable
  561. - `True`: Available (default)
  562. #### Returns
  563. - Success: No value is returned.
  564. - Failure: `Exception`
  565. #### Examples
  566. ```python
  567. from ragflow_sdk import RAGFlow
  568. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  569. dataset = rag_object.list_datasets(id="123")
  570. dataset = dataset[0]
  571. doc = dataset.list_documents(id="wdfxb5t547d")
  572. doc = doc[0]
  573. chunk = doc.add_chunk(content="xxxxxxx")
  574. chunk.update({"content":"sdfx..."})
  575. ```
  576. ---
  577. ### Retrieve chunks
  578. ```python
  579. 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,higlight:bool=False) -> list[Chunk]
  580. ```
  581. Retrieves chunks from specified datasets.
  582. #### Parameters
  583. ##### question: `str`, *Required*
  584. The user query or query keywords. Defaults to `""`.
  585. ##### dataset_ids: `list[str]`, *Required*
  586. The IDs of the datasets to search. Defaults to `None`. If you do not set this argument, ensure that you set `document_ids`.
  587. ##### document_ids: `list[str]`
  588. 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. If you do not set this argument, ensure that you set `dataset_ids`.
  589. ##### page: `int`
  590. The starting index for the documents to retrieve. Defaults to `1`.
  591. ##### page_size: `int`
  592. The maximum number of chunks to retrieve. Defaults to `30`.
  593. ##### Similarity_threshold: `float`
  594. The minimum similarity score. Defaults to `0.2`.
  595. ##### vector_similarity_weight: `float`
  596. 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.
  597. ##### top_k: `int`
  598. The number of chunks engaged in vector cosine computaton. Defaults to `1024`.
  599. ##### rerank_id: `str`
  600. The ID of the rerank model. Defaults to `None`.
  601. ##### keyword: `bool`
  602. Indicates whether to enable keyword-based matching:
  603. - `True`: Enable keyword-based matching.
  604. - `False`: Disable keyword-based matching (default).
  605. ##### highlight: `bool`
  606. Specifies whether to enable highlighting of matched terms in the results:
  607. - `True`: Enable highlighting of matched terms.
  608. - `False`: Disable highlighting of matched terms (default).
  609. #### Returns
  610. - Success: A list of `Chunk` objects representing the document chunks.
  611. - Failure: `Exception`
  612. #### Examples
  613. ```python
  614. from ragflow_sdk import RAGFlow
  615. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  616. dataset = rag_object.list_datasets(name="ragflow")
  617. dataset = dataset[0]
  618. name = 'ragflow_test.txt'
  619. path = './test_data/ragflow_test.txt'
  620. rag_object.create_document(dataset, name=name, blob=open(path, "rb").read())
  621. doc = dataset.list_documents(name=name)
  622. doc = doc[0]
  623. dataset.async_parse_documents([doc.id])
  624. for c in rag_object.retrieve(question="What's ragflow?",
  625. dataset_ids=[dataset.id], document_ids=[doc.id],
  626. page=1, page_size=30, similarity_threshold=0.2,
  627. vector_similarity_weight=0.3,
  628. top_k=1024
  629. ):
  630. print(c)
  631. ```
  632. ---
  633. ## CHAT ASSISTANT MANAGEMENT
  634. ---
  635. ### Create chat assistant
  636. ```python
  637. RAGFlow.create_chat(
  638. name: str,
  639. avatar: str = "",
  640. dataset_ids: list[str] = [],
  641. llm: Chat.LLM = None,
  642. prompt: Chat.Prompt = None
  643. ) -> Chat
  644. ```
  645. Creates a chat assistant.
  646. #### Parameters
  647. ##### name: `str`, *Required*
  648. The name of the chat assistant.
  649. ##### avatar: `str`
  650. Base64 encoding of the avatar. Defaults to `""`.
  651. ##### dataset_ids: `list[str]`
  652. The IDs of the associated datasets. Defaults to `[""]`.
  653. ##### llm: `Chat.LLM`
  654. 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:
  655. - `model_name`: `str`
  656. The chat model name. If it is `None`, the user's default chat model will be used.
  657. - `temperature`: `float`
  658. 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`.
  659. - `top_p`: `float`
  660. 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`
  661. - `presence_penalty`: `float`
  662. This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
  663. - `frequency penalty`: `float`
  664. Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
  665. - `max_token`: `int`
  666. The maximum length of the model's output, measured in the number of tokens (words or pieces of words). Defaults to `512`. If disabled, you lift the maximum token limit, allowing the model to determine the number of tokens in its responses.
  667. ##### prompt: `Chat.Prompt`
  668. Instructions for the LLM to follow. A `Prompt` object contains the following attributes:
  669. - `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`.
  670. - `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`.
  671. - `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`.
  672. - `variables`: `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations**. Note that:
  673. - `knowledge` is a reserved variable, which represents the retrieved chunks.
  674. - All the variables in 'System' should be curly bracketed.
  675. - The default value is `[{"key": "knowledge", "optional": True}]`.
  676. - `rerank_model`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  677. - `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`.
  678. - `opener`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  679. - `show_quote`: `bool` Indicates whether the source of text should be displayed. Defaults to `True`.
  680. - `prompt`: `str` The prompt content.
  681. #### Returns
  682. - Success: A `Chat` object representing the chat assistant.
  683. - Failure: `Exception`
  684. #### Examples
  685. ```python
  686. from ragflow_sdk import RAGFlow
  687. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  688. datasets = rag_object.list_datasets(name="kb_1")
  689. dataset_ids = []
  690. for dataset in datasets:
  691. dataset_ids.append(dataset.id)
  692. assistant = rag_object.create_chat("Miss R", dataset_ids=dataset_ids)
  693. ```
  694. ---
  695. ### Update chat assistant
  696. ```python
  697. Chat.update(update_message: dict)
  698. ```
  699. Updates configurations for the current chat assistant.
  700. #### Parameters
  701. ##### update_message: `dict[str, str|list[str]|dict[]]`, *Required*
  702. A dictionary representing the attributes to update, with the following keys:
  703. - `"name"`: `str` The revised name of the chat assistant.
  704. - `"avatar"`: `str` Base64 encoding of the avatar. Defaults to `""`
  705. - `"dataset_ids"`: `list[str]` The datasets to update.
  706. - `"llm"`: `dict` The LLM settings:
  707. - `"model_name"`, `str` The chat model name.
  708. - `"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.
  709. - `"top_p"`, `float` Also known as “nucleus sampling”, this parameter sets a threshold to select a smaller set of words to sample from.
  710. - `"presence_penalty"`, `float` This discourages the model from repeating the same information by penalizing words that have appeared in the conversation.
  711. - `"frequency penalty"`, `float` Similar to presence penalty, this reduces the model’s tendency to repeat the same words.
  712. - `"max_token"`, `int` The maximum length of the model's output, measured in the number of tokens (words or pieces of words). Defaults to `512`. If disabled, you lift the maximum token limit, allowing the model to determine the number of tokens in its responses.
  713. - `"prompt"` : Instructions for the LLM to follow.
  714. - `"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`.
  715. - `"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`.
  716. - `"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`.
  717. - `"variables"`: `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations**. Note that:
  718. - `knowledge` is a reserved variable, which represents the retrieved chunks.
  719. - All the variables in 'System' should be curly bracketed.
  720. - The default value is `[{"key": "knowledge", "optional": True}]`.
  721. - `"rerank_model"`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  722. - `"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`.
  723. - `"opener"`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  724. - `"show_quote`: `bool` Indicates whether the source of text should be displayed Defaults to `True`.
  725. - `"prompt"`: `str` The prompt content.
  726. #### Returns
  727. - Success: No value is returned.
  728. - Failure: `Exception`
  729. #### Examples
  730. ```python
  731. from ragflow_sdk import RAGFlow
  732. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  733. datasets = rag_object.list_datasets(name="kb_1")
  734. dataset_id = datasets[0].id
  735. assistant = rag_object.create_chat("Miss R", dataset_ids=[dataset_id])
  736. assistant.update({"name": "Stefan", "llm": {"temperature": 0.8}, "prompt": {"top_n": 8}})
  737. ```
  738. ---
  739. ### Delete chat assistants
  740. ```python
  741. RAGFlow.delete_chats(ids: list[str] = None)
  742. ```
  743. Deletes chat assistants by ID.
  744. #### Parameters
  745. ##### ids: `list[str]`
  746. 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.
  747. #### Returns
  748. - Success: No value is returned.
  749. - Failure: `Exception`
  750. #### Examples
  751. ```python
  752. from ragflow_sdk import RAGFlow
  753. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  754. rag_object.delete_chats(ids=["id_1","id_2"])
  755. ```
  756. ---
  757. ### List chat assistants
  758. ```python
  759. RAGFlow.list_chats(
  760. page: int = 1,
  761. page_size: int = 30,
  762. orderby: str = "create_time",
  763. desc: bool = True,
  764. id: str = None,
  765. name: str = None
  766. ) -> list[Chat]
  767. ```
  768. Lists chat assistants.
  769. #### Parameters
  770. ##### page: `int`
  771. Specifies the page on which the chat assistants will be displayed. Defaults to `1`.
  772. ##### page_size: `int`
  773. The number of chat assistants on each page. Defaults to `30`.
  774. ##### orderby: `str`
  775. The attribute by which the results are sorted. Available options:
  776. - `"create_time"` (default)
  777. - `"update_time"`
  778. ##### desc: `bool`
  779. Indicates whether the retrieved chat assistants should be sorted in descending order. Defaults to `True`.
  780. ##### id: `str`
  781. The ID of the chat assistant to retrieve. Defaults to `None`.
  782. ##### name: `str`
  783. The name of the chat assistant to retrieve. Defaults to `None`.
  784. #### Returns
  785. - Success: A list of `Chat` objects.
  786. - Failure: `Exception`.
  787. #### Examples
  788. ```python
  789. from ragflow_sdk import RAGFlow
  790. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  791. for assistant in rag_object.list_chats():
  792. print(assistant)
  793. ```
  794. ---
  795. ## SESSION MANAGEMENT
  796. ---
  797. ### Create session with chat assistant
  798. ```python
  799. Chat.create_session(name: str = "New session") -> Session
  800. ```
  801. Creates a session with the current chat assistant.
  802. #### Parameters
  803. ##### name: `str`
  804. The name of the chat session to create.
  805. #### Returns
  806. - Success: A `Session` object containing the following attributes:
  807. - `id`: `str` The auto-generated unique identifier of the created session.
  808. - `name`: `str` The name of the created session.
  809. - `message`: `list[Message]` The opening message of the created session. Default: `[{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]`
  810. - `chat_id`: `str` The ID of the associated chat assistant.
  811. - Failure: `Exception`
  812. #### Examples
  813. ```python
  814. from ragflow_sdk import RAGFlow
  815. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  816. assistant = rag_object.list_chats(name="Miss R")
  817. assistant = assistant[0]
  818. session = assistant.create_session()
  819. ```
  820. ---
  821. ### Update chat assistant's session
  822. ```python
  823. Session.update(update_message: dict)
  824. ```
  825. Updates the current session of the current chat assistant.
  826. #### Parameters
  827. ##### update_message: `dict[str, Any]`, *Required*
  828. A dictionary representing the attributes to update, with only one key:
  829. - `"name"`: `str` The revised name of the session.
  830. #### Returns
  831. - Success: No value is returned.
  832. - Failure: `Exception`
  833. #### Examples
  834. ```python
  835. from ragflow_sdk import RAGFlow
  836. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  837. assistant = rag_object.list_chats(name="Miss R")
  838. assistant = assistant[0]
  839. session = assistant.create_session("session_name")
  840. session.update({"name": "updated_name"})
  841. ```
  842. ---
  843. ### List chat assistant's sessions
  844. ```python
  845. Chat.list_sessions(
  846. page: int = 1,
  847. page_size: int = 30,
  848. orderby: str = "create_time",
  849. desc: bool = True,
  850. id: str = None,
  851. name: str = None
  852. ) -> list[Session]
  853. ```
  854. Lists sessions associated with the current chat assistant.
  855. #### Parameters
  856. ##### page: `int`
  857. Specifies the page on which the sessions will be displayed. Defaults to `1`.
  858. ##### page_size: `int`
  859. The number of sessions on each page. Defaults to `30`.
  860. ##### orderby: `str`
  861. The field by which sessions should be sorted. Available options:
  862. - `"create_time"` (default)
  863. - `"update_time"`
  864. ##### desc: `bool`
  865. Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True`.
  866. ##### id: `str`
  867. The ID of the chat session to retrieve. Defaults to `None`.
  868. ##### name: `str`
  869. The name of the chat session to retrieve. Defaults to `None`.
  870. #### Returns
  871. - Success: A list of `Session` objects associated with the current chat assistant.
  872. - Failure: `Exception`.
  873. #### Examples
  874. ```python
  875. from ragflow_sdk import RAGFlow
  876. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  877. assistant = rag_object.list_chats(name="Miss R")
  878. assistant = assistant[0]
  879. for session in assistant.list_sessions():
  880. print(session)
  881. ```
  882. ---
  883. ### Delete chat assistant's sessions
  884. ```python
  885. Chat.delete_sessions(ids:list[str] = None)
  886. ```
  887. Deletes sessions of the current chat assistant by ID.
  888. #### Parameters
  889. ##### ids: `list[str]`
  890. 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.
  891. #### Returns
  892. - Success: No value is returned.
  893. - Failure: `Exception`
  894. #### Examples
  895. ```python
  896. from ragflow_sdk import RAGFlow
  897. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  898. assistant = rag_object.list_chats(name="Miss R")
  899. assistant = assistant[0]
  900. assistant.delete_sessions(ids=["id_1","id_2"])
  901. ```
  902. ---
  903. ### Converse with chat assistant
  904. ```python
  905. Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
  906. ```
  907. Asks a specified chat assistant a question to start an AI-powered conversation.
  908. :::tip NOTE
  909. In streaming mode, not all responses include a reference, as this depends on the system's judgement.
  910. :::
  911. #### Parameters
  912. ##### question: `str`, *Required*
  913. The question to start an AI-powered conversation.
  914. ##### stream: `bool`
  915. Indicates whether to output responses in a streaming way:
  916. - `True`: Enable streaming (default).
  917. - `False`: Disable streaming.
  918. #### Returns
  919. - A `Message` object containing the response to the question if `stream` is set to `False`.
  920. - An iterator containing multiple `message` objects (`iter[Message]`) if `stream` is set to `True`
  921. The following shows the attributes of a `Message` object:
  922. ##### id: `str`
  923. The auto-generated message ID.
  924. ##### content: `str`
  925. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  926. ##### reference: `list[Chunk]`
  927. A list of `Chunk` objects representing references to the message, each containing the following attributes:
  928. - `id` `str`
  929. The chunk ID.
  930. - `content` `str`
  931. The content of the chunk.
  932. - `img_id` `str`
  933. The ID of the snapshot of the chunk. Applicable only when the source of the chunk is an image, PPT, PPTX, or PDF file.
  934. - `document_id` `str`
  935. The ID of the referenced document.
  936. - `document_name` `str`
  937. The name of the referenced document.
  938. - `position` `list[str]`
  939. The location information of the chunk within the referenced document.
  940. - `dataset_id` `str`
  941. The ID of the dataset to which the referenced document belongs.
  942. - `similarity` `float`
  943. 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`.
  944. - `vector_similarity` `float`
  945. A vector similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between vector embeddings.
  946. - `term_similarity` `float`
  947. A keyword similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between keywords.
  948. #### Examples
  949. ```python
  950. from ragflow_sdk import RAGFlow
  951. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  952. assistant = rag_object.list_chats(name="Miss R")
  953. assistant = assistant[0]
  954. session = assistant.create_session()
  955. print("\n==================== Miss R =====================\n")
  956. print("Hello. What can I do for you?")
  957. while True:
  958. question = input("\n==================== User =====================\n> ")
  959. print("\n==================== Miss R =====================\n")
  960. cont = ""
  961. for ans in session.ask(question, stream=True):
  962. print(ans.content[len(cont):], end='', flush=True)
  963. cont = ans.content
  964. ```
  965. ---
  966. ### Create session with agent
  967. ```python
  968. Agent.create_session(id,rag) -> Session
  969. ```
  970. Creates a session with the current agent.
  971. #### Returns
  972. - Success: A `Session` object containing the following attributes:
  973. - `id`: `str` The auto-generated unique identifier of the created session.
  974. - `message`: `list[Message]` The messages of the created session assistant. Default: `[{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]`
  975. - `agent_id`: `str` The ID of the associated agent.
  976. - Failure: `Exception`
  977. #### Examples
  978. ```python
  979. from ragflow_sdk import RAGFlow
  980. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  981. AGENT_ID = "AGENT_ID"
  982. session = create_session(AGENT_ID,rag_object)
  983. ```
  984. ---
  985. ### Converse with agent
  986. ```python
  987. Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
  988. ```
  989. Asks a specified agent a question to start an AI-powered conversation.
  990. :::tip NOTE
  991. In streaming mode, not all responses include a reference, as this depends on the system's judgement.
  992. :::
  993. #### Parameters
  994. ##### question: `str`, *Required*
  995. The question to start an AI-powered conversation.
  996. ##### stream: `bool`
  997. Indicates whether to output responses in a streaming way:
  998. - `True`: Enable streaming (default).
  999. - `False`: Disable streaming.
  1000. #### Returns
  1001. - A `Message` object containing the response to the question if `stream` is set to `False`
  1002. - An iterator containing multiple `message` objects (`iter[Message]`) if `stream` is set to `True`
  1003. The following shows the attributes of a `Message` object:
  1004. ##### id: `str`
  1005. The auto-generated message ID.
  1006. ##### content: `str`
  1007. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  1008. ##### reference: `list[Chunk]`
  1009. A list of `Chunk` objects representing references to the message, each containing the following attributes:
  1010. - `id` `str`
  1011. The chunk ID.
  1012. - `content` `str`
  1013. The content of the chunk.
  1014. - `image_id` `str`
  1015. The ID of the snapshot of the chunk. Applicable only when the source of the chunk is an image, PPT, PPTX, or PDF file.
  1016. - `document_id` `str`
  1017. The ID of the referenced document.
  1018. - `document_name` `str`
  1019. The name of the referenced document.
  1020. - `position` `list[str]`
  1021. The location information of the chunk within the referenced document.
  1022. - `dataset_id` `str`
  1023. The ID of the dataset to which the referenced document belongs.
  1024. - `similarity` `float`
  1025. 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`.
  1026. - `vector_similarity` `float`
  1027. A vector similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between vector embeddings.
  1028. - `term_similarity` `float`
  1029. A keyword similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between keywords.
  1030. #### Examples
  1031. ```python
  1032. from ragflow_sdk import RAGFlow,Agent
  1033. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1034. AGENT_id = "AGENT_ID"
  1035. session = Agent.create_session(AGENT_id,rag_object)
  1036. print("\n===== Miss R ====\n")
  1037. print("Hello. What can I do for you?")
  1038. while True:
  1039. question = input("\n===== User ====\n> ")
  1040. print("\n==== Miss R ====\n")
  1041. cont = ""
  1042. for ans in session.ask(question, stream=True):
  1043. print(ans.content[len(cont):], end='', flush=True)
  1044. cont = ans.content
  1045. ```
  1046. ---
  1047. ### List agent sessions
  1048. ```python
  1049. Agent.list_sessions(
  1050. agent_id,
  1051. rag
  1052. page: int = 1,
  1053. page_size: int = 30,
  1054. orderby: str = "update_time",
  1055. desc: bool = True,
  1056. id: str = None
  1057. ) -> List[Session]
  1058. ```
  1059. Lists sessions associated with the current agent.
  1060. #### Parameters
  1061. ##### page: `int`
  1062. Specifies the page on which the sessions will be displayed. Defaults to `1`.
  1063. ##### page_size: `int`
  1064. The number of sessions on each page. Defaults to `30`.
  1065. ##### orderby: `str`
  1066. The field by which sessions should be sorted. Available options:
  1067. - `"create_time"`
  1068. - `"update_time"`(default)
  1069. ##### desc: `bool`
  1070. Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True`.
  1071. ##### id: `str`
  1072. The ID of the agent session to retrieve. Defaults to `None`.
  1073. #### Returns
  1074. - Success: A list of `Session` objects associated with the current agent.
  1075. - Failure: `Exception`.
  1076. #### Examples
  1077. ```python
  1078. from ragflow_sdk import RAGFlow
  1079. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1080. agent_id = "2710f2269b4611ef8fdf0242ac120006"
  1081. sessions=Agent.list_sessions(agent_id,rag_object)
  1082. for session in sessions:
  1083. print(session)
  1084. ```
  1085. ---
  1086. ## AGENT MANAGEMENT
  1087. ---
  1088. ### List agents
  1089. ```python
  1090. RAGFlow.list_agents(
  1091. page: int = 1,
  1092. page_size: int = 30,
  1093. orderby: str = "create_time",
  1094. desc: bool = True,
  1095. id: str = None,
  1096. title: str = None
  1097. ) -> List[Agent]
  1098. ```
  1099. Lists agents.
  1100. #### Parameters
  1101. ##### page: `int`
  1102. Specifies the page on which the agents will be displayed. Defaults to `1`.
  1103. ##### page_size: `int`
  1104. The number of agents on each page. Defaults to `30`.
  1105. ##### orderby: `str`
  1106. The attribute by which the results are sorted. Available options:
  1107. - `"create_time"` (default)
  1108. - `"update_time"`
  1109. ##### desc: `bool`
  1110. Indicates whether the retrieved agents should be sorted in descending order. Defaults to `True`.
  1111. ##### id: `str`
  1112. The ID of the agent to retrieve. Defaults to `None`.
  1113. ##### name: `str`
  1114. The name of the agent to retrieve. Defaults to `None`.
  1115. #### Returns
  1116. - Success: A list of `Agent` objects.
  1117. - Failure: `Exception`.
  1118. #### Examples
  1119. ```python
  1120. from ragflow_sdk import RAGFlow
  1121. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1122. for agent in rag_object.list_agents():
  1123. print(agent)
  1124. ```
  1125. ---