liuhua liuhua Kevin Hu commited on
Commit
6afa2cc
·
1 Parent(s): 7bc5b54

Fix bugs in api and add examples (#3353)

Browse files

### What problem does this PR solve?

Fix bugs in api.
Add simple examples for api.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)

---------

Co-authored-by: liuhua <[email protected]>
Co-authored-by: Kevin Hu <[email protected]>

api/apps/sdk/dataset.py CHANGED
@@ -487,12 +487,14 @@ def list(tenant_id):
487
  """
488
  id = request.args.get("id")
489
  name = request.args.get("name")
490
- kbs = KnowledgebaseService.query(id=id, name=name, status=1)
491
- if not kbs:
492
- return get_error_data_result(message="The dataset doesn't exist")
493
- for kb in kbs:
494
- if not KnowledgebaseService.accessible(kb_id=kb.id,user_id=tenant_id):
495
- return get_error_data_result(message=f"You don't own the dataset {kb.id}")
 
 
496
  page_number = int(request.args.get("page", 1))
497
  items_per_page = int(request.args.get("page_size", 30))
498
  orderby = request.args.get("orderby", "create_time")
 
487
  """
488
  id = request.args.get("id")
489
  name = request.args.get("name")
490
+ if id:
491
+ kbs = KnowledgebaseService.get_kb_by_id(id,tenant_id)
492
+ if not kbs:
493
+ return get_error_data_result(f"You don't own the dataset {id}")
494
+ if name:
495
+ kbs = KnowledgebaseService.get_kb_by_name(name,tenant_id)
496
+ if not kbs:
497
+ return get_error_data_result(f"You don't own the dataset {name}")
498
  page_number = int(request.args.get("page", 1))
499
  items_per_page = int(request.args.get("page_size", 30))
500
  orderby = request.args.get("orderby", "create_time")
api/db/services/knowledgebase_service.py CHANGED
@@ -189,6 +189,22 @@ class KnowledgebaseService(CommonService):
189
  return False
190
  return True
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  @classmethod
193
  @DB.connection_context()
194
  def accessible4deletion(cls, kb_id, user_id):
 
189
  return False
190
  return True
191
 
192
+ @classmethod
193
+ @DB.connection_context()
194
+ def get_kb_by_id(cls, kb_id, user_id):
195
+ kbs = cls.model.select().join(UserTenant, on=(UserTenant.tenant_id == Knowledgebase.tenant_id)
196
+ ).where(cls.model.id == kb_id, UserTenant.user_id == user_id).paginate(0, 1)
197
+ kbs = kbs.dicts()
198
+ return list(kbs)
199
+
200
+ @classmethod
201
+ @DB.connection_context()
202
+ def get_kb_by_name(cls, kb_name, user_id):
203
+ kbs = cls.model.select().join(UserTenant, on=(UserTenant.tenant_id == Knowledgebase.tenant_id)
204
+ ).where(cls.model.name == kb_name, UserTenant.user_id == user_id).paginate(0, 1)
205
+ kbs = kbs.dicts()
206
+ return list(kbs)
207
+
208
  @classmethod
209
  @DB.connection_context()
210
  def accessible4deletion(cls, kb_id, user_id):
docs/references/http_api_reference.md CHANGED
@@ -187,7 +187,9 @@ curl --request DELETE \
187
  --url http://{address}/api/v1/datasets \
188
  --header 'Content-Type: application/json' \
189
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
190
- --data '{"ids": ["test_1", "test_2"]}'
 
 
191
  ```
192
 
193
  #### Request parameters
@@ -243,7 +245,7 @@ curl --request PUT \
243
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
244
  --data '
245
  {
246
- "name": "updated_dataset",
247
  }'
248
  ```
249
 
@@ -1152,7 +1154,7 @@ curl --request PUT \
1152
  --data '
1153
  {
1154
  "content": "ragflow123",
1155
- "important_keywords": [],
1156
  }'
1157
  ```
1158
 
 
187
  --url http://{address}/api/v1/datasets \
188
  --header 'Content-Type: application/json' \
189
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
190
+ --data '{
191
+ "ids": ["test_1", "test_2"]
192
+ }'
193
  ```
194
 
195
  #### Request parameters
 
245
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
246
  --data '
247
  {
248
+ "name": "updated_dataset"
249
  }'
250
  ```
251
 
 
1154
  --data '
1155
  {
1156
  "content": "ragflow123",
1157
+ "important_keywords": []
1158
  }'
1159
  ```
1160
 
docs/references/python_api_reference.md CHANGED
@@ -1379,7 +1379,7 @@ assistant = assistant[0]
1379
  session = assistant.create_session()
1380
 
1381
  print("\n==================== Miss R =====================\n")
1382
- print(assistant.get_prologue())
1383
 
1384
  while True:
1385
  question = input("\n==================== User =====================\n> ")
 
1379
  session = assistant.create_session()
1380
 
1381
  print("\n==================== Miss R =====================\n")
1382
+ print("Hello. What can I do for you?")
1383
 
1384
  while True:
1385
  question = input("\n==================== User =====================\n> ")
example/http/simple_example.sh ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # Create a dataset
18
+ echo -e "\n-- Create a dataset"
19
+ curl --request POST \
20
+ --url http://localhost:9380/api/v1/datasets \
21
+ --header 'Content-Type: application/json' \
22
+ --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm' \
23
+ --data '{
24
+ "name": "test"
25
+ }'
26
+
27
+ # Update the dataset
28
+ echo -e "\n-- Update the dataset"
29
+ curl --request PUT \
30
+ --url http://localhost:9380/api/v1/datasets/2e898768a0bc11efb46a0242ac120006 \
31
+ --header 'Content-Type: application/json' \
32
+ --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm' \
33
+ --data '
34
+ {
35
+ "name": "updated_dataset"
36
+ }'
37
+
38
+ # List datasets
39
+ echo -e "\n-- List datasets"
40
+ curl --request GET \
41
+ --url http://127.0.0.1:9380/api/v1/datasets \
42
+ --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm'
43
+
44
+ # Delete datasets
45
+ echo -e "\n-- Delete datasets"
46
+ curl --request DELETE \
47
+ --url http://localhost:9380/api/v1/datasets \
48
+ --header 'Content-Type: application/json' \
49
+ --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm' \
50
+ --data '{
51
+ "ids": ["301298b8a0bc11efa0440242ac120006"]
52
+ }'
example/sdk/simple_example.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ '''
18
+ The example is about CRUD operations (Create, Read, Update, Delete) on a dataset.
19
+ '''
20
+
21
+ from ragflow_sdk import RAGFlow
22
+ import sys
23
+
24
+ HOST_ADDRESS = "http://127.0.0.1"
25
+ API_KEY = "ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm"
26
+
27
+ try:
28
+ # create a ragflow instance
29
+ ragflow_instance = RAGFlow(api_key=API_KEY, base_url=HOST_ADDRESS)
30
+
31
+ # crate a dataset instance
32
+ dataset_instance = ragflow_instance.create_dataset(name="dataset_instance")
33
+
34
+ # update the dataset instance
35
+ updated_message = {"name":"updated_dataset"}
36
+ updated_dataset = dataset_instance.update(updated_message)
37
+
38
+ # get the dataset (list datasets)
39
+ dataset_list = ragflow_instance.list_datasets(id=dataset_instance.id)
40
+ dataset_instance_2 = dataset_list[0]
41
+ print(dataset_instance)
42
+ print(dataset_instance_2)
43
+
44
+ # delete the dataset (delete datasets)
45
+ to_be_deleted_datasets = [dataset_instance.id]
46
+ ragflow_instance.delete_datasets(ids=to_be_deleted_datasets)
47
+
48
+ print("test done")
49
+ sys.exit(0)
50
+
51
+ except Exception as e:
52
+ print(str(e))
53
+ sys.exit(-1)
54
+
55
+