-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add azure openai provider (#960)
- Loading branch information
1 parent
092d871
commit 5f6c85d
Showing
10 changed files
with
275 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use reqwest::Client; | ||
use serde_json::Value; | ||
use std::time::Duration; | ||
|
||
use super::base::{ConfigKey, Provider, ProviderMetadata, ProviderUsage, Usage}; | ||
use super::errors::ProviderError; | ||
use super::formats::openai::{create_request, get_usage, response_to_message}; | ||
use super::utils::{emit_debug_trace, get_model, handle_response_openai_compat, ImageFormat}; | ||
use crate::message::Message; | ||
use crate::model::ModelConfig; | ||
use mcp_core::tool::Tool; | ||
|
||
pub const AZURE_DEFAULT_MODEL: &str = "gpt-4o"; | ||
pub const AZURE_DOC_URL: &str = | ||
"https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models"; | ||
pub const AZURE_API_VERSION: &str = "2024-10-21"; | ||
pub const AZURE_OPENAI_KNOWN_MODELS: &[&str] = &[ | ||
"gpt-4o", | ||
"gpt-4o-mini", | ||
"o1", | ||
"o1-mini", | ||
"o1-preview", | ||
"gpt-4", | ||
]; | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct AzureProvider { | ||
#[serde(skip)] | ||
client: Client, | ||
endpoint: String, | ||
api_key: String, | ||
deployment_name: String, | ||
model: ModelConfig, | ||
} | ||
|
||
impl Default for AzureProvider { | ||
fn default() -> Self { | ||
let model = ModelConfig::new(AzureProvider::metadata().default_model); | ||
AzureProvider::from_env(model).expect("Failed to initialize Azure OpenAI provider") | ||
} | ||
} | ||
|
||
impl AzureProvider { | ||
pub fn from_env(model: ModelConfig) -> Result<Self> { | ||
let config = crate::config::Config::global(); | ||
let api_key: String = config.get_secret("AZURE_OPENAI_API_KEY")?; | ||
let endpoint: String = config.get("AZURE_OPENAI_ENDPOINT")?; | ||
let deployment_name: String = config.get("AZURE_OPENAI_DEPLOYMENT_NAME")?; | ||
|
||
let client = Client::builder() | ||
.timeout(Duration::from_secs(600)) | ||
.build()?; | ||
|
||
Ok(Self { | ||
client, | ||
endpoint, | ||
api_key, | ||
deployment_name, | ||
model, | ||
}) | ||
} | ||
|
||
async fn post(&self, payload: Value) -> Result<Value, ProviderError> { | ||
let url = format!( | ||
"{}/openai/deployments/{}/chat/completions?api-version={}", | ||
self.endpoint.trim_end_matches('/'), | ||
self.deployment_name, | ||
AZURE_API_VERSION | ||
); | ||
|
||
let response: reqwest::Response = self | ||
.client | ||
.post(&url) | ||
.header("api-key", &self.api_key) | ||
.json(&payload) | ||
.send() | ||
.await?; | ||
|
||
handle_response_openai_compat(response).await | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Provider for AzureProvider { | ||
fn metadata() -> ProviderMetadata { | ||
ProviderMetadata::new( | ||
"azure_openai", | ||
"Azure OpenAI", | ||
"Models through Azure OpenAI Service", | ||
"gpt-4o", | ||
AZURE_OPENAI_KNOWN_MODELS | ||
.iter() | ||
.map(|s| s.to_string()) | ||
.collect(), | ||
AZURE_DOC_URL, | ||
vec![ | ||
ConfigKey::new("AZURE_OPENAI_API_KEY", true, true, None), | ||
ConfigKey::new("AZURE_OPENAI_ENDPOINT", true, false, None), | ||
ConfigKey::new( | ||
"AZURE_OPENAI_DEPLOYMENT_NAME", | ||
true, | ||
false, | ||
Some("Name of your Azure OpenAI deployment"), | ||
), | ||
], | ||
) | ||
} | ||
|
||
fn get_model_config(&self) -> ModelConfig { | ||
self.model.clone() | ||
} | ||
|
||
#[tracing::instrument( | ||
skip(self, system, messages, tools), | ||
fields(model_config, input, output, input_tokens, output_tokens, total_tokens) | ||
)] | ||
async fn complete( | ||
&self, | ||
system: &str, | ||
messages: &[Message], | ||
tools: &[Tool], | ||
) -> Result<(Message, ProviderUsage), ProviderError> { | ||
let payload = create_request(&self.model, system, messages, tools, &ImageFormat::OpenAi)?; | ||
let response = self.post(payload.clone()).await?; | ||
|
||
let message = response_to_message(response.clone())?; | ||
let usage = match get_usage(&response) { | ||
Ok(usage) => usage, | ||
Err(ProviderError::UsageError(e)) => { | ||
tracing::warn!("Failed to get usage data: {}", e); | ||
Usage::default() | ||
} | ||
Err(e) => return Err(e), | ||
}; | ||
let model = get_model(&response); | ||
emit_debug_trace(self, &payload, &response, &usage); | ||
Ok((message, ProviderUsage::new(model, usage))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod anthropic; | ||
pub mod azure; | ||
pub mod base; | ||
pub mod databricks; | ||
pub mod errors; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.