Skip to main content

dify-api-client - A package for interacting with the Dify Service-API

Project description

dify-api-client

CI PyPI version Python 3.10+

This package is a fork of dify-client-python with custom modifications

It provides a convenient and powerful interface to interact with the Dify API, supporting both synchronous and asynchronous operations.

Main Features

  • Synchronous and Asynchronous Support: The client offers both synchronous and asynchronous methods, allowing for flexible integration into various Python codebases and frameworks.
  • Stream and Non-stream Support: Seamlessly work with both streaming and non-streaming endpoints of the Dify API for real-time and batch processing use cases.
  • Comprehensive Endpoint Coverage: Support completion, chat, workflows, feedback, file uploads, etc., the client covers all available Dify API endpoints.

Quick Start

Sync

Here's a quick example of how you can use the DifyClient to send a chat message.

import uuid
from dify_client import DifyClient, models

# Initialize the client with your API key
client = DifyClient(
    api_key="your-api-key",
    api_base="http://localhost/v1",
)
user = str(uuid.uuid4())

# Create a blocking chat request
blocking_chat_req = models.ChatRequest(
    query="Hi, dify-client-python!",
    inputs={"city": "Beijing"},
    user=user,
    response_mode=models.ResponseMode.BLOCKING,
)

# Send the chat message
chat_response = client.chat_messages(blocking_chat_req, timeout=60.)
print(chat_response)

# Create a streaming chat request
streaming_chat_req = models.ChatRequest(
    query="Hi, dify-client-python!",
    inputs={"city": "Beijing"},
    user=user,
    response_mode=models.ResponseMode.STREAMING,
)

# Send the chat message
for chunk in client.chat_messages(streaming_chat_req, timeout=60.):
    print(chunk)

Async

For asynchronous operations, use the AsyncDifyClient in a similar fashion:

import asyncio
import uuid

from dify_client import AsyncDifyClient, models
# Initialize the async client with your API key
async_client = AsyncDifyClient(
    api_key="your-api-key",
    api_base="http://localhost/v1",
)


# Define an asynchronous function to send a blocking chat message with BLOCKING ResponseMode
async def send_chat_message():
    user = str(uuid.uuid4())
    # Create a blocking chat request
    blocking_chat_req = models.ChatRequest(
        query="Hi, dify-client-python!",
        inputs={"city": "Beijing"},
        user=user,
        response_mode=models.ResponseMode.BLOCKING,
    )
    chat_response = await async_client.achat_messages(blocking_chat_req, timeout=60.)
    print(chat_response)


# Define an asynchronous function to send a chat message with STREAMING ResponseMode
async def send_chat_message_stream():
    user = str(uuid.uuid4())
    # Create a blocking chat request
    streaming_chat_req = models.ChatRequest(
        query="Hi, dify-client-python!",
        inputs={"city": "Beijing"},
        user=user,
        response_mode=models.ResponseMode.STREAMING,
    )
    async for chunk in await async_client.achat_messages(streaming_chat_req, timeout=60.):
        print(chunk)


# Run the asynchronous function
asyncio.gather(send_chat_message(), send_chat_message_stream())

Development

  • Setup env
uv sync --extra dev
  • Export python path to run dev
export PYTHONPATH=.

Project details


Download files

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

Source Distribution

dify_api_client-0.0.20.tar.gz (82.1 kB view details)

Uploaded Source

Built Distribution

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

dify_api_client-0.0.20-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file dify_api_client-0.0.20.tar.gz.

File metadata

  • Download URL: dify_api_client-0.0.20.tar.gz
  • Upload date:
  • Size: 82.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dify_api_client-0.0.20.tar.gz
Algorithm Hash digest
SHA256 b9307b63b6f9763abc8fd71cef2432923a3f5fe2139ca9064f38b1e66ce4aeba
MD5 11e13a0a323a6a3a066e18a8e5a13db5
BLAKE2b-256 773d0d91509a287b3ac0d16b274bd17fef7c35a20434aa864f657218c0512701

See more details on using hashes here.

File details

Details for the file dify_api_client-0.0.20-py3-none-any.whl.

File metadata

File hashes

Hashes for dify_api_client-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 703be23f1bc37f2c2cac0fc6f48ff90cff3e0348c2b4a901e17a8f6f0ae61642
MD5 342fbbb0e380801ae9aefb7fd77c9b04
BLAKE2b-256 306e732e2e154bfaf7a92767b6c0ca67d0e43bd9fda4b8b8c453e44ccd2a12a5

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