Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions examples/chat_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content);

// print response headers
for (key, value) in client.response_headers.unwrap().iter() {
println!("{}: {:?}", key, value);
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
let client = OpenAIClient::builder().with_api_key(api_key).build()?;

let req = CompletionRequest::new(
completion::GPT3_TEXT_DAVINCI_003.to_string(),
Expand Down
1 change: 0 additions & 1 deletion examples/openrouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", client.response_headers);

Ok(())
}
Expand Down
12 changes: 4 additions & 8 deletions src/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub struct OpenAIClient {
proxy: Option<String>,
timeout: Option<u64>,
headers: Option<HeaderMap>,
pub response_headers: Option<HeaderMap>,
}

impl OpenAIClientBuilder {
Expand Down Expand Up @@ -126,7 +125,6 @@ impl OpenAIClientBuilder {
proxy: self.proxy,
timeout: self.timeout,
headers: self.headers,
response_headers: None,
})
}
}
Expand Down Expand Up @@ -184,7 +182,7 @@ impl OpenAIClient {
}

async fn post<T: serde::de::DeserializeOwned>(
&mut self,
& self,
path: &str,
body: &impl serde::ser::Serialize,
) -> Result<T, APIError> {
Expand Down Expand Up @@ -213,7 +211,7 @@ impl OpenAIClient {
}

async fn post_form<T: serde::de::DeserializeOwned>(
&mut self,
&self,
path: &str,
form: Form,
) -> Result<T, APIError> {
Expand All @@ -231,16 +229,14 @@ impl OpenAIClient {
}

async fn handle_response<T: serde::de::DeserializeOwned>(
&mut self,
&self,
response: Response,
) -> Result<T, APIError> {
let status = response.status();
let headers = response.headers().clone();
if status.is_success() {
let text = response.text().await.unwrap_or_else(|_| "".to_string());
match serde_json::from_str::<T>(&text) {
Ok(parsed) => {
self.response_headers = Some(headers);
Ok(parsed)
}
Err(e) => Err(APIError::CustomError {
Expand All @@ -259,7 +255,7 @@ impl OpenAIClient {
}

pub async fn completion(
&mut self,
&self,
req: CompletionRequest,
) -> Result<CompletionResponse, APIError> {
self.post("completions", &req).await
Expand Down