AI icon indicating copy to clipboard operation
AI copied to clipboard

Refactor speech

Open NatashaTheRobot opened this issue 1 year ago • 0 comments

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)
    }
}

NatashaTheRobot avatar Apr 24 '24 07:04 NatashaTheRobot