dify-api-python


Namedify-api-python JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/zlz3907/dify-api-python
SummaryA Python client for the Dify API
upload_time2024-10-14 10:02:16
maintainerNone
docs_urlNone
authorzlz3907
requires_python>=3.7
licenseApache License 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Dify API Python

[English](README.md) | [中文](README_CN.md)

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:

```bash
pip install dify-api-python
```

## Usage

### Importing the Client

```python
from dify_api_python import DifyClient
```

### Initializing the Client

You can initialize the `DifyClient` in several ways:

1. Using parameters:
   ```python
   client = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')
   ```

2. Using a default configuration file:
   ```python
   client = DifyClient()
   ```

3. Using a custom configuration file:
   ```python
   client = DifyClient(config_path='/path/to/your/config.ini')
   ```

The configuration file should be in INI format and include the following:

```ini
[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:

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

For a streaming chat completion:

```python
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:

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

For a streaming text completion:

```python
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):

```python
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:

```python
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](LICENSE) file for details.

## Support

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


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zlz3907/dify-api-python",
    "name": "dify-api-python",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "zlz3907",
    "author_email": "zlz3907@gmail.com",
    "download_url": null,
    "platform": null,
    "description": "# Dify API Python\n\n[English](README.md) | [\u4e2d\u6587](README_CN.md)\n\nDify 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.\n\n## Installation\n\nInstall the package using pip:\n\n```bash\npip install dify-api-python\n```\n\n## Usage\n\n### Importing the Client\n\n```python\nfrom dify_api_python import DifyClient\n```\n\n### Initializing the Client\n\nYou can initialize the `DifyClient` in several ways:\n\n1. Using parameters:\n   ```python\n   client = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')\n   ```\n\n2. Using a default configuration file:\n   ```python\n   client = DifyClient()\n   ```\n\n3. Using a custom configuration file:\n   ```python\n   client = DifyClient(config_path='/path/to/your/config.ini')\n   ```\n\nThe configuration file should be in INI format and include the following:\n\n```ini\n[DEFAULT]\nAPI_KEY = your_api_key\nBASE_URL = https://api.dify.ai/v1\n```\n\nNote: If you provide `api_key` or `base_url` as parameters, they will override any values in the configuration file.\n\n### Making API Calls\n\n#### Chat Completion\n\nFor a chat completion request:\n\n```python\nresponse = client.chat_completion(\n    query=\"Hello, how are you?\",\n    user=\"user123\",\n    inputs={},\n    stream=False\n)\nprint(response)\n```\n\nFor a streaming chat completion:\n\n```python\nfor chunk in client.chat_completion(\n    query=\"Tell me a story\",\n    user=\"user123\",\n    inputs={},\n    stream=True\n):\n    print(chunk)\n```\n\n#### Text Completion\n\nFor a text completion request:\n\n```python\nresponse = client.text_completion(\n    prompt=\"Once upon a time\",\n    user_id=\"user123\",\n    inputs={},\n    stream=False\n)\nprint(response)\n```\n\nFor a streaming text completion:\n\n```python\nfor chunk in client.text_completion(\n    prompt=\"Write a poem about\",\n    user_id=\"user123\",\n    inputs={},\n    stream=True\n):\n    print(chunk)\n```\n\n#### Combined Chat Completion\n\nFor a combined chat completion (merges streaming response):\n\n```python\nresponse = client.chat_completion_combined(\n    query=\"Explain quantum computing\",\n    user=\"user123\",\n    inputs={}\n)\nprint(response)\n```\n\n### Example Usage\n\nHere's a complete example of how to use the Dify API Python client:\n\n```python\nfrom dify_api_python import DifyClient\n\n# Initialize DifyClient\nclient = DifyClient(api_key='your_api_key', base_url='https://api.dify.ai/v1')\n\ndef chat_with_dify(query, user_id=\"user123\", inputs={}):\n    \"\"\"\n    Chat with Dify API\n    \n    :param query: User's question or input\n    :param user_id: User ID, default is \"user123\"\n    :param inputs: Additional input parameters, default is an empty dictionary\n    :return: API response\n    \"\"\"\n    try:\n        response = client.chat_completion_combined(\n            query=query,\n            user=user_id,\n            inputs=inputs\n        )\n        return response\n    except Exception as e:\n        print(f\"An error occurred: {str(e)}\")\n        return None\n\n# Example usage\nif __name__ == \"__main__\":\n    user_query = \"Please explain the basic principles of quantum computing\"\n    result = chat_with_dify(user_query)\n    \n    if result:\n        print(\"Dify's answer:\")\n        print(result)\n    else:\n        print(\"Failed to get an answer\")\n```\n\n## Key Features\n\n- Flexible client initialization (via parameters or config file)\n- Support for both streaming and non-streaming API calls\n- Method to merge streaming responses\n- Easy-to-use interface for chat and text completions\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.\n\n## Support\n\nIf you encounter any issues or have questions, please open an issue on the GitHub repository.\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "A Python client for the Dify API",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/zlz3907/dify-api-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0120b3fa1def2a4466f28488e82f70af30c3a106abd43b8caa2bb41eff55e170",
                "md5": "0b0634754ab60f0b33a82d88466b9751",
                "sha256": "a638122119ff3b77fd61ec0b5a675f0c336c1a009af886e02d6c3270a303adbe"
            },
            "downloads": -1,
            "filename": "dify_api_python-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0b0634754ab60f0b33a82d88466b9751",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9488,
            "upload_time": "2024-10-14T10:02:16",
            "upload_time_iso_8601": "2024-10-14T10:02:16.083787Z",
            "url": "https://files.pythonhosted.org/packages/01/20/b3fa1def2a4466f28488e82f70af30c3a106abd43b8caa2bb41eff55e170/dify_api_python-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-14 10:02:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zlz3907",
    "github_project": "dify-api-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "dify-api-python"
}
        
Elapsed time: 0.32933s