Initial commit
This commit is contained in:
21
sdks/python-client/LICENSE
Normal file
21
sdks/python-client/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 LangGenius
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
1
sdks/python-client/MANIFEST.in
Normal file
1
sdks/python-client/MANIFEST.in
Normal file
@@ -0,0 +1 @@
|
||||
recursive-include dify_client *.py
|
100
sdks/python-client/README.md
Normal file
100
sdks/python-client/README.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# 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
|
||||
|
||||
```
|
||||
import json
|
||||
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="Hello", response_mode="blocking", user="user_id")
|
||||
completion_response.raise_for_status()
|
||||
|
||||
result = completion_response.text
|
||||
result = json.loads(result)
|
||||
|
||||
print(result.get('answer'))
|
||||
```
|
||||
|
||||
- chat generate with `streaming` response_mode
|
||||
|
||||
```
|
||||
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'))
|
||||
```
|
||||
|
||||
- Others
|
||||
|
||||
```
|
||||
import json
|
||||
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()
|
||||
parameters = json.loads(parameters.text)
|
||||
|
||||
print('[parameters]')
|
||||
print(parameters)
|
||||
|
||||
# Get Conversation List (only for chat)
|
||||
conversations = client.get_conversations(user="user_id")
|
||||
conversations.raise_for_status()
|
||||
conversations = json.loads(conversations.text)
|
||||
|
||||
print('[conversations]')
|
||||
print(conversations)
|
||||
|
||||
# Get Message List (only for chat)
|
||||
messages = client.get_conversation_messages(user="user_id", conversation_id="conversation_id")
|
||||
messages.raise_for_status()
|
||||
messages = json.loads(messages.text)
|
||||
|
||||
print('[messages]')
|
||||
print(messages)
|
||||
|
||||
# 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()
|
||||
rename_conversation_result = json.loads(rename_conversation_response.text)
|
||||
|
||||
print('[rename result]')
|
||||
print(rename_conversation_result)
|
||||
```
|
9
sdks/python-client/build.sh
Executable file
9
sdks/python-client/build.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
rm -rf build dist *.egg-info
|
||||
|
||||
pip install setuptools wheel twine
|
||||
python setup.py sdist bdist_wheel
|
||||
twine upload dist/*
|
1
sdks/python-client/dify_client/__init__.py
Normal file
1
sdks/python-client/dify_client/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from dify_client.client import ChatClient, CompletionClient
|
74
sdks/python-client/dify_client/client.py
Normal file
74
sdks/python-client/dify_client/client.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import requests
|
||||
|
||||
|
||||
class DifyClient:
|
||||
def __init__(self, api_key):
|
||||
self.api_key = api_key
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
|
||||
def _send_request(self, method, endpoint, data=None, params=None, stream=False):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
url = f"{self.base_url}{endpoint}"
|
||||
response = requests.request(method, url, json=data, params=params, headers=headers, stream=stream)
|
||||
|
||||
return response
|
||||
|
||||
def message_feedback(self, message_id, rating, user):
|
||||
data = {
|
||||
"rating": rating,
|
||||
"user": user
|
||||
}
|
||||
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
||||
|
||||
def get_application_parameters(self, user):
|
||||
params = {"user": user}
|
||||
return self._send_request("GET", "/parameters", params=params)
|
||||
|
||||
|
||||
class CompletionClient(DifyClient):
|
||||
def create_completion_message(self, inputs, query, response_mode, user):
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
"response_mode": response_mode,
|
||||
"user": user
|
||||
}
|
||||
return self._send_request("POST", "/completion-messages", data, stream=True if response_mode == "streaming" else False)
|
||||
|
||||
|
||||
class ChatClient(DifyClient):
|
||||
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None):
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
"user": user,
|
||||
"response_mode": response_mode
|
||||
}
|
||||
if conversation_id:
|
||||
data["conversation_id"] = conversation_id
|
||||
|
||||
return self._send_request("POST", "/chat-messages", data, stream=True if response_mode == "streaming" else False)
|
||||
|
||||
def get_conversation_messages(self, user, conversation_id=None, first_id=None, limit=None):
|
||||
params = {"user": user}
|
||||
|
||||
if conversation_id:
|
||||
params["conversation_id"] = conversation_id
|
||||
if first_id:
|
||||
params["first_id"] = first_id
|
||||
if limit:
|
||||
params["limit"] = limit
|
||||
|
||||
return self._send_request("GET", "/messages", params=params)
|
||||
|
||||
def get_conversations(self, user, first_id=None, limit=None, pinned=None):
|
||||
params = {"user": user, "first_id": first_id, "limit": limit, "pinned": pinned}
|
||||
return self._send_request("GET", "/conversations", params=params)
|
||||
|
||||
def rename_conversation(self, conversation_id, name, user):
|
||||
data = {"name": name, "user": user}
|
||||
return self._send_request("POST", f"/conversations/{conversation_id}/name", data)
|
28
sdks/python-client/setup.py
Normal file
28
sdks/python-client/setup.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from setuptools import setup
|
||||
|
||||
with open("README.md", "r", encoding="utf-8") as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setup(
|
||||
name="dify-client",
|
||||
version="0.1.7",
|
||||
author="Dify",
|
||||
author_email="hello@dify.ai",
|
||||
description="A package for interacting with the Dify Service-API",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
url="https://github.com/langgenius/dify",
|
||||
license='MIT',
|
||||
packages=['dify_client'],
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
python_requires=">=3.6",
|
||||
install_requires=[
|
||||
"requests"
|
||||
],
|
||||
keywords='dify nlp ai language-processing',
|
||||
include_package_data=True,
|
||||
)
|
0
sdks/python-client/tests/__init__.py
Normal file
0
sdks/python-client/tests/__init__.py
Normal file
49
sdks/python-client/tests/test_client.py
Normal file
49
sdks/python-client/tests/test_client.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import unittest
|
||||
from dify_client.client import ChatClient, CompletionClient, DifyClient
|
||||
|
||||
API_KEY = os.environ.get("API_KEY")
|
||||
APP_ID = os.environ.get("APP_ID")
|
||||
|
||||
|
||||
class TestChatClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.chat_client = ChatClient(API_KEY)
|
||||
|
||||
def test_create_chat_message(self):
|
||||
response = self.chat_client.create_chat_message({}, "Hello, World!", "test_user")
|
||||
self.assertIn("message_id", response)
|
||||
|
||||
def test_get_conversation_messages(self):
|
||||
response = self.chat_client.get_conversation_messages("test_user")
|
||||
self.assertIsInstance(response, list)
|
||||
|
||||
def test_get_conversations(self):
|
||||
response = self.chat_client.get_conversations("test_user")
|
||||
self.assertIsInstance(response, list)
|
||||
|
||||
|
||||
class TestCompletionClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.completion_client = CompletionClient(API_KEY)
|
||||
|
||||
def test_create_completion_message(self):
|
||||
response = self.completion_client.create_completion_message({}, "What's the weather like today?", "blocking", "test_user")
|
||||
self.assertIn("message_id", response)
|
||||
|
||||
|
||||
class TestDifyClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dify_client = DifyClient(API_KEY)
|
||||
|
||||
def test_message_feedback(self):
|
||||
response = self.dify_client.message_feedback("test_message_id", 5, "test_user")
|
||||
self.assertIn("success", response)
|
||||
|
||||
def test_get_application_parameters(self):
|
||||
response = self.dify_client.get_application_parameters("test_user")
|
||||
self.assertIsInstance(response, dict)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user