feat!: simplify chat completion call

BREAKING CHANGE: Chat completions must now be called directly from client.chat() without building a request in between.
This commit is contained in:
Ivan Gabriele
2024-03-03 19:10:25 +01:00
parent 8cb2c3cd0c
commit 7de2b19b98
4 changed files with 49 additions and 46 deletions

View File

@@ -1,9 +1,6 @@
use jrest::expect;
use mistralai_client::v1::{
chat_completion::{
ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionRequest,
ChatCompletionRequestOptions,
},
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionParams},
client::Client,
constants::OPEN_MISTRAL_7B,
};
@@ -22,32 +19,22 @@ fn test_chat_completion() {
role: ChatCompletionMessageRole::user,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
}];
let options = ChatCompletionRequestOptions {
let options = ChatCompletionParams {
temperature: Some(0.0),
random_seed: Some(42),
..Default::default()
};
let chat_completion_request = ChatCompletionRequest::new(model, messages, Some(options));
let result = client.chat(chat_completion_request);
let response = client.chat(model, messages, Some(options)).unwrap();
match result {
Ok(res) => {
expect!(res.model).to_be("open-mistral-7b".to_string());
expect!(res.object).to_be("chat.completion".to_string());
expect!(res.choices.len()).to_be(1);
expect!(res.choices[0].index).to_be(0);
expect!(res.choices[0].message.role.clone())
.to_be(ChatCompletionMessageRole::assistant);
expect!(res.choices[0].message.content.clone()).to_be(
"Tower. The Eiffel Tower is a famous landmark in Paris, France.".to_string(),
);
expect!(res.usage.prompt_tokens).to_be_greater_than(0);
expect!(res.usage.completion_tokens).to_be_greater_than(0);
expect!(res.usage.total_tokens).to_be_greater_than(21);
}
Err(err) => {
panic!("Error: {}", err);
}
}
expect!(response.model).to_be("open-mistral-7b".to_string());
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.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);
expect!(response.usage.completion_tokens).to_be_greater_than(0);
expect!(response.usage.total_tokens).to_be_greater_than(21);
}