opendev_http/adapters/
azure.rs1use serde_json::Value;
11
12const DEFAULT_API_VERSION: &str = "2024-02-15-preview";
13
14#[derive(Debug, Clone)]
20pub struct AzureOpenAiAdapter {
21 base_url: String,
23 deployment: String,
25 api_version: String,
27 api_url: String,
29}
30
31impl AzureOpenAiAdapter {
32 pub fn new(base_url: impl Into<String>, deployment: impl Into<String>) -> Self {
38 let base_url = base_url.into();
39 let deployment = deployment.into();
40 let api_version = DEFAULT_API_VERSION.to_string();
41 let api_url = build_azure_url(&base_url, &deployment, &api_version);
42 Self {
43 base_url,
44 deployment,
45 api_version,
46 api_url,
47 }
48 }
49
50 pub fn with_api_version(mut self, version: impl Into<String>) -> Self {
52 self.api_version = version.into();
53 self.api_url = build_azure_url(&self.base_url, &self.deployment, &self.api_version);
54 self
55 }
56
57 fn strip_model(payload: &mut Value) {
60 if let Some(obj) = payload.as_object_mut() {
61 obj.remove("model");
62 }
63 }
64}
65
66pub fn build_azure_url(base_url: &str, deployment: &str, api_version: &str) -> String {
68 let base = base_url.trim_end_matches('/');
69 format!("{base}/openai/deployments/{deployment}/chat/completions?api-version={api_version}")
70}
71
72#[async_trait::async_trait]
73impl super::base::ProviderAdapter for AzureOpenAiAdapter {
74 fn provider_name(&self) -> &str {
75 "azure"
76 }
77
78 fn convert_request(&self, mut payload: Value) -> Value {
79 Self::strip_model(&mut payload);
80 payload
82 .as_object_mut()
83 .map(|obj| obj.remove("_reasoning_effort"));
84 payload
85 }
86
87 fn convert_response(&self, response: Value) -> Value {
88 response
90 }
91
92 fn api_url(&self) -> &str {
93 &self.api_url
94 }
95
96 fn extra_headers(&self) -> Vec<(String, String)> {
97 vec![]
100 }
101}
102
103#[cfg(test)]
104#[path = "azure_tests.rs"]
105mod tests;