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

@@ -11,7 +11,7 @@ This is the PHP SDK for the Dify API, which allows you to easily integrate Dify
After installing the SDK, you can use it in your project like this:
```
```php
<?php
require 'vendor/autoload.php';
@@ -26,17 +26,50 @@ $difyClient = new DifyClient($apiKey);
// Create a completion client
$completionClient = new CompletionClient($apiKey);
$response = $completionClient->create_completion_message($inputs, $query, $response_mode, $user);
$response = $completionClient->create_completion_message(array("query" => "Who are you?"), "blocking", "user_id");
// Create a chat client
$chatClient = new ChatClient($apiKey);
$response = $chatClient->create_chat_message($inputs, $query, $user, $response_mode, $conversation_id);
$response = $chatClient->create_chat_message(array(), "Who are you?", "user_id", "blocking", $conversation_id);
$fileForVision = [
[
"type" => "image",
"transfer_method" => "remote_url",
"url" => "your_image_url"
]
];
// $fileForVision = [
// [
// "type" => "image",
// "transfer_method" => "local_file",
// "url" => "your_file_id"
// ]
// ];
// Create a completion client with vision model like gpt-4-vision
$response = $completionClient->create_completion_message(array("query" => "Describe this image."), "blocking", "user_id", $fileForVision);
// Create a chat client with vision model like gpt-4-vision
$response = $chatClient->create_chat_message(array(), "Describe this image.", "user_id", "blocking", $conversation_id, $fileForVision);
// File Upload
$fileForUpload = [
[
'tmp_name' => '/path/to/file/filename.jpg',
'name' => 'filename.jpg'
]
];
$response = $difyClient->file_upload("user_id", $fileForUpload);
$result = json_decode($response->getBody(), true);
echo 'upload_file_id: ' . $result['id'];
// Fetch application parameters
$response = $difyClient->get_application_parameters($user);
$response = $difyClient->get_application_parameters("user_id");
// Provide feedback for a message
$response = $difyClient->message_feedback($message_id, $rating, $user);
$response = $difyClient->message_feedback($message_id, $rating, "user_id");
// Other available methods:
// - get_conversation_messages()

View File

@@ -19,6 +19,13 @@ class DifyClient {
'Content-Type' => 'application/json',
],
]);
$this->file_client = new Client([
'base_uri' => $this->base_url,
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'multipart/form-data',
],
]);
}
protected function send_request($method, $endpoint, $data = null, $params = null, $stream = false) {
@@ -44,27 +51,57 @@ class DifyClient {
$params = ['user' => $user];
return $this->send_request('GET', 'parameters', null, $params);
}
public function file_upload($user, $files) {
$data = ['user' => $user];
$options = [
'multipart' => $this->prepareMultipart($data, $files)
];
return $this->file_client->request('POST', 'files/upload', $options);
}
protected function prepareMultipart($data, $files) {
$multipart = [];
foreach ($data as $key => $value) {
$multipart[] = [
'name' => $key,
'contents' => $value
];
}
foreach ($files as $file) {
$multipart[] = [
'name' => 'file',
'contents' => fopen($file['tmp_name'], 'r'),
'filename' => $file['name']
];
}
return $multipart;
}
}
class CompletionClient extends DifyClient {
public function create_completion_message($inputs, $query, $response_mode, $user) {
public function create_completion_message($inputs, $response_mode, $user, $files = null) {
$data = [
'inputs' => $inputs,
'query' => $query,
'response_mode' => $response_mode,
'user' => $user,
'files' => $files,
];
return $this->send_request('POST', 'completion-messages', $data, null, $response_mode === 'streaming');
}
}
class ChatClient extends DifyClient {
public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null) {
public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null, $files = null) {
$data = [
'inputs' => $inputs,
'query' => $query,
'user' => $user,
'response_mode' => $response_mode,
'files' => $files,
];
if ($conversation_id) {
$data['conversation_id'] = $conversation_id;