AI
AI copied to clipboard
Refactor speech
This is an example of how to refactor the API to create a layer between the developer with validation logic and simple parameters sent to OpenAI that would be very simple to use, document, and test.
to use:
func getSpeech(myOpenAIClient: OpenAI.APIClient) async {
do {
// ensures the developer is aware of the API limitations and validates the data
let myValidatedInput = try SpeechRequest.StringOf4096CharOrLess(string: "Hello, World")
let myValidatedSpeed = try SpeechRequest.ValidatedSpeed(speed: 1.5)
let speechRequest = SpeechRequest(input: myValidatedInput, model: .tts_1, speed: myValidatedSpeed)
let speech = try await speechRequest.request(fromClient: myOpenAIClient)
} catch {
print(error)
}
}