Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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