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

python_api_reference.md 24KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. #### name
  151. #### blob
  152. ### Returns
  153. ### Examples
  154. ---
  155. ## Retrieve document
  156. ```python
  157. RAGFlow.get_document(id:str=None,name:str=None) -> Document
  158. ```
  159. ### Parameters
  160. #### id: `str`, *Required*
  161. ID of the document to retrieve.
  162. #### name: `str`
  163. Name or title of the document.
  164. ### Returns
  165. A document object containing the following attributes:
  166. #### id: `str`
  167. Id of the retrieved document. Defaults to `""`.
  168. #### thumbnail: `str`
  169. Thumbnail image of the retrieved document. Defaults to `""`.
  170. #### knowledgebase_id: `str`
  171. Knowledge base ID related to the document. Defaults to `""`.
  172. #### parser_method: `str`
  173. Method used to parse the document. Defaults to `""`.
  174. #### parser_config: `ParserConfig`
  175. Configuration object for the parser. Defaults to `None`.
  176. #### source_type: `str`
  177. Source type of the document. Defaults to `""`.
  178. #### type: `str`
  179. Type or category of the document. Defaults to `""`.
  180. #### created_by: `str`
  181. Creator of the document. Defaults to `""`.
  182. #### name: `str`
  183. string
  184. ''
  185. Name or title of the document. Defaults to `""`.
  186. #### size: `int`
  187. Size of the document in bytes or some other unit. Defaults to `0`.
  188. #### token_count: `int`
  189. Number of tokens in the document. Defaults to `""`.
  190. #### chunk_count: `int`
  191. Number of chunks the document is split into. Defaults to `0`.
  192. #### progress: `float`
  193. Current processing progress as a percentage. Defaults to `0.0`.
  194. #### progress_msg: `str`
  195. Message indicating current progress status. Defaults to `""`.
  196. #### process_begin_at: `datetime`
  197. Start time of the document processing. Defaults to `None`.
  198. #### process_duation: `float`
  199. Duration of the processing in seconds or minutes. Defaults to `0.0`.
  200. ### Examples
  201. ```python
  202. from ragflow import RAGFlow
  203. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  204. doc = rag.get_document(id="wdfxb5t547d",name='testdocument.txt')
  205. print(doc)
  206. ```
  207. ---
  208. ## Save document settings
  209. ```python
  210. Document.save() -> bool
  211. ```
  212. ### Returns
  213. bool
  214. ### Examples
  215. ```python
  216. from ragflow import RAGFlow
  217. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  218. doc = rag.get_document(id="wdfxb5t547d")
  219. doc.parser_method= "manual"
  220. doc.save()
  221. ```
  222. ---
  223. ## Download document
  224. ```python
  225. Document.download() -> bytes
  226. ```
  227. ### Returns
  228. bytes of the document.
  229. ### Examples
  230. ```python
  231. from ragflow import RAGFlow
  232. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  233. doc = rag.get_document(id="wdfxb5t547d")
  234. open("~/ragflow.txt", "w+").write(doc.download())
  235. print(doc)
  236. ```
  237. ---
  238. ## List documents
  239. ```python
  240. Dataset.list_docs(keywords: str=None, offset: int=0, limit:int = -1) -> List[Document]
  241. ```
  242. ### Parameters
  243. #### keywords: `str`
  244. List documents whose name has the given keywords. Defaults to `None`.
  245. #### offset: `int`
  246. The beginning number of records for paging. Defaults to `0`.
  247. #### limit: `int`
  248. Records number to return, -1 means all of them. Records number to return, -1 means all of them.
  249. ### Returns
  250. List[Document]
  251. ### Examples
  252. ```python
  253. from ragflow import RAGFlow
  254. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  255. ds = rag.create_dataset(name="kb_1")
  256. filename1 = "~/ragflow.txt"
  257. rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
  258. filename2 = "~/infinity.txt"
  259. rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
  260. for d in ds.list_docs(keywords="rag", offset=0, limit=12):
  261. print(d)
  262. ```
  263. ---
  264. ## Delete documents
  265. ```python
  266. Document.delete() -> bool
  267. ```
  268. ### Returns
  269. bool
  270. description: delete success or not
  271. ### Examples
  272. ```python
  273. from ragflow import RAGFlow
  274. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  275. ds = rag.create_dataset(name="kb_1")
  276. filename1 = "~/ragflow.txt"
  277. rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
  278. filename2 = "~/infinity.txt"
  279. rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
  280. for d in ds.list_docs(keywords="rag", offset=0, limit=12):
  281. d.delete()
  282. ```
  283. ---
  284. ## Parse document
  285. ```python
  286. Document.async_parse() -> None
  287. RAGFLOW.async_parse_documents() -> None
  288. ```
  289. ### Parameters
  290. ????????????????????????????????????????????????????
  291. ### Returns
  292. ????????????????????????????????????????????????????
  293. ### Examples
  294. ```python
  295. #document parse and cancel
  296. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  297. ds = rag.create_dataset(name="dataset_name")
  298. name3 = 'ai.pdf'
  299. path = 'test_data/ai.pdf'
  300. rag.create_document(ds, name=name3, blob=open(path, "rb").read())
  301. doc = rag.get_document(name="ai.pdf")
  302. doc.async_parse()
  303. print("Async parsing initiated")
  304. ```
  305. ---
  306. ## Cancel document parsing
  307. ```python
  308. rag.async_cancel_parse_documents(ids)
  309. RAGFLOW.async_cancel_parse_documents()-> None
  310. ```
  311. ### Parameters
  312. #### ids, `list[]`
  313. ### Returns
  314. ?????????????????????????????????????????????????
  315. ### Examples
  316. ```python
  317. #documents parse and cancel
  318. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  319. ds = rag.create_dataset(name="God5")
  320. documents = [
  321. {'name': 'test1.txt', 'path': 'test_data/test1.txt'},
  322. {'name': 'test2.txt', 'path': 'test_data/test2.txt'},
  323. {'name': 'test3.txt', 'path': 'test_data/test3.txt'}
  324. ]
  325. # Create documents in bulk
  326. for doc_info in documents:
  327. with open(doc_info['path'], "rb") as file:
  328. created_doc = rag.create_document(ds, name=doc_info['name'], blob=file.read())
  329. docs = [rag.get_document(name=doc_info['name']) for doc_info in documents]
  330. ids = [doc.id for doc in docs]
  331. rag.async_parse_documents(ids)
  332. print("Async bulk parsing initiated")
  333. for doc in docs:
  334. for progress, msg in doc.join(interval=5, timeout=10):
  335. print(f"{doc.name}: Progress: {progress}, Message: {msg}")
  336. cancel_result = rag.async_cancel_parse_documents(ids)
  337. print("Async bulk parsing cancelled")
  338. ```
  339. ---
  340. ## Join document
  341. ??????????????????
  342. ```python
  343. Document.join(interval=15, timeout=3600) -> iteral[Tuple[float, str]]
  344. ```
  345. ### Parameters
  346. #### interval: `int`
  347. Time interval in seconds for progress report. Defaults to `15`.
  348. #### timeout: `int`
  349. Timeout in seconds. Defaults to `3600`.
  350. ### Returns
  351. iteral[Tuple[float, str]]
  352. ## Add chunk
  353. ```python
  354. Document.add_chunk(content:str) -> Chunk
  355. ```
  356. ### Parameters
  357. #### content: `str`, *Required*
  358. ### Returns
  359. chunk
  360. ### Examples
  361. ```python
  362. from ragflow import RAGFlow
  363. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  364. doc = rag.get_document(id="wdfxb5t547d")
  365. chunk = doc.add_chunk(content="xxxxxxx")
  366. ```
  367. ---
  368. ## Delete chunk
  369. ```python
  370. Chunk.delete() -> bool
  371. ```
  372. ### Returns
  373. bool
  374. ### Examples
  375. ```python
  376. from ragflow import RAGFlow
  377. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  378. doc = rag.get_document(id="wdfxb5t547d")
  379. chunk = doc.add_chunk(content="xxxxxxx")
  380. chunk.delete()
  381. ```
  382. ---
  383. ## Save chunk contents
  384. ```python
  385. Chunk.save() -> bool
  386. ```
  387. ### Returns
  388. bool
  389. ### Examples
  390. ```python
  391. from ragflow import RAGFlow
  392. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  393. doc = rag.get_document(id="wdfxb5t547d")
  394. chunk = doc.add_chunk(content="xxxxxxx")
  395. chunk.content = "sdfx"
  396. chunk.save()
  397. ```
  398. ---
  399. ## Retrieval
  400. ```python
  401. 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]
  402. ```
  403. ### Parameters
  404. #### question: `str`, *Required*
  405. The user query or query keywords. Defaults to `""`.
  406. #### datasets: `List[Dataset]`, *Required*
  407. The scope of datasets.
  408. #### document: `List[Document]`
  409. The scope of document. `None` means no limitation. Defaults to `None`.
  410. #### offset: `int`
  411. The beginning point of retrieved records. Defaults to `0`.
  412. #### limit: `int`
  413. The maximum number of records needed to return. Defaults to `6`.
  414. #### Similarity_threshold: `float`
  415. The minimum similarity score. Defaults to `0.2`.
  416. #### similarity_threshold_weight: `float`
  417. The weight of vector cosine similarity, 1 - x is the term similarity weight. Defaults to `0.3`.
  418. #### top_k: `int`
  419. Number of records engaged in vector cosine computaton. Defaults to `1024`.
  420. ### Returns
  421. List[Chunk]
  422. ### Examples
  423. ```python
  424. from ragflow import RAGFlow
  425. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  426. ds = rag.get_dataset(name="ragflow")
  427. name = 'ragflow_test.txt'
  428. path = 'test_data/ragflow_test.txt'
  429. rag.create_document(ds, name=name, blob=open(path, "rb").read())
  430. doc = rag.get_document(name=name)
  431. doc.async_parse()
  432. # Wait for parsing to complete
  433. for progress, msg in doc.join(interval=5, timeout=30):
  434. print(progress, msg)
  435. for c in rag.retrieval(question="What's ragflow?",
  436. datasets=[ds], documents=[doc],
  437. offset=0, limit=6, similarity_threshold=0.1,
  438. vector_similarity_weight=0.3,
  439. top_k=1024
  440. ):
  441. print(c)
  442. ```
  443. ---
  444. :::tip API GROUPING
  445. Chat APIs
  446. :::
  447. ## Create chat
  448. ```python
  449. RAGFlow.create_chat(
  450. name: str = "assistant",
  451. avatar: str = "path",
  452. knowledgebases: List[DataSet] = ["kb1"],
  453. llm: Chat.LLM = None,
  454. prompt: Chat.Prompt = None
  455. ) -> Chat
  456. ```
  457. ### Returns
  458. Chat
  459. description: assitant object.
  460. #### name: `str`
  461. The name of the created chat. Defaults to `"assistant"`.
  462. #### avatar: `str`
  463. The icon of the created chat. Defaults to `"path"`.
  464. #### knowledgebases: `List[DataSet]`
  465. Select knowledgebases associated. Defaults to `["kb1"]`.
  466. #### id: `str`
  467. The id of the created chat. Defaults to `""`.
  468. #### llm: `LLM`
  469. 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.
  470. - **model_name**, `str`
  471. Large language chat model. If it is `None`, it will return the user's default model.
  472. - **temperature**, `float`
  473. 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`.
  474. - **top_p**, `float`
  475. 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`
  476. - **presence_penalty**, `float`
  477. This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
  478. - **frequency penalty**, `float`
  479. Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
  480. - **max_token**, `int`
  481. This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words). Defaults to `512`.
  482. #### Prompt: `str`
  483. Instructions you need LLM to follow when LLM answers questions, like character design, answer length and answer language etc.
  484. Defaults:
  485. ```
  486. 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.
  487. Here is the knowledge base:
  488. {knowledge}
  489. The above is the knowledge base.
  490. ```
  491. ### Examples
  492. ```python
  493. from ragflow import RAGFlow
  494. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  495. kb = rag.get_dataset(name="kb_1")
  496. assi = rag.create_chat("Miss R", knowledgebases=[kb])
  497. ```
  498. ---
  499. ## Update chat
  500. ```python
  501. Chat.update(update_message: dict)
  502. ```
  503. ### Returns
  504. ```python
  505. no return
  506. ```
  507. ### Examples
  508. ```python
  509. from ragflow import RAGFlow
  510. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  511. kb = rag.get_knowledgebase(name="kb_1")
  512. assi = rag.create_chat("Miss R", knowledgebases=[kb])
  513. assi.update({"temperature":0.8})
  514. ```
  515. ---
  516. ## Delete chats
  517. ```python
  518. RAGFlow.delete_chats(ids: List[str] = None)
  519. ```
  520. ### Parameters
  521. #### ids: `str`
  522. IDs of the chats to be deleted.
  523. ### Returns
  524. ```python
  525. no return
  526. ```
  527. ### Examples
  528. ```python
  529. from ragflow import RAGFlow
  530. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  531. rag.delete_chats(ids=["id_1","id_2"])
  532. ```
  533. ---
  534. ## List chats
  535. ```python
  536. RAGFlow.list_chats(
  537. page: int = 1,
  538. page_size: int = 1024,
  539. orderby: str = "create_time",
  540. desc: bool = True,
  541. id: str = None,
  542. name: str = None
  543. ) -> List[Chat]
  544. ```
  545. ### Parameters
  546. #### page: `int`
  547. The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched.
  548. - `1`
  549. #### page_size: `int`
  550. The number of records to retrieve per page. This controls how many records will be included in each page.
  551. - `1024`
  552. #### orderby: `string`
  553. The field by which the records should be sorted. This specifies the attribute or column used to order the results.
  554. - `"create_time"`
  555. #### desc: `bool`
  556. A boolean flag indicating whether the sorting should be in descending order.
  557. - `True`
  558. #### id: `string`
  559. The ID of the chat to be retrieved.
  560. - `None`
  561. #### name: `string`
  562. The name of the chat to be retrieved.
  563. - `None`
  564. ### Returns
  565. A list of chat objects.
  566. ### Examples
  567. ```python
  568. from ragflow import RAGFlow
  569. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  570. for assi in rag.list_chats():
  571. print(assi)
  572. ```
  573. ---
  574. :::tip API GROUPING
  575. Chat-session APIs
  576. :::
  577. ## Create session
  578. ```python
  579. Chat.create_session(name: str = "New session") -> Session
  580. ```
  581. ### Returns
  582. A `session` object.
  583. #### id: `str`
  584. The id of the created session is used to identify different sessions.
  585. - id can not be provided in creating
  586. #### name: `str`
  587. The name of the created session. Defaults to `"New session"`.
  588. #### messages: `List[Message]`
  589. The messages of the created session.
  590. - messages cannot be provided.
  591. Defaults:
  592. ??????????????????????????????????????????????????????????????????????????????????????????????
  593. ```
  594. [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
  595. ```
  596. #### chat_id: `str`
  597. The id of associated chat
  598. - `chat_id` can't be changed
  599. ### Examples
  600. ```python
  601. from ragflow import RAGFlow
  602. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  603. assi = rag.list_chats(name="Miss R")
  604. assi = assi[0]
  605. sess = assi.create_session()
  606. ```
  607. ## Update session
  608. ```python
  609. Session.update(update_message:dict)
  610. ```
  611. ### Returns
  612. no return
  613. ### Examples
  614. ```python
  615. from ragflow import RAGFlow
  616. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  617. assi = rag.list_chats(name="Miss R")
  618. assi = assi[0]
  619. sess = assi.create_session("new_session")
  620. sess.update({"name": "Updated session"...})
  621. ```
  622. ---
  623. ## Chat
  624. ```python
  625. Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
  626. ```
  627. ### Parameters
  628. #### question: `str`, *Required*
  629. The question to start an AI chat. Defaults to `None`. ???????????????????
  630. #### stream: `bool`
  631. 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.
  632. ### Returns
  633. [Message, iter[Message]]
  634. #### id: `str`
  635. The id of the message. `id` is automatically generated. Defaults to `None`. ???????????????????
  636. #### content: `str`
  637. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  638. #### reference: `List[Chunk]`
  639. The auto-generated reference of the message. Each `chunk` object includes the following attributes:
  640. - **id**: `str`
  641. The id of the chunk. ?????????????????
  642. - **content**: `str`
  643. The content of the chunk. Defaults to `None`. ?????????????????????
  644. - **document_id**: `str`
  645. The ID of the document being referenced. Defaults to `""`.
  646. - **document_name**: `str`
  647. The name of the referenced document being referenced. Defaults to `""`.
  648. - **knowledgebase_id**: `str`
  649. The id of the knowledge base to which the relevant document belongs. Defaults to `""`.
  650. - **image_id**: `str`
  651. The id of the image related to the chunk. Defaults to `""`.
  652. - **similarity**: `float`
  653. 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`. ????????????????????????????????????
  654. - **vector_similarity**: `float`
  655. 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`. ?????????????????????????????????
  656. - **term_similarity**: `float`
  657. 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`. ???????????????????
  658. - **position**: `List[string]`
  659. 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`. ??????????????
  660. ### Examples
  661. ```python
  662. from ragflow import RAGFlow
  663. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  664. assi = rag.list_chats(name="Miss R")
  665. assi = assi[0]
  666. sess = assi.create_session()
  667. print("\n==================== Miss R =====================\n")
  668. print(assi.get_prologue())
  669. while True:
  670. question = input("\n==================== User =====================\n> ")
  671. print("\n==================== Miss R =====================\n")
  672. cont = ""
  673. for ans in sess.ask(question, stream=True):
  674. print(ans.content[len(cont):], end='', flush=True)
  675. cont = ans.content
  676. ```
  677. ---
  678. ## List sessions
  679. ```python
  680. Chat.list_sessions(
  681. page: int = 1,
  682. page_size: int = 1024,
  683. orderby: str = "create_time",
  684. desc: bool = True,
  685. id: str = None,
  686. name: str = None
  687. ) -> List[Session]
  688. ```
  689. ### Returns
  690. List[Session]
  691. description: the List contains information about multiple assistant object, with each dictionary containing information about one assistant.
  692. ### Examples
  693. ```python
  694. from ragflow import RAGFlow
  695. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  696. assi = rag.list_chats(name="Miss R")
  697. assi = assi[0]
  698. for sess in assi.list_sessions():
  699. print(sess)
  700. ```
  701. ### Parameters
  702. #### page: `int`
  703. The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched.
  704. - `1`
  705. #### page_size: `int`
  706. The number of records to retrieve per page. This controls how many records will be included in each page.
  707. - `1024`
  708. #### orderby: `string`
  709. The field by which the records should be sorted. This specifies the attribute or column used to order the results.
  710. - `"create_time"`
  711. #### desc: `bool`
  712. A boolean flag indicating whether the sorting should be in descending order.
  713. - `True`
  714. #### id: `string`
  715. The ID of the chat to be retrieved.
  716. - `None`
  717. #### name: `string`
  718. The name of the chat to be retrieved.
  719. - `None`
  720. ---
  721. ## Delete session
  722. ```python
  723. Chat.delete_sessions(ids:List[str] = None)
  724. ```
  725. ### Returns
  726. no return
  727. ### Examples
  728. ```python
  729. from ragflow import RAGFlow
  730. rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
  731. assi = rag.list_chats(name="Miss R")
  732. assi = assi[0]
  733. assi.delete_sessions(ids=["id_1","id_2"])
  734. ```
  735. ### Parameters
  736. #### ids: `List[string]`
  737. IDs of the sessions to be deleted.
  738. - `None`