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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. # DRAFT Python API Reference
  2. :::tip NOTE
  3. Knowledgebase APIs
  4. :::
  5. ## Create knowledge base
  6. ```python
  7. RAGFlow.create_dataset(
  8. name: str,
  9. avatar: str = "",
  10. description: str = "",
  11. language: str = "English",
  12. permission: str = "me",
  13. document_count: int = 0,
  14. chunk_count: int = 0,
  15. parse_method: str = "naive",
  16. parser_config: DataSet.ParserConfig = None
  17. ) -> DataSet
  18. ```
  19. Creates a knowledge base (dataset).
  20. ### Parameters
  21. #### name: `str`, *Required*
  22. The unique name of the dataset to create. It must adhere to the following requirements:
  23. - Permitted characters include:
  24. - English letters (a-z, A-Z)
  25. - Digits (0-9)
  26. - "_" (underscore)
  27. - Must begin with an English letter or underscore.
  28. - Maximum 65,535 characters.
  29. - Case-insensitive.
  30. #### avatar: `str`
  31. Base64 encoding of the avatar. Defaults to `""`
  32. #### tenant_id: `str`
  33. The id of the tenant associated with the created dataset is used to identify different users. Defaults to `None`.
  34. - If creating a dataset, tenant_id must not be provided.
  35. - If updating a dataset, tenant_id can't be changed.
  36. #### description: `str`
  37. The description of the created dataset. Defaults to `""`.
  38. #### language: `str`
  39. The language setting of the created dataset. Defaults to `"English"`. ????????????
  40. #### embedding_model: `str`
  41. The specific model used by the dataset to generate vector embeddings. Defaults to `""`.
  42. - If creating a dataset, embedding_model must not be provided.
  43. - If updating a dataset, embedding_model can't be changed.
  44. #### permission: `str`
  45. Specify who can operate on the dataset. Defaults to `"me"`.
  46. #### document_count: `int`
  47. The number of documents associated with the dataset. Defaults to `0`.
  48. - If updating a dataset, `document_count` can't be changed.
  49. #### chunk_count: `int`
  50. The number of data chunks generated or processed by the created dataset. Defaults to `0`.
  51. - If updating a dataset, chunk_count can't be changed.
  52. #### parse_method, `str`
  53. The method used by the dataset to parse and process data.
  54. - If updating parse_method in a dataset, chunk_count must be greater than 0. Defaults to `"naive"`.
  55. #### parser_config, `Dataset.ParserConfig`
  56. The configuration settings for the parser used by the dataset.
  57. ### Returns
  58. ```python
  59. DataSet
  60. description: dataset object
  61. ```
  62. ### Examples
  63. ```python
  64. from ragflow import RAGFlow
  65. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  66. ds = rag.create_dataset(name="kb_1")
  67. ```
  68. ---
  69. ## Delete knowledge bases
  70. ```python
  71. RAGFlow.delete_datasets(ids: List[str] = None)
  72. ```
  73. Deletes knowledge bases.
  74. ### Parameters
  75. #### ids: `List[str]`
  76. The ids of the datasets to be deleted.
  77. ### Returns
  78. ```python
  79. no return
  80. ```
  81. ### Examples
  82. ```python
  83. from ragflow import RAGFlow
  84. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  85. rag.delete_datasets(ids=["id_1","id_2"])
  86. ```
  87. ---
  88. ## List knowledge bases
  89. ```python
  90. RAGFlow.list_datasets(
  91. page: int = 1,
  92. page_size: int = 1024,
  93. orderby: str = "create_time",
  94. desc: bool = True,
  95. id: str = None,
  96. name: str = None
  97. ) -> List[DataSet]
  98. ```
  99. Lists all knowledge bases in the RAGFlow system.
  100. ### Parameters
  101. #### page: `int`
  102. The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched. Defaults to `1`.
  103. #### page_size: `int`
  104. The number of records to retrieve per page. This controls how many records will be included in each page. Defaults to `1024`.
  105. #### order_by: `str`
  106. The field by which the records should be sorted. This specifies the attribute or column used to order the results. Defaults to `"create_time"`.
  107. #### desc: `bool`
  108. Whether the sorting should be in descending order. Defaults to `True`.
  109. #### id: `str`
  110. The id of the dataset to be got. Defaults to `None`.
  111. #### name: `str`
  112. The name of the dataset to be got. Defaults to `None`.
  113. ### Returns
  114. ```python
  115. List[DataSet]
  116. description:the list of datasets.
  117. ```
  118. ### Examples
  119. ```python
  120. from ragflow import RAGFlow
  121. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  122. for ds in rag.list_datasets():
  123. print(ds)
  124. ```
  125. ---
  126. ## Update knowledge base
  127. ```python
  128. DataSet.update(update_message: dict)
  129. ```
  130. ### Returns
  131. ```python
  132. no return
  133. ```
  134. ### Examples
  135. ```python
  136. from ragflow import RAGFlow
  137. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  138. ds = rag.get_dataset(name="kb_1")
  139. ds.update({"parse_method":"manual", ...}}
  140. ```
  141. ---
  142. :::tip API GROUPING
  143. File management inside knowledge base
  144. :::
  145. ## Upload document
  146. ```python
  147. RAGFLOW.upload_document(ds:DataSet, name:str, blob:bytes)-> bool
  148. ```
  149. ### Parameters
  150. #### ds
  151. #### name
  152. #### blob
  153. ### Returns
  154. ### Examples
  155. ---
  156. ## Retrieve document
  157. ```python
  158. RAGFlow.get_document(id:str=None,name:str=None) -> Document
  159. ```
  160. ### Parameters
  161. #### id: `str`, *Required*
  162. ID of the document to retrieve.
  163. #### name: `str`
  164. Name or title of the document.
  165. ### Returns
  166. A document object containing the following attributes:
  167. #### id: `str`
  168. Id of the retrieved document. Defaults to `""`.
  169. #### thumbnail: `str`
  170. Thumbnail image of the retrieved document. Defaults to `""`.
  171. #### knowledgebase_id: `str`
  172. Knowledge base ID related to the document. Defaults to `""`.
  173. #### parser_method: `str`
  174. Method used to parse the document. Defaults to `""`.
  175. #### parser_config: `ParserConfig`
  176. Configuration object for the parser. Defaults to `None`.
  177. #### source_type: `str`
  178. Source type of the document. Defaults to `""`.
  179. #### type: `str`
  180. Type or category of the document. Defaults to `""`.
  181. #### created_by: `str`
  182. Creator of the document. Defaults to `""`.
  183. #### name: `str`
  184. string
  185. ''
  186. Name or title of the document. Defaults to `""`.
  187. #### size: `int`
  188. Size of the document in bytes or some other unit. Defaults to `0`.
  189. #### token_count: `int`
  190. Number of tokens in the document. Defaults to `""`.
  191. #### chunk_count: `int`
  192. Number of chunks the document is split into. Defaults to `0`.
  193. #### progress: `float`
  194. Current processing progress as a percentage. Defaults to `0.0`.
  195. #### progress_msg: `str`
  196. Message indicating current progress status. Defaults to `""`.
  197. #### process_begin_at: `datetime`
  198. Start time of the document processing. Defaults to `None`.
  199. #### process_duation: `float`
  200. Duration of the processing in seconds or minutes. Defaults to `0.0`.
  201. ### Examples
  202. ```python
  203. from ragflow import RAGFlow
  204. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  205. doc = rag.get_document(id="wdfxb5t547d",name='testdocument.txt')
  206. print(doc)
  207. ```
  208. ---
  209. ## Save document settings
  210. ```python
  211. Document.save() -> bool
  212. ```
  213. ### Returns
  214. bool
  215. ### Examples
  216. ```python
  217. from ragflow import RAGFlow
  218. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  219. doc = rag.get_document(id="wdfxb5t547d")
  220. doc.parser_method= "manual"
  221. doc.save()
  222. ```
  223. ---
  224. ## Download document
  225. ```python
  226. Document.download() -> bytes
  227. ```
  228. ### Returns
  229. bytes of the document.
  230. ### Examples
  231. ```python
  232. from ragflow import RAGFlow
  233. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  234. doc = rag.get_document(id="wdfxb5t547d")
  235. open("~/ragflow.txt", "w+").write(doc.download())
  236. print(doc)
  237. ```
  238. ---
  239. ## List documents
  240. ```python
  241. Dataset.list_docs(keywords: str=None, offset: int=0, limit:int = -1) -> List[Document]
  242. ```
  243. ### Parameters
  244. #### keywords: `str`
  245. List documents whose name has the given keywords. Defaults to `None`.
  246. #### offset: `int`
  247. The beginning number of records for paging. Defaults to `0`.
  248. #### limit: `int`
  249. Records number to return, -1 means all of them. Records number to return, -1 means all of them.
  250. ### Returns
  251. List[Document]
  252. ### Examples
  253. ```python
  254. from ragflow import RAGFlow
  255. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  256. ds = rag.create_dataset(name="kb_1")
  257. filename1 = "~/ragflow.txt"
  258. rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
  259. filename2 = "~/infinity.txt"
  260. rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
  261. for d in ds.list_docs(keywords="rag", offset=0, limit=12):
  262. print(d)
  263. ```
  264. ---
  265. ## Delete documents
  266. ```python
  267. Document.delete() -> bool
  268. ```
  269. ### Returns
  270. bool
  271. description: delete success or not
  272. ### Examples
  273. ```python
  274. from ragflow import RAGFlow
  275. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  276. ds = rag.create_dataset(name="kb_1")
  277. filename1 = "~/ragflow.txt"
  278. rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
  279. filename2 = "~/infinity.txt"
  280. rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
  281. for d in ds.list_docs(keywords="rag", offset=0, limit=12):
  282. d.delete()
  283. ```
  284. ---
  285. ## Parse document
  286. ```python
  287. Document.async_parse() -> None
  288. RAGFLOW.async_parse_documents() -> None
  289. ```
  290. ### Parameters
  291. ????????????????????????????????????????????????????
  292. ### Returns
  293. ????????????????????????????????????????????????????
  294. ### Examples
  295. ```python
  296. #document parse and cancel
  297. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  298. ds = rag.create_dataset(name="dataset_name")
  299. name3 = 'ai.pdf'
  300. path = 'test_data/ai.pdf'
  301. rag.create_document(ds, name=name3, blob=open(path, "rb").read())
  302. doc = rag.get_document(name="ai.pdf")
  303. doc.async_parse()
  304. print("Async parsing initiated")
  305. ```
  306. ---
  307. ## Cancel document parsing
  308. ```python
  309. rag.async_cancel_parse_documents(ids)
  310. RAGFLOW.async_cancel_parse_documents()-> None
  311. ```
  312. ### Parameters
  313. #### ids, `list[]`
  314. ### Returns
  315. ?????????????????????????????????????????????????
  316. ### Examples
  317. ```python
  318. #documents parse and cancel
  319. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  320. ds = rag.create_dataset(name="God5")
  321. documents = [
  322. {'name': 'test1.txt', 'path': 'test_data/test1.txt'},
  323. {'name': 'test2.txt', 'path': 'test_data/test2.txt'},
  324. {'name': 'test3.txt', 'path': 'test_data/test3.txt'}
  325. ]
  326. # Create documents in bulk
  327. for doc_info in documents:
  328. with open(doc_info['path'], "rb") as file:
  329. created_doc = rag.create_document(ds, name=doc_info['name'], blob=file.read())
  330. docs = [rag.get_document(name=doc_info['name']) for doc_info in documents]
  331. ids = [doc.id for doc in docs]
  332. rag.async_parse_documents(ids)
  333. print("Async bulk parsing initiated")
  334. for doc in docs:
  335. for progress, msg in doc.join(interval=5, timeout=10):
  336. print(f"{doc.name}: Progress: {progress}, Message: {msg}")
  337. cancel_result = rag.async_cancel_parse_documents(ids)
  338. print("Async bulk parsing cancelled")
  339. ```
  340. ---
  341. ## Join document
  342. ??????????????????
  343. ```python
  344. Document.join(interval=15, timeout=3600) -> iteral[Tuple[float, str]]
  345. ```
  346. ### Parameters
  347. #### interval: `int`
  348. Time interval in seconds for progress report. Defaults to `15`.
  349. #### timeout: `int`
  350. Timeout in seconds. Defaults to `3600`.
  351. ### Returns
  352. iteral[Tuple[float, str]]
  353. ## Add chunk
  354. ```python
  355. Document.add_chunk(content:str) -> Chunk
  356. ```
  357. ### Parameters
  358. #### content: `str`, *Required*
  359. ### Returns
  360. chunk
  361. ### Examples
  362. ```python
  363. from ragflow import RAGFlow
  364. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  365. doc = rag.get_document(id="wdfxb5t547d")
  366. chunk = doc.add_chunk(content="xxxxxxx")
  367. ```
  368. ---
  369. ## Delete chunk
  370. ```python
  371. Chunk.delete() -> bool
  372. ```
  373. ### Returns
  374. bool
  375. ### Examples
  376. ```python
  377. from ragflow import RAGFlow
  378. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  379. doc = rag.get_document(id="wdfxb5t547d")
  380. chunk = doc.add_chunk(content="xxxxxxx")
  381. chunk.delete()
  382. ```
  383. ---
  384. ## Save chunk contents
  385. ```python
  386. Chunk.save() -> bool
  387. ```
  388. ### Returns
  389. bool
  390. ### Examples
  391. ```python
  392. from ragflow import RAGFlow
  393. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  394. doc = rag.get_document(id="wdfxb5t547d")
  395. chunk = doc.add_chunk(content="xxxxxxx")
  396. chunk.content = "sdfx"
  397. chunk.save()
  398. ```
  399. ---
  400. ## Retrieval
  401. ```python
  402. RAGFlow.retrieval(question:str, datasets:List[Dataset], document=List[Document]=None, offset:int=0, limit:int=6, similarity_threshold:float=0.1, vector_similarity_weight:float=0.3, top_k:int=1024) -> List[Chunk]
  403. ```
  404. ### Parameters
  405. #### question: `str`, *Required*
  406. The user query or query keywords. Defaults to `""`.
  407. #### datasets: `List[Dataset]`, *Required*
  408. The scope of datasets.
  409. #### document: `List[Document]`
  410. The scope of document. `None` means no limitation. Defaults to `None`.
  411. #### offset: `int`
  412. The beginning point of retrieved records. Defaults to `0`.
  413. #### limit: `int`
  414. The maximum number of records needed to return. Defaults to `6`.
  415. #### Similarity_threshold: `float`
  416. The minimum similarity score. Defaults to `0.2`.
  417. #### similarity_threshold_weight: `float`
  418. The weight of vector cosine similarity, 1 - x is the term similarity weight. Defaults to `0.3`.
  419. #### top_k: `int`
  420. Number of records engaged in vector cosine computaton. Defaults to `1024`.
  421. ### Returns
  422. List[Chunk]
  423. ### Examples
  424. ```python
  425. from ragflow import RAGFlow
  426. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  427. ds = rag.get_dataset(name="ragflow")
  428. name = 'ragflow_test.txt'
  429. path = 'test_data/ragflow_test.txt'
  430. rag.create_document(ds, name=name, blob=open(path, "rb").read())
  431. doc = rag.get_document(name=name)
  432. doc.async_parse()
  433. # Wait for parsing to complete
  434. for progress, msg in doc.join(interval=5, timeout=30):
  435. print(progress, msg)
  436. for c in rag.retrieval(question="What's ragflow?",
  437. datasets=[ds], documents=[doc],
  438. offset=0, limit=6, similarity_threshold=0.1,
  439. vector_similarity_weight=0.3,
  440. top_k=1024
  441. ):
  442. print(c)
  443. ```
  444. ---
  445. :::tip API GROUPING
  446. Chat APIs
  447. :::
  448. ## Create chat
  449. ```python
  450. RAGFlow.create_chat(
  451. name: str = "assistant",
  452. avatar: str = "path",
  453. knowledgebases: List[DataSet] = ["kb1"],
  454. llm: Chat.LLM = None,
  455. prompt: Chat.Prompt = None
  456. ) -> Chat
  457. ```
  458. ### Returns
  459. Chat
  460. description: assitant object.
  461. #### name: `str`
  462. The name of the created chat. Defaults to `"assistant"`.
  463. #### avatar: `str`
  464. The icon of the created chat. Defaults to `"path"`.
  465. #### knowledgebases: `List[DataSet]`
  466. Select knowledgebases associated. Defaults to `["kb1"]`.
  467. #### id: `str`
  468. The id of the created chat. Defaults to `""`.
  469. #### llm: `LLM`
  470. 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.
  471. - **model_name**, `str`
  472. Large language chat model. If it is `None`, it will return the user's default model.
  473. - **temperature**, `float`
  474. This parameter controls the randomness of predictions by the model. A lower temperature makes the model more confident in its responses, while a higher temperature makes it more creative and diverse. Defaults to `0.1`.
  475. - **top_p**, `float`
  476. 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`
  477. - **presence_penalty**, `float`
  478. This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
  479. - **frequency penalty**, `float`
  480. Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
  481. - **max_token**, `int`
  482. This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words). Defaults to `512`.
  483. #### Prompt: `str`
  484. Instructions you need LLM to follow when LLM answers questions, like character design, answer length and answer language etc.
  485. Defaults:
  486. ```
  487. 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.
  488. Here is the knowledge base:
  489. {knowledge}
  490. The above is the knowledge base.
  491. ```
  492. ### Examples
  493. ```python
  494. from ragflow import RAGFlow
  495. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  496. kb = rag.get_dataset(name="kb_1")
  497. assi = rag.create_chat("Miss R", knowledgebases=[kb])
  498. ```
  499. ---
  500. ## Update chat
  501. ```python
  502. Chat.update(update_message: dict)
  503. ```
  504. ### Returns
  505. ```python
  506. no return
  507. ```
  508. ### Examples
  509. ```python
  510. from ragflow import RAGFlow
  511. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  512. kb = rag.get_knowledgebase(name="kb_1")
  513. assi = rag.create_chat("Miss R", knowledgebases=[kb])
  514. assi.update({"temperature":0.8})
  515. ```
  516. ---
  517. ## Delete chats
  518. ```python
  519. RAGFlow.delete_chats(ids: List[str] = None)
  520. ```
  521. ### Parameters
  522. #### ids: `str`
  523. IDs of the chats to be deleted.
  524. ### Returns
  525. ```python
  526. no return
  527. ```
  528. ### Examples
  529. ```python
  530. from ragflow import RAGFlow
  531. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  532. rag.delete_chats(ids=["id_1","id_2"])
  533. ```
  534. ---
  535. ## List chats
  536. ```python
  537. RAGFlow.list_chats(
  538. page: int = 1,
  539. page_size: int = 1024,
  540. orderby: str = "create_time",
  541. desc: bool = True,
  542. id: str = None,
  543. name: str = None
  544. ) -> List[Chat]
  545. ```
  546. ### Parameters
  547. #### page: `int`
  548. The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched.
  549. - `1`
  550. #### page_size: `int`
  551. The number of records to retrieve per page. This controls how many records will be included in each page.
  552. - `1024`
  553. #### orderby: `string`
  554. The field by which the records should be sorted. This specifies the attribute or column used to order the results.
  555. - `"create_time"`
  556. #### desc: `bool`
  557. A boolean flag indicating whether the sorting should be in descending order.
  558. - `True`
  559. #### id: `string`
  560. The ID of the chat to be retrieved.
  561. - `None`
  562. #### name: `string`
  563. The name of the chat to be retrieved.
  564. - `None`
  565. ### Returns
  566. A list of chat objects.
  567. ### Examples
  568. ```python
  569. from ragflow import RAGFlow
  570. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  571. for assi in rag.list_chats():
  572. print(assi)
  573. ```
  574. ---
  575. :::tip API GROUPING
  576. Chat-session APIs
  577. :::
  578. ## Create session
  579. ```python
  580. Chat.create_session(name: str = "New session") -> Session
  581. ```
  582. ### Returns
  583. A `session` object.
  584. #### id: `str`
  585. The id of the created session is used to identify different sessions.
  586. - id can not be provided in creating
  587. #### name: `str`
  588. The name of the created session. Defaults to `"New session"`.
  589. #### messages: `List[Message]`
  590. The messages of the created session.
  591. - messages cannot be provided.
  592. Defaults:
  593. ??????????????????????????????????????????????????????????????????????????????????????????????
  594. ```
  595. [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
  596. ```
  597. #### chat_id: `str`
  598. The id of associated chat
  599. - `chat_id` can't be changed
  600. ### Examples
  601. ```python
  602. from ragflow import RAGFlow
  603. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  604. assi = rag.list_chats(name="Miss R")
  605. assi = assi[0]
  606. sess = assi.create_session()
  607. ```
  608. ## Update session
  609. ```python
  610. Session.update(update_message:dict)
  611. ```
  612. ### Returns
  613. no return
  614. ### Examples
  615. ```python
  616. from ragflow import RAGFlow
  617. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  618. assi = rag.list_chats(name="Miss R")
  619. assi = assi[0]
  620. sess = assi.create_session("new_session")
  621. sess.update({"name": "Updated session"...})
  622. ```
  623. ---
  624. ## Chat
  625. ```python
  626. Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
  627. ```
  628. ### Parameters
  629. #### question: `str`, *Required*
  630. The question to start an AI chat. Defaults to `None`. ???????????????????
  631. #### stream: `bool`
  632. The approach of streaming text generation. When stream is True, it outputs results in a streaming fashion; otherwise, it outputs the complete result after the model has finished generating.
  633. ### Returns
  634. [Message, iter[Message]]
  635. #### id: `str`
  636. The id of the message. `id` is automatically generated. Defaults to `None`. ???????????????????
  637. #### content: `str`
  638. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  639. #### reference: `List[Chunk]`
  640. The auto-generated reference of the message. Each `chunk` object includes the following attributes:
  641. - **id**: `str`
  642. The id of the chunk. ?????????????????
  643. - **content**: `str`
  644. The content of the chunk. Defaults to `None`. ?????????????????????
  645. - **document_id**: `str`
  646. The ID of the document being referenced. Defaults to `""`.
  647. - **document_name**: `str`
  648. The name of the referenced document being referenced. Defaults to `""`.
  649. - **knowledgebase_id**: `str`
  650. The id of the knowledge base to which the relevant document belongs. Defaults to `""`.
  651. - **image_id**: `str`
  652. The id of the image related to the chunk. Defaults to `""`.
  653. - **similarity**: `float`
  654. A general similarity score, usually a composite score derived from various similarity measures . This score represents the degree of similarity between two objects. The value ranges between 0 and 1, where a value closer to 1 indicates higher similarity. Defaults to `None`. ????????????????????????????????????
  655. - **vector_similarity**: `float`
  656. A similarity score based on vector representations. This score is obtained by converting texts, words, or objects into vectors and then calculating the cosine similarity or other distance measures between these vectors to determine the similarity in vector space. A higher value indicates greater similarity in the vector space. Defaults to `None`. ?????????????????????????????????
  657. - **term_similarity**: `float`
  658. The similarity score based on terms or keywords. This score is calculated by comparing the similarity of key terms between texts or datasets, typically measuring how similar two words or phrases are in meaning or context. A higher value indicates a stronger similarity between terms. Defaults to `None`. ???????????????????
  659. - **position**: `List[string]`
  660. Indicates the position or index of keywords or specific terms within the text. An array is typically used to mark the location of keywords or specific elements, facilitating precise operations or analysis of the text. Defaults to `None`. ??????????????
  661. ### Examples
  662. ```python
  663. from ragflow import RAGFlow
  664. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  665. assi = rag.list_chats(name="Miss R")
  666. assi = assi[0]
  667. sess = assi.create_session()
  668. print("\n==================== Miss R =====================\n")
  669. print(assi.get_prologue())
  670. while True:
  671. question = input("\n==================== User =====================\n> ")
  672. print("\n==================== Miss R =====================\n")
  673. cont = ""
  674. for ans in sess.ask(question, stream=True):
  675. print(ans.content[len(cont):], end='', flush=True)
  676. cont = ans.content
  677. ```
  678. ---
  679. ## List sessions
  680. ```python
  681. Chat.list_sessions(
  682. page: int = 1,
  683. page_size: int = 1024,
  684. orderby: str = "create_time",
  685. desc: bool = True,
  686. id: str = None,
  687. name: str = None
  688. ) -> List[Session]
  689. ```
  690. ### Returns
  691. List[Session]
  692. description: the List contains information about multiple assistant object, with each dictionary containing information about one assistant.
  693. ### Examples
  694. ```python
  695. from ragflow import RAGFlow
  696. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  697. assi = rag.list_chats(name="Miss R")
  698. assi = assi[0]
  699. for sess in assi.list_sessions():
  700. print(sess)
  701. ```
  702. ### Parameters
  703. #### page: `int`
  704. The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched.
  705. - `1`
  706. #### page_size: `int`
  707. The number of records to retrieve per page. This controls how many records will be included in each page.
  708. - `1024`
  709. #### orderby: `string`
  710. The field by which the records should be sorted. This specifies the attribute or column used to order the results.
  711. - `"create_time"`
  712. #### desc: `bool`
  713. A boolean flag indicating whether the sorting should be in descending order.
  714. - `True`
  715. #### id: `string`
  716. The ID of the chat to be retrieved.
  717. - `None`
  718. #### name: `string`
  719. The name of the chat to be retrieved.
  720. - `None`
  721. ---
  722. ## Delete session
  723. ```python
  724. Chat.delete_sessions(ids:List[str] = None)
  725. ```
  726. ### Returns
  727. no return
  728. ### Examples
  729. ```python
  730. from ragflow import RAGFlow
  731. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  732. assi = rag.list_chats(name="Miss R")
  733. assi = assi[0]
  734. assi.delete_sessions(ids=["id_1","id_2"])
  735. ```
  736. ### Parameters
  737. #### ids: `List[string]`
  738. IDs of the sessions to be deleted.
  739. - `None`