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

python_api_reference.md 32KB

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