2024-03-04 04:57:48 +01:00
|
|
|
use jrest::expect;
|
|
|
|
|
use mistralai_client::v1::{
|
2024-03-09 11:28:50 +01:00
|
|
|
chat::{ChatMessage, ChatMessageRole, ChatParams, ChatResponseChoiceFinishReason},
|
2024-03-04 04:57:48 +01:00
|
|
|
client::Client,
|
|
|
|
|
constants::Model,
|
2024-03-09 11:28:50 +01:00
|
|
|
tool::{Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},
|
2024-03-04 04:57:48 +01:00
|
|
|
};
|
|
|
|
|
|
2024-03-09 11:28:50 +01:00
|
|
|
mod setup;
|
|
|
|
|
|
2024-03-04 04:57:48 +01:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_client_chat_async() {
|
2024-03-09 11:28:50 +01:00
|
|
|
setup::setup();
|
|
|
|
|
|
2024-03-04 04:57:48 +01:00
|
|
|
let client = Client::new(None, None, None, None).unwrap();
|
|
|
|
|
|
|
|
|
|
let model = Model::OpenMistral7b;
|
2024-03-09 11:28:50 +01:00
|
|
|
let messages = vec![ChatMessage::new_user_message(
|
2024-06-07 14:36:18 +02:00
|
|
|
"Guess the next word: \"Eiffel ...\"?",
|
2024-03-09 11:28:50 +01:00
|
|
|
)];
|
|
|
|
|
let options = ChatParams {
|
2024-06-07 15:53:25 +02:00
|
|
|
temperature: 0.0,
|
2024-03-04 04:57:48 +01:00
|
|
|
random_seed: Some(42),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
|
.chat_async(model, messages, Some(options))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
expect!(response.model).to_be(Model::OpenMistral7b);
|
|
|
|
|
expect!(response.object).to_be("chat.completion".to_string());
|
2024-03-09 11:28:50 +01:00
|
|
|
|
2024-03-04 04:57:48 +01:00
|
|
|
expect!(response.choices.len()).to_be(1);
|
|
|
|
|
expect!(response.choices[0].index).to_be(0);
|
2024-03-09 11:28:50 +01:00
|
|
|
expect!(response.choices[0].finish_reason.clone()).to_be(ChatResponseChoiceFinishReason::Stop);
|
|
|
|
|
|
|
|
|
|
expect!(response.choices[0].message.role.clone()).to_be(ChatMessageRole::Assistant);
|
2024-06-07 15:53:25 +02:00
|
|
|
expect!(response.choices[0]
|
|
|
|
|
.message
|
|
|
|
|
.content
|
|
|
|
|
.clone()
|
|
|
|
|
.contains("Tower"))
|
|
|
|
|
.to_be(true);
|
2024-03-09 11:28:50 +01:00
|
|
|
|
|
|
|
|
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(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_client_chat_async_with_function_calling() {
|
|
|
|
|
setup::setup();
|
|
|
|
|
|
|
|
|
|
let tools = vec![Tool::new(
|
|
|
|
|
"get_city_temperature".to_string(),
|
|
|
|
|
"Get the current temperature in a city.".to_string(),
|
|
|
|
|
vec![ToolFunctionParameter::new(
|
|
|
|
|
"city".to_string(),
|
|
|
|
|
"The name of the city.".to_string(),
|
|
|
|
|
ToolFunctionParameterType::String,
|
|
|
|
|
)],
|
|
|
|
|
)];
|
|
|
|
|
|
|
|
|
|
let client = Client::new(None, None, None, None).unwrap();
|
|
|
|
|
|
|
|
|
|
let model = Model::MistralSmallLatest;
|
|
|
|
|
let messages = vec![ChatMessage::new_user_message(
|
|
|
|
|
"What's the current temperature in Paris?",
|
|
|
|
|
)];
|
|
|
|
|
let options = ChatParams {
|
2024-06-07 15:53:25 +02:00
|
|
|
temperature: 0.0,
|
2024-03-09 11:28:50 +01:00
|
|
|
random_seed: Some(42),
|
|
|
|
|
tool_choice: Some(ToolChoice::Any),
|
|
|
|
|
tools: Some(tools),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
|
.chat_async(model, messages, Some(options))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
expect!(response.model).to_be(Model::MistralSmallLatest);
|
|
|
|
|
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].finish_reason.clone())
|
|
|
|
|
.to_be(ChatResponseChoiceFinishReason::ToolCalls);
|
|
|
|
|
|
|
|
|
|
expect!(response.choices[0].message.role.clone()).to_be(ChatMessageRole::Assistant);
|
|
|
|
|
expect!(response.choices[0].message.content.clone()).to_be("".to_string());
|
|
|
|
|
// expect!(response.choices[0].message.tool_calls.clone()).to_be(Some(vec![ToolCall {
|
|
|
|
|
// function: ToolCallFunction {
|
|
|
|
|
// name: "get_city_temperature".to_string(),
|
|
|
|
|
// arguments: "{\"city\": \"Paris\"}".to_string(),
|
|
|
|
|
// },
|
|
|
|
|
// }]));
|
|
|
|
|
|
2024-03-04 04:57:48 +01:00
|
|
|
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(0);
|
|
|
|
|
}
|