Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion openai-hs/src/OpenAI/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ module OpenAI.Client
AudioTranslationRequest (..),
createTranscription,
createAudioTranslation,
TextToSpeechRequest(..),
defaultTextToSpeechRequest,
TextToSpeechResponse,
createTextToSpeech,


-- * Engine (deprecated)
EngineId (..),
Expand Down Expand Up @@ -203,6 +208,7 @@ createAudioTranslation sc atr =
bnd <- liftIO MP.genBoundary
createAudioTranslationInternal sc (bnd, atr)

EP1 (createTextToSpeech, TextToSpeechRequest, TextToSpeechResponse)
EP1 (createTranscriptionInternal, (BSL.ByteString, AudioTranscriptionRequest), AudioResponseData)
EP1 (createAudioTranslationInternal, (BSL.ByteString, AudioTranslationRequest), AudioResponseData)

Expand Down Expand Up @@ -237,7 +243,8 @@ EP2 (engineCreateEmbedding, EngineId, EngineEmbeddingCreate, (OpenAIList EngineE
:<|> createImageVariation'
)
:<|> (createEmbedding')
:<|> ( createTranscriptionInternal'
:<|> (createTextToSpeech'
:<|> createTranscriptionInternal'
:<|> createAudioTranslationInternal'
)
:<|> (createFileInternal' :<|> deleteFile')
Expand Down
3 changes: 2 additions & 1 deletion openai-servant/openai-servant.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

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

Expand Down Expand Up @@ -52,6 +52,7 @@ library
, base >=4.7 && <5
, bytestring
, casing
, http-media
, mime-types
, servant
, servant-auth
Expand Down
2 changes: 2 additions & 0 deletions openai-servant/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies:
- time
- vector
- mime-types
- http-media


ghc-options:
- -Wall
Expand Down
19 changes: 18 additions & 1 deletion openai-servant/src/OpenAI/Api.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
-- | The API
module OpenAI.Api where

Expand All @@ -6,9 +7,24 @@ import Servant.API
import Servant.Auth
import Servant.Auth.Client
import Servant.Multipart.API
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.ByteString.Lazy (ByteString)
import Network.HTTP.Media ((//))

type OpenAIAuth = Auth '[Bearer] ()

data Audio

instance Accept Audio where
contentTypes _ = "audio" // "mpeg" :|
[ "audio" // "opus",
"audio" // "aac",
"audio" // "flac"
]

instance MimeUnrender Audio ByteString where
mimeUnrender _ bs = Right bs

type OpenAIApi =
"v1" :> OpenAIApiInternal

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

type AudioApi =
OpenAIAuth :> "transcriptions" :> MultipartForm Tmp AudioTranscriptionRequest :> Post '[JSON] AudioResponseData
OpenAIAuth :> "speech" :> ReqBody '[JSON] TextToSpeechRequest :> Post '[Audio] TextToSpeechResponse
:<|> OpenAIAuth :> "transcriptions" :> MultipartForm Tmp AudioTranscriptionRequest :> Post '[JSON] AudioResponseData
:<|> OpenAIAuth :> "translations" :> MultipartForm Tmp AudioTranslationRequest :> Post '[JSON] AudioResponseData

type FilesApi =
Expand Down
28 changes: 28 additions & 0 deletions openai-servant/src/OpenAI/Resources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ module OpenAI.Resources
AudioResponseData (..),
AudioTranscriptionRequest (..),
AudioTranslationRequest (..),
TextToSpeechRequest (..),
defaultTextToSpeechRequest,
TextToSpeechResponse,


-- * Fine tuning (out of date)
FineTuneId (..),
Expand Down Expand Up @@ -507,6 +511,30 @@ data AudioResponseData = AudioResponseData
$(deriveJSON (jsonOpts 5) ''AudioResponseData)

-- | Audio create API

data TextToSpeechRequest = TextToSpeechRequest
{ ttsModel :: ModelId,
ttsInput :: T.Text,
ttsVoice :: T.Text,
ttsResponseFormat :: Maybe T.Text,
ttsSpeed :: Maybe Float
}
deriving (Show, Eq)

$(deriveJSON (jsonOpts 3) ''TextToSpeechRequest)

defaultTextToSpeechRequest :: ModelId -> T.Text -> T.Text -> TextToSpeechRequest
defaultTextToSpeechRequest model voice input =
TextToSpeechRequest
{ ttsModel = model,
ttsVoice = voice,
ttsInput = input,
ttsResponseFormat = Nothing,
ttsSpeed = Nothing
}

type TextToSpeechResponse = BSL.ByteString

data AudioTranscriptionRequest = AudioTranscriptionRequest
{ audtsrFile :: FilePath,
audtsrModel :: ModelId,
Expand Down