use std::env;
use crate::ProviderError;
pub const AZURE_DEFAULT_API_VERSION: &str = "2024-02-15-preview";
#[derive(Clone)]
pub struct AzureOpenAIConfig {
pub endpoint: String,
pub deployment_name: String,
pub api_key: String,
pub api_version: String,
pub model: Option<String>,
pub temperature: Option<f32>,
pub max_tokens: Option<usize>,
pub top_p: Option<f32>,
}
impl std::fmt::Debug for AzureOpenAIConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AzureOpenAIConfig")
.field("endpoint", &self.endpoint)
.field("deployment_name", &self.deployment_name)
.field("api_key", &"***")
.field("api_version", &self.api_version)
.field("model", &self.model)
.field("temperature", &self.temperature)
.field("max_tokens", &self.max_tokens)
.field("top_p", &self.top_p)
.finish()
}
}
impl AzureOpenAIConfig {
pub fn new(
endpoint: impl Into<String>,
deployment_name: impl Into<String>,
api_key: impl Into<String>,
) -> Self {
Self {
endpoint: endpoint.into(),
deployment_name: deployment_name.into(),
api_key: api_key.into(),
api_version: AZURE_DEFAULT_API_VERSION.to_string(),
model: None,
temperature: None,
max_tokens: None,
top_p: None,
}
}
pub fn from_env_result() -> Result<Self, ProviderError> {
let endpoint = env::var("AZURE_OPENAI_ENDPOINT").map_err(|_| {
ProviderError::Config("AZURE_OPENAI_ENDPOINT environment variable not set".to_string())
})?;
let deployment_name = env::var("AZURE_OPENAI_DEPLOYMENT_NAME").map_err(|_| {
ProviderError::Config(
"AZURE_OPENAI_DEPLOYMENT_NAME environment variable not set".to_string(),
)
})?;
let api_key = env::var("AZURE_OPENAI_API_KEY").map_err(|_| {
ProviderError::Config("AZURE_OPENAI_API_KEY environment variable not set".to_string())
})?;
let api_version = env::var("AZURE_OPENAI_API_VERSION")
.unwrap_or_else(|_| AZURE_DEFAULT_API_VERSION.to_string());
let model = env::var("AZURE_OPENAI_MODEL").ok();
Ok(Self {
endpoint,
deployment_name,
api_key,
api_version,
model,
temperature: None,
max_tokens: None,
top_p: None,
})
}
pub fn with_api_version(mut self, version: impl Into<String>) -> Self {
self.api_version = version.into();
self
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn with_temperature(mut self, temp: f32) -> Self {
self.temperature = Some(temp);
self
}
pub fn with_max_tokens(mut self, max: usize) -> Self {
self.max_tokens = Some(max);
self
}
pub fn with_top_p(mut self, p: f32) -> Self {
self.top_p = Some(p);
self
}
pub(crate) fn chat_url(&self) -> String {
format!(
"{}/openai/deployments/{}/chat/completions?api-version={}",
self.endpoint.trim_end_matches('/'),
self.deployment_name,
self.api_version,
)
}
pub(crate) fn effective_model(&self) -> &str {
self.model.as_deref().unwrap_or(&self.deployment_name)
}
}