mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-10 03:16:51 +08:00
Feat/sdk vision support (#1531)
Co-authored-by: Joel <iamjoel007@gmail.com>
This commit is contained in:
@@ -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())
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user