feat!: add client.chat_stream() method

BREAKING CHANGE: You can't set the `stream` option for `client.chat*()`.

Either use `client.chat_stream()` if you want to use streams
or use `client.chat()` / `client.chat_async()` otherwise.
This commit is contained in:
Ivan Gabriele
2024-03-04 08:16:06 +01:00
parent f91e794d71
commit 4a4219d3ea
7 changed files with 252 additions and 46 deletions

View File

@@ -0,0 +1,40 @@
use futures::stream::StreamExt;
use jrest::expect;
use mistralai_client::v1::{
chat_completion::{ChatCompletionParams, ChatMessage, ChatMessageRole},
client::Client,
constants::Model,
};
#[tokio::test]
async fn test_client_chat_stream() {
let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b;
let messages = vec![ChatMessage {
role: ChatMessageRole::user,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
}];
let options = ChatCompletionParams {
temperature: Some(0.0),
random_seed: Some(42),
..Default::default()
};
let stream_result = client.chat_stream(model, messages, Some(options)).await;
let mut stream = stream_result.expect("Failed to create stream.");
while let Some(chunk_result) = stream.next().await {
match chunk_result {
Ok(chunk) => {
if chunk.choices[0].delta.role == Some(ChatMessageRole::assistant)
|| chunk.choices[0].finish_reason == Some("stop".to_string())
{
expect!(chunk.choices[0].delta.content.len()).to_be(0);
} else {
expect!(chunk.choices[0].delta.content.len()).to_be_greater_than(0);
}
}
Err(e) => eprintln!("Error processing chunk: {:?}", e),
}
}
}