Initial commit

This commit is contained in:
2025-10-14 14:17:21 +08:00
commit ac715a8b88
35011 changed files with 3834178 additions and 0 deletions

25
dify_1.0.0/sdks/README.md Normal file
View File

@@ -0,0 +1,25 @@
# SDK
## Java
https://github.com/langgenius/java-client/
## Go
https://github.com/langgenius/dify-sdk-go
## Ruby
https://github.com/langgenius/ruby-sdk
## Python
TODO move to another place
## PHP
TODO move to another place
## Node.js
TODO move to another place

View File

@@ -0,0 +1,48 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# npm
package-lock.json
# yarn
.pnp.cjs
.pnp.loader.mjs
.yarn/
.yarnrc.yml
# pmpm
pnpm-lock.yaml

View File

@@ -0,0 +1,63 @@
# Dify Node.js SDK
This is the Node.js SDK for the Dify API, which allows you to easily integrate Dify into your Node.js applications.
## Install
```bash
npm install dify-client
```
## Usage
After installing the SDK, you can use it in your project like this:
```js
import { DifyClient, ChatClient, CompletionClient } from 'dify-client'
const API_KEY = 'your-api-key-here'
const user = `random-user-id`
const query = 'Please tell me a short story in 10 words or less.'
const remote_url_files = [{
type: 'image',
transfer_method: 'remote_url',
url: 'your_url_address'
}]
// Create a completion client
const completionClient = new CompletionClient(API_KEY)
// Create a completion message
completionClient.createCompletionMessage({'query': query}, user)
// Create a completion message with vision model
completionClient.createCompletionMessage({'query': 'Describe the picture.'}, user, false, remote_url_files)
// Create a chat client
const chatClient = new ChatClient(API_KEY)
// Create a chat message in stream mode
const response = await chatClient.createChatMessage({}, query, user, true, null)
const stream = response.data;
stream.on('data', data => {
console.log(data);
});
stream.on('end', () => {
console.log('stream done');
});
// Create a chat message with vision model
chatClient.createChatMessage({}, 'Describe the picture.', user, false, null, remote_url_files)
// Fetch conversations
chatClient.getConversations(user)
// Fetch conversation messages
chatClient.getConversationMessages(conversationId, user)
// Rename conversation
chatClient.renameConversation(conversationId, name, user)
const client = new DifyClient(API_KEY)
// Fetch application parameters
client.getApplicationParameters(user)
// Provide feedback for a message
client.messageFeedback(messageId, rating, user)
```
Replace 'your-api-key-here' with your actual Dify API key.Replace 'your-app-id-here' with your actual Dify APP ID.
## License
This SDK is released under the MIT License.

View File

@@ -0,0 +1,91 @@
// Types.d.ts
export const BASE_URL: string;
export type RequestMethods = 'GET' | 'POST' | 'PATCH' | 'DELETE';
interface Params {
[key: string]: any;
}
interface HeaderParams {
[key: string]: string;
}
interface User {
}
export declare class DifyClient {
constructor(apiKey: string, baseUrl?: string);
updateApiKey(apiKey: string): void;
sendRequest(
method: RequestMethods,
endpoint: string,
data?: any,
params?: Params,
stream?: boolean,
headerParams?: HeaderParams
): Promise<any>;
messageFeedback(message_id: string, rating: number, user: User): Promise<any>;
getApplicationParameters(user: User): Promise<any>;
fileUpload(data: FormData): Promise<any>;
textToAudio(text: string ,user: string, streaming?: boolean): Promise<any>;
getMeta(user: User): Promise<any>;
}
export declare class CompletionClient extends DifyClient {
createCompletionMessage(
inputs: any,
user: User,
stream?: boolean,
files?: File[] | null
): Promise<any>;
}
export declare class ChatClient extends DifyClient {
createChatMessage(
inputs: any,
query: string,
user: User,
stream?: boolean,
conversation_id?: string | null,
files?: File[] | null
): Promise<any>;
getSuggested(message_id: string, user: User): Promise<any>;
stopMessage(task_id: string, user: User) : Promise<any>;
getConversations(
user: User,
first_id?: string | null,
limit?: number | null,
pinned?: boolean | null
): Promise<any>;
getConversationMessages(
user: User,
conversation_id?: string,
first_id?: string | null,
limit?: number | null
): Promise<any>;
renameConversation(conversation_id: string, name: string, user: User,auto_generate:boolean): Promise<any>;
deleteConversation(conversation_id: string, user: User): Promise<any>;
audioToText(data: FormData): Promise<any>;
}
export declare class WorkflowClient extends DifyClient {
run(inputs: any, user: User, stream?: boolean,): Promise<any>;
stop(task_id: string, user: User): Promise<any>;
}

View File

@@ -0,0 +1,360 @@
import axios from "axios";
export const BASE_URL = "https://api.dify.ai/v1";
export const routes = {
// app's
feedback: {
method: "POST",
url: (message_id) => `/messages/${message_id}/feedbacks`,
},
application: {
method: "GET",
url: () => `/parameters`,
},
fileUpload: {
method: "POST",
url: () => `/files/upload`,
},
textToAudio: {
method: "POST",
url: () => `/text-to-audio`,
},
getMeta: {
method: "GET",
url: () => `/meta`,
},
// completion's
createCompletionMessage: {
method: "POST",
url: () => `/completion-messages`,
},
// chat's
createChatMessage: {
method: "POST",
url: () => `/chat-messages`,
},
getSuggested:{
method: "GET",
url: (message_id) => `/messages/${message_id}/suggested`,
},
stopChatMessage: {
method: "POST",
url: (task_id) => `/chat-messages/${task_id}/stop`,
},
getConversations: {
method: "GET",
url: () => `/conversations`,
},
getConversationMessages: {
method: "GET",
url: () => `/messages`,
},
renameConversation: {
method: "POST",
url: (conversation_id) => `/conversations/${conversation_id}/name`,
},
deleteConversation: {
method: "DELETE",
url: (conversation_id) => `/conversations/${conversation_id}`,
},
audioToText: {
method: "POST",
url: () => `/audio-to-text`,
},
// workflows
runWorkflow: {
method: "POST",
url: () => `/workflows/run`,
},
stopWorkflow: {
method: "POST",
url: (task_id) => `/workflows/${task_id}/stop`,
}
};
export class DifyClient {
constructor(apiKey, baseUrl = BASE_URL) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}
updateApiKey(apiKey) {
this.apiKey = apiKey;
}
async sendRequest(
method,
endpoint,
data = null,
params = null,
stream = false,
headerParams = {}
) {
const headers = {
...{
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
...headerParams
};
const url = `${this.baseUrl}${endpoint}`;
let response;
if (stream) {
response = await axios({
method,
url,
data,
params,
headers,
responseType: "stream",
});
} else {
response = await axios({
method,
url,
...(method !== "GET" && { data }),
params,
headers,
responseType: "json",
});
}
return response;
}
messageFeedback(message_id, rating, user) {
const data = {
rating,
user,
};
return this.sendRequest(
routes.feedback.method,
routes.feedback.url(message_id),
data
);
}
getApplicationParameters(user) {
const params = { user };
return this.sendRequest(
routes.application.method,
routes.application.url(),
null,
params
);
}
fileUpload(data) {
return this.sendRequest(
routes.fileUpload.method,
routes.fileUpload.url(),
data,
null,
false,
{
"Content-Type": 'multipart/form-data'
}
);
}
textToAudio(text, user, streaming = false) {
const data = {
text,
user,
streaming
};
return this.sendRequest(
routes.textToAudio.method,
routes.textToAudio.url(),
data,
null,
streaming
);
}
getMeta(user) {
const params = { user };
return this.sendRequest(
routes.meta.method,
routes.meta.url(),
null,
params
);
}
}
export class CompletionClient extends DifyClient {
createCompletionMessage(inputs, user, stream = false, files = null) {
const data = {
inputs,
user,
response_mode: stream ? "streaming" : "blocking",
files,
};
return this.sendRequest(
routes.createCompletionMessage.method,
routes.createCompletionMessage.url(),
data,
null,
stream
);
}
runWorkflow(inputs, user, stream = false, files = null) {
const data = {
inputs,
user,
response_mode: stream ? "streaming" : "blocking",
};
return this.sendRequest(
routes.runWorkflow.method,
routes.runWorkflow.url(),
data,
null,
stream
);
}
}
export class ChatClient extends DifyClient {
createChatMessage(
inputs,
query,
user,
stream = false,
conversation_id = null,
files = null
) {
const data = {
inputs,
query,
user,
response_mode: stream ? "streaming" : "blocking",
files,
};
if (conversation_id) data.conversation_id = conversation_id;
return this.sendRequest(
routes.createChatMessage.method,
routes.createChatMessage.url(),
data,
null,
stream
);
}
getSuggested(message_id, user) {
const data = { user };
return this.sendRequest(
routes.getSuggested.method,
routes.getSuggested.url(message_id),
data
);
}
stopMessage(task_id, user) {
const data = { user };
return this.sendRequest(
routes.stopChatMessage.method,
routes.stopChatMessage.url(task_id),
data
);
}
getConversations(user, first_id = null, limit = null, pinned = null) {
const params = { user, first_id: first_id, limit, pinned };
return this.sendRequest(
routes.getConversations.method,
routes.getConversations.url(),
null,
params
);
}
getConversationMessages(
user,
conversation_id = "",
first_id = null,
limit = null
) {
const params = { user };
if (conversation_id) params.conversation_id = conversation_id;
if (first_id) params.first_id = first_id;
if (limit) params.limit = limit;
return this.sendRequest(
routes.getConversationMessages.method,
routes.getConversationMessages.url(),
null,
params
);
}
renameConversation(conversation_id, name, user, auto_generate) {
const data = { name, user, auto_generate };
return this.sendRequest(
routes.renameConversation.method,
routes.renameConversation.url(conversation_id),
data
);
}
deleteConversation(conversation_id, user) {
const data = { user };
return this.sendRequest(
routes.deleteConversation.method,
routes.deleteConversation.url(conversation_id),
data
);
}
audioToText(data) {
return this.sendRequest(
routes.audioToText.method,
routes.audioToText.url(),
data,
null,
false,
{
"Content-Type": 'multipart/form-data'
}
);
}
}
export class WorkflowClient extends DifyClient {
run(inputs,user,stream) {
const data = {
inputs,
response_mode: stream ? "streaming" : "blocking",
user
};
return this.sendRequest(
routes.runWorkflow.method,
routes.runWorkflow.url(),
data,
null,
stream
);
}
stop(task_id, user) {
const data = { user };
return this.sendRequest(
routes.stopWorkflow.method,
routes.stopWorkflow.url(task_id),
data
);
}
}

View File

@@ -0,0 +1,65 @@
import { DifyClient, BASE_URL, routes } from ".";
import axios from 'axios'
jest.mock('axios')
describe('Client', () => {
let difyClient
beforeEach(() => {
difyClient = new DifyClient('test')
})
test('should create a client', () => {
expect(difyClient).toBeDefined();
})
// test updateApiKey
test('should update the api key', () => {
difyClient.updateApiKey('test2');
expect(difyClient.apiKey).toBe('test2');
})
});
describe('Send Requests', () => {
let difyClient
beforeEach(() => {
difyClient = new DifyClient('test')
})
afterEach(() => {
jest.resetAllMocks()
})
it('should make a successful request to the application parameter', async () => {
const method = 'GET'
const endpoint = routes.application.url
const expectedResponse = { data: 'response' }
axios.mockResolvedValue(expectedResponse)
await difyClient.sendRequest(method, endpoint)
expect(axios).toHaveBeenCalledWith({
method,
url: `${BASE_URL}${endpoint}`,
params: null,
headers: {
Authorization: `Bearer ${difyClient.apiKey}`,
'Content-Type': 'application/json',
},
responseType: 'json',
})
})
it('should handle errors from the API', async () => {
const method = 'GET'
const endpoint = '/test-endpoint'
const errorMessage = 'Request failed with status code 404'
axios.mockRejectedValue(new Error(errorMessage))
await expect(difyClient.sendRequest(method, endpoint)).rejects.toThrow(
errorMessage
)
})
})

View File

@@ -0,0 +1,35 @@
{
"name": "dify-client",
"version": "2.3.2",
"description": "This is the Node.js SDK for the Dify.AI API, which allows you to easily integrate Dify.AI into your Node.js applications.",
"main": "index.js",
"type": "module",
"types":"index.d.ts",
"keywords": [
"Dify",
"Dify.AI",
"LLM"
],
"author": "Joel",
"contributors": [
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
],
"license": "MIT",
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
},
"dependencies": {
"axios": "^1.3.5"
},
"devDependencies": {
"@babel/core": "^7.21.8",
"@babel/preset-env": "^7.21.5",
"babel-jest": "^29.5.0",
"jest": "^29.5.0"
}
}

View File

@@ -0,0 +1,84 @@
# Dify PHP SDK
This is the PHP SDK for the Dify API, which allows you to easily integrate Dify into your PHP applications.
## Requirements
- PHP 7.2 or later
- Guzzle HTTP client library
## Usage
After installing the SDK, you can use it in your project like this:
```php
<?php
require 'vendor/autoload.php';
use YourVendorName\DifyPHP\DifyClient;
use YourVendorName\DifyPHP\CompletionClient;
use YourVendorName\DifyPHP\ChatClient;
$apiKey = 'your-api-key-here';
$difyClient = new DifyClient($apiKey);
// Create a completion client
$completionClient = new CompletionClient($apiKey);
$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(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_id");
// Provide feedback for a message
$response = $difyClient->message_feedback($message_id, $rating, "user_id");
// Other available methods:
// - get_conversation_messages()
// - get_conversations()
// - rename_conversation()
```
Replace 'your-api-key-here' with your actual Dify API key.
## License
This SDK is released under the MIT License.

View File

@@ -0,0 +1,216 @@
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
class DifyClient {
protected $api_key;
protected $base_url;
protected $client;
public function __construct($api_key, $base_url = null) {
$this->api_key = $api_key;
$this->base_url = $base_url ?? "https://api.dify.ai/v1/";
$this->client = new Client([
'base_uri' => $this->base_url,
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'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) {
$options = [
'json' => $data,
'query' => $params,
'stream' => $stream,
];
$response = $this->client->request($method, $endpoint, $options);
return $response;
}
public function message_feedback($message_id, $rating, $user) {
$data = [
'rating' => $rating,
'user' => $user,
];
return $this->send_request('POST', "messages/{$message_id}/feedbacks", $data);
}
public function get_application_parameters($user) {
$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;
}
public function text_to_audio($text, $user, $streaming = false) {
$data = [
'text' => $text,
'user' => $user,
'streaming' => $streaming
];
return $this->send_request('POST', 'text-to-audio', $data);
}
public function get_meta($user) {
$params = [
'user' => $user
];
return $this->send_request('GET', 'meta',null, $params);
}
}
class CompletionClient extends DifyClient {
public function create_completion_message($inputs, $response_mode, $user, $files = null) {
$data = [
'inputs' => $inputs,
'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, $files = null) {
$data = [
'inputs' => $inputs,
'query' => $query,
'user' => $user,
'response_mode' => $response_mode,
'files' => $files,
];
if ($conversation_id) {
$data['conversation_id'] = $conversation_id;
}
return $this->send_request('POST', 'chat-messages', $data, null, $response_mode === 'streaming');
}
public function get_suggestions($message_id, $user) {
$params = [
'user' => $user
]
return $this->send_request('GET', "messages/{$message_id}/suggested", null, $params);
}
public function stop_message($task_id, $user) {
$data = ['user' => $user];
return $this->send_request('POST', "chat-messages/{$task_id}/stop", $data);
}
public function get_conversations($user, $first_id = null, $limit = null, $pinned = null) {
$params = [
'user' => $user,
'first_id' => $first_id,
'limit' => $limit,
'pinned'=> $pinned,
];
return $this->send_request('GET', 'conversations', null, $params);
}
public function get_conversation_messages($user, $conversation_id = null, $first_id = null, $limit = null) {
$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 $this->send_request('GET', 'messages', null, $params);
}
public function rename_conversation($conversation_id, $name,$auto_generate, $user) {
$data = [
'name' => $name,
'user' => $user,
'auto_generate' => $auto_generate
];
return $this->send_request('PATCH', "conversations/{$conversation_id}", $data);
}
public function delete_conversation($conversation_id, $user) {
$data = [
'user' => $user,
];
return $this->send_request('DELETE', "conversations/{$conversation_id}", $data);
}
public function audio_to_text($audio_file, $user) {
$data = [
'user' => $user,
];
$options = [
'multipart' => $this->prepareMultipart($data, $files)
];
return $this->file_client->request('POST', 'audio-to-text', $options);
}
}
class WorkflowClient extends DifyClient{
public function run($inputs, $response_mode, $user) {
$data = [
'inputs' => $inputs,
'response_mode' => $response_mode,
'user' => $user,
];
return $this->send_request('POST', 'workflows/run', $data);
}
public function stop($task_id, $user) {
$data = [
'user' => $user,
];
return $this->send_request('POST', "workflows/tasks/{$task_id}/stop",$data);
}
}

View 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.

View File

@@ -0,0 +1 @@
recursive-include dify_client *.py

View File

@@ -0,0 +1,185 @@
# 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
```python
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": "What's the weather like today?"},
response_mode="blocking", user="user_id")
completion_response.raise_for_status()
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
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'))
```
- 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
```python
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()
print('[parameters]')
print(parameters.json())
# Get Conversation List (only for chat)
conversations = client.get_conversations(user="user_id")
conversations.raise_for_status()
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()
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.raise_for_status()
print('[rename result]')
print(rename_conversation_response.json())
```

View 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/*

View File

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

View File

@@ -0,0 +1,459 @@
import json
import requests
class DifyClient:
def __init__(self, api_key, base_url: str = "https://api.dify.ai/v1"):
self.api_key = api_key
self.base_url = base_url
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=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
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)
def file_upload(self, user, files):
data = {"user": user}
return self._send_request_with_files(
"POST", "/files/upload", data=data, files=files
)
def text_to_audio(self, text: str, user: str, streaming: bool = False):
data = {"text": text, "user": user, "streaming": streaming}
return self._send_request("POST", "/text-to-audio", data=data)
def get_meta(self, user):
params = {"user": user}
return self._send_request("GET", "/meta", params=params)
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,
files=None,
):
data = {
"inputs": inputs,
"query": query,
"user": user,
"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,
)
def get_suggested(self, message_id, user: str):
params = {"user": user}
return self._send_request(
"GET", f"/messages/{message_id}/suggested", params=params
)
def stop_message(self, task_id, user):
data = {"user": user}
return self._send_request("POST", f"/chat-messages/{task_id}/stop", data)
def get_conversations(self, user, last_id=None, limit=None, pinned=None):
params = {"user": user, "last_id": last_id, "limit": limit, "pinned": pinned}
return self._send_request("GET", "/conversations", params=params)
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 rename_conversation(
self, conversation_id, name, auto_generate: bool, user: str
):
data = {"name": name, "auto_generate": auto_generate, "user": user}
return self._send_request(
"POST", f"/conversations/{conversation_id}/name", data
)
def delete_conversation(self, conversation_id, user):
data = {"user": user}
return self._send_request("DELETE", f"/conversations/{conversation_id}", data)
def audio_to_text(self, audio_file, user):
data = {"user": user}
files = {"audio_file": audio_file}
return self._send_request_with_files("POST", "/audio-to-text", data, files)
class WorkflowClient(DifyClient):
def run(
self, inputs: dict, response_mode: str = "streaming", user: str = "abc-123"
):
data = {"inputs": inputs, "response_mode": response_mode, "user": user}
return self._send_request("POST", "/workflows/run", data)
def stop(self, task_id, user):
data = {"user": user}
return self._send_request("POST", f"/workflows/tasks/{task_id}/stop", data)
def get_result(self, workflow_run_id):
return self._send_request("GET", f"/workflows/run/{workflow_run_id}")
class KnowledgeBaseClient(DifyClient):
def __init__(
self,
api_key,
base_url: str = "https://api.dify.ai/v1",
dataset_id: str | None = None,
):
"""
Construct a KnowledgeBaseClient object.
Args:
api_key (str): API key of Dify.
base_url (str, optional): Base URL of Dify API. Defaults to 'https://api.dify.ai/v1'.
dataset_id (str, optional): ID of the dataset. Defaults to None. You don't need this if you just want to
create a new dataset. or list datasets. otherwise you need to set this.
"""
super().__init__(api_key=api_key, base_url=base_url)
self.dataset_id = dataset_id
def _get_dataset_id(self):
if self.dataset_id is None:
raise ValueError("dataset_id is not set")
return self.dataset_id
def create_dataset(self, name: str, **kwargs):
return self._send_request("POST", "/datasets", {"name": name}, **kwargs)
def list_datasets(self, page: int = 1, page_size: int = 20, **kwargs):
return self._send_request(
"GET", f"/datasets?page={page}&limit={page_size}", **kwargs
)
def create_document_by_text(
self, name, text, extra_params: dict | None = None, **kwargs
):
"""
Create a document by text.
:param name: Name of the document
:param text: Text content of the document
:param extra_params: extra parameters pass to the API, such as indexing_technique, process_rule. (optional)
e.g.
{
'indexing_technique': 'high_quality',
'process_rule': {
'rules': {
'pre_processing_rules': [
{'id': 'remove_extra_spaces', 'enabled': True},
{'id': 'remove_urls_emails', 'enabled': True}
],
'segmentation': {
'separator': '\n',
'max_tokens': 500
}
},
'mode': 'custom'
}
}
:return: Response from the API
"""
data = {
"indexing_technique": "high_quality",
"process_rule": {"mode": "automatic"},
"name": name,
"text": text,
}
if extra_params is not None and isinstance(extra_params, dict):
data.update(extra_params)
url = f"/datasets/{self._get_dataset_id()}/document/create_by_text"
return self._send_request("POST", url, json=data, **kwargs)
def update_document_by_text(
self, document_id, name, text, extra_params: dict | None = None, **kwargs
):
"""
Update a document by text.
:param document_id: ID of the document
:param name: Name of the document
:param text: Text content of the document
:param extra_params: extra parameters pass to the API, such as indexing_technique, process_rule. (optional)
e.g.
{
'indexing_technique': 'high_quality',
'process_rule': {
'rules': {
'pre_processing_rules': [
{'id': 'remove_extra_spaces', 'enabled': True},
{'id': 'remove_urls_emails', 'enabled': True}
],
'segmentation': {
'separator': '\n',
'max_tokens': 500
}
},
'mode': 'custom'
}
}
:return: Response from the API
"""
data = {"name": name, "text": text}
if extra_params is not None and isinstance(extra_params, dict):
data.update(extra_params)
url = (
f"/datasets/{self._get_dataset_id()}/documents/{document_id}/update_by_text"
)
return self._send_request("POST", url, json=data, **kwargs)
def create_document_by_file(
self, file_path, original_document_id=None, extra_params: dict | None = None
):
"""
Create a document by file.
:param file_path: Path to the file
:param original_document_id: pass this ID if you want to replace the original document (optional)
:param extra_params: extra parameters pass to the API, such as indexing_technique, process_rule. (optional)
e.g.
{
'indexing_technique': 'high_quality',
'process_rule': {
'rules': {
'pre_processing_rules': [
{'id': 'remove_extra_spaces', 'enabled': True},
{'id': 'remove_urls_emails', 'enabled': True}
],
'segmentation': {
'separator': '\n',
'max_tokens': 500
}
},
'mode': 'custom'
}
}
:return: Response from the API
"""
files = {"file": open(file_path, "rb")}
data = {
"process_rule": {"mode": "automatic"},
"indexing_technique": "high_quality",
}
if extra_params is not None and isinstance(extra_params, dict):
data.update(extra_params)
if original_document_id is not None:
data["original_document_id"] = original_document_id
url = f"/datasets/{self._get_dataset_id()}/document/create_by_file"
return self._send_request_with_files(
"POST", url, {"data": json.dumps(data)}, files
)
def update_document_by_file(
self, document_id, file_path, extra_params: dict | None = None
):
"""
Update a document by file.
:param document_id: ID of the document
:param file_path: Path to the file
:param extra_params: extra parameters pass to the API, such as indexing_technique, process_rule. (optional)
e.g.
{
'indexing_technique': 'high_quality',
'process_rule': {
'rules': {
'pre_processing_rules': [
{'id': 'remove_extra_spaces', 'enabled': True},
{'id': 'remove_urls_emails', 'enabled': True}
],
'segmentation': {
'separator': '\n',
'max_tokens': 500
}
},
'mode': 'custom'
}
}
:return:
"""
files = {"file": open(file_path, "rb")}
data = {}
if extra_params is not None and isinstance(extra_params, dict):
data.update(extra_params)
url = (
f"/datasets/{self._get_dataset_id()}/documents/{document_id}/update_by_file"
)
return self._send_request_with_files(
"POST", url, {"data": json.dumps(data)}, files
)
def batch_indexing_status(self, batch_id: str, **kwargs):
"""
Get the status of the batch indexing.
:param batch_id: ID of the batch uploading
:return: Response from the API
"""
url = f"/datasets/{self._get_dataset_id()}/documents/{batch_id}/indexing-status"
return self._send_request("GET", url, **kwargs)
def delete_dataset(self):
"""
Delete this dataset.
:return: Response from the API
"""
url = f"/datasets/{self._get_dataset_id()}"
return self._send_request("DELETE", url)
def delete_document(self, document_id):
"""
Delete a document.
:param document_id: ID of the document
:return: Response from the API
"""
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}"
return self._send_request("DELETE", url)
def list_documents(
self,
page: int | None = None,
page_size: int | None = None,
keyword: str | None = None,
**kwargs,
):
"""
Get a list of documents in this dataset.
:return: Response from the API
"""
params = {}
if page is not None:
params["page"] = page
if page_size is not None:
params["limit"] = page_size
if keyword is not None:
params["keyword"] = keyword
url = f"/datasets/{self._get_dataset_id()}/documents"
return self._send_request("GET", url, params=params, **kwargs)
def add_segments(self, document_id, segments, **kwargs):
"""
Add segments to a document.
:param document_id: ID of the document
:param segments: List of segments to add, example: [{"content": "1", "answer": "1", "keyword": ["a"]}]
:return: Response from the API
"""
data = {"segments": segments}
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments"
return self._send_request("POST", url, json=data, **kwargs)
def query_segments(
self,
document_id,
keyword: str | None = None,
status: str | None = None,
**kwargs,
):
"""
Query segments in this document.
:param document_id: ID of the document
:param keyword: query keyword, optional
:param status: status of the segment, optional, e.g. completed
"""
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments"
params = {}
if keyword is not None:
params["keyword"] = keyword
if status is not None:
params["status"] = status
if "params" in kwargs:
params.update(kwargs["params"])
return self._send_request("GET", url, params=params, **kwargs)
def delete_document_segment(self, document_id, segment_id):
"""
Delete a segment from a document.
:param document_id: ID of the document
:param segment_id: ID of the segment
:return: Response from the API
"""
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments/{segment_id}"
return self._send_request("DELETE", url)
def update_document_segment(self, document_id, segment_id, segment_data, **kwargs):
"""
Update a segment in a document.
:param document_id: ID of the document
:param segment_id: ID of the segment
:param segment_data: Data of the segment, example: {"content": "1", "answer": "1", "keyword": ["a"], "enabled": True}
:return: Response from the API
"""
data = {"segment": segment_data}
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments/{segment_id}"
return self._send_request("POST", url, json=data, **kwargs)

View File

@@ -0,0 +1,26 @@
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.12",
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,
)

View File

@@ -0,0 +1,274 @@
import os
import time
import unittest
from dify_client.client import (
ChatClient,
CompletionClient,
DifyClient,
KnowledgeBaseClient,
)
API_KEY = os.environ.get("API_KEY")
APP_ID = os.environ.get("APP_ID")
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.dify.ai/v1")
FILE_PATH_BASE = os.path.dirname(__file__)
class TestKnowledgeBaseClient(unittest.TestCase):
def setUp(self):
self.knowledge_base_client = KnowledgeBaseClient(API_KEY, base_url=API_BASE_URL)
self.README_FILE_PATH = os.path.abspath(
os.path.join(FILE_PATH_BASE, "../README.md")
)
self.dataset_id = None
self.document_id = None
self.segment_id = None
self.batch_id = None
def _get_dataset_kb_client(self):
self.assertIsNotNone(self.dataset_id)
return KnowledgeBaseClient(
API_KEY, base_url=API_BASE_URL, dataset_id=self.dataset_id
)
def test_001_create_dataset(self):
response = self.knowledge_base_client.create_dataset(name="test_dataset")
data = response.json()
self.assertIn("id", data)
self.dataset_id = data["id"]
self.assertEqual("test_dataset", data["name"])
# the following tests require to be executed in order because they use
# the dataset/document/segment ids from the previous test
self._test_002_list_datasets()
self._test_003_create_document_by_text()
time.sleep(1)
self._test_004_update_document_by_text()
# self._test_005_batch_indexing_status()
time.sleep(1)
self._test_006_update_document_by_file()
time.sleep(1)
self._test_007_list_documents()
self._test_008_delete_document()
self._test_009_create_document_by_file()
time.sleep(1)
self._test_010_add_segments()
self._test_011_query_segments()
self._test_012_update_document_segment()
self._test_013_delete_document_segment()
self._test_014_delete_dataset()
def _test_002_list_datasets(self):
response = self.knowledge_base_client.list_datasets()
data = response.json()
self.assertIn("data", data)
self.assertIn("total", data)
def _test_003_create_document_by_text(self):
client = self._get_dataset_kb_client()
response = client.create_document_by_text("test_document", "test_text")
data = response.json()
self.assertIn("document", data)
self.document_id = data["document"]["id"]
self.batch_id = data["batch"]
def _test_004_update_document_by_text(self):
client = self._get_dataset_kb_client()
self.assertIsNotNone(self.document_id)
response = client.update_document_by_text(
self.document_id, "test_document_updated", "test_text_updated"
)
data = response.json()
self.assertIn("document", data)
self.assertIn("batch", data)
self.batch_id = data["batch"]
def _test_005_batch_indexing_status(self):
client = self._get_dataset_kb_client()
response = client.batch_indexing_status(self.batch_id)
response.json()
self.assertEqual(response.status_code, 200)
def _test_006_update_document_by_file(self):
client = self._get_dataset_kb_client()
self.assertIsNotNone(self.document_id)
response = client.update_document_by_file(
self.document_id, self.README_FILE_PATH
)
data = response.json()
self.assertIn("document", data)
self.assertIn("batch", data)
self.batch_id = data["batch"]
def _test_007_list_documents(self):
client = self._get_dataset_kb_client()
response = client.list_documents()
data = response.json()
self.assertIn("data", data)
def _test_008_delete_document(self):
client = self._get_dataset_kb_client()
self.assertIsNotNone(self.document_id)
response = client.delete_document(self.document_id)
data = response.json()
self.assertIn("result", data)
self.assertEqual("success", data["result"])
def _test_009_create_document_by_file(self):
client = self._get_dataset_kb_client()
response = client.create_document_by_file(self.README_FILE_PATH)
data = response.json()
self.assertIn("document", data)
self.document_id = data["document"]["id"]
self.batch_id = data["batch"]
def _test_010_add_segments(self):
client = self._get_dataset_kb_client()
response = client.add_segments(
self.document_id, [{"content": "test text segment 1"}]
)
data = response.json()
self.assertIn("data", data)
self.assertGreater(len(data["data"]), 0)
segment = data["data"][0]
self.segment_id = segment["id"]
def _test_011_query_segments(self):
client = self._get_dataset_kb_client()
response = client.query_segments(self.document_id)
data = response.json()
self.assertIn("data", data)
self.assertGreater(len(data["data"]), 0)
def _test_012_update_document_segment(self):
client = self._get_dataset_kb_client()
self.assertIsNotNone(self.segment_id)
response = client.update_document_segment(
self.document_id,
self.segment_id,
{"content": "test text segment 1 updated"},
)
data = response.json()
self.assertIn("data", data)
self.assertGreater(len(data["data"]), 0)
segment = data["data"]
self.assertEqual("test text segment 1 updated", segment["content"])
def _test_013_delete_document_segment(self):
client = self._get_dataset_kb_client()
self.assertIsNotNone(self.segment_id)
response = client.delete_document_segment(self.document_id, self.segment_id)
data = response.json()
self.assertIn("result", data)
self.assertEqual("success", data["result"])
def _test_014_delete_dataset(self):
client = self._get_dataset_kb_client()
response = client.delete_dataset()
self.assertEqual(204, response.status_code)
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("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", "your_conversation_id"
)
self.assertIn("answer", response.text)
def test_get_conversations(self):
response = self.chat_client.get_conversations("test_user")
self.assertIn("data", response.text)
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(
{"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):
def setUp(self):
self.dify_client = DifyClient(API_KEY)
def test_message_feedback(self):
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.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__":
unittest.main()