Stream

Trait Stream 

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

    // Provided methods
    fn get_stream_response_string(
        &self,
        url: &str,
        api_key: &str,
    ) -> impl Future<Output = Result<BoxStream<'static, Result<String, OapiError>>, OapiError>> + Send + Sync { ... }
    fn get_stream_response(
        &self,
        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, url: &str, api_key: &str, ) -> impl Future<Output = Result<BoxStream<'static, Result<String, OapiError>>, OapiError>> + Send + Sync

Sends a streaming POST request to the specified URL with the provided api-key.

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

const DEEPSEEK_API_KEY: LazyLock<&str> =
    LazyLock::new(|| include_str!("../.././keys/deepseek_domestic_key").trim());
const DEEPSEEK_CHAT_URL: &'static str = "https://api.deepseek.com/chat/completions";
const DEEPSEEK_MODEL: &'static str = "deepseek-chat";

#[tokio::main]
async fn main() {
    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: true,
        ..Default::default()
    };

    let mut response = request
        .get_stream_response_string(DEEPSEEK_CHAT_URL, *DEEPSEEK_API_KEY)
        .await
        .unwrap();

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

fn get_stream_response( &self, url: &str, api_key: &str, ) -> impl Future<Output = Result<BoxStream<'static, Result<Self::Response, OapiError>>, OapiError>> + Send + Sync

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§