use svir::openai::chat::{Decoder, Encoder};
use svir::prelude::*;
const RESPONSE: &str = concat!(
"data: {\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"A greeting.\"}}]}\n\n",
"data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"}}]}\n\n",
"data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\" there.\"},\"finish_reason\":\"stop\"}]}\n\n",
"data: {\"choices\":[],\"usage\":{\"prompt_tokens\":9,\"completion_tokens\":5}}\n\n",
"data: [DONE]\n\n",
);
fn main() -> Result<(), Error> {
let request = Request::new("any-model").system("Be brief.").user("Hello!");
let body = Encoder::new().include_usage(true).encode(&request)?;
let length = body.len();
let bytes = body.into_bytes()?;
println!("{length} bytes: {}", String::from_utf8_lossy(&bytes));
let mut decoder = Decoder::strict();
for piece in RESPONSE.as_bytes().chunks(11) {
for event in decoder.push(piece) {
println!("{:?}", event?);
}
}
decoder.finish()
}