Struct oaapi::Client

source ·
pub struct Client { /* private fields */ }
Expand description

The client of the OpenAI API.

Implementations§

source§

impl Client

source

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 the reqwest.
  • 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

pub fn from_env() -> Result<Self, VarError>

Creates a new client with the API key loaded from the environment variable: OPENAI_API_KEY.

§Example
use oaapi::Client;

let client = Client::from_env().unwrap();
source§

impl Client

source

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

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

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

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

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

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

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

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

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

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

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

source

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

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§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more