Feat/sdk vision support (#1531)

Co-authored-by: Joel <iamjoel007@gmail.com>
This commit is contained in:
Garfield Dai
2023-11-20 17:54:01 +08:00
committed by GitHub
parent ac3496e681
commit 5b7071e4b0
11 changed files with 319 additions and 62 deletions

View File

@@ -14,8 +14,7 @@ Write your code with sdk:
- completion generate with `blocking` response_mode
```
import json
```python
from dify_client import CompletionClient
api_key = "your_api_key"
@@ -24,18 +23,50 @@ api_key = "your_api_key"
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 = 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.text
result = json.loads(result)
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
@@ -55,10 +86,67 @@ for line in chat_response.iter_lines(decode_unicode=True):
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
```
import json
```python
from dify_client import ChatClient
api_key = "your_api_key"
@@ -69,32 +157,29 @@ 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)
print(parameters.json())
# 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)
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()
messages = json.loads(messages.text)
print('[messages]')
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 = 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)
print(rename_conversation_response.json())
```

View File

@@ -1 +1 @@
from dify_client.client import ChatClient, CompletionClient
from dify_client.client import ChatClient, CompletionClient, DifyClient

View File

@@ -6,14 +6,24 @@ class DifyClient:
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):
def _send_request(self, method, endpoint, json=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)
response = requests.request(method, url, json=json, params=params, headers=headers, stream=stream)
return response
def _send_request_with_files(self, method, endpoint, data, files):
headers = {
"Authorization": f"Bearer {self.api_key}"
}
url = f"{self.base_url}{endpoint}"
response = requests.request(method, url, data=data, headers=headers, files=files)
return response
@@ -28,30 +38,39 @@ class DifyClient:
params = {"user": user}
return self._send_request("GET", "/parameters", params=params)
class CompletionClient(DifyClient):
def create_completion_message(self, inputs, query, response_mode, user):
def file_upload(self, user, files):
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)
return self._send_request_with_files("POST", "/files/upload", data=data, files=files)
class CompletionClient(DifyClient):
def create_completion_message(self, inputs, response_mode, user, files=None):
data = {
"inputs": inputs,
"response_mode": response_mode,
"user": user,
"files": files
}
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):
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None, files=None):
data = {
"inputs": inputs,
"query": query,
"user": user,
"response_mode": response_mode
"response_mode": response_mode,
"files": files
}
if conversation_id:
data["conversation_id"] = conversation_id
return self._send_request("POST", "/chat-messages", data, stream=True if response_mode == "streaming" else False)
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}

View File

@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
setup(
name="dify-client",
version="0.1.8",
version="0.1.10",
author="Dify",
author_email="hello@dify.ai",
description="A package for interacting with the Dify Service-API",

View File

@@ -12,15 +12,33 @@ class TestChatClient(unittest.TestCase):
def test_create_chat_message(self):
response = self.chat_client.create_chat_message({}, "Hello, World!", "test_user")
self.assertIn("message_id", response)
self.assertIn("answer", response.text)
def test_create_chat_message_with_vision_model_by_remote_url(self):
files = [{
"type": "image",
"transfer_method": "remote_url",
"url": "your_image_url"
}]
response = self.chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
self.assertIn("answer", response.text)
def test_create_chat_message_with_vision_model_by_local_file(self):
files = [{
"type": "image",
"transfer_method": "local_file",
"upload_file_id": "your_file_id"
}]
response = self.chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
self.assertIn("answer", response.text)
def test_get_conversation_messages(self):
response = self.chat_client.get_conversation_messages("test_user")
self.assertIsInstance(response, list)
response = self.chat_client.get_conversation_messages("test_user", "your_conversation_id")
self.assertIn("answer", response.text)
def test_get_conversations(self):
response = self.chat_client.get_conversations("test_user")
self.assertIsInstance(response, list)
self.assertIn("data", response.text)
class TestCompletionClient(unittest.TestCase):
@@ -28,8 +46,29 @@ class TestCompletionClient(unittest.TestCase):
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)
response = self.completion_client.create_completion_message({"query": "What's the weather like today?"},
"blocking", "test_user")
self.assertIn("answer", response.text)
def test_create_completion_message_with_vision_model_by_remote_url(self):
files = [{
"type": "image",
"transfer_method": "remote_url",
"url": "your_image_url"
}]
response = self.completion_client.create_completion_message(
{"query": "Describe the picture."}, "blocking", "test_user", files)
self.assertIn("answer", response.text)
def test_create_completion_message_with_vision_model_by_local_file(self):
files = [{
"type": "image",
"transfer_method": "local_file",
"upload_file_id": "your_file_id"
}]
response = self.completion_client.create_completion_message(
{"query": "Describe the picture."}, "blocking", "test_user", files)
self.assertIn("answer", response.text)
class TestDifyClient(unittest.TestCase):
@@ -37,12 +76,24 @@ class TestDifyClient(unittest.TestCase):
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)
response = self.dify_client.message_feedback("your_message_id", 'like', "test_user")
self.assertIn("success", response.text)
def test_get_application_parameters(self):
response = self.dify_client.get_application_parameters("test_user")
self.assertIsInstance(response, dict)
self.assertIn("user_input_form", response.text)
def test_file_upload(self):
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 = self.dify_client.file_upload("test_user", files)
self.assertIn("name", response.text)
if __name__ == "__main__":