Skip to content

Commit 142692f

Browse files
committed
TTS API
1 parent b534c50 commit 142692f

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

openai-hs/src/OpenAI/Client.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ module OpenAI.Client
6868
AudioTranslationRequest (..),
6969
createTranscription,
7070
createAudioTranslation,
71+
TextToSpeechRequest(..),
72+
defaultTextToSpeechRequest,
73+
TextToSpeechResponse,
74+
createTextToSpeech,
75+
7176

7277
-- * Engine (deprecated)
7378
EngineId (..),
@@ -203,6 +208,7 @@ createAudioTranslation sc atr =
203208
bnd <- liftIO MP.genBoundary
204209
createAudioTranslationInternal sc (bnd, atr)
205210

211+
EP1 (createTextToSpeech, TextToSpeechRequest, TextToSpeechResponse)
206212
EP1 (createTranscriptionInternal, (BSL.ByteString, AudioTranscriptionRequest), AudioResponseData)
207213
EP1 (createAudioTranslationInternal, (BSL.ByteString, AudioTranslationRequest), AudioResponseData)
208214

@@ -237,7 +243,8 @@ EP2 (engineCreateEmbedding, EngineId, EngineEmbeddingCreate, (OpenAIList EngineE
237243
:<|> createImageVariation'
238244
)
239245
:<|> (createEmbedding')
240-
:<|> ( createTranscriptionInternal'
246+
:<|> (createTextToSpeech'
247+
:<|> createTranscriptionInternal'
241248
:<|> createAudioTranslationInternal'
242249
)
243250
:<|> (createFileInternal' :<|> deleteFile')

openai-servant/openai-servant.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.35.2.
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -52,6 +52,7 @@ library
5252
, base >=4.7 && <5
5353
, bytestring
5454
, casing
55+
, http-media
5556
, mime-types
5657
, servant
5758
, servant-auth

openai-servant/package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ dependencies:
2727
- time
2828
- vector
2929
- mime-types
30+
- http-media
31+
3032

3133
ghc-options:
3234
- -Wall

openai-servant/src/OpenAI/Api.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
-- | The API
23
module OpenAI.Api where
34

@@ -6,9 +7,24 @@ import Servant.API
67
import Servant.Auth
78
import Servant.Auth.Client
89
import Servant.Multipart.API
10+
import Data.List.NonEmpty (NonEmpty((:|)))
11+
import Data.ByteString.Lazy (ByteString)
12+
import Network.HTTP.Media ((//))
913

1014
type OpenAIAuth = Auth '[Bearer] ()
1115

16+
data Audio
17+
18+
instance Accept Audio where
19+
contentTypes _ = "audio" // "mpeg" :|
20+
[ "audio" // "opus",
21+
"audio" // "aac",
22+
"audio" // "flac"
23+
]
24+
25+
instance MimeUnrender Audio ByteString where
26+
mimeUnrender _ bs = Right bs
27+
1228
type OpenAIApi =
1329
"v1" :> OpenAIApiInternal
1430

@@ -46,7 +62,8 @@ type EmbeddingsApi =
4662
OpenAIAuth :> ReqBody '[JSON] EmbeddingCreate :> Post '[JSON] EmbeddingResponse
4763

4864
type AudioApi =
49-
OpenAIAuth :> "transcriptions" :> MultipartForm Tmp AudioTranscriptionRequest :> Post '[JSON] AudioResponseData
65+
OpenAIAuth :> "speech" :> ReqBody '[JSON] TextToSpeechRequest :> Post '[Audio] TextToSpeechResponse
66+
:<|> OpenAIAuth :> "transcriptions" :> MultipartForm Tmp AudioTranscriptionRequest :> Post '[JSON] AudioResponseData
5067
:<|> OpenAIAuth :> "translations" :> MultipartForm Tmp AudioTranslationRequest :> Post '[JSON] AudioResponseData
5168

5269
type FilesApi =

openai-servant/src/OpenAI/Resources.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ module OpenAI.Resources
5252
AudioResponseData (..),
5353
AudioTranscriptionRequest (..),
5454
AudioTranslationRequest (..),
55+
TextToSpeechRequest (..),
56+
defaultTextToSpeechRequest,
57+
TextToSpeechResponse,
58+
5559

5660
-- * Fine tuning (out of date)
5761
FineTuneId (..),
@@ -507,6 +511,30 @@ data AudioResponseData = AudioResponseData
507511
$(deriveJSON (jsonOpts 5) ''AudioResponseData)
508512

509513
-- | Audio create API
514+
515+
data TextToSpeechRequest = TextToSpeechRequest
516+
{ ttsModel :: T.Text,
517+
ttsInput :: T.Text,
518+
ttsVoice :: T.Text,
519+
ttsResponseFormat :: Maybe T.Text,
520+
ttsSpeed :: Maybe Float
521+
}
522+
deriving (Show, Eq)
523+
524+
$(deriveJSON (jsonOpts 3) ''TextToSpeechRequest)
525+
526+
defaultTextToSpeechRequest :: T.Text -> T.Text -> T.Text -> TextToSpeechRequest
527+
defaultTextToSpeechRequest model voice input =
528+
TextToSpeechRequest
529+
{ ttsModel = model,
530+
ttsVoice = voice,
531+
ttsInput = input,
532+
ttsResponseFormat = Nothing,
533+
ttsSpeed = Nothing
534+
}
535+
536+
type TextToSpeechResponse = BSL.ByteString
537+
510538
data AudioTranscriptionRequest = AudioTranscriptionRequest
511539
{ audtsrFile :: FilePath,
512540
audtsrModel :: ModelId,

0 commit comments

Comments
 (0)