-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDify-Knowledge-Python-SDK.py
More file actions
328 lines (295 loc) · 12.5 KB
/
Dify-Knowledge-Python-SDK.py
File metadata and controls
328 lines (295 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import json
import requests
import json
import requests
class DifySdkClient:
def __init__(self, api_key, api_url):
"""
Initialize the SDK client with API key and base URL.
:param api_key: Your API key for authentication.
:param api_url: Base URL of the Dify API.
"""
self.api_key = api_key
self.api_url = api_url
def create_document_from_text(self, dataset_id, document_name, document_text, indexing_technique='high_quality',
process_rule=None):
"""
Create a document from text content.
:param dataset_id: ID of the knowledge base (dataset).
:param document_name: Name of the document.
:param document_text: Content of the document.
:param indexing_technique: Indexing method (high_quality or economy).
:param process_rule: Processing rules for document creation.
:return: Response containing document creation result.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/document/create_by_text"
if process_rule is None:
process_rule = {"mode": "automatic"}
data = {
"name": document_name,
"text": document_text,
"indexing_technique": indexing_technique,
"process_rule": process_rule
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
def create_document_from_file(self, dataset_id, file_path, process_rule=None, original_document_id=None,
indexing_technique='high_quality'):
"""
Create a document by uploading a file.
:param dataset_id: ID of the knowledge base (dataset).
:param file_path: Path to the file to be uploaded.
:param process_rule: Processing rules for document creation.
:param original_document_id: ID of the original document (for updates).
:param indexing_technique: Indexing method (high_quality or economy).
:return: Response containing document creation result.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}/document/create_by_file"
if process_rule is None:
process_rule = {"mode": "automatic"}
data = {
"indexing_technique": indexing_technique,
"process_rule": process_rule
}
if original_document_id:
data['original_document_id'] = original_document_id
data_string = json.dumps(data)
files = {
"data": (None, data_string, 'text/plain'),
"file": open(file_path, "rb")
}
response = requests.post(url, headers=headers, files=files)
response.raise_for_status()
return response.json()
def get_datasets(self, page=1, limit=20):
"""
Retrieve a list of knowledge bases (datasets).
:param page: Page number for pagination.
:param limit: Number of results per page (default: 20).
:return: List of datasets.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets?page={page}&limit={limit}"
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('data', [])
def create_dataset(self, name, permission='all_team_members'):
"""
Create a new dataset (knowledge base).
:param name: Name of the new dataset.
:param permission: Permission level (default: all_team_members).
:return: Response containing dataset creation result.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets"
data = {
"name": name,
"permission": permission
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
def delete_dataset(self, dataset_id):
"""
Delete a dataset (knowledge base).
:param dataset_id: ID of the dataset to delete.
:return: Response confirming deletion.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}"
response = requests.delete(url, headers=headers)
response.raise_for_status()
def update_document_via_text(self, dataset_id, document_id, document_name=None, document_text=None, process_rule=None):
"""
Update a document using text content.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document to update.
:param document_name: New name for the document (optional).
:param document_text: New text content for the document (optional).
:param process_rule: Processing rules (optional).
:return: Response containing document update result.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/update_by_text"
data = {}
if document_name:
data["name"] = document_name
if document_text:
data["text"] = document_text
if process_rule:
data["process_rule"] = process_rule
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
def update_document_from_file(self, dataset_id, document_id, file_path, document_name=None, process_rule=None):
"""
Update a document by uploading a new file.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document to update.
:param file_path: Path to the new file.
:param document_name: New name for the document (optional).
:param process_rule: Processing rules (optional).
:return: Response containing document update result.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/update_by_file"
data = {}
if document_name:
data["name"] = document_name
if process_rule is None:
process_rule = {"mode": "automatic"}
data["process_rule"] = process_rule
data_string = json.dumps(data)
files = {
"data": (None, data_string, 'text/plain'),
"file": open(file_path, "rb")
}
response = requests.post(url, headers=headers, files=files)
response.raise_for_status()
return response.json()
def index_status(self, dataset_id, batch):
"""
Get the indexing status of a document batch.
:param dataset_id: ID of the knowledge base.
:param batch: Batch ID of the document.
:return: Indexing status of the document.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{batch}/indexing-status"
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def delete_document(self, dataset_id, document_id):
"""
Delete a document from a dataset.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document to delete.
:return: Response confirming deletion.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}"
response = requests.delete(url, headers=headers)
response.raise_for_status()
def get_docs(self, dataset_id, page=1, limit=20):
"""
Retrieve a list of documents in a dataset.
:param dataset_id: ID of the knowledge base.
:param page: Page number for pagination.
:param limit: Number of documents per page (default: 20).
:return: List of documents.
"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents?page={page}&limit={limit}"
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('data', [])
def add_segment(self, dataset_id, document_id, segments):
"""
Add segments to a document.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document to add segments to.
:param segments: List of segment data (content, answer, keywords).
:return: Response confirming the segments were added.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/segments"
data = {"segments": segments}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
def get_document_segments(self, dataset_id, document_id, keyword=None, status=None):
"""
Retrieve segments of a document.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document.
:param keyword: Keyword for filtering (optional).
:param status: Status for filtering (optional).
:return: List of document segments.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/segments"
params = {}
if keyword:
params['keyword'] = keyword
if status:
params['status'] = status
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
def delete_document_segment(self, dataset_id, document_id, segment_id):
"""
Delete a segment from a document.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document.
:param segment_id: ID of the segment to delete.
:return: Response confirming the segment was deleted.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}"
response = requests.delete(url, headers=headers)
response.raise_for_status()
def update_document_segment(self, dataset_id, document_id, segment_id, content, answer=None, keywords=None, enabled=True):
"""
Update a segment of a document.
:param dataset_id: ID of the knowledge base.
:param document_id: ID of the document.
:param segment_id: ID of the segment to update.
:param content: New content for the segment.
:param answer: New answer for the segment (optional).
:param keywords: New keywords for the segment (optional).
:param enabled: Whether the segment is enabled (default: True).
:return: Response confirming the update.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
url = f"{self.api_url}/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}"
segment_data = {"content": content, "enabled": enabled}
if answer:
segment_data["answer"] = answer
if keywords:
segment_data["keywords"] = keywords
data = {"segment": segment_data}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
api_key = 'your_api_key'
api_url = 'your_api_url'
client = DifySdkClient(api_key, your_api_url)