pub struct Client { /* private fields */ }Expand description
The client of the OpenAI API.
Implementations§
source§impl Client
impl Client
sourcepub fn new(
api_key: ApiKey,
organization_id: Option<OrganizationId>,
client: Option<Client>
) -> Self
pub fn new( api_key: ApiKey, organization_id: Option<OrganizationId>, client: Option<Client> ) -> Self
Creates a new client.
§Arguments
api_key- The API key of the OpenAI API.client- The HTTP client of thereqwest.organization_id- The organization ID of the OpenAI API.
§Example
use oaapi::ApiKey;
use oaapi::OrganizationId;
use oaapi::Client;
let api_key = ApiKey::new("your-api-key");
let inner_client = oaapi::reqwest::Client::new();
let organization_id = OrganizationId::new("your-organization-id");
let client = Client::new(
api_key,
Some(organization_id),
Some(inner_client)
);source§impl Client
impl Client
sourcepub async fn audio_speech(
&self,
request_body: SpeechRequestBody,
buffer_size: Option<usize>
) -> ApiResult<(Receiver<SpeechStreamResult>, JoinHandle<()>)>
pub async fn audio_speech( &self, request_body: SpeechRequestBody, buffer_size: Option<usize> ) -> ApiResult<(Receiver<SpeechStreamResult>, JoinHandle<()>)>
Speeches the given text.
§Arguments
request_body- The request body of the speech.buffer_size- The buffer size of the stream.
§Returns
- The receiver of the stream of speech audio.
- The handle of the stream.
§Example
use oaapi::Client;
use oaapi::audio::SpeechRequestBody;
use oaapi::audio::Voice;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let request_body = SpeechRequestBody {
input: "Hello, world!".into(),
voice: Voice::Alloy,
..Default::default()
};
let (receiver, handle) = client.audio_speech(request_body, None).await?;
// Receive the stream of speech audio.
// Abort the stream when it is not needed.
handle.abort();
Ok(())
}sourcepub async fn audio_transcribe_into_json(
&self,
request_body: TranscriptionsRequestBody
) -> AudioApiResult<JsonResponse>
pub async fn audio_transcribe_into_json( &self, request_body: TranscriptionsRequestBody ) -> AudioApiResult<JsonResponse>
Transcribes the given audio into the JSON.
§Arguments
request_body- The request body of the transcriptions.
§Example
use oaapi::Client;
use oaapi::audio::TranscriptionsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranscriptionsRequestBody {
file,
..Default::default()
};
let response = client
.audio_transcribe_into_json(request_body)
.await?;
Ok(())
}sourcepub async fn audio_transcribe_into_plain_text(
&self,
request_body: TranscriptionsRequestBody
) -> AudioApiResult<String>
pub async fn audio_transcribe_into_plain_text( &self, request_body: TranscriptionsRequestBody ) -> AudioApiResult<String>
Transcribes the given audio into plain text.
§Arguments
request_body- The request body of the transcriptions.
§Example
use oaapi::Client;
use oaapi::audio::TranscriptionsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranscriptionsRequestBody {
file,
..Default::default()
};
let response = client
.audio_transcribe_into_plain_text(request_body)
.await?;
Ok(())
}sourcepub async fn audio_transcribe_into_verbose_json(
&self,
request_body: TranscriptionsRequestBody
) -> AudioApiResult<VerboseJsonResponse>
pub async fn audio_transcribe_into_verbose_json( &self, request_body: TranscriptionsRequestBody ) -> AudioApiResult<VerboseJsonResponse>
Transcribes the given audio into the verbose JSON.
§Arguments
request_body- The request body of the transcriptions.
§Example
use oaapi::Client;
use oaapi::audio::TranscriptionsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranscriptionsRequestBody {
file,
..Default::default()
};
let response = client
.audio_transcribe_into_verbose_json(request_body)
.await?;
Ok(())
}sourcepub async fn audio_transcribe_into_srt(
&self,
request_body: TranscriptionsRequestBody
) -> AudioApiResult<SubRip>
pub async fn audio_transcribe_into_srt( &self, request_body: TranscriptionsRequestBody ) -> AudioApiResult<SubRip>
Transcribes the given audio into the SubRip Subtitle.
§Arguments
request_body- The request body of the transcriptions.
§Example
use oaapi::Client;
use oaapi::audio::TranscriptionsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranscriptionsRequestBody {
file,
..Default::default()
};
let response = client
.audio_transcribe_into_srt(request_body)
.await?;
Ok(())
}sourcepub async fn audio_transcribe_into_vtt(
&self,
request_body: TranscriptionsRequestBody
) -> AudioApiResult<WebVtt>
pub async fn audio_transcribe_into_vtt( &self, request_body: TranscriptionsRequestBody ) -> AudioApiResult<WebVtt>
Transcribes the given audio into the WebVTT.
§Arguments
request_body- The request body of the transcriptions.
§Example
use oaapi::Client;
use oaapi::audio::TranscriptionsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranscriptionsRequestBody {
file,
..Default::default()
};
let response = client
.audio_transcribe_into_vtt(request_body)
.await?;
Ok(())
}sourcepub async fn audio_translate_into_json(
&self,
request_body: TranslationsRequestBody
) -> AudioApiResult<JsonResponse>
pub async fn audio_translate_into_json( &self, request_body: TranslationsRequestBody ) -> AudioApiResult<JsonResponse>
Translates the given audio into the JSON.
§Arguments
request_body- The request body of the translations.
§Example
use oaapi::Client;
use oaapi::audio::TranslationsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranslationsRequestBody {
file,
..Default::default()
};
let response = client
.audio_translate_into_json(request_body)
.await?;
Ok(())
}sourcepub async fn audio_translate_into_plain_text(
&self,
request_body: TranslationsRequestBody
) -> AudioApiResult<String>
pub async fn audio_translate_into_plain_text( &self, request_body: TranslationsRequestBody ) -> AudioApiResult<String>
Translates the given audio into plain text.
§Arguments
request_body- The request body of the translations.
§Example
use oaapi::Client;
use oaapi::audio::TranslationsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranslationsRequestBody {
file,
..Default::default()
};
let response = client
.audio_translate_into_plain_text(request_body)
.await?;
Ok(())
}sourcepub async fn audio_translate_into_verbose_json(
&self,
request_body: TranslationsRequestBody
) -> AudioApiResult<VerboseJsonResponse>
pub async fn audio_translate_into_verbose_json( &self, request_body: TranslationsRequestBody ) -> AudioApiResult<VerboseJsonResponse>
Translates the given audio into the verbose JSON.
§Arguments
request_body- The request body of the translations.
§Example
use oaapi::Client;
use oaapi::audio::TranslationsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranslationsRequestBody {
file,
..Default::default()
};
let response = client
.audio_translate_into_verbose_json(request_body)
.await?;
Ok(())
}sourcepub async fn audio_translate_into_srt(
&self,
request_body: TranslationsRequestBody
) -> AudioApiResult<SubRip>
pub async fn audio_translate_into_srt( &self, request_body: TranslationsRequestBody ) -> AudioApiResult<SubRip>
Translates the given audio into the SubRip Subtitle.
§Arguments
request_body- The request body of the translations.
§Example
use oaapi::Client;
use oaapi::audio::TranslationsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranslationsRequestBody {
file,
..Default::default()
};
let response = client
.audio_translate_into_srt(request_body)
.await?;
Ok(())
}sourcepub async fn audio_translate_into_vtt(
&self,
request_body: TranslationsRequestBody
) -> AudioApiResult<WebVtt>
pub async fn audio_translate_into_vtt( &self, request_body: TranslationsRequestBody ) -> AudioApiResult<WebVtt>
Translates the given audio into the WebVTT.
§Arguments
request_body- The request body of the translations.
§Example
use oaapi::Client;
use oaapi::audio::TranslationsRequestBody;
use oaapi::audio::File;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let file = File::from_file_path(
"path/to/audio/file".into(),
)?;
let request_body = TranslationsRequestBody {
file,
..Default::default()
};
let response = client
.audio_translate_into_vtt(request_body)
.await?;
Ok(())
}source§impl Client
impl Client
sourcepub async fn chat_complete(
&self,
request_body: CompletionsRequestBody
) -> ChatApiResult<ChatCompletionObject>
pub async fn chat_complete( &self, request_body: CompletionsRequestBody ) -> ChatApiResult<ChatCompletionObject>
Completes the given chat.
§Arguments
request_body- The request body of the completions.
§Example
use oaapi::Client;
use oaapi::chat::CompletionsRequestBody;
use oaapi::chat::SystemMessage;
use oaapi::chat::UserMessage;
use oaapi::chat::ChatModel;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let request_body = CompletionsRequestBody {
messages: vec![
SystemMessage::new("Prompt.", None).into(),
UserMessage::new("Chat message from user.".into(), None).into(),
],
model: ChatModel::Gpt35Turbo,
..Default::default()
};
let response = client
.chat_complete(request_body)
.await?;
Ok(())
}sourcepub async fn chat_complete_stream(
&self,
request_body: CompletionsRequestBody,
buffer_size: Option<usize>
) -> ChatApiResult<(Receiver<ChatStreamResult>, JoinHandle<()>)>
pub async fn chat_complete_stream( &self, request_body: CompletionsRequestBody, buffer_size: Option<usize> ) -> ChatApiResult<(Receiver<ChatStreamResult>, JoinHandle<()>)>
Completes the given chat with the stream.
§Arguments
request_body- The request body of the completions.buffer_size- The buffer size of the stream.
§Returns
- The receiver of the stream of chat completions.
- The handle of the stream.
§Example
use oaapi::Client;
use oaapi::chat::CompletionsRequestBody;
use oaapi::chat::SystemMessage;
use oaapi::chat::UserMessage;
use oaapi::chat::ChatModel;
use oaapi::chat::StreamOption;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = Client::from_env()?;
let request_body = CompletionsRequestBody {
messages: vec![
SystemMessage::new("Prompt.", None).into(),
UserMessage::new("Chat message from user.".into(), None).into(),
],
model: ChatModel::Gpt35Turbo,
stream: Some(StreamOption::ReturnStream),
..Default::default()
};
let (receiver, handle) = client
.chat_complete_stream(request_body, None)
.await?;
// Receive the stream of chat completions.
// Abort the stream when it is not needed.
handle.abort();
Ok(())
}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more