Skip to main content

A Python client for the Dify API

Project description

Dify API Python

English | 中文

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. The library also includes a method to merge stream responses, making it convenient to use in scenarios where streaming is not well supported.

Installation

Install the package using pip:

pip install dify-api-python

Usage

Importing the Client

from dify_api_python import DifyClient

Initializing the Client

You can initialize the DifyClient in several ways:

  1. Using parameters:

    client = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')
    
  2. Using a default configuration file:

    client = DifyClient()
    
  3. 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.

Making API Calls

Chat Completion

For a chat completion request:

response = client.chat_completion(
    query="Hello, how are you?",
    user="user123",
    inputs={},
    stream=False
)
print(response)

For a streaming chat completion:

for chunk in client.chat_completion(
    query="Tell me a story",
    user="user123",
    inputs={},
    stream=True
):
    print(chunk)

Text Completion

For a text completion request:

response = client.text_completion(
    prompt="Once upon a time",
    user_id="user123",
    inputs={},
    stream=False
)
print(response)

For a streaming text completion:

for chunk in client.text_completion(
    prompt="Write a poem about",
    user_id="user123",
    inputs={},
    stream=True
):
    print(chunk)

Combined Chat Completion

For a combined chat completion (merges streaming response):

response = client.chat_completion_combined(
    query="Explain quantum computing",
    user="user123",
    inputs={}
)
print(response)

Example Usage

Here's a complete example of how to use the Dify API Python client:

from dify_api_python import DifyClient

# Initialize DifyClient
client = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')

def chat_with_dify(query, user_id="user123", inputs={}):
    """
    Chat with Dify API
    
    :param query: User's question or input
    :param user_id: User ID, default is "user123"
    :param inputs: Additional input parameters, default is an empty dictionary
    :return: API response
    """
    try:
        response = client.chat_completion_combined(
            query=query,
            user=user_id,
            inputs=inputs
        )
        return response
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None

# Example usage
if __name__ == "__main__":
    user_query = "Please explain the basic principles of quantum computing"
    result = chat_with_dify(user_query)
    
    if result:
        print("Dify's answer:")
        print(result)
    else:
        print("Failed to get an answer")

Key Features

  • Flexible client initialization (via parameters or config file)
  • Support for both streaming and non-streaming API calls
  • Method to merge streaming responses
  • Easy-to-use interface for chat and text completions

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Support

If you encounter any issues or have questions, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dify_api_python-0.1.5-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file dify_api_python-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for dify_api_python-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8c00fff1897dbca48fe73b4145411aa09d39df8c78a33c9fffce391fb5bc57c0
MD5 663533ecfb0ffbaa7741de1a25e30492
BLAKE2b-256 2e2828345a96f25215d1cb13b1ca03ba61ae4731cdcc75fd5c8fbf02a0f0afc2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page