Skip to content

Commit

Permalink
Merge pull request #60 from pkusunjy/remote
Browse files Browse the repository at this point in the history
Documentation and example about audio text-to-speech method
  • Loading branch information
D7EAD authored Jun 3, 2024
2 parents 87712c5 + 2675124 commit b2ff165
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
27 changes: 27 additions & 0 deletions documentation/audio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

This class and its associated <code>liboai::OpenAI</code> interface allow access to the <a href="https://beta.openai.com/docs/api-reference/audio">Audio</a> endpoint of the OpenAI API; this endpoint's functionality can be found below.</p>
- Turn audio to text.
- Turn text to audio.

<br>
<h2>Methods</h2>
Expand Down Expand Up @@ -62,6 +63,32 @@ liboai::FutureResponse translate_async(
) const& noexcept(false);
```

<h3>Text to speech</h3>
<p>Turn text into lifelike spoken audio. Returns a <code>liboai::Response</code> containing response data. The audio data is in the <code>content</code> field of the <code>liboai::Response</code></p>

```cpp
liboai::Response speech(
const std::string& model,
const std::string& voice,
const std::string& input,
std::optional<std::string> response_format = std::nullopt,
std::optional<float> speed = std::nullopt
) const& noexcept(false);
```
<h3>Text to speech (async)</h3>
<p>Asynchronously turn text into lifelike spoken audio. Returns a <code>liboai::FutureResponse</code> containing response data. The audio data is in the <code>content</code> field of the <code>liboai::Response</code></p>
```cpp
liboai::FutureResponse speech_async(
const std::string& model,
const std::string& voice,
const std::string& input,
std::optional<std::string> response_format = std::nullopt,
std::optional<float> speed = std::nullopt
) const& noexcept(false);
```

<p>All function parameters marked <code>optional</code> are not required and are resolved on OpenAI's end if not supplied.</p>

<br>
Expand Down
3 changes: 1 addition & 2 deletions documentation/audio/examples/create_speech.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ using namespace liboai;
int main() {
OpenAI oai;

std::ofstream ocout("demo.mp3", std::ios::binary);

if (oai.auth.SetKeyEnv("OPENAI_API_KEY")) {
try {
Response res = oai.Audio->speech(
"tts-1",
"alloy",
"Today is a wonderful day to build something people love!"
);
std::ofstream ocout("demo.mp3", std::ios::binary);
ocout << res.content;
ocout.close();
std::cout << res.content.size() << std::endl;
Expand Down
3 changes: 1 addition & 2 deletions documentation/audio/examples/create_speech_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ int main() {

if (oai.auth.SetKeyEnv("OPENAI_API_KEY")) {
try {
std::ofstream ocout("demo.mp3", std::ios::binary);
auto fut = oai.Audio->speech_async(
"tts-1",
"alloy",
Expand All @@ -20,7 +19,7 @@ int main() {

// get the contained response
auto res = fut.get();

std::ofstream ocout("demo.mp3", std::ios::binary);
ocout << res.content;
ocout.close();
std::cout << res.content.size() << std::endl;
Expand Down

0 comments on commit b2ff165

Please sign in to comment.