Skip to main content

PostStream

Trait PostStream 

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

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

Required Associated Types§

Provided Methods§

Source

fn get_stream_response_string( &self, client: &Client, base_url: &str, api_key: &str, ) -> impl Future<Output = Result<BoxStream<'static, Result<String, OapiError>>, OapiError>> + Send + Sync

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

The returned stream stops after the data: [DONE] sentinel. Errors terminate the stream.

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};

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";

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

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

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

fn get_stream_response( &self, client: &Client, base_url: &str, api_key: &str, ) -> impl Future<Output = Result<BoxStream<'static, Result<Self::Response, OapiError>>, 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 or on the first error, whichever comes first.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§