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

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

View File

@@ -6,6 +6,8 @@ use crate::v1::{
constants::API_URL_BASE,
};
use super::chat_completion::{ChatCompletionMessage, ChatCompletionParams};
pub struct Client {
pub api_key: String,
pub endpoint: String,
@@ -80,18 +82,22 @@ impl Client {
let result = request.with_json(params).unwrap().send();
match result {
Ok(res) => {
print!("{:?}", res.as_str().unwrap());
Ok(response) => {
print!("{:?}", response.as_str().unwrap());
if (200..=299).contains(&res.status_code) {
Ok(res)
if (200..=299).contains(&response.status_code) {
Ok(response)
} else {
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();
match result {
Ok(res) => {
if (200..=299).contains(&res.status_code) {
Ok(res)
Ok(response) => {
if (200..=299).contains(&response.status_code) {
Ok(response)
} else {
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 result = response.json::<ChatCompletionResponse>();
match result {
Ok(r) => Ok(r),
Err(e) => Err(self.new_error(e)),
Ok(response) => Ok(response),
Err(error) => Err(self.new_error(error)),
}
}