Skip to content

Commit af51c1f

Browse files
committed
feat: add missing knowledge base APIs
- Add retrieve method to KnowledgeBaseClient - Add get_document method to KnowledgeBaseClient - Add download_document method to KnowledgeBaseClient - Add get_document_segment method to KnowledgeBaseClient - Add corresponding async methods
1 parent 3f04309 commit af51c1f

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

dify_client/async_client.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,45 @@ async def list_documents(
879879
url = f"/datasets/{self._get_dataset_id()}/documents"
880880
return await self._send_request("GET", url, params=params, **kwargs)
881881

882+
async def get_document(self, document_id: str, metadata: str = "all"):
883+
"""Get detailed information about a specific document.
884+
885+
Args:
886+
document_id: Document ID
887+
metadata: Metadata inclusion mode ('all', 'only', 'without')
888+
889+
Returns:
890+
httpx.Response object
891+
"""
892+
params = {"metadata": metadata}
893+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}"
894+
return await self._send_request("GET", url, params=params)
895+
896+
async def download_document(self, document_id: str):
897+
"""Download a specific document.
898+
899+
Args:
900+
document_id: Document ID
901+
902+
Returns:
903+
httpx.Response object with the file download URL
904+
"""
905+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/download"
906+
return await self._send_request("GET", url)
907+
908+
async def get_document_segment(self, document_id: str, segment_id: str):
909+
"""Get detailed information about a specific segment.
910+
911+
Args:
912+
document_id: Document ID
913+
segment_id: Segment ID
914+
915+
Returns:
916+
httpx.Response object
917+
"""
918+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments/{segment_id}"
919+
return await self._send_request("GET", url)
920+
882921
async def add_segments(self, document_id: str, segments: list[dict], **kwargs):
883922
"""Add segments to a document."""
884923
data = {"segments": segments}
@@ -927,6 +966,34 @@ async def update_document_segment(
927966
return await self._send_request("POST", url, json=data, **kwargs)
928967

929968
# Advanced Knowledge Base APIs
969+
async def retrieve(
970+
self,
971+
query: str,
972+
retrieval_model: Dict[str, Any] = None,
973+
external_retrieval_model: Dict[str, Any] = None,
974+
attachment_ids: List[str] = None,
975+
):
976+
"""Retrieve chunks from the knowledge base.
977+
978+
Args:
979+
query: Search query text
980+
retrieval_model: Retrieval model configuration (optional)
981+
external_retrieval_model: External retrieval model configuration (optional)
982+
attachment_ids: List of attachment IDs to include in retrieval context (optional)
983+
984+
Returns:
985+
httpx.Response object
986+
"""
987+
data = {"query": query}
988+
if retrieval_model:
989+
data["retrieval_model"] = retrieval_model
990+
if external_retrieval_model:
991+
data["external_retrieval_model"] = external_retrieval_model
992+
if attachment_ids:
993+
data["attachment_ids"] = attachment_ids
994+
url = f"/datasets/{self._get_dataset_id()}/retrieve"
995+
return await self._send_request("POST", url, json=data)
996+
930997
async def hit_testing(
931998
self,
932999
query: str,

dify_client/client.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,48 @@ def list_documents(
10401040
url = f"/datasets/{self._get_dataset_id()}/documents"
10411041
return self._send_request("GET", url, params=params, **kwargs)
10421042

1043+
def get_document(self, document_id: str, metadata: str = "all"):
1044+
"""Get detailed information about a specific document.
1045+
1046+
Args:
1047+
document_id: Document ID
1048+
metadata: Metadata inclusion mode ('all', 'only', 'without')
1049+
1050+
Returns:
1051+
httpx.Response object
1052+
"""
1053+
self._validate_params(document_id=document_id)
1054+
params = {"metadata": metadata}
1055+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}"
1056+
return self._send_request("GET", url, params=params)
1057+
1058+
def download_document(self, document_id: str):
1059+
"""Download a specific document.
1060+
1061+
Args:
1062+
document_id: Document ID
1063+
1064+
Returns:
1065+
httpx.Response object with the file download URL
1066+
"""
1067+
self._validate_params(document_id=document_id)
1068+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/download"
1069+
return self._send_request("GET", url)
1070+
1071+
def get_document_segment(self, document_id: str, segment_id: str):
1072+
"""Get detailed information about a specific segment.
1073+
1074+
Args:
1075+
document_id: Document ID
1076+
segment_id: Segment ID
1077+
1078+
Returns:
1079+
httpx.Response object
1080+
"""
1081+
self._validate_params(document_id=document_id, segment_id=segment_id)
1082+
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments/{segment_id}"
1083+
return self._send_request("GET", url)
1084+
10431085
def add_segments(self, document_id: str, segments: list[dict], **kwargs):
10441086
"""
10451087
Add segments to a document.
@@ -1105,6 +1147,35 @@ def update_document_segment(
11051147
return self._send_request("POST", url, json=data, **kwargs)
11061148

11071149
# Advanced Knowledge Base APIs
1150+
def retrieve(
1151+
self,
1152+
query: str,
1153+
retrieval_model: Dict[str, Any] = None,
1154+
external_retrieval_model: Dict[str, Any] = None,
1155+
attachment_ids: List[str] = None,
1156+
):
1157+
"""Retrieve chunks from the knowledge base.
1158+
1159+
Args:
1160+
query: Search query text
1161+
retrieval_model: Retrieval model configuration (optional)
1162+
external_retrieval_model: External retrieval model configuration (optional)
1163+
attachment_ids: List of attachment IDs to include in retrieval context (optional)
1164+
1165+
Returns:
1166+
httpx.Response object
1167+
"""
1168+
self._validate_params(query=query)
1169+
data = {"query": query}
1170+
if retrieval_model:
1171+
data["retrieval_model"] = retrieval_model
1172+
if external_retrieval_model:
1173+
data["external_retrieval_model"] = external_retrieval_model
1174+
if attachment_ids:
1175+
data["attachment_ids"] = attachment_ids
1176+
url = f"/datasets/{self._get_dataset_id()}/retrieve"
1177+
return self._send_request("POST", url, json=data)
1178+
11081179
def hit_testing(
11091180
self,
11101181
query: str,

0 commit comments

Comments
 (0)