Dify API Python is a Python implementation for interacting with Dify's API. It provides an easy-to-use interface for making API calls, with support for both streaming and non-streaming responses.
Install the package using pip:
pip install dify-api-python
from dify_api_python import DifyClient
You can initialize the DifyClient
in several ways:
-
Using parameters:
client = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')
-
Using a default configuration file:
client = DifyClient()
-
Using a custom configuration file:
client = DifyClient(config_path='/path/to/your/config.ini')
The configuration file should be in INI format and include the following:
[DEFAULT]
API_KEY = your_api_key
BASE_URL = https://api.dify.ai/v1
Note: If you provide api_key
or base_url
as parameters, they will override any values in the configuration file.
The DifyClient
class acts as a wrapper for the DifySDK
class. All methods available in the SDK can be called directly on the client object. Here are the available methods:
# Non-streaming
response = client.chat_message(
query="Hello, how are you?",
user="user123",
inputs={},
files=None,
conversation_id=None,
stream=False
)
print(response)
# Streaming
for chunk in client.chat_message(
query="Tell me a story",
user="user123",
inputs={},
files=None,
conversation_id=None,
stream=True
):
print(chunk)
# Non-streaming
response = client.completion_message(
prompt="Once upon a time",
user="user123",
inputs={},
stream=False
)
print(response)
# Streaming
for chunk in client.completion_message(
prompt="Write a poem about",
user="user123",
inputs={},
stream=True
):
print(chunk)
result = client.stop_chat_completion(task_id, user="user123")
messages = client.get_conversation_messages(conversation_id, user="user123", first_id=None, limit=20)
conversations = client.get_conversations(user="user123", last_id=None, limit=20, pinned=None)
result = client.delete_conversation(conversation_id, user="user123")
result = client.rename_conversation(conversation_id, name="New Name", user="user123", auto_generate=False)
feedback = client.get_message_feedback(message_id, rating, user="user123")
questions = client.get_suggested_questions(message_id, user="user123")
with open('audio.mp3', 'rb') as audio_file:
result = client.audio_to_text(audio_file, user="user123")
audio_content, content_type = client.text_to_audio(text="Hello, world!", message_id=None, user="user123")
parameters = client.get_app_parameters(user="user123")
meta = client.get_app_meta(user="user123")
with open('document.pdf', 'rb') as file:
result = client.upload_file(file, user="user123")
The DifyClient
provides some additional methods:
# Combined chat completion (merges streaming response)
response = client.chat_completion_combined(
query="Explain quantum computing",
user="user123",
inputs={}
)
# Reset a user's conversation
client.reset_conversation("user123")
# Get a user's conversation ID
conversation_id = client.get_conversation_id("user123")
- Flexible client initialization (via parameters or config file)
- Support for both streaming and non-streaming API calls
- Method to merge streaming responses (
chat_completion_combined
) - Easy-to-use interface for all Dify API endpoints
- Conversation management
- Audio conversion (text-to-speech and speech-to-text)
- File upload functionality
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
If you encounter any issues or have questions, please open an issue on the GitHub repository.