use crate::providers::traits::Provider;
use anyhow::Result;
use async_trait::async_trait;
use reqwest::Client;
pub struct MyProvider {
api_key: String,
base_url: String,
client: Client,
}
impl MyProvider {
pub fn new(api_key: &str, base_url: Option<&str>) -> Self {
Self {
api_key: api_key.to_string(),
base_url: base_url.unwrap_or("https://api.example.com").to_string(),
client: Client::new(),
}
}
}
#[async_trait]
impl Provider for MyProvider {
async fn chat_with_system(
&self,
system_prompt: Option<&str>,
message: &str,
model: &str,
temperature: f64,
) -> Result<String> {
let url = format!("{}/v1/chat/completions", self.base_url);
let mut messages = Vec::new();
if let Some(sys) = system_prompt {
messages.push(serde_json::json!({
"role": "system",
"content": sys
}));
}
messages.push(serde_json::json!({
"role": "user",
"content": message
}));
let body = serde_json::json!({
"model": model,
"messages": messages,
"temperature": temperature,
});
let resp = self.client
.post(&url)
.header("Authorization", format!("Bearer {}", self.api_key))
.json(&body)
.send()
.await?
.json::<serde_json::Value>()
.await?;
resp["choices"][0]["message"]["content"]
.as_str()
.map(|s| s.to_string())
.ok_or_else(|| anyhow::anyhow!("No content in response"))
}
}