Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create audio speech stream support #188

Closed
wants to merge 6 commits into from
Closed

Conversation

batanus
Copy link

@batanus batanus commented Mar 27, 2024

What

OpenAI supports sending audio speech by chunks (see my issue here: #185). This PR adds support for this feature.

Why

With this change, clients can start audio playback with less latency, as far as first audio chunk will be received.

Affected Areas

In OpenAIProtocol was added new method audioCreateSpeechStream

Copy link
Contributor

@SunburstEnzo SunburstEnzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! 😊 Minor things really; don't forget to update the readme either!

Sources/OpenAI/OpenAI.swift Show resolved Hide resolved
organizationIdentifier: configuration.organizationIdentifier,
timeoutInterval: configuration.timeoutInterval)
let session = StreamingSession<AudioSpeechResult>(urlRequest: request)
session.onReceiveContent = {_, object in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
session.onReceiveContent = {_, object in
session.onReceiveContent = { _, object in

session.onReceiveContent = {_, object in
onResult(.success(object))
}
session.onProcessingError = {_, error in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
session.onProcessingError = {_, error in
session.onProcessingError = { _, error in

query: AudioSpeechQuery
) -> AsyncThrowingStream<AudioSpeechResult, Error> {
return AsyncThrowingStream { continuation in
return audioCreateSpeechStream(query: query) { result in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return audioCreateSpeechStream(query: query) { result in
return audioCreateSpeechStream(query: query) { result in

Example:
```
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 1.0)
openAI.audioCreateSpeech(query: query) { result in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
openAI.audioCreateSpeech(query: query) { result in
openAI.audioCreateSpeechStream(query: query) { result in

Don't forget to change the docs

@batanus
Copy link
Author

batanus commented Mar 30, 2024

@SunburstEnzo Thanks for the comments! PR updated

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@batanus
Copy link
Author

batanus commented Apr 2, 2024

Closing this PR as new PR with the same changes was opened:

@batanus batanus closed this Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants