use reqwest::Client;
use serde_json::json;
use crate::client::get_api_key;
#[derive(Debug)]
pub enum OpenAIError {
MissingApiKey,
RequestError(reqwest::Error),
InvalidResponse,
}
impl From<reqwest::Error> for OpenAIError {
fn from(err: reqwest::Error) -> Self {
OpenAIError::RequestError(err)
}
}
pub async fn openai_chat_with_model(prompt: &str, model: &str) -> Result<String, OpenAIError> {
let api_key = get_api_key().ok_or(OpenAIError::MissingApiKey)?;
let client = Client::new();
let response = client
.post("https://api.openai.com/v1/chat/completions")
.bearer_auth(api_key)
.json(&json!({
"model": model,
"messages": [
{
"role": "user",
"content": prompt
}
]
}))
.send()
.await?;
let json: serde_json::Value = response.json().await?;
if let Some(content) = json["choices"][0]["message"]["content"].as_str() {
Ok(content.to_string())
} else {
Err(OpenAIError::InvalidResponse)
}
}
pub async fn openai_chat(prompt: &str) -> Result<String, OpenAIError> {
openai_chat_with_model(prompt, "gpt-3.5-turbo").await
}