pub trait Stream:
Post
+ Serialize
+ Sync
+ Send {
// Provided method
fn get_stream_response(
&self,
url: &str,
api_key: &str,
) -> impl Future<Output = Result<BoxStream<'static, Result<String, Error>>, Error>> + Send + Sync { ... }
}
Provided Methods§
Sourcefn get_stream_response(
&self,
url: &str,
api_key: &str,
) -> impl Future<Output = Result<BoxStream<'static, Result<String, Error>>, Error>> + Send + Sync
fn get_stream_response( &self, url: &str, api_key: &str, ) -> impl Future<Output = Result<BoxStream<'static, Result<String, Error>>, Error>> + 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(DEEPSEEK_CHAT_URL, *DEEPSEEK_API_KEY)
.await
.unwrap();
while let Some(chunk) = response.next().await {
println!("{}", chunk.unwrap());
}
}
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.