writinwaters commited on
Commit
d607735
·
1 Parent(s): ab13701

Initial draft of Python APIs (#2719)

Browse files

### What problem does this PR solve?



### Type of change

- [x] Documentation Update

Files changed (1) hide show
  1. api/python_api_reference.md +1227 -0
api/python_api_reference.md ADDED
@@ -0,0 +1,1227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DRAFT Python API Reference
2
+
3
+ :::tip NOTE
4
+ Knowledgebase APIs
5
+ :::
6
+
7
+ ## Create knowledge base
8
+
9
+ ```python
10
+ RAGFlow.create_dataset(
11
+ name: str,
12
+ avatar: str = "",
13
+ description: str = "",
14
+ language: str = "English",
15
+ permission: str = "me",
16
+ document_count: int = 0,
17
+ chunk_count: int = 0,
18
+ parse_method: str = "naive",
19
+ parser_config: DataSet.ParserConfig = None
20
+ ) -> DataSet
21
+ ```
22
+
23
+ Creates a knowledge base (dataset).
24
+
25
+ ### Parameters
26
+
27
+ #### name: `str`, *Required*
28
+
29
+ The unique name of the dataset to create. It must adhere to the following requirements:
30
+
31
+ - Permitted characters include:
32
+ - English letters (a-z, A-Z)
33
+ - Digits (0-9)
34
+ - "_" (underscore)
35
+ - Must begin with an English letter or underscore.
36
+ - Maximum 65,535 characters.
37
+ - Case-insensitive.
38
+
39
+ #### avatar: `str`
40
+
41
+ The url or ???????????????????????? path to the avatar image associated with the created dataset. Defaults to `""`
42
+
43
+ #### tenant_id: `str` ?????????????????
44
+
45
+ The id of the tenant associated with the created dataset is used to identify different users. Defaults to `None`.
46
+
47
+ - If creating a dataset, tenant_id must not be provided.
48
+ - If updating a dataset, tenant_id can't be changed.
49
+
50
+ #### description: `str`
51
+
52
+ The description of the created dataset. Defaults to `""`.
53
+
54
+ #### language: `str`
55
+
56
+ The language setting of the created dataset. Defaults to `"English"`. ????????????
57
+
58
+ #### embedding_model: `str` ????????????????
59
+
60
+ The specific model or algorithm used by the dataset to generate vector embeddings. Defaults to `""`.
61
+
62
+ - If creating a dataset, embedding_model must not be provided.
63
+ - If updating a dataset, embedding_model can't be changed.
64
+
65
+ #### permission: `str`
66
+
67
+ Specify who can operate on the dataset. Defaults to `"me"`.
68
+
69
+ #### document_count: `int`
70
+
71
+ The number of documents associated with the dataset. Defaults to `0`.
72
+
73
+ - If updating a dataset, `document_count` can't be changed.
74
+
75
+ #### chunk_count: `int`
76
+
77
+ The number of data chunks generated or processed by the created dataset. Defaults to `0`.
78
+
79
+ - If updating a dataset, chunk_count can't be changed.
80
+
81
+ #### parse_method, `str`
82
+
83
+ The method used by the dataset to parse and process data.
84
+
85
+ - If updating parse_method in a dataset, chunk_count must be greater than 0. Defaults to `"naive"`.
86
+
87
+ #### parser_config, `Dataset.ParserConfig`
88
+
89
+ The configuration settings for the parser used by the dataset.
90
+
91
+ ### Returns
92
+
93
+ - Success: An `infinity.local_infinity.table.LocalTable` object in Python module mode or an `infinity.remote_thrift.table.RemoteTable` object in client-server mode.
94
+ - Failure: `InfinityException`
95
+ - `error_code`: `int` A non-zero value indicating a specific error condition.
96
+ - `error_msg`: `str` A message providing additional details about the error.
97
+
98
+ ### Examples
99
+
100
+ ```python
101
+ from ragflow import RAGFlow
102
+
103
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
104
+ ds = rag.create_dataset(name="kb_1")
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Delete knowledge base
110
+
111
+ ```python
112
+ DataSet.delete() -> bool
113
+ ```
114
+
115
+ Deletes a knowledge base.
116
+
117
+ ### Returns
118
+
119
+ `bool`
120
+
121
+ description:the case of updating an dateset, `True` or `False`.
122
+
123
+ ### Examples
124
+
125
+ ```python
126
+ from ragflow import RAGFlow
127
+
128
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
129
+ ds = rag.create_dataset(name="kb_1")
130
+ ds.delete()
131
+ ```
132
+
133
+ ---
134
+
135
+ ## List knowledge bases
136
+
137
+ ```python
138
+ RAGFlow.list_datasets(
139
+ page: int = 1,
140
+ page_size: int = 1024,
141
+ orderby: str = "create_time",
142
+ desc: bool = True
143
+ ) -> List[DataSet]
144
+ ```
145
+
146
+ Lists all knowledge bases in the RAGFlow system.
147
+
148
+ ### Parameters
149
+
150
+ #### page: `int`
151
+
152
+ The current page number to retrieve from the paginated data. This parameter determines which set of records will be fetched. Defaults to `1`.
153
+
154
+ #### page_size: `int`
155
+
156
+ The number of records to retrieve per page. This controls how many records will be included in each page. Defaults to `1024`.
157
+
158
+ #### order_by: `str`
159
+
160
+ The field by which the records should be sorted. This specifies the attribute or column used to order the results. Defaults to `"create_time"`.
161
+
162
+ #### desc: `bool`
163
+
164
+ Whether the sorting should be in descending order. Defaults to `True`.
165
+
166
+ ### Returns
167
+
168
+ ```python
169
+ List[DataSet]
170
+ description:the list of datasets.
171
+ ```
172
+
173
+ ### Examples
174
+
175
+ ```python
176
+ from ragflow import RAGFlow
177
+
178
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
179
+ for ds in rag.list_datasets():
180
+ print(ds)
181
+ ```
182
+
183
+ ---
184
+
185
+ ## Retrieve knowledge base
186
+
187
+ ```python
188
+ RAGFlow.get_dataset(
189
+ id: str = None,
190
+ name: str = None
191
+ ) -> DataSet
192
+ ```
193
+
194
+ Retrieves a knowledge base by name.
195
+
196
+ ### Parameters
197
+
198
+ #### name: `str`
199
+
200
+ The name of the dataset to be got. If `id` is not provided, `name` is required.
201
+
202
+ #### id: `str`
203
+
204
+ The id of the dataset to be got. If `name` is not provided, `id` is required.
205
+
206
+ ### Returns
207
+
208
+ ```python
209
+ DataSet
210
+ description: dataset object
211
+ ```
212
+
213
+ ### Examples
214
+
215
+ ```python
216
+ from ragflow import RAGFlow
217
+
218
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
219
+ ds = rag.get_dataset(name="ragflow")
220
+ print(ds)
221
+ ```
222
+
223
+ ---
224
+
225
+ ## Save knowledge base configurations
226
+
227
+ ```python
228
+ DataSet.save() -> bool
229
+ ```
230
+
231
+ ### Returns
232
+
233
+ ```python
234
+ bool
235
+ description:the case of updating an dateset, True or False.
236
+ ```
237
+
238
+ ### Examples
239
+
240
+ ```python
241
+ from ragflow import RAGFlow
242
+
243
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
244
+ ds = rag.get_dataset(name="kb_1")
245
+ ds.parse_method = "manual"
246
+ ds.save()
247
+ ```
248
+
249
+ ---
250
+
251
+ :::tip API GROUPING
252
+ File management inside knowledge base
253
+ :::
254
+
255
+ ## Upload document
256
+
257
+ ```python
258
+ RAGFLOW.upload_document(ds:DataSet, name:str, blob:bytes)-> bool
259
+ ```
260
+
261
+ ### Parameters
262
+
263
+ #### ds
264
+
265
+ #### name
266
+
267
+ #### blob
268
+
269
+
270
+
271
+ ### Returns
272
+
273
+
274
+ ### Examples
275
+
276
+ ---
277
+
278
+ ## Retrieve document
279
+
280
+ ```python
281
+ RAGFlow.get_document(id:str=None,name:str=None) -> Document
282
+ ```
283
+
284
+ ### Parameters
285
+
286
+ #### id: `str`, *Required*
287
+
288
+ ID of the document to retrieve.
289
+
290
+ #### name: `str`
291
+
292
+ Name or title of the document.
293
+
294
+ ### Returns
295
+
296
+ A document object containing the following attributes:
297
+
298
+ #### id: `str`
299
+
300
+ Id of the retrieved document. Defaults to `""`.
301
+
302
+ #### thumbnail: `str`
303
+
304
+ Thumbnail image of the retrieved document. Defaults to `""`.
305
+
306
+ #### knowledgebase_id: `str`
307
+
308
+ Knowledge base ID related to the document. Defaults to `""`.
309
+
310
+ #### parser_method: `str`
311
+
312
+ Method used to parse the document. Defaults to `""`.
313
+
314
+ #### parser_config: `ParserConfig`
315
+
316
+ Configuration object for the parser. Defaults to `None`.
317
+
318
+ #### source_type: `str`
319
+
320
+ Source type of the document. Defaults to `""`.
321
+
322
+ #### type: `str`
323
+
324
+ Type or category of the document. Defaults to `""`.
325
+
326
+ #### created_by: `str`
327
+
328
+ Creator of the document. Defaults to `""`.
329
+
330
+ #### name: `str`
331
+ string
332
+ ''
333
+ Name or title of the document. Defaults to `""`.
334
+
335
+ #### size: `int`
336
+
337
+ Size of the document in bytes or some other unit. Defaults to `0`.
338
+
339
+ #### token_count: `int`
340
+
341
+ Number of tokens in the document. Defaults to `""`.
342
+
343
+ #### chunk_count: `int`
344
+
345
+ Number of chunks the document is split into. Defaults to `0`.
346
+
347
+ #### progress: `float`
348
+
349
+ Current processing progress as a percentage. Defaults to `0.0`.
350
+
351
+ #### progress_msg: `str`
352
+
353
+ Message indicating current progress status. Defaults to `""`.
354
+
355
+ #### process_begin_at: `datetime`
356
+
357
+ Start time of the document processing. Defaults to `None`.
358
+
359
+ #### process_duation: `float`
360
+
361
+ Duration of the processing in seconds or minutes. Defaults to `0.0`.
362
+
363
+ ### Examples
364
+
365
+ ```python
366
+ from ragflow import RAGFlow
367
+
368
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
369
+ doc = rag.get_document(id="wdfxb5t547d",name='testdocument.txt')
370
+ print(doc)
371
+ ```
372
+
373
+ ---
374
+
375
+ ## Save document settings
376
+
377
+ ```python
378
+ Document.save() -> bool
379
+ ```
380
+
381
+ ### Returns
382
+
383
+ bool
384
+
385
+ ### Examples
386
+
387
+ ```python
388
+ from ragflow import RAGFlow
389
+
390
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
391
+ doc = rag.get_document(id="wdfxb5t547d")
392
+ doc.parser_method= "manual"
393
+ doc.save()
394
+ ```
395
+
396
+ ---
397
+
398
+ ## Download document
399
+
400
+ ```python
401
+ Document.download() -> bytes
402
+ ```
403
+
404
+ ### Returns
405
+
406
+ bytes of the document.
407
+
408
+ ### Examples
409
+
410
+ ```python
411
+ from ragflow import RAGFlow
412
+
413
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
414
+ doc = rag.get_document(id="wdfxb5t547d")
415
+ open("~/ragflow.txt", "w+").write(doc.download())
416
+ print(doc)
417
+ ```
418
+
419
+ ---
420
+
421
+ ## List documents
422
+
423
+ ```python
424
+ Dataset.list_docs(keywords: str=None, offset: int=0, limit:int = -1) -> List[Document]
425
+ ```
426
+
427
+ ### Parameters
428
+
429
+ #### keywords: `str`
430
+
431
+ List documents whose name has the given keywords. Defaults to `None`.
432
+
433
+ #### offset: `int`
434
+
435
+ The beginning number of records for paging. Defaults to `0`.
436
+
437
+ #### limit: `int`
438
+
439
+ Records number to return, -1 means all of them. Records number to return, -1 means all of them.
440
+
441
+ ### Returns
442
+
443
+ List[Document]
444
+
445
+ ### Examples
446
+
447
+ ```python
448
+ from ragflow import RAGFlow
449
+
450
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
451
+ ds = rag.create_dataset(name="kb_1")
452
+
453
+ filename1 = "~/ragflow.txt"
454
+ rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
455
+
456
+ filename2 = "~/infinity.txt"
457
+ rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
458
+
459
+ for d in ds.list_docs(keywords="rag", offset=0, limit=12):
460
+ print(d)
461
+ ```
462
+
463
+ ---
464
+
465
+ ## Delete documents
466
+
467
+ ```python
468
+ Document.delete() -> bool
469
+ ```
470
+ ### Returns
471
+
472
+ bool
473
+ description: delete success or not
474
+
475
+ ### Examples
476
+
477
+ ```python
478
+ from ragflow import RAGFlow
479
+
480
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
481
+ ds = rag.create_dataset(name="kb_1")
482
+
483
+ filename1 = "~/ragflow.txt"
484
+ rag.create_document(ds, name=filename1 , blob=open(filename1 , "rb").read())
485
+
486
+ filename2 = "~/infinity.txt"
487
+ rag.create_document(ds, name=filename2 , blob=open(filename2 , "rb").read())
488
+ for d in ds.list_docs(keywords="rag", offset=0, limit=12):
489
+ d.delete()
490
+ ```
491
+
492
+ ---
493
+
494
+ ## Parse document
495
+
496
+ ```python
497
+ Document.async_parse() -> None
498
+ RAGFLOW.async_parse_documents() -> None
499
+ ```
500
+
501
+ ### Parameters
502
+
503
+ ????????????????????????????????????????????????????
504
+
505
+ ### Returns
506
+
507
+ ????????????????????????????????????????????????????
508
+
509
+ ### Examples
510
+
511
+ ```python
512
+ #document parse and cancel
513
+ rag = RAGFlow(API_KEY, HOST_ADDRESS)
514
+ ds = rag.create_dataset(name="dataset_name")
515
+ name3 = 'ai.pdf'
516
+ path = 'test_data/ai.pdf'
517
+ rag.create_document(ds, name=name3, blob=open(path, "rb").read())
518
+ doc = rag.get_document(name="ai.pdf")
519
+ doc.async_parse()
520
+ print("Async parsing initiated")
521
+ ```
522
+
523
+ ---
524
+
525
+ ## Cancel document parsing
526
+
527
+ ```python
528
+ rag.async_cancel_parse_documents(ids)
529
+ RAGFLOW.async_cancel_parse_documents()-> None
530
+ ```
531
+
532
+ ### Parameters
533
+
534
+ #### ids, `list[]`
535
+
536
+ ### Returns
537
+
538
+ ?????????????????????????????????????????????????
539
+
540
+ ### Examples
541
+
542
+ ```python
543
+ #documents parse and cancel
544
+ rag = RAGFlow(API_KEY, HOST_ADDRESS)
545
+ ds = rag.create_dataset(name="God5")
546
+ documents = [
547
+ {'name': 'test1.txt', 'path': 'test_data/test1.txt'},
548
+ {'name': 'test2.txt', 'path': 'test_data/test2.txt'},
549
+ {'name': 'test3.txt', 'path': 'test_data/test3.txt'}
550
+ ]
551
+
552
+ # Create documents in bulk
553
+ for doc_info in documents:
554
+ with open(doc_info['path'], "rb") as file:
555
+ created_doc = rag.create_document(ds, name=doc_info['name'], blob=file.read())
556
+ docs = [rag.get_document(name=doc_info['name']) for doc_info in documents]
557
+ ids = [doc.id for doc in docs]
558
+
559
+ rag.async_parse_documents(ids)
560
+ print("Async bulk parsing initiated")
561
+
562
+ for doc in docs:
563
+ for progress, msg in doc.join(interval=5, timeout=10):
564
+ print(f"{doc.name}: Progress: {progress}, Message: {msg}")
565
+
566
+ cancel_result = rag.async_cancel_parse_documents(ids)
567
+ print("Async bulk parsing cancelled")
568
+ ```
569
+
570
+ ---
571
+
572
+ ## Join document
573
+
574
+ ??????????????????
575
+
576
+ ```python
577
+ Document.join(interval=15, timeout=3600) -> iteral[Tuple[float, str]]
578
+ ```
579
+
580
+ ### Parameters
581
+
582
+ #### interval: `int`
583
+
584
+ Time interval in seconds for progress report. Defaults to `15`.
585
+
586
+ #### timeout: `int`
587
+
588
+ Timeout in seconds. Defaults to `3600`.
589
+
590
+ ### Returns
591
+
592
+ iteral[Tuple[float, str]]
593
+
594
+ ## Add chunk
595
+
596
+ ```python
597
+ Document.add_chunk(content:str) -> Chunk
598
+ ```
599
+
600
+ ### Parameters
601
+
602
+ #### content: `str`, *Required*
603
+
604
+ ### Returns
605
+
606
+ chunk
607
+
608
+ ### Examples
609
+
610
+ ```python
611
+ from ragflow import RAGFlow
612
+
613
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
614
+ doc = rag.get_document(id="wdfxb5t547d")
615
+ chunk = doc.add_chunk(content="xxxxxxx")
616
+ ```
617
+
618
+ ---
619
+
620
+ ## Delete chunk
621
+
622
+ ```python
623
+ Chunk.delete() -> bool
624
+ ```
625
+
626
+ ### Returns
627
+
628
+ bool
629
+
630
+ ### Examples
631
+
632
+ ```python
633
+ from ragflow import RAGFlow
634
+
635
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
636
+ doc = rag.get_document(id="wdfxb5t547d")
637
+ chunk = doc.add_chunk(content="xxxxxxx")
638
+ chunk.delete()
639
+ ```
640
+
641
+ ---
642
+
643
+ ## Save chunk contents
644
+
645
+ ```python
646
+ Chunk.save() -> bool
647
+ ```
648
+
649
+ ### Returns
650
+
651
+ bool
652
+
653
+ ### Examples
654
+
655
+ ```python
656
+ from ragflow import RAGFlow
657
+
658
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
659
+ doc = rag.get_document(id="wdfxb5t547d")
660
+ chunk = doc.add_chunk(content="xxxxxxx")
661
+ chunk.content = "sdfx"
662
+ chunk.save()
663
+ ```
664
+
665
+ ---
666
+
667
+ ## Retrieval
668
+
669
+ ```python
670
+ 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]
671
+ ```
672
+
673
+ ### Parameters
674
+
675
+ #### question: `str`, *Required*
676
+
677
+ The user query or query keywords. Defaults to `""`.
678
+
679
+ #### datasets: `List[Dataset]`, *Required*
680
+
681
+ The scope of datasets.
682
+
683
+ #### document: `List[Document]`
684
+
685
+ The scope of document. `None` means no limitation. Defaults to `None`.
686
+
687
+ #### offset: `int`
688
+
689
+ The beginning point of retrieved records. Defaults to `0`.
690
+
691
+ #### limit: `int`
692
+
693
+ The maximum number of records needed to return. Defaults to `6`.
694
+
695
+ #### Similarity_threshold: `float`
696
+
697
+ The minimum similarity score. Defaults to `0.2`.
698
+
699
+ #### similarity_threshold_weight: `float`
700
+
701
+ The weight of vector cosine similarity, 1 - x is the term similarity weight. Defaults to `0.3`.
702
+
703
+ #### top_k: `int`
704
+
705
+ Number of records engaged in vector cosine computaton. Defaults to `1024`.
706
+
707
+ ### Returns
708
+
709
+ List[Chunk]
710
+
711
+ ### Examples
712
+
713
+ ```python
714
+ from ragflow import RAGFlow
715
+
716
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
717
+ ds = rag.get_dataset(name="ragflow")
718
+ name = 'ragflow_test.txt'
719
+ path = 'test_data/ragflow_test.txt'
720
+ rag.create_document(ds, name=name, blob=open(path, "rb").read())
721
+ doc = rag.get_document(name=name)
722
+ doc.async_parse()
723
+ # Wait for parsing to complete
724
+ for progress, msg in doc.join(interval=5, timeout=30):
725
+ print(progress, msg)
726
+ for c in rag.retrieval(question="What's ragflow?",
727
+ datasets=[ds], documents=[doc],
728
+ offset=0, limit=6, similarity_threshold=0.1,
729
+ vector_similarity_weight=0.3,
730
+ top_k=1024
731
+ ):
732
+ print(c)
733
+ ```
734
+
735
+ ---
736
+
737
+ :::tip API GROUPING
738
+ Chat assistant APIs
739
+ :::
740
+
741
+ ## Create assistant
742
+
743
+ ```python
744
+ RAGFlow.create_assistant(
745
+ name: str = "assistant",
746
+ avatar: str = "path",
747
+ knowledgebases: List[DataSet] = ["kb1"],
748
+ llm: Assistant.LLM = None,
749
+ prompt: Assistant.Prompt = None
750
+ ) -> Assistant
751
+ ```
752
+
753
+ ### Returns
754
+
755
+ Assistant object.
756
+
757
+ #### name: `str`
758
+
759
+ The name of the created assistant. Defaults to `"assistant"`.
760
+
761
+ #### avatar: `str`
762
+
763
+ The icon of the created assistant. Defaults to `"path"`.
764
+
765
+ #### knowledgebases: `List[DataSet]`
766
+
767
+ Select knowledgebases associated. Defaults to `["kb1"]`.
768
+
769
+ #### id: `str`
770
+
771
+ The id of the created assistant. Defaults to `""`.
772
+
773
+ #### llm: `LLM`
774
+
775
+ 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.
776
+
777
+ - **model_name**, `str`
778
+ Large language chat model. If it is `None`, it will return the user's default model.
779
+ - **temperature**, `float`
780
+ 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`.
781
+ - **top_p**, `float`
782
+ 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`
783
+ - **presence_penalty**, `float`
784
+ This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
785
+ - **frequency penalty**, `float`
786
+ Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
787
+ - **max_token**, `int`
788
+ This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words). Defaults to `512`.
789
+
790
+ #### Prompt: `str`
791
+
792
+ Instructions you need LLM to follow when LLM answers questions, like character design, answer length and answer language etc.
793
+
794
+ Defaults:
795
+ ```
796
+ 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.
797
+ Here is the knowledge base:
798
+ {knowledge}
799
+ The above is the knowledge base.
800
+ ```
801
+
802
+ ### Examples
803
+
804
+ ```python
805
+ from ragflow import RAGFlow
806
+
807
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
808
+ kb = rag.get_dataset(name="kb_1")
809
+ assi = rag.create_assistant("Miss R", knowledgebases=[kb])
810
+ ```
811
+
812
+ ---
813
+
814
+ ## Save updates to a chat assistant
815
+
816
+ ```python
817
+ Assistant.save() -> bool
818
+ ```
819
+
820
+ ### Returns
821
+
822
+ ```python
823
+ bool
824
+ description:the case of updating an assistant, True or False.
825
+ ```
826
+
827
+ ### Examples
828
+
829
+ ```python
830
+ from ragflow import RAGFlow
831
+
832
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
833
+ kb = rag.get_knowledgebase(name="kb_1")
834
+ assi = rag.create_assistant("Miss R", knowledgebases=[kb])
835
+ assi.llm.temperature = 0.8
836
+ assi.save()
837
+ ```
838
+
839
+ ---
840
+
841
+ ## Delete assistant
842
+
843
+ ```python
844
+ Assistant.delete() -> bool
845
+ ```
846
+
847
+ ### Returns
848
+
849
+ ```python
850
+ bool
851
+ description:the case of deleting an assistant, True or False.
852
+ ```
853
+
854
+ ### Examples
855
+
856
+ ```python
857
+ from ragflow import RAGFlow
858
+
859
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
860
+ kb = rag.get_knowledgebase(name="kb_1")
861
+ assi = rag.create_assistant("Miss R", knowledgebases=[kb])
862
+ assi.delete()
863
+ ```
864
+
865
+ ---
866
+
867
+ ## Retrieve assistant
868
+
869
+ ```python
870
+ RAGFlow.get_assistant(id: str = None, name: str = None) -> Assistant
871
+ ```
872
+
873
+ ### Parameters
874
+
875
+ #### id: `str`
876
+
877
+ ID of the assistant to retrieve. If `name` is not provided, `id` is required.
878
+
879
+ #### name: `str`
880
+
881
+ Name of the assistant to retrieve. If `id` is not provided, `name` is required.
882
+
883
+ ### Returns
884
+
885
+ Assistant object.
886
+
887
+ #### name: `str`
888
+
889
+ The name of the created assistant. Defaults to `"assistant"`.
890
+
891
+ #### avatar: `str`
892
+
893
+ The icon of the created assistant. Defaults to `"path"`.
894
+
895
+ #### knowledgebases: `List[DataSet]`
896
+
897
+ Select knowledgebases associated. Defaults to `["kb1"]`.
898
+
899
+ #### id: `str`
900
+
901
+ The id of the created assistant. Defaults to `""`.
902
+
903
+ #### llm: `LLM`
904
+
905
+ 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.
906
+
907
+ - **model_name**, `str`
908
+ Large language chat model. If it is `None`, it will return the user's default model.
909
+ - **temperature**, `float`
910
+ 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`.
911
+ - **top_p**, `float`
912
+ 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`
913
+ - **presence_penalty**, `float`
914
+ This discourages the model from repeating the same information by penalizing words that have already appeared in the conversation. Defaults to `0.2`.
915
+ - **frequency penalty**, `float`
916
+ Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`.
917
+ - **max_token**, `int`
918
+ This sets the maximum length of the model’s output, measured in the number of tokens (words or pieces of words). Defaults to `512`.
919
+
920
+ #### Prompt: `str`
921
+
922
+ Instructions you need LLM to follow when LLM answers questions, like character design, answer length and answer language etc.
923
+
924
+ Defaults:
925
+ ```
926
+ 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.
927
+ Here is the knowledge base:
928
+ {knowledge}
929
+ The above is the knowledge base.
930
+ ```
931
+
932
+ ### Examples
933
+
934
+ ```python
935
+ from ragflow import RAGFlow
936
+
937
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
938
+ assi = rag.get_assistant(name="Miss R")
939
+ ```
940
+
941
+ ---
942
+
943
+ ## List assistants
944
+
945
+ ```python
946
+ RAGFlow.list_assistants() -> List[Assistant]
947
+ ```
948
+
949
+ ### Returns
950
+
951
+ A list of assistant objects.
952
+
953
+ ### Examples
954
+
955
+ ```python
956
+ from ragflow import RAGFlow
957
+
958
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
959
+ for assi in rag.list_assistants():
960
+ print(assi)
961
+ ```
962
+
963
+ ---
964
+
965
+ :::tip API GROUPING
966
+ Chat-session APIs
967
+ :::
968
+
969
+ ## Create session
970
+
971
+ ```python
972
+ assistant_1.create_session(name: str = "New session") -> Session
973
+ ```
974
+
975
+ ### Returns
976
+
977
+ A `session` object.
978
+
979
+ #### id: `str`
980
+
981
+ The id of the created session is used to identify different sessions.
982
+ - `id` cannot be provided in creating
983
+ - `id` is required in updating
984
+
985
+ #### name: `str`
986
+
987
+ The name of the created session. Defaults to `"New session"`.
988
+
989
+ #### messages: `List[Message]`
990
+
991
+ The messages of the created session.
992
+ - messages cannot be provided.
993
+
994
+ Defaults:
995
+
996
+ ??????????????????????????????????????????????????????????????????????????????????????????????
997
+
998
+ ```
999
+ [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
1000
+ ```
1001
+
1002
+ #### assistant_id: `str`
1003
+
1004
+ The id of associated assistant. Defaults to `""`.
1005
+ - `assistant_id` is required in creating if you use HTTP API.
1006
+
1007
+ ### Examples
1008
+
1009
+ ```python
1010
+ from ragflow import RAGFlow
1011
+
1012
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1013
+ assi = rag.get_assistant(name="Miss R")
1014
+ sess = assi.create_session()
1015
+ ```
1016
+
1017
+ ## Retrieve session
1018
+
1019
+ ```python
1020
+ Assistant.get_session(id: str) -> Session
1021
+ ```
1022
+
1023
+ ### Parameters
1024
+
1025
+ #### id: `str`, *Required*
1026
+
1027
+ ???????????????????????????????
1028
+
1029
+ ### Returns
1030
+
1031
+ ### Returns
1032
+
1033
+ A `session` object.
1034
+
1035
+ #### id: `str`
1036
+
1037
+ The id of the created session is used to identify different sessions.
1038
+ - `id` cannot be provided in creating
1039
+ - `id` is required in updating
1040
+
1041
+ #### name: `str`
1042
+
1043
+ The name of the created session. Defaults to `"New session"`.
1044
+
1045
+ #### messages: `List[Message]`
1046
+
1047
+ The messages of the created session.
1048
+ - messages cannot be provided.
1049
+
1050
+ Defaults:
1051
+
1052
+ ??????????????????????????????????????????????????????????????????????????????????????????????
1053
+
1054
+ ```
1055
+ [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
1056
+ ```
1057
+
1058
+ #### assistant_id: `str`
1059
+
1060
+
1061
+ ???????????????????????????????????????How to get
1062
+
1063
+ The id of associated assistant. Defaults to `""`.
1064
+ - `assistant_id` is required in creating if you use HTTP API.
1065
+
1066
+ ### Examples
1067
+
1068
+ ```python
1069
+ from ragflow import RAGFlow
1070
+
1071
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1072
+ assi = rag.get_assistant(name="Miss R")
1073
+ sess = assi.get_session(id="d5c55d2270dd11ef9bd90242ac120007")
1074
+ ```
1075
+
1076
+ ---
1077
+
1078
+ ## Save session settings
1079
+
1080
+ ```python
1081
+ Session.save() -> bool
1082
+ ```
1083
+
1084
+ ### Returns
1085
+
1086
+ bool
1087
+ description:the case of updating a session, True or False.
1088
+
1089
+ ### Examples
1090
+
1091
+ ```python
1092
+ from ragflow import RAGFlow
1093
+
1094
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1095
+ assi = rag.get_assistant(name="Miss R")
1096
+ sess = assi.get_session(id="d5c55d2270dd11ef9bd90242ac120007")
1097
+ sess.name = "Updated session"
1098
+ sess.save()
1099
+ ```
1100
+
1101
+ ---
1102
+
1103
+ ## Chat
1104
+
1105
+ ```python
1106
+ Session.chat(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
1107
+ ```
1108
+
1109
+ ### Parameters
1110
+
1111
+ #### question: `str`, *Required*
1112
+
1113
+ The question to start an AI chat. Defaults to `None`. ???????????????????
1114
+
1115
+ #### stream: `bool`
1116
+
1117
+ 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.
1118
+
1119
+ #### session_id: `str` ??????????????????
1120
+
1121
+ ### Returns
1122
+
1123
+ [Message, iter[Message]]
1124
+
1125
+ #### id: `str`
1126
+
1127
+ The id of the message. `id` is automatically generated. Defaults to `None`. ???????????????????
1128
+
1129
+ #### content: `str`
1130
+
1131
+ The content of the message. Defaults to `"Hi! I am your assistant, can I help you?"`.
1132
+
1133
+ #### reference: `List[Chunk]`
1134
+
1135
+ The auto-generated reference of the message. Each `chunk` object includes the following attributes:
1136
+
1137
+ - **id**: `str`
1138
+ The id of the chunk. ?????????????????
1139
+ - **content**: `str`
1140
+ The content of the chunk. Defaults to `None`. ?????????????????????
1141
+ - **document_id**: `str`
1142
+ The ID of the document being referenced. Defaults to `""`.
1143
+ - **document_name**: `str`
1144
+ The name of the referenced document being referenced. Defaults to `""`.
1145
+ - **knowledgebase_id**: `str`
1146
+ The id of the knowledge base to which the relevant document belongs. Defaults to `""`.
1147
+ - **image_id**: `str`
1148
+ The id of the image related to the chunk. Defaults to `""`.
1149
+ - **similarity**: `float`
1150
+ 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`. ????????????????????????????????????
1151
+ - **vector_similarity**: `float`
1152
+ 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`. ?????????????????????????????????
1153
+ - **term_similarity**: `float`
1154
+ 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`. ???????????????????
1155
+ - **position**: `List[string]`
1156
+ 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`. ??????????????
1157
+
1158
+ ### Examples
1159
+
1160
+ ```python
1161
+ from ragflow import RAGFlow
1162
+
1163
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1164
+ assi = rag.get_assistant(name="Miss R")
1165
+ sess = assi.create_session()
1166
+
1167
+ print("\n==================== Miss R =====================\n")
1168
+ print(assi.get_prologue())
1169
+
1170
+ while True:
1171
+ question = input("\n==================== User =====================\n> ")
1172
+ print("\n==================== Miss R =====================\n")
1173
+
1174
+ cont = ""
1175
+ for ans in sess.chat(question, stream=True):
1176
+ print(ans.content[len(cont):], end='', flush=True)
1177
+ cont = ans.content
1178
+ ```
1179
+
1180
+ ---
1181
+
1182
+ ## List sessions
1183
+
1184
+ ```python
1185
+ Assistant.list_session() -> List[Session]
1186
+ ```
1187
+
1188
+ ### Returns
1189
+
1190
+ List[Session]
1191
+ description: the List contains information about multiple assistant object, with each dictionary containing information about one assistant.
1192
+
1193
+ ### Examples
1194
+
1195
+ ```python
1196
+ from ragflow import RAGFlow
1197
+
1198
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1199
+ assi = rag.get_assistant(name="Miss R")
1200
+
1201
+ for sess in assi.list_session():
1202
+ print(sess)
1203
+ ```
1204
+
1205
+ ---
1206
+
1207
+ ## Delete session
1208
+
1209
+ ```python
1210
+ Session.delete() -> bool
1211
+ ```
1212
+
1213
+ ### Returns
1214
+
1215
+ bool
1216
+ description:the case of deleting a session, True or False.
1217
+
1218
+ ### Examples
1219
+
1220
+ ```python
1221
+ from ragflow import RAGFlow
1222
+
1223
+ rag = RAGFlow(api_key="xxxxxx", base_url="http://xxx.xx.xx.xxx:9380")
1224
+ assi = rag.get_assistant(name="Miss R")
1225
+ sess = assi.create_session()
1226
+ sess.delete()
1227
+ ```