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

python_api_reference.md 38KB

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