dify-client


Namedify-client JSON
Version 0.1.10 PyPI version JSON
download
home_pagehttps://github.com/langgenius/dify
SummaryA package for interacting with the Dify Service-API
upload_time2023-11-15 13:46:31
maintainer
docs_urlNone
authorDify
requires_python>=3.6
licenseMIT
keywords dify nlp ai language-processing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dify-client

A Dify App Service-API Client, using for build a webapp by request Service-API

## Usage

First, install `dify-client` python sdk package:

```
pip install dify-client
```

Write your code with sdk:

- completion generate with `blocking` response_mode

```python
from dify_client import CompletionClient

api_key = "your_api_key"

# Initialize CompletionClient
completion_client = CompletionClient(api_key)

# Create Completion Message using CompletionClient
completion_response = completion_client.create_completion_message(inputs={"query": "What's the weather like today?"},
                                                                  response_mode="blocking", user="user_id")
completion_response.raise_for_status()

result = completion_response.json()

print(result.get('answer'))
```

- completion using vision model, like gpt-4-vision

```python
from dify_client import CompletionClient

api_key = "your_api_key"

# Initialize CompletionClient
completion_client = CompletionClient(api_key)

files = [{
    "type": "image",
    "transfer_method": "remote_url",
    "url": "your_image_url"
}]

# files = [{
#     "type": "image",
#     "transfer_method": "local_file",
#     "upload_file_id": "your_file_id"
# }]

# Create Completion Message using CompletionClient
completion_response = completion_client.create_completion_message(inputs={"query": "Describe the picture."},
                                                                  response_mode="blocking", user="user_id", files=files)
completion_response.raise_for_status()

result = completion_response.json()

print(result.get('answer'))
```

- chat generate with `streaming` response_mode

```python
import json
from dify_client import ChatClient

api_key = "your_api_key"

# Initialize ChatClient
chat_client = ChatClient(api_key)

# Create Chat Message using ChatClient
chat_response = chat_client.create_chat_message(inputs={}, query="Hello", user="user_id", response_mode="streaming")
chat_response.raise_for_status()

for line in chat_response.iter_lines(decode_unicode=True):
    line = line.split('data:', 1)[-1]
    if line.strip():
        line = json.loads(line.strip())
        print(line.get('answer'))
```

- chat using vision model, like gpt-4-vision

```python
from dify_client import ChatClient

api_key = "your_api_key"

# Initialize ChatClient
chat_client = ChatClient(api_key)

files = [{
    "type": "image",
    "transfer_method": "remote_url",
    "url": "your_image_url"
}]

# files = [{
#     "type": "image",
#     "transfer_method": "local_file",
#     "upload_file_id": "your_file_id"
# }]

# Create Chat Message using ChatClient
chat_response = chat_client.create_chat_message(inputs={}, query="Describe the picture.", user="user_id",
                                                response_mode="blocking", files=files)
chat_response.raise_for_status()

result = chat_response.json()

print(result.get("answer"))
```

- upload file when using vision model

```python
from dify_client import DifyClient

api_key = "your_api_key"

# Initialize Client
dify_client = DifyClient(api_key)

file_path = "your_image_file_path"
file_name = "panda.jpeg"
mime_type = "image/jpeg"

with open(file_path, "rb") as file:
    files = {
        "file": (file_name, file, mime_type)
    }
    response = dify_client.file_upload("user_id", files)

    result = response.json()
    print(f'upload_file_id: {result.get("id")}')
```
  


- Others

```python
from dify_client import ChatClient

api_key = "your_api_key"

# Initialize Client
client = ChatClient(api_key)

# Get App parameters
parameters = client.get_application_parameters(user="user_id")
parameters.raise_for_status()

print('[parameters]')
print(parameters.json())

# Get Conversation List (only for chat)
conversations = client.get_conversations(user="user_id")
conversations.raise_for_status()

print('[conversations]')
print(conversations.json())

# Get Message List (only for chat)
messages = client.get_conversation_messages(user="user_id", conversation_id="conversation_id")
messages.raise_for_status()

print('[messages]')
print(messages.json())

# Rename Conversation (only for chat)
rename_conversation_response = client.rename_conversation(conversation_id="conversation_id",
                                                          name="new_name", user="user_id")
rename_conversation_response.raise_for_status()

print('[rename result]')
print(rename_conversation_response.json())
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/langgenius/dify",
    "name": "dify-client",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "dify nlp ai language-processing",
    "author": "Dify",
    "author_email": "hello@dify.ai",
    "download_url": "https://files.pythonhosted.org/packages/18/04/67d2aaa608087c0ad9f96530956d72e502cce20ffad041015c510a1afdb0/dify-client-0.1.10.tar.gz",
    "platform": null,
    "description": "# dify-client\n\nA Dify App Service-API Client, using for build a webapp by request Service-API\n\n## Usage\n\nFirst, install `dify-client` python sdk package:\n\n```\npip install dify-client\n```\n\nWrite your code with sdk:\n\n- completion generate with `blocking` response_mode\n\n```python\nfrom dify_client import CompletionClient\n\napi_key = \"your_api_key\"\n\n# Initialize CompletionClient\ncompletion_client = CompletionClient(api_key)\n\n# Create Completion Message using CompletionClient\ncompletion_response = completion_client.create_completion_message(inputs={\"query\": \"What's the weather like today?\"},\n                                                                  response_mode=\"blocking\", user=\"user_id\")\ncompletion_response.raise_for_status()\n\nresult = completion_response.json()\n\nprint(result.get('answer'))\n```\n\n- completion using vision model, like gpt-4-vision\n\n```python\nfrom dify_client import CompletionClient\n\napi_key = \"your_api_key\"\n\n# Initialize CompletionClient\ncompletion_client = CompletionClient(api_key)\n\nfiles = [{\n    \"type\": \"image\",\n    \"transfer_method\": \"remote_url\",\n    \"url\": \"your_image_url\"\n}]\n\n# files = [{\n#     \"type\": \"image\",\n#     \"transfer_method\": \"local_file\",\n#     \"upload_file_id\": \"your_file_id\"\n# }]\n\n# Create Completion Message using CompletionClient\ncompletion_response = completion_client.create_completion_message(inputs={\"query\": \"Describe the picture.\"},\n                                                                  response_mode=\"blocking\", user=\"user_id\", files=files)\ncompletion_response.raise_for_status()\n\nresult = completion_response.json()\n\nprint(result.get('answer'))\n```\n\n- chat generate with `streaming` response_mode\n\n```python\nimport json\nfrom dify_client import ChatClient\n\napi_key = \"your_api_key\"\n\n# Initialize ChatClient\nchat_client = ChatClient(api_key)\n\n# Create Chat Message using ChatClient\nchat_response = chat_client.create_chat_message(inputs={}, query=\"Hello\", user=\"user_id\", response_mode=\"streaming\")\nchat_response.raise_for_status()\n\nfor line in chat_response.iter_lines(decode_unicode=True):\n    line = line.split('data:', 1)[-1]\n    if line.strip():\n        line = json.loads(line.strip())\n        print(line.get('answer'))\n```\n\n- chat using vision model, like gpt-4-vision\n\n```python\nfrom dify_client import ChatClient\n\napi_key = \"your_api_key\"\n\n# Initialize ChatClient\nchat_client = ChatClient(api_key)\n\nfiles = [{\n    \"type\": \"image\",\n    \"transfer_method\": \"remote_url\",\n    \"url\": \"your_image_url\"\n}]\n\n# files = [{\n#     \"type\": \"image\",\n#     \"transfer_method\": \"local_file\",\n#     \"upload_file_id\": \"your_file_id\"\n# }]\n\n# Create Chat Message using ChatClient\nchat_response = chat_client.create_chat_message(inputs={}, query=\"Describe the picture.\", user=\"user_id\",\n                                                response_mode=\"blocking\", files=files)\nchat_response.raise_for_status()\n\nresult = chat_response.json()\n\nprint(result.get(\"answer\"))\n```\n\n- upload file when using vision model\n\n```python\nfrom dify_client import DifyClient\n\napi_key = \"your_api_key\"\n\n# Initialize Client\ndify_client = DifyClient(api_key)\n\nfile_path = \"your_image_file_path\"\nfile_name = \"panda.jpeg\"\nmime_type = \"image/jpeg\"\n\nwith open(file_path, \"rb\") as file:\n    files = {\n        \"file\": (file_name, file, mime_type)\n    }\n    response = dify_client.file_upload(\"user_id\", files)\n\n    result = response.json()\n    print(f'upload_file_id: {result.get(\"id\")}')\n```\n  \n\n\n- Others\n\n```python\nfrom dify_client import ChatClient\n\napi_key = \"your_api_key\"\n\n# Initialize Client\nclient = ChatClient(api_key)\n\n# Get App parameters\nparameters = client.get_application_parameters(user=\"user_id\")\nparameters.raise_for_status()\n\nprint('[parameters]')\nprint(parameters.json())\n\n# Get Conversation List (only for chat)\nconversations = client.get_conversations(user=\"user_id\")\nconversations.raise_for_status()\n\nprint('[conversations]')\nprint(conversations.json())\n\n# Get Message List (only for chat)\nmessages = client.get_conversation_messages(user=\"user_id\", conversation_id=\"conversation_id\")\nmessages.raise_for_status()\n\nprint('[messages]')\nprint(messages.json())\n\n# Rename Conversation (only for chat)\nrename_conversation_response = client.rename_conversation(conversation_id=\"conversation_id\",\n                                                          name=\"new_name\", user=\"user_id\")\nrename_conversation_response.raise_for_status()\n\nprint('[rename result]')\nprint(rename_conversation_response.json())\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A package for interacting with the Dify Service-API",
    "version": "0.1.10",
    "project_urls": {
        "Homepage": "https://github.com/langgenius/dify"
    },
    "split_keywords": [
        "dify",
        "nlp",
        "ai",
        "language-processing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb408c8e260d9ed2b85071037defe11d6bfc1ab9eb2eea87c05fdaa1d2509b98",
                "md5": "cb4c803e5f285655b06dab7c6f0ab43e",
                "sha256": "7888536866591c5f42f9cae5275807c17d60f38467ee340a536eaed02314deb7"
            },
            "downloads": -1,
            "filename": "dify_client-0.1.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cb4c803e5f285655b06dab7c6f0ab43e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 4326,
            "upload_time": "2023-11-15T13:46:27",
            "upload_time_iso_8601": "2023-11-15T13:46:27.385980Z",
            "url": "https://files.pythonhosted.org/packages/bb/40/8c8e260d9ed2b85071037defe11d6bfc1ab9eb2eea87c05fdaa1d2509b98/dify_client-0.1.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "180467d2aaa608087c0ad9f96530956d72e502cce20ffad041015c510a1afdb0",
                "md5": "7feca476ff3bef0d3f2345871122f784",
                "sha256": "117d7a61e221a95d12fa3eaf6e4f0563720a050ea4f1cbf849af44760307bf49"
            },
            "downloads": -1,
            "filename": "dify-client-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "7feca476ff3bef0d3f2345871122f784",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 4059,
            "upload_time": "2023-11-15T13:46:31",
            "upload_time_iso_8601": "2023-11-15T13:46:31.601553Z",
            "url": "https://files.pythonhosted.org/packages/18/04/67d2aaa608087c0ad9f96530956d72e502cce20ffad041015c510a1afdb0/dify-client-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-15 13:46:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "langgenius",
    "github_project": "dify",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "dify-client"
}
        
Elapsed time: 0.19738s