add workflowClient ,fix rename bug (#7352)

This commit is contained in:
Krasus.Chen 2024-08-16 20:21:08 +08:00 committed by GitHub
parent 5a729a69cd
commit 7d4a0a417a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 239 additions and 67 deletions

View File

@ -33,6 +33,10 @@ export declare class DifyClient {
getApplicationParameters(user: User): Promise<any>; getApplicationParameters(user: User): Promise<any>;
fileUpload(data: FormData): 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 { export declare class CompletionClient extends DifyClient {
@ -54,6 +58,18 @@ export declare class ChatClient extends DifyClient {
files?: File[] | null files?: File[] | null
): Promise<any>; ): 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( getConversationMessages(
user: User, user: User,
conversation_id?: string, conversation_id?: string,
@ -61,9 +77,15 @@ export declare class ChatClient extends DifyClient {
limit?: number | null limit?: number | null
): Promise<any>; ): Promise<any>;
getConversations(user: User, first_id?: string | null, limit?: number | null, pinned?: boolean | null): Promise<any>; renameConversation(conversation_id: string, name: string, user: User,auto_generate:boolean): Promise<any>;
renameConversation(conversation_id: string, name: string, user: User): Promise<any>;
deleteConversation(conversation_id: string, user: User): 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

@ -2,30 +2,55 @@ import axios from "axios";
export const BASE_URL = "https://api.dify.ai/v1"; export const BASE_URL = "https://api.dify.ai/v1";
export const routes = { export const routes = {
application: { // app's
method: "GET",
url: () => `/parameters`,
},
feedback: { feedback: {
method: "POST", method: "POST",
url: (message_id) => `/messages/${message_id}/feedbacks`, 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: { createCompletionMessage: {
method: "POST", method: "POST",
url: () => `/completion-messages`, url: () => `/completion-messages`,
}, },
// chat's
createChatMessage: { createChatMessage: {
method: "POST", method: "POST",
url: () => `/chat-messages`, url: () => `/chat-messages`,
}, },
getConversationMessages: { getSuggested:{
method: "GET", method: "GET",
url: () => `/messages`, url: (message_id) => `/messages/${message_id}/suggested`,
},
stopChatMessage: {
method: "POST",
url: (task_id) => `/chat-messages/${task_id}/stop`,
}, },
getConversations: { getConversations: {
method: "GET", method: "GET",
url: () => `/conversations`, url: () => `/conversations`,
}, },
getConversationMessages: {
method: "GET",
url: () => `/messages`,
},
renameConversation: { renameConversation: {
method: "POST", method: "POST",
url: (conversation_id) => `/conversations/${conversation_id}/name`, url: (conversation_id) => `/conversations/${conversation_id}/name`,
@ -34,14 +59,21 @@ export const routes = {
method: "DELETE", method: "DELETE",
url: (conversation_id) => `/conversations/${conversation_id}`, url: (conversation_id) => `/conversations/${conversation_id}`,
}, },
fileUpload: { audioToText: {
method: "POST", method: "POST",
url: () => `/files/upload`, url: () => `/audio-to-text`,
}, },
// workflows
runWorkflow: { runWorkflow: {
method: "POST", method: "POST",
url: () => `/workflows/run`, url: () => `/workflows/run`,
}, },
stopWorkflow: {
method: "POST",
url: (task_id) => `/workflows/${task_id}/stop`,
}
}; };
export class DifyClient { export class DifyClient {
@ -129,6 +161,31 @@ export class DifyClient {
} }
); );
} }
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 { export class CompletionClient extends DifyClient {
@ -191,6 +248,34 @@ export class ChatClient extends DifyClient {
); );
} }
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( getConversationMessages(
user, user,
conversation_id = "", conversation_id = "",
@ -213,16 +298,6 @@ export class ChatClient extends DifyClient {
); );
} }
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
);
}
renameConversation(conversation_id, name, user, auto_generate) { renameConversation(conversation_id, name, user, auto_generate) {
const data = { name, user, auto_generate }; const data = { name, user, auto_generate };
return this.sendRequest( return this.sendRequest(
@ -240,4 +315,46 @@ export class ChatClient extends DifyClient {
data 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

@ -129,6 +129,28 @@ class ChatClient extends DifyClient {
return $this->send_request('POST', 'chat-messages', $data, null, $response_mode === 'streaming'); 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) { public function get_conversation_messages($user, $conversation_id = null, $first_id = null, $limit = null) {
$params = ['user' => $user]; $params = ['user' => $user];
@ -145,34 +167,22 @@ class ChatClient extends DifyClient {
return $this->send_request('GET', 'messages', null, $params); return $this->send_request('GET', 'messages', null, $params);
} }
public function rename_conversation($conversation_id, $name,$auto_generate, $user) {
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 rename_conversation($conversation_id, $name, $user) {
$data = [ $data = [
'name' => $name, 'name' => $name,
'user' => $user, 'user' => $user,
'auto_generate' => $auto_generate
]; ];
return $this->send_request('PATCH', "conversations/{$conversation_id}", $data); 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) { public function audio_to_text($audio_file, $user) {
$data = [ $data = [
'user' => $user, 'user' => $user,
@ -184,11 +194,23 @@ class ChatClient extends DifyClient {
} }
}
public function get_suggestions($message_id, $user) { class WorkflowClient extends DifyClient{
$params = [ public function run($inputs, $response_mode, $user) {
'user' => $user $data = [
] 'inputs' => $inputs,
return $this->send_request('GET', "messages/{$message_id}/suggested", null, $params); '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

@ -17,6 +17,7 @@ class DifyClient:
return response return response
def _send_request_with_files(self, method, endpoint, data, files): def _send_request_with_files(self, method, endpoint, data, files):
headers = { headers = {
"Authorization": f"Bearer {self.api_key}" "Authorization": f"Bearer {self.api_key}"
@ -83,12 +84,18 @@ class ChatClient(DifyClient):
return self._send_request("POST", "/chat-messages", data, return self._send_request("POST", "/chat-messages", data,
stream=True if response_mode == "streaming" else False) 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): def stop_message(self, task_id, user):
data = {"user": user} data = {"user": user}
return self._send_request("POST", f"/chat-messages/{task_id}/stop", data) 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): def get_conversation_messages(self, user, conversation_id=None, first_id=None, limit=None):
params = {"user": user} params = {"user": user}
@ -102,21 +109,25 @@ class ChatClient(DifyClient):
return self._send_request("GET", "/messages", params=params) return self._send_request("GET", "/messages", params=params)
def get_conversations(self, user, last_id=None, limit=None, pinned=None): def rename_conversation(self, conversation_id, name,auto_generate:bool, user:str):
params = {"user": user, "last_id": last_id, "limit": limit, "pinned": pinned} data = {"name": name, "auto_generate": auto_generate,"user": user}
return self._send_request("GET", "/conversations", params=params)
def rename_conversation(self, conversation_id, name, user):
data = {"name": name, "user": user}
return self._send_request("POST", f"/conversations/{conversation_id}/name", data) 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): def audio_to_text(self, audio_file, user):
data = {"user": user} data = {"user": user}
files = {"audio_file": audio_file} files = {"audio_file": audio_file}
return self._send_request_with_files("POST", "/audio-to-text", data, files) return self._send_request_with_files("POST", "/audio-to-text", data, files)
def get_suggested(self, message_id, user:str): class WorkflowClient(DifyClient):
params = {"user": user} def run(self, inputs:dict, response_mode:str="streaming", user:str="abc-123"):
return self._send_request("GET", f"/messages/{message_id}/suggested", params=params) 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)