A Python SDK for interacting with the Dify Knowledge Base API. This library provides convenient methods for creating, updating, and managing documents and datasets within the Dify Knowledge Base system.
- Create, update, and delete datasets (knowledge bases)
- Create, update, and delete documents (text and files)
- Query document indexing status
- Manage document segments (add, update, delete)
To install the SDK, clone this repository and install the required requests package:
git clone https://github.com/andyuan997/Dify-Knowledge-Python-SDK.git
cd Dify-Knowledge-Python-SDK
pip install requests# Initialize the client with your API key
api_key = 'your_api_key'
api_url = 'your_api_url'
client = DifySdkClient(api_key, your_api_url)dataset_id = client.create_dataset('My New Dataset')
print(f"Created dataset with ID: {dataset_id}")document = client.create_document_from_text(dataset_id, 'Document Title', 'This is the content of the document.')
print(f"Created document: {document}")status = client.index_status(dataset_id, 'batch_id')
print(f"Document index status: {status}")segment = {
"content": "This is the segment content.",
"answer": "This is the segment answer.",
"keywords": ["keyword1", "keyword2"]
}
client.add_segment(dataset_id, document_id, [segment])
print("Added segment to document.")Feel free to submit issues or pull requests. All contributions are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.