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

@@ -87,8 +87,7 @@ fn main() {
..Default::default() ..Default::default()
}; };
let chat_completion_request = ChatCompletionRequest::new(model, messages, Some(options)); let result = client.chat(model, messages, Some(options)).unwrap();
let result = client.chat(chat_completion_request).unwrap();
println!("Assistant: {}", result.choices[0].message.content); println!("Assistant: {}", result.choices[0].message.content);
// => "Assistant: Tower. [...]" // => "Assistant: Tower. [...]"
} }

View File

@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::v1::common; use crate::v1::common;
#[derive(Debug)] #[derive(Debug)]
pub struct ChatCompletionRequestOptions { pub struct ChatCompletionParams {
pub tools: Option<String>, pub tools: Option<String>,
pub temperature: Option<f32>, pub temperature: Option<f32>,
pub max_tokens: Option<u32>, pub max_tokens: Option<u32>,
@@ -12,7 +12,7 @@ pub struct ChatCompletionRequestOptions {
pub stream: Option<bool>, pub stream: Option<bool>,
pub safe_prompt: Option<bool>, pub safe_prompt: Option<bool>,
} }
impl Default for ChatCompletionRequestOptions { impl Default for ChatCompletionParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
tools: None, tools: None,
@@ -53,9 +53,9 @@ impl ChatCompletionRequest {
pub fn new( pub fn new(
model: String, model: String,
messages: Vec<ChatCompletionMessage>, messages: Vec<ChatCompletionMessage>,
options: Option<ChatCompletionRequestOptions>, options: Option<ChatCompletionParams>,
) -> Self { ) -> Self {
let ChatCompletionRequestOptions { let ChatCompletionParams {
tools, tools,
temperature, temperature,
max_tokens, max_tokens,

View File

@@ -6,6 +6,8 @@ use crate::v1::{
constants::API_URL_BASE, constants::API_URL_BASE,
}; };
use super::chat_completion::{ChatCompletionMessage, ChatCompletionParams};
pub struct Client { pub struct Client {
pub api_key: String, pub api_key: String,
pub endpoint: String, pub endpoint: String,
@@ -80,18 +82,22 @@ impl Client {
let result = request.with_json(params).unwrap().send(); let result = request.with_json(params).unwrap().send();
match result { match result {
Ok(res) => { Ok(response) => {
print!("{:?}", res.as_str().unwrap()); print!("{:?}", response.as_str().unwrap());
if (200..=299).contains(&res.status_code) { if (200..=299).contains(&response.status_code) {
Ok(res) Ok(response)
} else { } else {
Err(APIError { Err(APIError {
message: format!("{}: {}", res.status_code, res.as_str().unwrap()), message: format!(
"{}: {}",
response.status_code,
response.as_str().unwrap()
),
}) })
} }
} }
Err(e) => Err(self.new_error(e)), Err(error) => Err(self.new_error(error)),
} }
} }
@@ -101,16 +107,20 @@ impl Client {
let result = request.send(); let result = request.send();
match result { match result {
Ok(res) => { Ok(response) => {
if (200..=299).contains(&res.status_code) { if (200..=299).contains(&response.status_code) {
Ok(res) Ok(response)
} else { } else {
Err(APIError { Err(APIError {
message: format!("{}: {}", res.status_code, res.as_str().unwrap()), message: format!(
"{}: {}",
response.status_code,
response.as_str().unwrap()
),
}) })
} }
} }
Err(e) => Err(self.new_error(e)), Err(error) => Err(self.new_error(error)),
} }
} }
@@ -132,12 +142,19 @@ impl Client {
// } // }
// } // }
pub fn chat(&self, request: ChatCompletionRequest) -> Result<ChatCompletionResponse, APIError> { pub fn chat(
&self,
model: String,
messages: Vec<ChatCompletionMessage>,
options: Option<ChatCompletionParams>,
) -> Result<ChatCompletionResponse, APIError> {
let request = ChatCompletionRequest::new(model, messages, options);
let response = self.post("/chat/completions", &request)?; let response = self.post("/chat/completions", &request)?;
let result = response.json::<ChatCompletionResponse>(); let result = response.json::<ChatCompletionResponse>();
match result { match result {
Ok(r) => Ok(r), Ok(response) => Ok(response),
Err(e) => Err(self.new_error(e)), Err(error) => Err(self.new_error(error)),
} }
} }

View File

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