openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! Create a Realtime transcription session via
//! `POST /realtime/transcription_sessions`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Transcription sessions are the speech-to-text variant of Realtime
//! sessions: the resulting ephemeral token connects a client straight
//! to the transcription WebSocket, with no model response generation.

use serde::Serialize;
use url::Url;

use crate::{
    errors::OapiError,
    realtime::RealtimeSession,
    rest::post::{Post, PostNoStream},
};

/// Creates a Realtime transcription session.
#[derive(Debug, Clone, Serialize)]
pub struct CreateTranscriptionSessionRequest {
    /// The session configuration, as raw JSON (e.g.
    /// `{"input_audio_format": "pcm16",
    /// "input_audio_transcription": {"model": "gpt-4o-transcribe"}}`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session: Option<serde_json::Value>,
    /// Additional JSON properties flattened into the request body, for
    /// fields not covered by the typed struct.
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
}

impl Post for CreateTranscriptionSessionRequest {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("realtime")
            .push("transcription_sessions");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateTranscriptionSessionRequest {
    type Response = RealtimeSession;
}