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:
@@ -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,
|
||||
|
||||
@@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user