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:
@@ -1,6 +1,6 @@
|
||||
use jrest::expect;
|
||||
use mistralai_client::v1::{
|
||||
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionParams},
|
||||
chat_completion::{ChatCompletionParams, ChatMessage, ChatMessageRole},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
};
|
||||
@@ -10,8 +10,8 @@ async fn test_client_chat_async() {
|
||||
let client = Client::new(None, None, None, None).unwrap();
|
||||
|
||||
let model = Model::OpenMistral7b;
|
||||
let messages = vec![ChatCompletionMessage {
|
||||
role: ChatCompletionMessageRole::user,
|
||||
let messages = vec![ChatMessage {
|
||||
role: ChatMessageRole::user,
|
||||
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
|
||||
}];
|
||||
let options = ChatCompletionParams {
|
||||
@@ -29,7 +29,7 @@ async fn test_client_chat_async() {
|
||||
expect!(response.object).to_be("chat.completion".to_string());
|
||||
expect!(response.choices.len()).to_be(1);
|
||||
expect!(response.choices[0].index).to_be(0);
|
||||
expect!(response.choices[0].message.role.clone()).to_be(ChatCompletionMessageRole::assistant);
|
||||
expect!(response.choices[0].message.role.clone()).to_be(ChatMessageRole::assistant);
|
||||
expect!(response.choices[0].message.content.clone())
|
||||
.to_be("Tower. The Eiffel Tower is a famous landmark in Paris, France.".to_string());
|
||||
expect!(response.usage.prompt_tokens).to_be_greater_than(0);
|
||||
|
||||
40
tests/v1_client_chat_stream_test.rs
Normal file
40
tests/v1_client_chat_stream_test.rs
Normal 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use jrest::expect;
|
||||
use mistralai_client::v1::{
|
||||
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionParams},
|
||||
chat_completion::{ChatCompletionParams, ChatMessage, ChatMessageRole},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
};
|
||||
@@ -10,8 +10,8 @@ fn test_client_chat() {
|
||||
let client = Client::new(None, None, None, None).unwrap();
|
||||
|
||||
let model = Model::OpenMistral7b;
|
||||
let messages = vec![ChatCompletionMessage {
|
||||
role: ChatCompletionMessageRole::user,
|
||||
let messages = vec![ChatMessage {
|
||||
role: ChatMessageRole::user,
|
||||
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
|
||||
}];
|
||||
let options = ChatCompletionParams {
|
||||
@@ -26,7 +26,7 @@ fn test_client_chat() {
|
||||
expect!(response.object).to_be("chat.completion".to_string());
|
||||
expect!(response.choices.len()).to_be(1);
|
||||
expect!(response.choices[0].index).to_be(0);
|
||||
expect!(response.choices[0].message.role.clone()).to_be(ChatCompletionMessageRole::assistant);
|
||||
expect!(response.choices[0].message.role.clone()).to_be(ChatMessageRole::assistant);
|
||||
expect!(response.choices[0].message.content.clone())
|
||||
.to_be("Tower. The Eiffel Tower is a famous landmark in Paris, France.".to_string());
|
||||
expect!(response.usage.prompt_tokens).to_be_greater_than(0);
|
||||
|
||||
Reference in New Issue
Block a user