選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Dify PHP SDK
  2. This is the PHP SDK for the Dify API, which allows you to easily integrate Dify into your PHP applications.
  3. ## Requirements
  4. - PHP 7.2 or later
  5. - Guzzle HTTP client library
  6. ## Usage
  7. If you want to try the example, you can run `composer install` in this directory.
  8. In exist project, copy the `dify-client.php` to you project, and merge the following to your `composer.json` file, then run `composer install && composer dump-autoload` to install. Guzzle does not require 7.9, other versions have not been tested, but you can try.
  9. ```json
  10. {
  11. "require": {
  12. "guzzlehttp/guzzle": "^7.9"
  13. },
  14. "autoload": {
  15. "files": ["path/to/dify-client.php"]
  16. }
  17. }
  18. ```
  19. After installing the SDK, you can use it in your project like this:
  20. ```php
  21. <?php
  22. require 'vendor/autoload.php';
  23. $apiKey = 'your-api-key-here';
  24. $difyClient = new DifyClient($apiKey);
  25. // Create a completion client
  26. $completionClient = new CompletionClient($apiKey);
  27. $response = $completionClient->create_completion_message(array("query" => "Who are you?"), "blocking", "user_id");
  28. // Create a chat client
  29. $chatClient = new ChatClient($apiKey);
  30. $response = $chatClient->create_chat_message(array(), "Who are you?", "user_id", "blocking", $conversation_id);
  31. $fileForVision = [
  32. [
  33. "type" => "image",
  34. "transfer_method" => "remote_url",
  35. "url" => "your_image_url"
  36. ]
  37. ];
  38. // $fileForVision = [
  39. // [
  40. // "type" => "image",
  41. // "transfer_method" => "local_file",
  42. // "url" => "your_file_id"
  43. // ]
  44. // ];
  45. // Create a completion client with vision model like gpt-4-vision
  46. $response = $completionClient->create_completion_message(array("query" => "Describe this image."), "blocking", "user_id", $fileForVision);
  47. // Create a chat client with vision model like gpt-4-vision
  48. $response = $chatClient->create_chat_message(array(), "Describe this image.", "user_id", "blocking", $conversation_id, $fileForVision);
  49. // File Upload
  50. $fileForUpload = [
  51. [
  52. 'tmp_name' => '/path/to/file/filename.jpg',
  53. 'name' => 'filename.jpg'
  54. ]
  55. ];
  56. $response = $difyClient->file_upload("user_id", $fileForUpload);
  57. $result = json_decode($response->getBody(), true);
  58. echo 'upload_file_id: ' . $result['id'];
  59. // Fetch application parameters
  60. $response = $difyClient->get_application_parameters("user_id");
  61. // Provide feedback for a message
  62. $response = $difyClient->message_feedback($message_id, $rating, "user_id");
  63. // Other available methods:
  64. // - get_conversation_messages()
  65. // - get_conversations()
  66. // - rename_conversation()
  67. ```
  68. Replace 'your-api-key-here' with your actual Dify API key.
  69. ## License
  70. This SDK is released under the MIT License.