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

python_api_reference.md 30KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. # DRAFT Python API Reference
  2. **THE API REFERENCES BELOW ARE STILL UNDER DEVELOPMENT.**
  3. :::tip NOTE
  4. Knowledge Base Management
  5. :::
  6. ## Create knowledge base
  7. ```python
  8. RAGFlow.create_dataset(
  9. name: str,
  10. avatar: str = "",
  11. description: str = "",
  12. language: str = "English",
  13. permission: str = "me",
  14. document_count: int = 0,
  15. chunk_count: int = 0,
  16. parse_method: str = "naive",
  17. parser_config: DataSet.ParserConfig = None
  18. ) -> DataSet
  19. ```
  20. Creates a knowledge base (dataset).
  21. ### Parameters
  22. #### name: `str`, *Required*
  23. The unique name of the dataset to create. It must adhere to the following requirements:
  24. - Permitted characters include:
  25. - English letters (a-z, A-Z)
  26. - Digits (0-9)
  27. - "_" (underscore)
  28. - Must begin with an English letter or underscore.
  29. - Maximum 65,535 characters.
  30. - Case-insensitive.
  31. #### avatar: `str`
  32. Base64 encoding of the avatar. Defaults to `""`
  33. #### description
  34. #### tenant_id: `str`
  35. The id of the tenant associated with the created dataset is used to identify different users. Defaults to `None`.
  36. - If creating a dataset, tenant_id must not be provided.
  37. - If updating a dataset, tenant_id can't be changed.
  38. #### description: `str`
  39. The description of the created dataset. Defaults to `""`.
  40. #### language: `str`
  41. The language setting of the created dataset. Defaults to `"English"`. ????????????
  42. #### permission
  43. Specify who can operate on the dataset. Defaults to `"me"`.
  44. #### document_count: `int`
  45. The number of documents associated with the dataset. Defaults to `0`.
  46. #### chunk_count: `int`
  47. The number of data chunks generated or processed by the created dataset. Defaults to `0`.
  48. #### parse_method, `str`
  49. The method used by the dataset to parse and process data. Defaults to `"naive"`.
  50. #### parser_config
  51. The parser configuration of the dataset. A `ParserConfig` object contains the following attributes:
  52. - `chunk_token_count`: Defaults to `128`.
  53. - `layout_recognize`: Defaults to `True`.
  54. - `delimiter`: Defaults to `'\n!?。;!?'`.
  55. - `task_page_size`: Defaults to `12`.
  56. ### Returns
  57. - Success: A `dataset` object.
  58. - Failure: `Exception`
  59. ### Examples
  60. ```python
  61. from ragflow import RAGFlow
  62. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  63. ds = rag_object.create_dataset(name="kb_1")
  64. ```
  65. ---
  66. ## Delete knowledge bases
  67. ```python
  68. RAGFlow.delete_datasets(ids: list[str] = None)
  69. ```
  70. Deletes knowledge bases by name or ID.
  71. ### Parameters
  72. #### ids
  73. The IDs of the knowledge bases to delete.
  74. ### Returns
  75. - Success: No value is returned.
  76. - Failure: `Exception`
  77. ### Examples
  78. ```python
  79. rag.delete_datasets(ids=["id_1","id_2"])
  80. ```
  81. ---
  82. ## List knowledge bases
  83. ```python
  84. RAGFlow.list_datasets(
  85. page: int = 1,
  86. page_size: int = 1024,
  87. orderby: str = "create_time",
  88. desc: bool = True,
  89. id: str = None,
  90. name: str = None
  91. ) -> list[DataSet]
  92. ```
  93. Retrieves a list of knowledge bases.
  94. ### Parameters
  95. #### page: `int`
  96. The current page number to retrieve from the paginated results. Defaults to `1`.
  97. #### page_size: `int`
  98. The number of records on each page. Defaults to `1024`.
  99. #### order_by: `str`
  100. The field by which the records should be sorted. This specifies the attribute or column used to order the results. Defaults to `"create_time"`.
  101. #### desc: `bool`
  102. Whether the sorting should be in descending order. Defaults to `True`.
  103. #### id: `str`
  104. The id of the dataset to be got. Defaults to `None`.
  105. #### name: `str`
  106. The name of the dataset to be got. Defaults to `None`.
  107. ### Returns
  108. - Success: A list of `DataSet` objects representing the retrieved knowledge bases.
  109. - Failure: `Exception`.
  110. ### Examples
  111. #### List all knowledge bases
  112. ```python
  113. for ds in rag_object.list_datasets():
  114. print(ds)
  115. ```
  116. #### Retrieve a knowledge base by ID
  117. ```python
  118. dataset = rag_object.list_datasets(id = "id_1")
  119. print(dataset[0])
  120. ```
  121. ---
  122. ## Update knowledge base
  123. ```python
  124. DataSet.update(update_message: dict)
  125. ```
  126. Updates the current knowledge base.
  127. ### Parameters
  128. #### update_message: `dict[str, str|int]`, *Required*
  129. - `"name"`: `str` The name of the knowledge base to update.
  130. - `"tenant_id"`: `str` The `"tenant_id` you get after calling `create_dataset()`.
  131. - `"embedding_model"`: `str` The embedding model for generating vector embeddings.
  132. - Ensure that `"chunk_count"` is `0` before updating `"embedding_model"`.
  133. - `"parser_method"`: `str`
  134. - `"naive"`: General
  135. - `"manual`: Manual
  136. - `"qa"`: Q&A
  137. - `"table"`: Table
  138. - `"paper"`: Paper
  139. - `"book"`: Book
  140. - `"laws"`: Laws
  141. - `"presentation"`: Presentation
  142. - `"picture"`: Picture
  143. - `"one"`:One
  144. - `"knowledge_graph"`: Knowledge Graph
  145. - `"email"`: Email
  146. ### Returns
  147. - Success: No value is returned.
  148. - Failure: `Exception`
  149. ### Examples
  150. ```python
  151. from ragflow import RAGFlow
  152. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  153. dataset = rag.list_datasets(name="kb_1")
  154. dataset.update({"embedding_model":"BAAI/bge-zh-v1.5", "parse_method":"manual"})
  155. ```
  156. ---
  157. :::tip API GROUPING
  158. File Management within Knowledge Base
  159. :::
  160. ## Upload documents
  161. ```python
  162. DataSet.upload_documents(document_list: list[dict])
  163. ```
  164. Updloads documents to the current knowledge base.
  165. ### Parameters
  166. #### document_list
  167. A list of dictionaries representing the documents to upload, each containing the following keys:
  168. - `"name"`: (Optional) File path to the document to upload.
  169. Ensure that each file path has a suffix.
  170. - `"blob"`: (Optional) The document to upload in binary format.
  171. ### Returns
  172. - Success: No value is returned.
  173. - Failure: `Exception`
  174. ### Examples
  175. ```python
  176. dataset = rag.create_dataset(name="kb_name")
  177. dataset.upload_documents([{name="1.txt", blob="123"}, ...])
  178. ```
  179. ---
  180. ## Update document
  181. ```python
  182. Document.update(update_message:dict)
  183. ```
  184. Updates configurations for the current document.
  185. ### Parameters
  186. #### update_message: `dict`
  187. only `name`, `parser_config`, and `parser_method` can be changed
  188. ### Returns
  189. - Success: No value is returned.
  190. - Failure: `Exception`
  191. ### Examples
  192. ```python
  193. from ragflow import RAGFlow
  194. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  195. dataset=rag.list_datasets(id='id')
  196. dataset=dataset[0]
  197. doc = dataset.list_documents(id="wdfxb5t547d")
  198. doc = doc[0]
  199. doc.update([{"parser_method": "manual"}])
  200. ```
  201. ---
  202. ## Download document
  203. ```python
  204. Document.download() -> bytes
  205. ```
  206. ### Returns
  207. bytes of the document.
  208. ### Examples
  209. ```python
  210. from ragflow import RAGFlow
  211. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  212. ds=rag.list_datasets(id="id")
  213. ds=ds[0]
  214. doc = ds.list_documents(id="wdfxb5t547d")
  215. doc = doc[0]
  216. open("~/ragflow.txt", "wb+").write(doc.download())
  217. print(doc)
  218. ```
  219. ---
  220. ## List documents
  221. ```python
  222. Dataset.list_documents(id:str =None, keywords: str=None, offset: int=0, limit:int = 1024,order_by:str = "create_time", desc: bool = True) -> list[Document]
  223. ```
  224. ### Parameters
  225. #### id
  226. The id of the document to be got
  227. #### keywords
  228. List documents whose name has the given keywords. Defaults to `None`.
  229. #### offset
  230. The beginning number of records for paging. Defaults to `0`.
  231. #### limit
  232. Records number to return, `-1` means all of them. Records number to return, `-1` means all of them.
  233. #### orderby
  234. The field by which the records should be sorted. This specifies the attribute or column used to order the results.
  235. #### desc
  236. A boolean flag indicating whether the sorting should be in descending order.
  237. ### Returns
  238. list[Document]
  239. A document object containing the following attributes:
  240. #### id
  241. Id of the retrieved document. Defaults to `""`.
  242. #### thumbnail
  243. Thumbnail image of the retrieved document. Defaults to `""`.
  244. #### knowledgebase_id
  245. Knowledge base ID related to the document. Defaults to `""`.
  246. #### parser_method
  247. Method used to parse the document. Defaults to `""`.
  248. #### parser_config: `ParserConfig`
  249. Configuration object for the parser. Defaults to `None`.
  250. #### source_type
  251. Source type of the document. Defaults to `""`.
  252. #### type
  253. Type or category of the document. Defaults to `""`.
  254. #### created_by: `str`
  255. Creator of the document. Defaults to `""`.
  256. #### name
  257. Name or title of the document. Defaults to `""`.
  258. #### size: `int`
  259. Size of the document in bytes or some other unit. Defaults to `0`.
  260. #### token_count: `int`
  261. Number of tokens in the document. Defaults to `""`.
  262. #### chunk_count: `int`
  263. Number of chunks the document is split into. Defaults to `0`.
  264. #### progress: `float`
  265. Current processing progress as a percentage. Defaults to `0.0`.
  266. #### progress_msg: `str`
  267. Message indicating current progress status. Defaults to `""`.
  268. #### process_begin_at: `datetime`
  269. Start time of the document processing. Defaults to `None`.
  270. #### process_duation: `float`
  271. Duration of the processing in seconds or minutes. Defaults to `0.0`.
  272. ### Examples
  273. ```python
  274. from ragflow import RAGFlow
  275. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  276. dataset = rag.create_dataset(name="kb_1")
  277. filename1 = "~/ragflow.txt"
  278. blob=open(filename1 , "rb").read()
  279. list_files=[{"name":filename1,"blob":blob}]
  280. dataset.upload_documents(list_files)
  281. for d in dataset.list_documents(keywords="rag", offset=0, limit=12):
  282. print(d)
  283. ```
  284. ---
  285. ## Delete documents
  286. ```python
  287. DataSet.delete_documents(ids: list[str] = None)
  288. ```
  289. ### Returns
  290. - Success: No value is returned.
  291. - Failure: `Exception`
  292. ### Examples
  293. ```python
  294. from ragflow import RAGFlow
  295. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  296. ds = rag.list_datasets(name="kb_1")
  297. ds = ds[0]
  298. ds.delete_documents(ids=["id_1","id_2"])
  299. ```
  300. ---
  301. ## Parse and stop parsing document
  302. ```python
  303. DataSet.async_parse_documents(document_ids:list[str]) -> None
  304. DataSet.async_cancel_parse_documents(document_ids:list[str])-> None
  305. ```
  306. ### Parameters
  307. #### document_ids: `list[str]`
  308. The ids of the documents to be parsed
  309. ????????????????????????????????????????????????????
  310. ### Returns
  311. - Success: No value is returned.
  312. - Failure: `Exception`
  313. ### Examples
  314. ```python
  315. #documents parse and cancel
  316. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  317. ds = rag.create_dataset(name="dataset_name")
  318. documents = [
  319. {'name': 'test1.txt', 'blob': open('./test_data/test1.txt',"rb").read()},
  320. {'name': 'test2.txt', 'blob': open('./test_data/test2.txt',"rb").read()},
  321. {'name': 'test3.txt', 'blob': open('./test_data/test3.txt',"rb").read()}
  322. ]
  323. ds.upload_documents(documents)
  324. documents=ds.list_documents(keywords="test")
  325. ids=[]
  326. for document in documents:
  327. ids.append(document.id)
  328. ds.async_parse_documents(ids)
  329. print("Async bulk parsing initiated")
  330. ds.async_cancel_parse_documents(ids)
  331. print("Async bulk parsing cancelled")
  332. ```
  333. ---
  334. ## List chunks
  335. ```python
  336. Document.list_chunks(keywords: str = None, offset: int = 0, limit: int = -1, id : str = None) -> list[Chunk]
  337. ```
  338. ### Parameters
  339. - `keywords`: `str`
  340. List chunks whose name has the given keywords
  341. default: `None`
  342. - `offset`: `int`
  343. The beginning number of records for paging
  344. default: `1`
  345. - `limit`: `int`
  346. Records number to return
  347. default: `30`
  348. - `id`: `str`
  349. The ID of the chunk to be retrieved
  350. default: `None`
  351. ### Returns
  352. list[chunk]
  353. ### Examples
  354. ```python
  355. from ragflow import RAGFlow
  356. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  357. ds = rag.list_datasets("123")
  358. ds = ds[0]
  359. ds.async_parse_documents(["wdfxb5t547d"])
  360. for c in doc.list_chunks(keywords="rag", offset=0, limit=12):
  361. print(c)
  362. ```
  363. ## Add chunk
  364. ```python
  365. Document.add_chunk(content:str) -> Chunk
  366. ```
  367. ### Parameters
  368. #### content: `str`, *Required*
  369. Contains the main text or information of the chunk.
  370. #### important_keywords :`list[str]`
  371. list the key terms or phrases that are significant or central to the chunk's content.
  372. ### Returns
  373. chunk
  374. ### Examples
  375. ```python
  376. from ragflow import RAGFlow
  377. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  378. ds = rag.list_datasets(id="123")
  379. ds = ds[0]
  380. doc = ds.list_documents(id="wdfxb5t547d")
  381. doc = doc[0]
  382. chunk = doc.add_chunk(content="xxxxxxx")
  383. ```
  384. ---
  385. ## Delete chunk
  386. ```python
  387. Document.delete_chunks(chunk_ids: list[str])
  388. ```
  389. ### Parameters
  390. #### chunk_ids:`list[str]`
  391. The list of chunk_id
  392. ### Returns
  393. - Success: No value is returned.
  394. - Failure: `Exception`
  395. ### Examples
  396. ```python
  397. from ragflow import RAGFlow
  398. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  399. ds = rag.list_datasets(id="123")
  400. ds = ds[0]
  401. doc = ds.list_documents(id="wdfxb5t547d")
  402. doc = doc[0]
  403. chunk = doc.add_chunk(content="xxxxxxx")
  404. doc.delete_chunks(["id_1","id_2"])
  405. ```
  406. ---
  407. ## Update chunk
  408. ```python
  409. Chunk.update(update_message: dict)
  410. ```
  411. ### Parameters
  412. - `content`: `str`
  413. Contains the main text or information of the chunk
  414. - `important_keywords`: `list[str]`
  415. List the key terms or phrases that are significant or central to the chunk's content
  416. - `available`: `int`
  417. Indicating the availability status, `0` means unavailable and `1` means available
  418. ### Returns
  419. - Success: No value is returned.
  420. - Failure: `Exception`
  421. ### Examples
  422. ```python
  423. from ragflow import RAGFlow
  424. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  425. ds = rag.list_datasets(id="123")
  426. ds = ds[0]
  427. doc = ds.list_documents(id="wdfxb5t547d")
  428. doc = doc[0]
  429. chunk = doc.add_chunk(content="xxxxxxx")
  430. chunk.update({"content":"sdfx...})
  431. ```
  432. ---
  433. ## Retrieval
  434. ```python
  435. RAGFlow.retrieve(question:str="", datasets:list[str]=None, document=list[str]=None, offset:int=1, limit:int=30, similarity_threshold:float=0.2, vector_similarity_weight:float=0.3, top_k:int=1024,rerank_id:str=None,keyword:bool=False,higlight:bool=False) -> list[Chunk]
  436. ```
  437. ### Parameters
  438. #### question: `str`, *Required*
  439. The user query or query keywords. Defaults to `""`.
  440. #### datasets: `list[Dataset]`, *Required*
  441. The scope of datasets.
  442. #### document: `list[Document]`
  443. The scope of document. `None` means no limitation. Defaults to `None`.
  444. #### offset: `int`
  445. The beginning point of retrieved records. Defaults to `0`.
  446. #### limit: `int`
  447. The maximum number of records needed to return. Defaults to `6`.
  448. #### Similarity_threshold: `float`
  449. The minimum similarity score. Defaults to `0.2`.
  450. #### similarity_threshold_weight: `float`
  451. The weight of vector cosine similarity, 1 - x is the term similarity weight. Defaults to `0.3`.
  452. #### top_k: `int`
  453. Number of records engaged in vector cosine computaton. Defaults to `1024`.
  454. #### rerank_id:`str`
  455. ID of the rerank model. Defaults to `None`.
  456. #### keyword:`bool`
  457. Indicating whether keyword-based matching is enabled (True) or disabled (False).
  458. #### highlight:`bool`
  459. Specifying whether to enable highlighting of matched terms in the results (True) or not (False).
  460. ### Returns
  461. list[Chunk]
  462. ### Examples
  463. ```python
  464. from ragflow import RAGFlow
  465. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  466. ds = rag.list_datasets(name="ragflow")
  467. ds = ds[0]
  468. name = 'ragflow_test.txt'
  469. path = './test_data/ragflow_test.txt'
  470. rag.create_document(ds, name=name, blob=open(path, "rb").read())
  471. doc = ds.list_documents(name=name)
  472. doc = doc[0]
  473. ds.async_parse_documents([doc.id])
  474. for c in rag.retrieve(question="What's ragflow?",
  475. datasets=[ds.id], documents=[doc.id],
  476. offset=1, limit=30, similarity_threshold=0.2,
  477. vector_similarity_weight=0.3,
  478. top_k=1024
  479. ):
  480. print(c)
  481. ```
  482. ---
  483. :::tip API GROUPING
  484. Chat Assistant Management
  485. :::
  486. ## Create chat assistant
  487. ```python
  488. RAGFlow.create_chat(
  489. name: str = "assistant",
  490. avatar: str = "path",
  491. knowledgebases: list[DataSet] = [],
  492. llm: Chat.LLM = None,
  493. prompt: Chat.Prompt = None
  494. ) -> Chat
  495. ```
  496. Creates a chat assistant.
  497. ### Returns
  498. - Success: A `Chat` object representing the chat assistant.
  499. - Failure: `Exception`
  500. #### name: `str`
  501. The name of the chat assistant. Defaults to `"assistant"`.
  502. #### avatar: `str`
  503. Base64 encoding of the avatar. Defaults to `""`.
  504. #### knowledgebases: `list[str]`
  505. The associated knowledge bases. Defaults to `["kb1"]`.
  506. #### llm: `LLM`
  507. 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.
  508. - **model_name**, `str`
  509. The chat model name. If it is `None`, the user's default chat model will be returned.
  510. - **temperature**, `float`
  511. Controls the randomness of the model's predictions. A lower temperature increases the model's conficence in its responses; a higher temperature increases creativity and diversity. Defaults to `0.1`.
  512. - **top_p**, `float`
  513. 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`
  514. - **presence_penalty**, `float`
  515. This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
  516. - **frequency penalty**, `float`
  517. Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
  518. - **max_token**, `int`
  519. This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words). Defaults to `512`.
  520. #### Prompt: `str`
  521. Instructions for the LLM to follow.
  522. - `"similarity_threshold"`: `float` A similarity score to evaluate distance between two lines of text. It's weighted keywords similarity and vector cosine similarity. If the similarity between query and chunk is less than this threshold, the chunk will be filtered out. Defaults to `0.2`.
  523. - `"keywords_similarity_weight"`: `float` It's weighted keywords similarity and vector cosine similarity or rerank score (0~1). Defaults to `0.7`.
  524. - `"top_n"`: `int` Not all the chunks whose similarity score is above the 'similarity threshold' will be feed to LLMs. LLM can only see these 'Top N' chunks. Defaults to `8`.
  525. - `"variables"`: `list[dict[]]` If you use dialog APIs, the variables might help you chat with your clients with different strategies. The variables are used to fill in the 'System' part in prompt in order to give LLM a hint. The 'knowledge' is a very special variable which will be filled-in with the retrieved chunks. All the variables in 'System' should be curly bracketed. Defaults to `[{"key": "knowledge", "optional": True}]`
  526. - `"rerank_model"`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  527. - `"empty_response"`: `str` If nothing is retrieved in the knowledge base for the user's question, this will be used as the response. To allow the LLM to improvise when nothing is retrieved, leave this blank. Defaults to `None`.
  528. - `"opener"`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  529. - `"show_quote`: `bool` Indicates whether the source of text should be displayed Defaults to `True`.
  530. - `"prompt"`: `str` The prompt content. Defaults to `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.
  531. Here is the knowledge base:
  532. {knowledge}
  533. The above is the knowledge base.`.
  534. ### Examples
  535. ```python
  536. from ragflow import RAGFlow
  537. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  538. knowledge_base = rag.list_datasets(name="kb_1")
  539. assistant = rag.create_chat("Miss R", knowledgebases=knowledge_base)
  540. ```
  541. ---
  542. ## Update chat
  543. ```python
  544. Chat.update(update_message: dict)
  545. ```
  546. Updates the current chat assistant.
  547. ### Parameters
  548. #### update_message: `dict[str, Any]`, *Required*
  549. - `"name"`: `str` The name of the chat assistant to update.
  550. - `"avatar"`: `str` Base64 encoding of the avatar. Defaults to `""`
  551. - `"knowledgebases"`: `list[str]` Knowledge bases to update.
  552. - `"llm"`: `dict` The LLM settings:
  553. - `"model_name"`, `str` The chat model name.
  554. - `"temperature"`, `float` Controls the randomness of the model's predictions.
  555. - `"top_p"`, `float` Also known as “nucleus sampling”, this parameter sets a threshold to select a smaller set of words to sample from.
  556. - `"presence_penalty"`, `float` This discourages the model from repeating the same information by penalizing words that have appeared in the conversation.
  557. - `"frequency penalty"`, `float` Similar to presence penalty, this reduces the model’s tendency to repeat the same words.
  558. - `"max_token"`, `int` This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words).
  559. - `"prompt"` : Instructions for the LLM to follow.
  560. - `"similarity_threshold"`: `float` A score to evaluate distance between two lines of text. It's weighted keywords similarity and vector cosine similarity. If the similarity between query and chunk is less than this threshold, the chunk will be filtered out. Defaults to `0.2`.
  561. - `"keywords_similarity_weight"`: `float` It's weighted keywords similarity and vector cosine similarity or rerank score (0~1). Defaults to `0.7`.
  562. - `"top_n"`: `int` Not all the chunks whose similarity score is above the 'similarity threshold' will be feed to LLMs. LLM can only see these 'Top N' chunks. Defaults to `8`.
  563. - `"variables"`: `list[dict[]]` If you use dialog APIs, the variables might help you chat with your clients with different strategies. The variables are used to fill in the 'System' part in prompt in order to give LLM a hint. The 'knowledge' is a very special variable which will be filled-in with the retrieved chunks. All the variables in 'System' should be curly bracketed. Defaults to `[{"key": "knowledge", "optional": True}]`
  564. - `"rerank_model"`: `str` If it is not specified, vector cosine similarity will be used; otherwise, reranking score will be used. Defaults to `""`.
  565. - `"empty_response"`: `str` If nothing is retrieved in the knowledge base for the user's question, this will be used as the response. To allow the LLM to improvise when nothing is retrieved, leave this blank. Defaults to `None`.
  566. - `"opener"`: `str` The opening greeting for the user. Defaults to `"Hi! I am your assistant, can I help you?"`.
  567. - `"show_quote`: `bool` Indicates whether the source of text should be displayed Defaults to `True`.
  568. - `"prompt"`: `str` The prompt content. Defaults to `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.
  569. Here is the knowledge base:
  570. {knowledge}
  571. The above is the knowledge base.`.
  572. ### Returns
  573. - Success: No value is returned.
  574. - Failure: `Exception`
  575. ### Examples
  576. ```python
  577. from ragflow import RAGFlow
  578. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  579. knowledge_base = rag.list_datasets(name="kb_1")
  580. assistant = rag.create_chat("Miss R", knowledgebases=knowledge_base)
  581. assistant.update({"name": "Stefan", "llm": {"temperature": 0.8}, "prompt": {"top_n": 8}})
  582. ```
  583. ---
  584. ## Delete chats
  585. Deletes specified chat assistants.
  586. ```python
  587. RAGFlow.delete_chats(ids: list[str] = None)
  588. ```
  589. ### Parameters
  590. #### ids
  591. IDs of the chat assistants to delete. If not specified, all chat assistants will be deleted.
  592. ### Returns
  593. - Success: No value is returned.
  594. - Failure: `Exception`
  595. ### Examples
  596. ```python
  597. from ragflow import RAGFlow
  598. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  599. rag.delete_chats(ids=["id_1","id_2"])
  600. ```
  601. ---
  602. ## List chats
  603. ```python
  604. RAGFlow.list_chats(
  605. page: int = 1,
  606. page_size: int = 1024,
  607. orderby: str = "create_time",
  608. desc: bool = True,
  609. id: str = None,
  610. name: str = None
  611. ) -> list[Chat]
  612. ```
  613. ### Parameters
  614. #### page
  615. Specifies the page on which the records will be displayed. Defaults to `1`.
  616. #### page_size
  617. The number of records on each page. Defaults to `1024`.
  618. #### order_by
  619. The attribute by which the results are sorted. Defaults to `"create_time"`.
  620. #### desc
  621. Indicates whether to sort the results in descending order. Defaults to `True`.
  622. #### id: `string`
  623. The ID of the chat to retrieve. Defaults to `None`.
  624. #### name: `string`
  625. The name of the chat to retrieve. Defaults to `None`.
  626. ### Returns
  627. - Success: A list of `Chat` objects.
  628. - Failure: `Exception`.
  629. ### Examples
  630. ```python
  631. from ragflow import RAGFlow
  632. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  633. for assistant in rag.list_chats():
  634. print(assistant)
  635. ```
  636. ---
  637. :::tip API GROUPING
  638. Chat-session APIs
  639. :::
  640. ## Create session
  641. ```python
  642. Chat.create_session(name: str = "New session") -> Session
  643. ```
  644. Creates a chat session.
  645. ### Parameters
  646. #### name
  647. The name of the chat session to create.
  648. ### Returns
  649. - Success: A `Session` object containing the following attributes:
  650. - `id`: `str` The auto-generated unique identifier of the created session.
  651. - `name`: `str` The name of the created session.
  652. - `message`: `list[Message]` The messages of the created session assistant. Default: `[{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]`
  653. - `chat_id`: `str` The ID of the associated chat assistant.
  654. - Failure: `Exception`
  655. ### Examples
  656. ```python
  657. from ragflow import RAGFlow
  658. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  659. assistant = rag.list_chats(name="Miss R")
  660. assistant = assistant[0]
  661. session = assistant.create_session()
  662. ```
  663. ## Update session
  664. ```python
  665. Session.update(update_message: dict)
  666. ```
  667. Updates the current session.
  668. ### Parameters
  669. #### update_message: `dict[str, Any]`, *Required*
  670. - `"name"`: `str` The name of the session to update.
  671. ### Returns
  672. - Success: No value is returned.
  673. - Failure: `Exception`
  674. ### Examples
  675. ```python
  676. from ragflow import RAGFlow
  677. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  678. assistant = rag.list_chats(name="Miss R")
  679. assistant = assistant[0]
  680. session = assistant.create_session("session_name")
  681. session.update({"name": "updated_name"})
  682. ```
  683. ---
  684. ## Chat
  685. ```python
  686. Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
  687. ```
  688. Asks a question to start a conversation.
  689. ### Parameters
  690. #### question *Required*
  691. The question to start an AI chat. Defaults to `None`.
  692. #### stream
  693. Indicates whether to output responses in a streaming way:
  694. - `True`: Enable streaming.
  695. - `False`: (Default) Disable streaming.
  696. ### Returns
  697. - A `Message` object containing the response to the question if `stream` is set to `False`
  698. - An iterator containing multiple `message` objects (`iter[Message]`) if `stream` is set to `True`
  699. The following shows the attributes of a `Message` object:
  700. #### id: `str`
  701. The auto-generated message ID.
  702. #### content: `str`
  703. The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
  704. #### reference: `list[Chunk]`
  705. A list of `Chunk` objects representing references to the message, each containing the following attributes:
  706. - **id**: `str`
  707. The chunk ID.
  708. - **content**: `str`
  709. The content of the chunk.
  710. - **image_id**: `str`
  711. The ID of the snapshot of the chunk.
  712. - **document_id**: `str`
  713. The ID of the referenced document.
  714. - **document_name**: `str`
  715. The name of the referenced document.
  716. - **position**: `list[str]`
  717. The location information of the chunk within the referenced document.
  718. - **knowledgebase_id**: `str`
  719. The ID of the knowledge base to which the referenced document belongs.
  720. - **similarity**: `float`
  721. A composite similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity.
  722. - **vector_similarity**: `float`
  723. A vector similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between vector embeddings.
  724. - **term_similarity**: `float`
  725. A keyword similarity score of the chunk ranging from `0` to `1`, with a higher value indicating greater similarity between keywords.
  726. ### Examples
  727. ```python
  728. from ragflow import RAGFlow
  729. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  730. assistant = rag.list_chats(name="Miss R")
  731. assistant = assistant[0]
  732. session = assistant.create_session()
  733. print("\n==================== Miss R =====================\n")
  734. print(assistant.get_prologue())
  735. while True:
  736. question = input("\n==================== User =====================\n> ")
  737. print("\n==================== Miss R =====================\n")
  738. cont = ""
  739. for ans in session.ask(question, stream=True):
  740. print(answer.content[len(cont):], end='', flush=True)
  741. cont = answer.content
  742. ```
  743. ---
  744. ## List sessions
  745. ```python
  746. Chat.list_sessions(
  747. page: int = 1,
  748. page_size: int = 1024,
  749. orderby: str = "create_time",
  750. desc: bool = True,
  751. id: str = None,
  752. name: str = None
  753. ) -> list[Session]
  754. ```
  755. Lists sessions associated with the current chat assistant.
  756. ### Parameters
  757. #### page
  758. Specifies the page on which records will be displayed. Defaults to `1`.
  759. #### page_size
  760. The number of records on each page. Defaults to `1024`.
  761. #### orderby
  762. The field by which the records should be sorted. This specifies the attribute or column used to sort the results. Defaults to `"create_time"`.
  763. #### desc
  764. Whether the sorting should be in descending order. Defaults to `True`.
  765. #### id
  766. The ID of the chat session to retrieve. Defaults to `None`.
  767. #### name
  768. The name of the chat to retrieve. Defaults to `None`.
  769. ### Returns
  770. - Success: A list of `Session` objects associated with the current chat assistant.
  771. - Failure: `Exception`.
  772. ### Examples
  773. ```python
  774. from ragflow import RAGFlow
  775. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  776. assistant = rag.list_chats(name="Miss R")
  777. assistant = assistant[0]
  778. for session in assistant.list_sessions():
  779. print(session)
  780. ```
  781. ---
  782. ## Delete sessions
  783. ```python
  784. Chat.delete_sessions(ids:list[str] = None)
  785. ```
  786. Deletes specified sessions or all sessions associated with the current chat assistant.
  787. ### Parameters
  788. #### ids
  789. IDs of the sessions to delete. If not specified, all sessions associated with the current chat assistant will be deleted.
  790. ### Returns
  791. - Success: No value is returned.
  792. - Failure: `Exception`
  793. ### Examples
  794. ```python
  795. from ragflow import RAGFlow
  796. rag = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  797. assistant = rag.list_chats(name="Miss R")
  798. assistant = assistant[0]
  799. assistant.delete_sessions(ids=["id_1","id_2"])
  800. ```