您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

python_api_reference.md 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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-large-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. ## CHUNK MANAGEMENT WITHIN DATASET
  463. ---
  464. ### Add chunk
  465. ```python
  466. Document.add_chunk(content:str, important_keywords:list[str] = []) -> Chunk
  467. ```
  468. Adds a chunk to the current document.
  469. #### Parameters
  470. ##### content: `str`, *Required*
  471. The text content of the chunk.
  472. ##### important_keywords: `list[str]`
  473. The key terms or phrases to tag with the chunk.
  474. #### Returns
  475. - Success: A `Chunk` object.
  476. - Failure: `Exception`.
  477. A `Chunk` object contains the following attributes:
  478. - `id`: `str`: The chunk ID.
  479. - `content`: `str` The text content of the chunk.
  480. - `important_keywords`: `list[str]` A list of key terms or phrases tagged with the chunk.
  481. - `create_time`: `str` The time when the chunk was created (added to the document).
  482. - `create_timestamp`: `float` The timestamp representing the creation time of the chunk, expressed in seconds since January 1, 1970.
  483. - `dataset_id`: `str` The ID of the associated dataset.
  484. - `document_name`: `str` The name of the associated document.
  485. - `document_id`: `str` The ID of the associated document.
  486. - `available`: `bool` The chunk's availability status in the dataset. Value options:
  487. - `False`: Unavailable
  488. - `True`: Available (default)
  489. #### Examples
  490. ```python
  491. from ragflow_sdk import RAGFlow
  492. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  493. dataset = rag_object.list_datasets(id="123")
  494. dtaset = dataset[0]
  495. doc = dataset.list_documents(id="wdfxb5t547d")
  496. doc = doc[0]
  497. chunk = doc.add_chunk(content="xxxxxxx")
  498. ```
  499. ---
  500. ### List chunks
  501. ```python
  502. Document.list_chunks(keywords: str = None, page: int = 1, page_size: int = 30, id : str = None) -> list[Chunk]
  503. ```
  504. Lists chunks in the current document.
  505. #### Parameters
  506. ##### keywords: `str`
  507. The keywords used to match chunk content. Defaults to `None`
  508. ##### page: `int`
  509. Specifies the page on which the chunks will be displayed. Defaults to `1`.
  510. ##### page_size: `int`
  511. The maximum number of chunks on each page. Defaults to `30`.
  512. ##### id: `str`
  513. The ID of the chunk to retrieve. Default: `None`
  514. #### Returns
  515. - Success: A list of `Chunk` objects.
  516. - Failure: `Exception`.
  517. #### Examples
  518. ```python
  519. from ragflow_sdk import RAGFlow
  520. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  521. dataset = rag_object.list_datasets("123")
  522. dataset = dataset[0]
  523. docs = dataset.list_documents(keywords="test", page=1, page_size=12)
  524. for chunk in docs[0].list_chunks(keywords="rag", page=0, page_size=12):
  525. print(chunk)
  526. ```
  527. ---
  528. ### Delete chunks
  529. ```python
  530. Document.delete_chunks(chunk_ids: list[str])
  531. ```
  532. Deletes chunks by ID.
  533. #### Parameters
  534. ##### chunk_ids: `list[str]`
  535. The IDs of the chunks to delete. Defaults to `None`. If it is not specified, all chunks of the current document will be deleted.
  536. #### Returns
  537. - Success: No value is returned.
  538. - Failure: `Exception`
  539. #### Examples
  540. ```python
  541. from ragflow_sdk import RAGFlow
  542. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  543. dataset = rag_object.list_datasets(id="123")
  544. dataset = dataset[0]
  545. doc = dataset.list_documents(id="wdfxb5t547d")
  546. doc = doc[0]
  547. chunk = doc.add_chunk(content="xxxxxxx")
  548. doc.delete_chunks(["id_1","id_2"])
  549. ```
  550. ---
  551. ### Update chunk
  552. ```python
  553. Chunk.update(update_message: dict)
  554. ```
  555. Updates content or configurations for the current chunk.
  556. #### Parameters
  557. ##### update_message: `dict[str, str|list[str]|int]` *Required*
  558. A dictionary representing the attributes to update, with the following keys:
  559. - `"content"`: `str` The text content of the chunk.
  560. - `"important_keywords"`: `list[str]` A list of key terms or phrases to tag with the chunk.
  561. - `"available"`: `bool` The chunk's availability status in the dataset. Value options:
  562. - `False`: Unavailable
  563. - `True`: Available (default)
  564. #### Returns
  565. - Success: No value is returned.
  566. - Failure: `Exception`
  567. #### Examples
  568. ```python
  569. from ragflow_sdk import RAGFlow
  570. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  571. dataset = rag_object.list_datasets(id="123")
  572. dataset = dataset[0]
  573. doc = dataset.list_documents(id="wdfxb5t547d")
  574. doc = doc[0]
  575. chunk = doc.add_chunk(content="xxxxxxx")
  576. chunk.update({"content":"sdfx..."})
  577. ```
  578. ---
  579. ### Retrieve chunks
  580. ```python
  581. 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]
  582. ```
  583. Retrieves chunks from specified datasets.
  584. #### Parameters
  585. ##### question: `str`, *Required*
  586. The user query or query keywords. Defaults to `""`.
  587. ##### dataset_ids: `list[str]`, *Required*
  588. The IDs of the datasets to search. Defaults to `None`. If you do not set this argument, ensure that you set `document_ids`.
  589. ##### document_ids: `list[str]`
  590. 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`.
  591. ##### page: `int`
  592. The starting index for the documents to retrieve. Defaults to `1`.
  593. ##### page_size: `int`
  594. The maximum number of chunks to retrieve. Defaults to `30`.
  595. ##### Similarity_threshold: `float`
  596. The minimum similarity score. Defaults to `0.2`.
  597. ##### vector_similarity_weight: `float`
  598. 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.
  599. ##### top_k: `int`
  600. The number of chunks engaged in vector cosine computaton. Defaults to `1024`.
  601. ##### rerank_id: `str`
  602. The ID of the rerank model. Defaults to `None`.
  603. ##### keyword: `bool`
  604. Indicates whether to enable keyword-based matching:
  605. - `True`: Enable keyword-based matching.
  606. - `False`: Disable keyword-based matching (default).
  607. ##### highlight: `bool`
  608. Specifies whether to enable highlighting of matched terms in the results:
  609. - `True`: Enable highlighting of matched terms.
  610. - `False`: Disable highlighting of matched terms (default).
  611. #### Returns
  612. - Success: A list of `Chunk` objects representing the document chunks.
  613. - Failure: `Exception`
  614. #### Examples
  615. ```python
  616. from ragflow_sdk import RAGFlow
  617. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  618. dataset = rag_object.list_datasets(name="ragflow")
  619. dataset = dataset[0]
  620. name = 'ragflow_test.txt'
  621. path = './test_data/ragflow_test.txt'
  622. documents =[{"displayed_name":"test_retrieve_chunks.txt","blob":open(path, "rb").read()}]
  623. docs = dataset.upload_documents(documents)
  624. doc = docs[0]
  625. doc.add_chunk(content="This is a chunk addition test")
  626. for c in rag_object.retrieve(dataset_ids=[dataset.id],document_ids=[doc.id]):
  627. print(c)
  628. ```
  629. ---
  630. ## CHAT ASSISTANT MANAGEMENT
  631. ---
  632. ### Create chat assistant
  633. ```python
  634. RAGFlow.create_chat(
  635. name: str,
  636. avatar: str = "",
  637. dataset_ids: list[str] = [],
  638. llm: Chat.LLM = None,
  639. prompt: Chat.Prompt = None
  640. ) -> Chat
  641. ```
  642. Creates a chat assistant.
  643. #### Parameters
  644. ##### name: `str`, *Required*
  645. The name of the chat assistant.
  646. ##### avatar: `str`
  647. Base64 encoding of the avatar. Defaults to `""`.
  648. ##### dataset_ids: `list[str]`
  649. The IDs of the associated datasets. Defaults to `[""]`.
  650. ##### llm: `Chat.LLM`
  651. 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:
  652. - `model_name`: `str`
  653. The chat model name. If it is `None`, the user's default chat model will be used.
  654. - `temperature`: `float`
  655. 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`.
  656. - `top_p`: `float`
  657. 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`
  658. - `presence_penalty`: `float`
  659. This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
  660. - `frequency penalty`: `float`
  661. Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
  662. - `max_token`: `int`
  663. 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.
  664. ##### prompt: `Chat.Prompt`
  665. Instructions for the LLM to follow. A `Prompt` object contains the following attributes:
  666. - `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`.
  667. - `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`.
  668. - `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`.
  669. - `variables`: `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations**. Note that:
  670. - `knowledge` is a reserved variable, which represents the retrieved chunks.
  671. - All the variables in 'System' should be curly bracketed.
  672. - The default value is `[{"key": "knowledge", "optional": True}]`.
  673. - `rerank_model`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  674. - `top_k`: `int` Refers to the process of reordering or selecting the top-k items from a list or set based on a specific ranking criterion. Default to 1024.
  675. - `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`.
  676. - `opener`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  677. - `show_quote`: `bool` Indicates whether the source of text should be displayed. Defaults to `True`.
  678. - `prompt`: `str` The prompt content.
  679. #### Returns
  680. - Success: A `Chat` object representing the chat assistant.
  681. - Failure: `Exception`
  682. #### Examples
  683. ```python
  684. from ragflow_sdk import RAGFlow
  685. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  686. datasets = rag_object.list_datasets(name="kb_1")
  687. dataset_ids = []
  688. for dataset in datasets:
  689. dataset_ids.append(dataset.id)
  690. assistant = rag_object.create_chat("Miss R", dataset_ids=dataset_ids)
  691. ```
  692. ---
  693. ### Update chat assistant
  694. ```python
  695. Chat.update(update_message: dict)
  696. ```
  697. Updates configurations for the current chat assistant.
  698. #### Parameters
  699. ##### update_message: `dict[str, str|list[str]|dict[]]`, *Required*
  700. A dictionary representing the attributes to update, with the following keys:
  701. - `"name"`: `str` The revised name of the chat assistant.
  702. - `"avatar"`: `str` Base64 encoding of the avatar. Defaults to `""`
  703. - `"dataset_ids"`: `list[str]` The datasets to update.
  704. - `"llm"`: `dict` The LLM settings:
  705. - `"model_name"`, `str` The chat model name.
  706. - `"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.
  707. - `"top_p"`, `float` Also known as “nucleus sampling”, this parameter sets a threshold to select a smaller set of words to sample from.
  708. - `"presence_penalty"`, `float` This discourages the model from repeating the same information by penalizing words that have appeared in the conversation.
  709. - `"frequency penalty"`, `float` Similar to presence penalty, this reduces the model’s tendency to repeat the same words.
  710. - `"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.
  711. - `"prompt"` : Instructions for the LLM to follow.
  712. - `"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`.
  713. - `"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`.
  714. - `"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`.
  715. - `"variables"`: `list[dict[]]` This argument lists the variables to use in the 'System' field of **Chat Configurations**. Note that:
  716. - `knowledge` is a reserved variable, which represents the retrieved chunks.
  717. - All the variables in 'System' should be curly bracketed.
  718. - The default value is `[{"key": "knowledge", "optional": True}]`.
  719. - `"rerank_model"`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  720. - `"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`.
  721. - `"opener"`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  722. - `"show_quote`: `bool` Indicates whether the source of text should be displayed Defaults to `True`.
  723. - `"prompt"`: `str` The prompt content.
  724. #### Returns
  725. - Success: No value is returned.
  726. - Failure: `Exception`
  727. #### Examples
  728. ```python
  729. from ragflow_sdk import RAGFlow
  730. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  731. datasets = rag_object.list_datasets(name="kb_1")
  732. dataset_id = datasets[0].id
  733. assistant = rag_object.create_chat("Miss R", dataset_ids=[dataset_id])
  734. assistant.update({"name": "Stefan", "llm": {"temperature": 0.8}, "prompt": {"top_n": 8}})
  735. ```
  736. ---
  737. ### Delete chat assistants
  738. ```python
  739. RAGFlow.delete_chats(ids: list[str] = None)
  740. ```
  741. Deletes chat assistants by ID.
  742. #### Parameters
  743. ##### ids: `list[str]`
  744. 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.
  745. #### Returns
  746. - Success: No value is returned.
  747. - Failure: `Exception`
  748. #### Examples
  749. ```python
  750. from ragflow_sdk import RAGFlow
  751. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  752. rag_object.delete_chats(ids=["id_1","id_2"])
  753. ```
  754. ---
  755. ### List chat assistants
  756. ```python
  757. RAGFlow.list_chats(
  758. page: int = 1,
  759. page_size: int = 30,
  760. orderby: str = "create_time",
  761. desc: bool = True,
  762. id: str = None,
  763. name: str = None
  764. ) -> list[Chat]
  765. ```
  766. Lists chat assistants.
  767. #### Parameters
  768. ##### page: `int`
  769. Specifies the page on which the chat assistants will be displayed. Defaults to `1`.
  770. ##### page_size: `int`
  771. The number of chat assistants on each page. Defaults to `30`.
  772. ##### orderby: `str`
  773. The attribute by which the results are sorted. Available options:
  774. - `"create_time"` (default)
  775. - `"update_time"`
  776. ##### desc: `bool`
  777. Indicates whether the retrieved chat assistants should be sorted in descending order. Defaults to `True`.
  778. ##### id: `str`
  779. The ID of the chat assistant to retrieve. Defaults to `None`.
  780. ##### name: `str`
  781. The name of the chat assistant to retrieve. Defaults to `None`.
  782. #### Returns
  783. - Success: A list of `Chat` objects.
  784. - Failure: `Exception`.
  785. #### Examples
  786. ```python
  787. from ragflow_sdk import RAGFlow
  788. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  789. for assistant in rag_object.list_chats():
  790. print(assistant)
  791. ```
  792. ---
  793. ## SESSION MANAGEMENT
  794. ---
  795. ### Create session with chat assistant
  796. ```python
  797. Chat.create_session(name: str = "New session") -> Session
  798. ```
  799. Creates a session with the current chat assistant.
  800. #### Parameters
  801. ##### name: `str`
  802. The name of the chat session to create.
  803. #### Returns
  804. - Success: A `Session` object containing the following attributes:
  805. - `id`: `str` The auto-generated unique identifier of the created session.
  806. - `name`: `str` The name of the created session.
  807. - `message`: `list[Message]` The opening message of the created session. Default: `[{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]`
  808. - `chat_id`: `str` The ID of the associated chat assistant.
  809. - Failure: `Exception`
  810. #### Examples
  811. ```python
  812. from ragflow_sdk import RAGFlow
  813. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  814. assistant = rag_object.list_chats(name="Miss R")
  815. assistant = assistant[0]
  816. session = assistant.create_session()
  817. ```
  818. ---
  819. ### Update chat assistant's session
  820. ```python
  821. Session.update(update_message: dict)
  822. ```
  823. Updates the current session of the current chat assistant.
  824. #### Parameters
  825. ##### update_message: `dict[str, Any]`, *Required*
  826. A dictionary representing the attributes to update, with only one key:
  827. - `"name"`: `str` The revised name of the session.
  828. #### Returns
  829. - Success: No value is returned.
  830. - Failure: `Exception`
  831. #### Examples
  832. ```python
  833. from ragflow_sdk import RAGFlow
  834. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  835. assistant = rag_object.list_chats(name="Miss R")
  836. assistant = assistant[0]
  837. session = assistant.create_session("session_name")
  838. session.update({"name": "updated_name"})
  839. ```
  840. ---
  841. ### List chat assistant's sessions
  842. ```python
  843. Chat.list_sessions(
  844. page: int = 1,
  845. page_size: int = 30,
  846. orderby: str = "create_time",
  847. desc: bool = True,
  848. id: str = None,
  849. name: str = None
  850. ) -> list[Session]
  851. ```
  852. Lists sessions associated with the current chat assistant.
  853. #### Parameters
  854. ##### page: `int`
  855. Specifies the page on which the sessions will be displayed. Defaults to `1`.
  856. ##### page_size: `int`
  857. The number of sessions on each page. Defaults to `30`.
  858. ##### orderby: `str`
  859. The field by which sessions should be sorted. Available options:
  860. - `"create_time"` (default)
  861. - `"update_time"`
  862. ##### desc: `bool`
  863. Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True`.
  864. ##### id: `str`
  865. The ID of the chat session to retrieve. Defaults to `None`.
  866. ##### name: `str`
  867. The name of the chat session to retrieve. Defaults to `None`.
  868. #### Returns
  869. - Success: A list of `Session` objects associated with the current chat assistant.
  870. - Failure: `Exception`.
  871. #### Examples
  872. ```python
  873. from ragflow_sdk import RAGFlow
  874. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  875. assistant = rag_object.list_chats(name="Miss R")
  876. assistant = assistant[0]
  877. for session in assistant.list_sessions():
  878. print(session)
  879. ```
  880. ---
  881. ### Delete chat assistant's sessions
  882. ```python
  883. Chat.delete_sessions(ids:list[str] = None)
  884. ```
  885. Deletes sessions of the current chat assistant by ID.
  886. #### Parameters
  887. ##### ids: `list[str]`
  888. 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.
  889. #### Returns
  890. - Success: No value is returned.
  891. - Failure: `Exception`
  892. #### Examples
  893. ```python
  894. from ragflow_sdk import RAGFlow
  895. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  896. assistant = rag_object.list_chats(name="Miss R")
  897. assistant = assistant[0]
  898. assistant.delete_sessions(ids=["id_1","id_2"])
  899. ```
  900. ---
  901. ### Converse with chat assistant
  902. ```python
  903. Session.ask(question: str = "", stream: bool = False, **kwargs) -> Optional[Message, iter[Message]]
  904. ```
  905. Asks a specified chat assistant a question to start an AI-powered conversation.
  906. :::tip NOTE
  907. In streaming mode, not all responses include a reference, as this depends on the system's judgement.
  908. :::
  909. #### Parameters
  910. ##### question: `str`, *Required*
  911. The question to start an AI-powered conversation. Defalut to `""`
  912. ##### stream: `bool`
  913. Indicates whether to output responses in a streaming way:
  914. - `True`: Enable streaming (default).
  915. - `False`: Disable streaming.
  916. ##### **kwargs
  917. The parameters in prompt(system).
  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, **kwargs) -> Session
  969. ```
  970. Creates a session with the current agent.
  971. #### Parameters
  972. ##### id: `str`, *Required*
  973. The id of agent
  974. ##### rag:`RAGFlow object`
  975. The RAGFlow object
  976. ##### **kwargs
  977. The parameters in `begin` component.
  978. #### Returns
  979. - Success: A `Session` object containing the following attributes:
  980. - `id`: `str` The auto-generated unique identifier of the created session.
  981. - `message`: `list[Message]` The messages of the created session assistant. Default: `[{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]`
  982. - `agent_id`: `str` The ID of the associated agent.
  983. - Failure: `Exception`
  984. #### Examples
  985. ```python
  986. from ragflow_sdk import RAGFlow, Agent
  987. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  988. AGENT_ID = "AGENT_ID"
  989. session = Agent.create_session(AGENT_ID, rag_object)
  990. ```
  991. ---
  992. ### Converse with agent
  993. ```python
  994. Session.ask(question: str="", stream: bool = False) -> Optional[Message, iter[Message]]
  995. ```
  996. Asks a specified agent a question to start an AI-powered conversation.
  997. :::tip NOTE
  998. In streaming mode, not all responses include a reference, as this depends on the system's judgement.
  999. :::
  1000. #### Parameters
  1001. ##### question: `str`
  1002. The question to start an AI-powered conversation. If the `begin` component takes parameters, a question is not required.
  1003. ##### stream: `bool`
  1004. Indicates whether to output responses in a streaming way:
  1005. - `True`: Enable streaming (default).
  1006. - `False`: Disable streaming.
  1007. #### Returns
  1008. - A `Message` object containing the response to the question if `stream` is set to `False`
  1009. - An iterator containing multiple `message` objects (`iter[Message]`) if `stream` is set to `True`
  1010. The following shows the attributes of a `Message` object:
  1011. ##### id: `str`
  1012. The auto-generated message ID.
  1013. ##### content: `str`
  1014. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  1015. ##### reference: `list[Chunk]`
  1016. A list of `Chunk` objects representing references to the message, each containing the following attributes:
  1017. - `id` `str`
  1018. The chunk ID.
  1019. - `content` `str`
  1020. The content of the chunk.
  1021. - `image_id` `str`
  1022. The ID of the snapshot of the chunk. Applicable only when the source of the chunk is an image, PPT, PPTX, or PDF file.
  1023. - `document_id` `str`
  1024. The ID of the referenced document.
  1025. - `document_name` `str`
  1026. The name of the referenced document.
  1027. - `position` `list[str]`
  1028. The location information of the chunk within the referenced document.
  1029. - `dataset_id` `str`
  1030. The ID of the dataset to which the referenced document belongs.
  1031. - `similarity` `float`
  1032. 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`.
  1033. - `vector_similarity` `float`
  1034. A vector similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between vector embeddings.
  1035. - `term_similarity` `float`
  1036. A keyword similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between keywords.
  1037. #### Examples
  1038. ```python
  1039. from ragflow_sdk import RAGFlow, Agent
  1040. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1041. AGENT_id = "AGENT_ID"
  1042. session = Agent.create_session(AGENT_id, rag_object)
  1043. print("\n===== Miss R ====\n")
  1044. print("Hello. What can I do for you?")
  1045. while True:
  1046. question = input("\n===== User ====\n> ")
  1047. print("\n==== Miss R ====\n")
  1048. cont = ""
  1049. for ans in session.ask(question, stream=True):
  1050. print(ans.content[len(cont):], end='', flush=True)
  1051. cont = ans.content
  1052. ```
  1053. ---
  1054. ### List agent sessions
  1055. ```python
  1056. Agent.list_sessions(
  1057. agent_id,
  1058. rag
  1059. page: int = 1,
  1060. page_size: int = 30,
  1061. orderby: str = "update_time",
  1062. desc: bool = True,
  1063. id: str = None
  1064. ) -> List[Session]
  1065. ```
  1066. Lists sessions associated with the current agent.
  1067. #### Parameters
  1068. ##### page: `int`
  1069. Specifies the page on which the sessions will be displayed. Defaults to `1`.
  1070. ##### page_size: `int`
  1071. The number of sessions on each page. Defaults to `30`.
  1072. ##### orderby: `str`
  1073. The field by which sessions should be sorted. Available options:
  1074. - `"create_time"`
  1075. - `"update_time"`(default)
  1076. ##### desc: `bool`
  1077. Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True`.
  1078. ##### id: `str`
  1079. The ID of the agent session to retrieve. Defaults to `None`.
  1080. #### Returns
  1081. - Success: A list of `Session` objects associated with the current agent.
  1082. - Failure: `Exception`.
  1083. #### Examples
  1084. ```python
  1085. from ragflow_sdk import RAGFlow
  1086. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1087. agent_id = "2710f2269b4611ef8fdf0242ac120006"
  1088. sessions=Agent.list_sessions(agent_id,rag_object)
  1089. for session in sessions:
  1090. print(session)
  1091. ```
  1092. ---
  1093. ## AGENT MANAGEMENT
  1094. ---
  1095. ### List agents
  1096. ```python
  1097. RAGFlow.list_agents(
  1098. page: int = 1,
  1099. page_size: int = 30,
  1100. orderby: str = "create_time",
  1101. desc: bool = True,
  1102. id: str = None,
  1103. title: str = None
  1104. ) -> List[Agent]
  1105. ```
  1106. Lists agents.
  1107. #### Parameters
  1108. ##### page: `int`
  1109. Specifies the page on which the agents will be displayed. Defaults to `1`.
  1110. ##### page_size: `int`
  1111. The number of agents on each page. Defaults to `30`.
  1112. ##### orderby: `str`
  1113. The attribute by which the results are sorted. Available options:
  1114. - `"create_time"` (default)
  1115. - `"update_time"`
  1116. ##### desc: `bool`
  1117. Indicates whether the retrieved agents should be sorted in descending order. Defaults to `True`.
  1118. ##### id: `str`
  1119. The ID of the agent to retrieve. Defaults to `None`.
  1120. ##### name: `str`
  1121. The name of the agent to retrieve. Defaults to `None`.
  1122. #### Returns
  1123. - Success: A list of `Agent` objects.
  1124. - Failure: `Exception`.
  1125. #### Examples
  1126. ```python
  1127. from ragflow_sdk import RAGFlow
  1128. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  1129. for agent in rag_object.list_agents():
  1130. print(agent)
  1131. ```
  1132. ---