Skip to main content

PostStream

Trait PostStream 

Source
pub trait PostStream:
    Post
    + Serialize
    + Sync
    + Send {
    type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync + 'static;

    // Provided methods
    fn get_stream_response_string(
        &self,
        client: &Client,
        base_url: &str,
        options: &RequestOptions,
    ) -> impl Future<Output = Result<impl Stream<Item = Result<String, OapiError>> + Send + Unpin + 'static, OapiError>> + Send + Sync { ... }
    fn get_stream_response(
        &self,
        client: &Client,
        base_url: &str,
        options: &RequestOptions,
    ) -> impl Future<Output = Result<impl Stream<Item = Result<Self::Response, OapiError>> + Send + Unpin + 'static, OapiError>> + Send + Sync { ... }
}

Required Associated Types§

Provided Methods§

Source

fn get_stream_response_string( &self, client: &Client, base_url: &str, options: &RequestOptions, ) -> impl Future<Output = Result<impl Stream<Item = Result<String, OapiError>> + Send + Unpin + 'static, OapiError>> + Send + Sync

Sends a streaming POST request and returns the raw SSE data items as strings.

The returned stream yields every SSE data payload verbatim, including the final "[DONE]" sentinel; use Self::get_stream_response when you want the sentinel consumed and the items deserialized. SSE framing errors surface as OapiError::SseParseError items.

The client is supplied by the caller; see crate::rest::default_client for a sensible default.

§Example
use std::sync::LazyLock;
use futures_util::StreamExt;
use openai_interface::chat::create::request::{Message, RequestBody};
use openai_interface::rest::{default_client, post::PostStream, RequestOptions};

const DEEPSEEK_API_KEY: &str = "YOUR_API_KEY";
const DEEPSEEK_CHAT_URL: &'static str = "https://api.deepseek.com";
const DEEPSEEK_MODEL: &'static str = "deepseek-chat";

// Needs the `ferritls` cargo feature; drop this line if you install
// your own rustls crypto provider (see `openai_interface::rest`).
openai_interface::rest::install_crypto_provider().ok();

let request = RequestBody {
    messages: vec![
        Message::system("This is a request of test purpose. Reply briefly"),
        Message::user("What's your name?"),
    ],
    model: DEEPSEEK_MODEL.to_string(),
    stream: Some(true),
    ..Default::default()
};

let options = RequestOptions::bearer(DEEPSEEK_API_KEY);
let mut response = request
    .get_stream_response_string(&default_client(), DEEPSEEK_CHAT_URL, &options)
    .await?;

while let Some(chunk) = response.next().await {
    println!("{}", chunk?);
}
Source

fn get_stream_response( &self, client: &Client, base_url: &str, options: &RequestOptions, ) -> impl Future<Output = Result<impl Stream<Item = Result<Self::Response, OapiError>> + Send + Unpin + 'static, OapiError>> + Send + Sync

Sends a streaming POST request and deserializes every SSE data item into Self::Response.

The stream ends after the data: [DONE] sentinel. Deserialization failures surface as OapiError::DeserializationError items without ending the stream; wrap the stream with crate::rest::skip_deserialization_errors to drop them instead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl PostStream for CompletionRequest

Source§

impl PostStream for openai_interface::chat::create::request::RequestBody

Source§

impl PostStream for openai_interface::responses::create::request::RequestBody