From 316d33d343a56134c8aa64cbacece4f0c0d4ac71 Mon Sep 17 00:00:00 2001 From: yakupseymen Date: Wed, 24 Jan 2024 17:11:17 +0300 Subject: [PATCH 1/2] Text to Speech --- README.md | 15 +++++++++++++++ src/OpenAi.php | 12 ++++++++++++ src/Url.php | 9 +++++++++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 8ec6d3c..762bc93 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ Please visit https://orhanerday.gitbook.io/openai-php-api-1/ - Embeddings - [x] [Create embeddings](https://beta.openai.com/docs/api-reference/embeddings/create) - Audio + - [x] [Text to Speech (TTS)](https://platform.openai.com/docs/guides/text-to-speech) - [x] [Create transcription](https://platform.openai.com/docs/api-reference/audio/create) - [x] [Create translation](https://platform.openai.com/docs/api-reference/audio/create) - Files @@ -660,6 +661,20 @@ $engines = $open_ai->engines(); ## Audio +### Text To Speech (TTS) + +```php + +$result = $open_ai->tts([ + "model" => "tts-1", // tts-1-hd + "input" => "I'm going to use the stones again. Hey, we'd be going in short-handed, you know", + "voice" => "alloy", // echo, fable, onyx, nova, and shimmer +]); + +// Save audio file +file_put_contents('tts-result.mp3', $result); +``` + ### Create Transcription Transcribes audio into the input language. diff --git a/src/OpenAi.php b/src/OpenAi.php index e3ff4c7..d9585a2 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -838,6 +838,18 @@ public function listRunSteps($threadId, $runId, $query = []) return $this->sendRequest($url, 'GET'); } + /** + * @param $opts + * @return bool|string + */ + public function tts($opts) + { + $url = Url::ttsUrl(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param int $timeout */ diff --git a/src/Url.php b/src/Url.php index 4808c77..492d81e 100644 --- a/src/Url.php +++ b/src/Url.php @@ -178,4 +178,13 @@ public static function threadsUrl(): string { return self::OPEN_AI_URL . "/threads"; } + + /** + * @param + * @return string + */ + public static function ttsUrl(): string + { + return self::OPEN_AI_URL . "/threads"; + } } From ab5e06ae6111a8514665045167ea595ad224f6e6 Mon Sep 17 00:00:00 2001 From: yakupseymen Date: Wed, 24 Jan 2024 22:22:28 +0300 Subject: [PATCH 2/2] fix wrong api url --- src/Url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Url.php b/src/Url.php index 492d81e..f3c90ef 100644 --- a/src/Url.php +++ b/src/Url.php @@ -185,6 +185,6 @@ public static function threadsUrl(): string */ public static function ttsUrl(): string { - return self::OPEN_AI_URL . "/threads"; + return self::OPEN_AI_URL . "/audio/speech"; } }