gws_core

Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version

DifyService

Service to interact with Dify API

Attributes
api_key: strroute: str
Functions
__init__

Initialize self. See help(type(self)) for accurate signature.

route : str
api_key : str
create_dataset_metadata

Create metadata for a specific document in a dataset. Parameters

dataset_id : str Knowledge Base ID body : DifyCreateMetadataRequest Metadata to create Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
metadata : DifyCreateDatasetMetadataRequest
Return type : DifyCreateDatasetMetadataResponse
delete_document

Delete a document from a dataset.

Parameters

dataset_id : str Knowledge Base ID document_id : str Document ID to delete

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
document_id : str
get_all_documents

Get all documents in a dataset.

Parameters

dataset_id : str Knowledge Base ID

Returns

List[DifyDatasetDocument] List of documents in the dataset

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
Return type : List
get_base_url

Get the base URL of the Dify API before the first '/'. Returns: str: Base URL of the Dify API

Return type : str
get_dataset_all_metadata

Get metadata for a specific dataset. Parameters

dataset_id : str Knowledge Base ID Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
Return type : DifyGetDatasetMetadataResponse
get_dataset_metadata

Get metadata for a specific dataset. Parameters

dataset_id : str Knowledge Base ID metadata_name : str Metadata name to get Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
metadata_name : str
Return type : gws_core.impl.dify.dify_class.DifyGetDatasetMetadataResponseMetadata | None
get_document_chunks

Get chunks from a specific document in a knowledge base.

This method retrieves chunks from a specific document within a dataset.

Parameters

dataset_id : str Knowledge Base ID document_id : str Document ID to get chunks from keyword : Optional[str], optional Search keyword to filter chunks, by default None status : str, optional Search status filter, by default "completed" page : int, optional Page number for pagination, by default 1 limit : int, optional Number of items to return per page (1-100), by default 20

Returns

DifyDocumentChunksResponse Response containing the document chunks and pagination info

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
document_id : str
keyword : Optional
status : str - completed
page : int - 1
limit : int - 20
Return type : DifyDocumentChunksResponse
get_document_file

Get information about a document's uploaded file and optionally download it.

This method retrieves information about the uploaded file associated with a document and can optionally download the file to a temporary directory.

Parameters

dataset_id : str Knowledge Base ID document_id : str Document ID to get the file information for download_to_temp : bool, optional Whether to download the file to a temporary directory, by default True

Returns

Download url

Raises

requests.exceptions.HTTPError If the API request fails RuntimeError If file download fails

dataset_id : str
document_id : str
Return type : DifyUploadFileResponse
get_document_page

Get a list of documents in a dataset.

Parameters

dataset_id : str Knowledge Base ID page : int Page number for pagination limit : int Number of items to return per page (1-100)

Returns

DifyGetDocumentsResponse Response containing the list of documents and pagination info

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
page : int
limit : int
Return type : DifyGetDocumentsResponse
get_or_create_dataset_metadata

Get or create the access right metadata in Dify.

dataset_id : str
metadata : DifyCreateDatasetMetadataRequest
Return type : DifyGetDatasetMetadataResponseMetadata
search_chunks

Retrieve chunks from a Knowledge Base (dataset).

This method retrieves relevant chunks from the specified dataset based on the query and search parameters.

Parameters

dataset_id : str Knowledge Base ID query : str Query keyword to search for search_method : Literal['keyword_search', 'semantic_search', 'full_text_search', 'hybrid_search'], optional Search method to use, by default 'semantic_search' reranking_enable : bool, optional Whether to enable reranking, by default False reranking_provider_name : Optional[str], optional Rerank model provider, required if reranking is enabled reranking_model_name : Optional[str], optional Rerank model name, required if reranking is enabled weights : Optional[float], optional Semantic search weight setting in hybrid search mode top_k : Optional[int], optional Number of results to return score_threshold_enabled : bool, optional Whether to enable score threshold, by default False score_threshold : Optional[float], optional Score threshold value, used if score_threshold_enabled is True

Returns

DifyChunksResponse Response containing query info and retrieved chunks

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
query : str
search_method : Literal - semantic_search
reranking_enable : bool - False
reranking_provider_name : Optional
reranking_model_name : Optional
weights : Optional
top_k : Optional - 5
score_threshold_enabled : bool - False
score_threshold : Optional - 0
Return type : DifyChunksResponse
send_document
doc_path : str
dataset_id : str
options : DifySendDocumentOptions
filename : str
Return type : DifySendDocumentResponse
send_message_stream

Call the Dify API chat endpoint with streaming response.

Args: query: The user's message conversation_id: Optional ID for continuing a conversation user: Optional user identifier inputs: Optional dictionary of input variables files: Optional list of files to include

Returns: Generator that yields response chunks (string or objects)

query : str
user : str
conversation_id : Optional
inputs : Optional
files : Optional
Return type : Generator
update_document

Update a document in a dataset. Parameters

doc_path : str Path to the document file dataset_id : str Knowledge Base ID document_id : str Document ID to update options : DifyUpdateDocumentOptions Options for updating the document Returns

DifySendDocumentResponse Response containing the updated document information Raises

requests.exceptions.HTTPError If the API request fails

doc_path : str
dataset_id : str
document_id : str
options : DifySendDocumentOptions
filename : str
Return type : DifySendDocumentResponse
update_document_metadata

Update metadata for a specific document in a dataset.

Parameters

dataset_id : str Knowledge Base ID document_id : str Document ID to update metadata for metadata : Dict[str, Any] Metadata to update

Raises

requests.exceptions.HTTPError If the API request fails

dataset_id : str
body : List
from_credentials @staticmethod
credentials : CredentialsDataOther