Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

python_api_reference.md 24KB

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