Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

python_api_reference.md 27KB

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