add some api to DifyClient (#7314)

This commit is contained in:
Krasus.Chen 2024-08-15 19:26:59 +08:00 committed by GitHub
parent ed85d8281a
commit 6e7f5fae09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 2 deletions

View File

@ -9,9 +9,9 @@ class DifyClient {
protected $base_url;
protected $client;
public function __construct($api_key) {
public function __construct($api_key, $base_url = null) {
$this->api_key = $api_key;
$this->base_url = "https://api.dify.ai/v1/";
$this->base_url = $base_url ?? "https://api.dify.ai/v1/";
$this->client = new Client([
'base_uri' => $this->base_url,
'headers' => [
@ -80,6 +80,25 @@ class DifyClient {
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 {
@ -126,6 +145,16 @@ class ChatClient extends DifyClient {
return $this->send_request('GET', 'messages', 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,
@ -143,4 +172,23 @@ class ChatClient extends DifyClient {
];
return $this->send_request('PATCH', "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);
}
public function get_suggestions($message_id, $user) {
$params = [
'user' => $user
]
return $this->send_request('GET', "messages/{$message_id}/suggested", null, $params);
}
}

View File

@ -43,6 +43,18 @@ class DifyClient:
"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", f"/meta", params=params)
class CompletionClient(DifyClient):
@ -71,6 +83,12 @@ class ChatClient(DifyClient):
return self._send_request("POST", "/chat-messages", data,
stream=True if response_mode == "streaming" else False)
def stop_message(self, task_id, user):
data = {"user": user}
return self._send_request("POST", f"/chat-messages/{task_id}/stop", data)
def get_conversation_messages(self, user, conversation_id=None, first_id=None, limit=None):
params = {"user": user}
@ -91,3 +109,14 @@ class ChatClient(DifyClient):
def rename_conversation(self, conversation_id, name, user):
data = {"name": name, "user": user}
return self._send_request("POST", f"/conversations/{conversation_id}/name", 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)
def get_suggested(self, message_id, user:str):
params = {"user": user}
return self._send_request("GET", f"/messages/{message_id}/suggested", params=params)