use std::fmt::Debug;
use super::openai::TranscriptionResponse;
use crate::client::{
self, ApiKey, Capabilities, Capable, DebugExt, Nothing, Provider, ProviderBuilder,
ProviderClient,
};
use crate::completion::GetTokenUsage;
use crate::http_client::multipart::Part;
use crate::http_client::{self, HttpClientExt, MultipartForm, bearer_auth_header};
use crate::transcription::TranscriptionError;
use crate::{
embeddings::{self, EmbeddingError},
providers::openai,
transcription::{self},
};
use bytes::Bytes;
use serde::Deserialize;
use serde_json::json;
const DEFAULT_API_VERSION: &str = "2024-10-21";
#[derive(Debug, Clone)]
pub struct AzureExt {
endpoint: String,
api_version: String,
}
impl DebugExt for AzureExt {
fn fields(&self) -> impl Iterator<Item = (&'static str, &dyn std::fmt::Debug)> {
[
("endpoint", (&self.endpoint as &dyn Debug)),
("api_version", (&self.api_version as &dyn Debug)),
]
.into_iter()
}
}
#[derive(Debug, Clone)]
pub struct AzureExtBuilder {
endpoint: Option<String>,
api_version: String,
}
impl Default for AzureExtBuilder {
fn default() -> Self {
Self {
endpoint: None,
api_version: DEFAULT_API_VERSION.into(),
}
}
}
pub type Client<H = reqwest::Client> = client::Client<AzureExt, H>;
pub type ClientBuilder<H = crate::markers::Missing> =
client::ClientBuilder<AzureExtBuilder, AzureOpenAIAuth, H>;
impl Provider for AzureExt {
type Builder = AzureExtBuilder;
const VERIFY_PATH: &'static str = "";
}
impl<H> Capabilities<H> for AzureExt {
type Completion = Capable<CompletionModel<H>>;
type Embeddings = Capable<EmbeddingModel<H>>;
type Transcription = Capable<TranscriptionModel<H>>;
type ModelListing = Nothing;
#[cfg(feature = "image")]
type ImageGeneration = Nothing;
#[cfg(feature = "audio")]
type AudioGeneration = Capable<AudioGenerationModel<H>>;
type Rerank = Nothing;
}
impl ProviderBuilder for AzureExtBuilder {
type Extension<H>
= AzureExt
where
H: HttpClientExt;
type ApiKey = AzureOpenAIAuth;
const BASE_URL: &'static str = "";
fn build<H>(
builder: &client::ClientBuilder<Self, Self::ApiKey, H>,
) -> http_client::Result<Self::Extension<H>>
where
H: HttpClientExt,
{
let AzureExtBuilder {
endpoint,
api_version,
..
} = builder.ext().clone();
match endpoint {
Some(endpoint) => Ok(AzureExt {
endpoint,
api_version,
}),
None => Err(http_client::Error::Instance(
"Azure client must be provided an endpoint prior to building".into(),
)),
}
}
fn finish<H>(
&self,
mut builder: client::ClientBuilder<Self, Self::ApiKey, H>,
) -> http_client::Result<client::ClientBuilder<Self, Self::ApiKey, H>> {
use AzureOpenAIAuth::*;
let auth = builder.get_api_key().clone();
match auth {
Token(token) => bearer_auth_header(builder.headers_mut(), token.as_str())?,
ApiKey(key) => {
let k = http::HeaderName::from_static("api-key");
let v = http::HeaderValue::from_str(key.as_str())?;
builder.headers_mut().insert(k, v);
}
}
Ok(builder)
}
}
impl<H> ClientBuilder<H> {
pub fn api_version(mut self, api_version: &str) -> Self {
self.ext_mut().api_version = api_version.into();
self
}
}
impl<H> client::ClientBuilder<AzureExtBuilder, AzureOpenAIAuth, H> {
pub fn azure_endpoint(self, endpoint: String) -> ClientBuilder<H> {
self.over_ext(|AzureExtBuilder { api_version, .. }| AzureExtBuilder {
endpoint: Some(endpoint),
api_version,
})
}
}
#[derive(Clone)]
pub enum AzureOpenAIAuth {
ApiKey(String),
Token(String),
}
impl ApiKey for AzureOpenAIAuth {}
impl std::fmt::Debug for AzureOpenAIAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ApiKey(_) => write!(f, "API key <REDACTED>"),
Self::Token(_) => write!(f, "Token <REDACTED>"),
}
}
}
impl<S> From<S> for AzureOpenAIAuth
where
S: Into<String>,
{
fn from(token: S) -> Self {
AzureOpenAIAuth::Token(token.into())
}
}
impl<T> Client<T>
where
T: HttpClientExt,
{
fn endpoint(&self) -> &str {
&self.ext().endpoint
}
fn api_version(&self) -> &str {
&self.ext().api_version
}
fn post_embedding(&self, deployment_id: &str) -> http_client::Result<http_client::Builder> {
let url = format!(
"{}/openai/deployments/{}/embeddings?api-version={}",
self.endpoint(),
deployment_id.trim_start_matches('/'),
self.api_version()
);
self.post(&url)
}
#[cfg(feature = "audio")]
fn post_audio_generation(
&self,
deployment_id: &str,
) -> http_client::Result<http_client::Builder> {
let url = format!(
"{}/openai/deployments/{}/audio/speech?api-version={}",
self.endpoint(),
deployment_id.trim_start_matches('/'),
self.api_version()
);
self.post(url)
}
fn post_transcription(&self, deployment_id: &str) -> http_client::Result<http_client::Builder> {
let url = format!(
"{}/openai/deployments/{}/audio/translations?api-version={}",
self.endpoint(),
deployment_id.trim_start_matches('/'),
self.api_version()
);
self.post(&url)
}
#[cfg(feature = "image")]
fn post_image_generation(
&self,
deployment_id: &str,
) -> http_client::Result<http_client::Builder> {
let url = format!(
"{}/openai/deployments/{}/images/generations?api-version={}",
self.endpoint(),
deployment_id.trim_start_matches('/'),
self.api_version()
);
self.post(&url)
}
}
pub struct AzureOpenAIClientParams {
api_key: String,
version: String,
header: String,
}
impl ProviderClient for Client {
type Input = AzureOpenAIClientParams;
type Error = crate::client::ProviderClientError;
fn from_env() -> Result<Self, Self::Error> {
let auth = if let Some(api_key) = crate::client::optional_env_var("AZURE_API_KEY")? {
AzureOpenAIAuth::ApiKey(api_key)
} else if let Some(token) = crate::client::optional_env_var("AZURE_TOKEN")? {
AzureOpenAIAuth::Token(token)
} else {
return Err(crate::client::ProviderClientError::InvalidConfiguration(
"either `AZURE_API_KEY` or `AZURE_TOKEN` must be set",
));
};
let api_version = crate::client::required_env_var("AZURE_API_VERSION")?;
let azure_endpoint = crate::client::required_env_var("AZURE_ENDPOINT")?;
Self::builder()
.api_key(auth)
.azure_endpoint(azure_endpoint)
.api_version(&api_version)
.build()
.map_err(Into::into)
}
fn from_val(
AzureOpenAIClientParams {
api_key,
version,
header,
}: Self::Input,
) -> Result<Self, Self::Error> {
let auth = AzureOpenAIAuth::ApiKey(api_key.to_string());
Self::builder()
.api_key(auth)
.azure_endpoint(header)
.api_version(&version)
.build()
.map_err(Into::into)
}
}
#[derive(Debug, Deserialize)]
struct ApiErrorResponse {
message: String,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ApiResponse<T> {
Ok(T),
Err(ApiErrorResponse),
}
pub const TEXT_EMBEDDING_3_LARGE: &str = "text-embedding-3-large";
pub const TEXT_EMBEDDING_3_SMALL: &str = "text-embedding-3-small";
pub const TEXT_EMBEDDING_ADA_002: &str = "text-embedding-ada-002";
fn model_dimensions_from_identifier(identifier: &str) -> Option<usize> {
match identifier {
TEXT_EMBEDDING_3_LARGE => Some(3_072),
TEXT_EMBEDDING_3_SMALL | TEXT_EMBEDDING_ADA_002 => Some(1_536),
_ => None,
}
}
#[derive(Debug, Deserialize)]
pub struct EmbeddingResponse {
pub object: String,
pub data: Vec<EmbeddingData>,
pub model: String,
pub usage: Usage,
}
#[derive(Debug, Deserialize)]
pub struct EmbeddingData {
pub object: String,
pub embedding: Vec<f64>,
pub index: usize,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Usage {
pub prompt_tokens: usize,
pub total_tokens: usize,
}
impl GetTokenUsage for Usage {
fn token_usage(&self) -> crate::completion::Usage {
let mut usage = crate::completion::Usage::new();
usage.input_tokens = self.prompt_tokens as u64;
usage.total_tokens = self.total_tokens as u64;
usage.output_tokens = usage.total_tokens - usage.input_tokens;
usage
}
}
impl std::fmt::Display for Usage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Prompt tokens: {} Total tokens: {}",
self.prompt_tokens, self.total_tokens
)
}
}
#[derive(Clone)]
pub struct EmbeddingModel<T = reqwest::Client> {
client: Client<T>,
pub model: String,
ndims: usize,
}
impl<T> embeddings::EmbeddingModel for EmbeddingModel<T>
where
T: HttpClientExt + Default + Clone + 'static,
{
const MAX_DOCUMENTS: usize = 1024;
type Client = Client<T>;
fn make(client: &Self::Client, model: impl Into<String>, dims: Option<usize>) -> Self {
Self::new(client.clone(), model, dims)
}
fn ndims(&self) -> usize {
self.ndims
}
async fn embed_texts(
&self,
documents: impl IntoIterator<Item = String>,
) -> Result<Vec<embeddings::Embedding>, EmbeddingError> {
let documents = documents.into_iter().collect::<Vec<_>>();
let mut body = json!({
"input": documents,
});
let body_object = body.as_object_mut().ok_or_else(|| {
EmbeddingError::ResponseError("embedding request body must be a JSON object".into())
})?;
if self.ndims > 0 && self.model.as_str() != TEXT_EMBEDDING_ADA_002 {
body_object.insert("dimensions".to_owned(), json!(self.ndims));
}
let body = serde_json::to_vec(&body)?;
let req = self
.client
.post_embedding(self.model.as_str())?
.body(body)
.map_err(|e| EmbeddingError::HttpError(e.into()))?;
let response = self.client.send(req).await?;
let status = response.status();
if status.is_success() {
let response_body: Vec<u8> = response.into_body().await?;
let parsed: ApiResponse<EmbeddingResponse> = serde_json::from_slice(&response_body)?;
match parsed {
ApiResponse::Ok(response) => {
tracing::info!(target: "rig",
"Azure embedding token usage: {}",
response.usage
);
if response.data.len() != documents.len() {
return Err(EmbeddingError::ResponseError(
"Response data length does not match input length".into(),
));
}
Ok(response
.data
.into_iter()
.zip(documents.into_iter())
.map(|(embedding, document)| embeddings::Embedding {
document,
vec: embedding.embedding,
})
.collect())
}
ApiResponse::Err(err) => {
tracing::warn!(message = %err.message, "provider returned an error response");
Err(EmbeddingError::from_http_response(
status,
String::from_utf8_lossy(&response_body).into_owned(),
))
}
}
} else {
let text = http_client::text(response).await?;
Err(EmbeddingError::from_http_response(status, text))
}
}
}
impl<T> EmbeddingModel<T> {
pub fn new(client: Client<T>, model: impl Into<String>, ndims: Option<usize>) -> Self {
let model = model.into();
let ndims = ndims
.or(model_dimensions_from_identifier(&model))
.unwrap_or_default();
Self {
client,
model,
ndims,
}
}
pub fn with_model(client: Client<T>, model: &str, ndims: Option<usize>) -> Self {
let ndims = ndims.unwrap_or_default();
Self {
client,
model: model.into(),
ndims,
}
}
}
pub const O1: &str = "o1";
pub const O1_PREVIEW: &str = "o1-preview";
pub const O1_MINI: &str = "o1-mini";
pub const GPT_4O: &str = "gpt-4o";
pub const GPT_4O_MINI: &str = "gpt-4o-mini";
pub const GPT_4O_REALTIME_PREVIEW: &str = "gpt-4o-realtime-preview";
pub const GPT_4_TURBO: &str = "gpt-4";
pub const GPT_4: &str = "gpt-4";
pub const GPT_4_32K: &str = "gpt-4-32k";
pub const GPT_4_32K_0613: &str = "gpt-4-32k";
pub const GPT_35_TURBO: &str = "gpt-3.5-turbo";
pub const GPT_35_TURBO_INSTRUCT: &str = "gpt-3.5-turbo-instruct";
pub const GPT_35_TURBO_16K: &str = "gpt-3.5-turbo-16k";
pub type CompletionModel<H = reqwest::Client> =
openai::completion::GenericCompletionModel<AzureExt, H>;
impl openai::completion::OpenAICompatibleProvider for AzureExt {
const PROVIDER_NAME: &'static str = "azure.openai";
type StreamingUsage = openai::Usage;
type Response = openai::CompletionResponse;
fn completion_path(&self, model: &str) -> String {
format!(
"{}/openai/deployments/{}/chat/completions?api-version={}",
self.endpoint,
model.trim_start_matches('/'),
self.api_version
)
}
}
#[derive(Clone)]
pub struct TranscriptionModel<T = reqwest::Client> {
client: Client<T>,
pub model: String,
}
impl<T> TranscriptionModel<T> {
pub fn new(client: Client<T>, model: impl Into<String>) -> Self {
Self {
client,
model: model.into(),
}
}
}
impl<T> transcription::TranscriptionModel for TranscriptionModel<T>
where
T: HttpClientExt + Clone + 'static,
{
type Response = TranscriptionResponse;
type Client = Client<T>;
fn make(client: &Self::Client, model: impl Into<String>) -> Self {
Self::new(client.clone(), model)
}
async fn transcription(
&self,
request: transcription::TranscriptionRequest,
) -> Result<
transcription::TranscriptionResponse<Self::Response>,
transcription::TranscriptionError,
> {
let data = request.data;
let mut body =
MultipartForm::new().part(Part::bytes("file", data).filename(request.filename.clone()));
if let Some(prompt) = request.prompt {
body = body.text("prompt", prompt.clone());
}
if let Some(ref temperature) = request.temperature {
body = body.text("temperature", temperature.to_string());
}
if let Some(ref additional_params) = request.additional_params {
let params = additional_params.as_object().ok_or_else(|| {
TranscriptionError::RequestError(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"additional transcription parameters must be a JSON object",
)))
})?;
for (key, value) in params {
body = body.text(key.to_owned(), value.to_string());
}
}
let req = self
.client
.post_transcription(&self.model)?
.body(body)
.map_err(|e| TranscriptionError::HttpError(e.into()))?;
let response = self.client.send_multipart::<Bytes>(req).await?;
let status = response.status();
let response_body = response.into_body().into_future().await?.to_vec();
if status.is_success() {
match serde_json::from_slice::<ApiResponse<TranscriptionResponse>>(&response_body)? {
ApiResponse::Ok(response) => response.try_into(),
ApiResponse::Err(api_error_response) => {
tracing::warn!(message = %api_error_response.message, "provider returned an error response");
Err(TranscriptionError::from_http_response(
status,
String::from_utf8_lossy(&response_body).into_owned(),
))
}
}
} else {
Err(TranscriptionError::from_http_response(
status,
String::from_utf8_lossy(&response_body).to_string(),
))
}
}
}
#[cfg(feature = "image")]
pub use image_generation::*;
#[cfg(feature = "image")]
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
mod image_generation {
use crate::http_client::HttpClientExt;
use crate::image_generation;
use crate::image_generation::{ImageGenerationError, ImageGenerationRequest};
use crate::providers::azure::{ApiResponse, Client};
use crate::providers::openai::ImageGenerationResponse;
use bytes::Bytes;
use serde_json::json;
#[derive(Clone)]
pub struct ImageGenerationModel<T = reqwest::Client> {
client: Client<T>,
pub model: String,
}
impl<T> image_generation::ImageGenerationModel for ImageGenerationModel<T>
where
T: HttpClientExt + Clone + Default + std::fmt::Debug + Send + 'static,
{
type Response = ImageGenerationResponse;
type Client = Client<T>;
fn make(client: &Self::Client, model: impl Into<String>) -> Self {
Self {
client: client.clone(),
model: model.into(),
}
}
async fn image_generation(
&self,
generation_request: ImageGenerationRequest,
) -> Result<image_generation::ImageGenerationResponse<Self::Response>, ImageGenerationError>
{
let request = json!({
"model": self.model,
"prompt": generation_request.prompt,
"size": format!("{}x{}", generation_request.width, generation_request.height),
"response_format": "b64_json"
});
let body = serde_json::to_vec(&request)?;
let req = self
.client
.post_image_generation(&self.model)?
.body(body)
.map_err(|e| ImageGenerationError::HttpError(e.into()))?;
let response = self.client.send::<_, Bytes>(req).await?;
let status = response.status();
let response_body = response.into_body().into_future().await?.to_vec();
if !status.is_success() {
return Err(ImageGenerationError::from_http_response(
status,
String::from_utf8_lossy(&response_body).into_owned(),
));
}
match serde_json::from_slice::<ApiResponse<ImageGenerationResponse>>(&response_body)? {
ApiResponse::Ok(response) => response.try_into(),
ApiResponse::Err(err) => {
tracing::warn!(message = %err.message, "provider returned an error response");
Err(ImageGenerationError::from_http_response(
status,
String::from_utf8_lossy(&response_body).into_owned(),
))
}
}
}
}
}
#[cfg(feature = "audio")]
pub use audio_generation::*;
#[cfg(feature = "audio")]
#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
mod audio_generation {
use super::Client;
use crate::audio_generation::{
self, AudioGenerationError, AudioGenerationRequest, AudioGenerationResponse,
};
use crate::http_client::HttpClientExt;
use bytes::Bytes;
use serde_json::json;
#[derive(Clone)]
pub struct AudioGenerationModel<T = reqwest::Client> {
client: Client<T>,
model: String,
}
impl<T> AudioGenerationModel<T> {
pub fn new(client: Client<T>, deployment_name: impl Into<String>) -> Self {
Self {
client,
model: deployment_name.into(),
}
}
}
impl<T> audio_generation::AudioGenerationModel for AudioGenerationModel<T>
where
T: HttpClientExt + Clone + Default + std::fmt::Debug + Send + 'static,
{
type Response = Bytes;
type Client = Client<T>;
fn make(client: &Self::Client, model: impl Into<String>) -> Self {
Self::new(client.clone(), model)
}
async fn audio_generation(
&self,
request: AudioGenerationRequest,
) -> Result<AudioGenerationResponse<Self::Response>, AudioGenerationError> {
let request = json!({
"model": self.model,
"input": request.text,
"voice": request.voice,
"speed": request.speed,
});
let body = serde_json::to_vec(&request)?;
let req = self
.client
.post_audio_generation("/audio/speech")?
.header("Content-Type", "application/json")
.body(body)
.map_err(|e| AudioGenerationError::HttpError(e.into()))?;
let response = self.client.send::<_, Bytes>(req).await?;
let status = response.status();
let response_body = response.into_body().into_future().await?;
if !status.is_success() {
return Err(AudioGenerationError::from_http_response(
status,
String::from_utf8_lossy(&response_body).into_owned(),
));
}
Ok(AudioGenerationResponse {
audio: response_body.to_vec(),
response: response_body,
})
}
}
}
#[cfg(test)]
mod azure_tests {
use schemars::JsonSchema;
use super::*;
use crate::completion::{CompletionError, CompletionRequest};
use crate::OneOrMany;
use crate::client::{completion::CompletionClient, embeddings::EmbeddingsClient};
use crate::completion::CompletionModel;
use crate::embeddings::EmbeddingModel;
use crate::prelude::TypedPrompt;
use crate::providers::openai::GPT_5_MINI;
#[cfg(any(feature = "image", feature = "audio"))]
fn test_client(
http_client: crate::test_utils::RecordingHttpClient,
) -> Client<crate::test_utils::RecordingHttpClient> {
Client::builder()
.api_key("test-key")
.azure_endpoint("https://example.openai.azure.com".to_string())
.http_client(http_client)
.build()
.expect("build client")
}
#[cfg(feature = "image")]
#[tokio::test]
async fn image_generation_non_success_response_preserves_status_and_body() {
use crate::image_generation::{
ImageGenerationError, ImageGenerationModel as ImageGenerationModelTrait,
ImageGenerationRequest,
};
use crate::test_utils::RecordingHttpClient;
let body = r#"{"error":{"message":"invalid image request"}}"#;
let http_client =
RecordingHttpClient::with_error_response(http::StatusCode::BAD_REQUEST, body);
let model = ImageGenerationModel::make(&test_client(http_client), "dall-e-3");
let error = model
.image_generation(ImageGenerationRequest {
prompt: "draw a cat".to_string(),
width: 256,
height: 256,
additional_params: None,
})
.await
.expect_err("image generation should fail with non-success status");
assert!(matches!(error, ImageGenerationError::HttpError(_)));
assert_eq!(
error.provider_response_status(),
Some(http::StatusCode::BAD_REQUEST)
);
assert_eq!(error.provider_response_body(), Some(body));
}
#[cfg(feature = "audio")]
#[tokio::test]
async fn audio_generation_non_success_response_preserves_status_and_body() {
use crate::audio_generation::{
AudioGenerationError, AudioGenerationModel as _, AudioGenerationRequest,
};
use crate::test_utils::RecordingHttpClient;
let body = r#"{"error":{"message":"invalid voice"}}"#;
let http_client =
RecordingHttpClient::with_error_response(http::StatusCode::UNPROCESSABLE_ENTITY, body);
let model = AudioGenerationModel::new(test_client(http_client), "tts-1");
let error = match model
.audio_generation(AudioGenerationRequest {
text: "hello".to_string(),
voice: "alloy".to_string(),
speed: 1.0,
additional_params: None,
})
.await
{
Err(error) => error,
Ok(_) => panic!("audio generation should fail with non-success status"),
};
assert!(matches!(error, AudioGenerationError::HttpError(_)));
assert_eq!(
error.provider_response_status(),
Some(http::StatusCode::UNPROCESSABLE_ENTITY)
);
assert_eq!(error.provider_response_body(), Some(body));
}
#[tokio::test]
async fn transcription_http_non_success_preserves_status_and_body() {
use crate::test_utils::RecordingHttpClient;
use crate::transcription::{TranscriptionError, TranscriptionModel as _};
let body = r#"{"error":{"message":"bad audio","type":"invalid_request_error"}}"#;
let http_client =
RecordingHttpClient::with_error_response(http::StatusCode::BAD_REQUEST, body);
let client = Client::builder()
.api_key("test-key")
.azure_endpoint("https://example.openai.azure.com".to_string())
.http_client(http_client)
.build()
.expect("build client");
let model = TranscriptionModel::new(client, "whisper");
let error = match model
.transcription_request()
.data(vec![0u8; 16])
.send()
.await
{
Err(error) => error,
Ok(_) => panic!("transcription should fail with non-success status"),
};
assert!(matches!(error, TranscriptionError::HttpError(_)));
assert_eq!(
error.provider_response_status(),
Some(http::StatusCode::BAD_REQUEST)
);
assert_eq!(error.provider_response_body(), Some(body));
}
#[tokio::test]
async fn embedding_http_non_success_preserves_status_and_body() {
use crate::embeddings::EmbeddingModel as _;
use crate::test_utils::RecordingHttpClient;
let body = r#"{"error":{"message":"bad embedding","type":"invalid_request_error"}}"#;
let http_client =
RecordingHttpClient::with_error_response(http::StatusCode::BAD_REQUEST, body);
let client = Client::builder()
.api_key("test-key")
.azure_endpoint("https://example.openai.azure.com".to_string())
.http_client(http_client)
.build()
.expect("build client");
let model = super::EmbeddingModel::new(client, TEXT_EMBEDDING_3_SMALL, None);
let error = match model.embed_texts(vec!["Hello, world!".to_string()]).await {
Err(error) => error,
Ok(_) => panic!("embedding should fail with non-success status"),
};
assert!(matches!(error, EmbeddingError::HttpError(_)));
assert_eq!(
error.provider_response_status(),
Some(http::StatusCode::BAD_REQUEST)
);
assert_eq!(error.provider_response_body(), Some(body));
}
#[tokio::test]
async fn completion_pins_deployment_url_under_model_override() {
use crate::completion::CompletionModel as _;
use crate::test_utils::RecordingHttpClient;
let http_client = RecordingHttpClient::with_error_response(
http::StatusCode::BAD_REQUEST,
r#"{"error":{"message":"x"}}"#,
);
let client = Client::builder()
.api_key("test-key")
.azure_endpoint("https://example.openai.azure.com".to_string())
.http_client(http_client.clone())
.build()
.expect("build client");
let model = super::CompletionModel::new(client, GPT_4O_MINI);
let _ = model
.completion(CompletionRequest {
model: Some("other-deployment".to_string()),
preamble: None,
chat_history: OneOrMany::one("Hello!".into()),
documents: vec![],
max_tokens: None,
temperature: None,
tools: vec![],
tool_choice: None,
additional_params: None,
output_schema: None,
})
.await;
let requests = http_client.requests();
let request = requests.first().expect("request should be captured");
assert!(
request
.uri
.contains("/openai/deployments/gpt-4o-mini/chat/completions"),
"unexpected uri: {}",
request.uri
);
let body: serde_json::Value =
serde_json::from_slice(&request.body).expect("captured body should be JSON");
assert_eq!(body["model"], "other-deployment");
}
#[tokio::test]
async fn completion_http_non_success_preserves_status_and_body() {
use crate::completion::CompletionModel as _;
use crate::test_utils::RecordingHttpClient;
let body = r#"{"error":{"message":"bad completion","type":"invalid_request_error"}}"#;
let http_client =
RecordingHttpClient::with_error_response(http::StatusCode::BAD_REQUEST, body);
let client = Client::builder()
.api_key("test-key")
.azure_endpoint("https://example.openai.azure.com".to_string())
.http_client(http_client)
.build()
.expect("build client");
let model = super::CompletionModel::new(client, GPT_4O_MINI);
let error = match model
.completion(CompletionRequest {
model: None,
preamble: Some("You are a helpful assistant.".to_string()),
chat_history: OneOrMany::one("Hello!".into()),
documents: vec![],
max_tokens: Some(100),
temperature: Some(0.0),
tools: vec![],
tool_choice: None,
additional_params: None,
output_schema: None,
})
.await
{
Err(error) => error,
Ok(_) => panic!("completion should fail with non-success status"),
};
assert!(matches!(error, CompletionError::HttpError(_)));
assert_eq!(
error.provider_response_status(),
Some(http::StatusCode::BAD_REQUEST)
);
assert_eq!(error.provider_response_body(), Some(body));
}
#[tokio::test]
#[ignore]
async fn test_azure_embedding() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
let client = Client::from_env()?;
let model = client.embedding_model(TEXT_EMBEDDING_3_SMALL);
let embeddings = model.embed_texts(vec!["Hello, world!".to_string()]).await?;
tracing::info!("Azure embedding: {:?}", embeddings);
Ok(())
}
#[tokio::test]
#[ignore]
async fn test_azure_embedding_dimensions() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
let ndims = 256;
let client = Client::from_env()?;
let model = client.embedding_model_with_ndims(TEXT_EMBEDDING_3_SMALL, ndims);
let embedding = model.embed_text("Hello, world!").await?;
anyhow::ensure!(
embedding.vec.len() == ndims,
"expected embedding dimensions {ndims}, got {}",
embedding.vec.len()
);
tracing::info!("Azure dimensions embedding: {:?}", embedding);
Ok(())
}
#[tokio::test]
#[ignore]
async fn test_azure_completion() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
let client = Client::from_env()?;
let model = client.completion_model(GPT_4O_MINI);
let completion = model
.completion(CompletionRequest {
model: None,
preamble: Some("You are a helpful assistant.".to_string()),
chat_history: OneOrMany::one("Hello!".into()),
documents: vec![],
max_tokens: Some(100),
temperature: Some(0.0),
tools: vec![],
tool_choice: None,
additional_params: None,
output_schema: None,
})
.await?;
tracing::info!("Azure completion: {:?}", completion);
Ok(())
}
#[tokio::test]
#[ignore]
async fn test_azure_structured_output() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
#[derive(Debug, Deserialize, JsonSchema)]
struct Person {
name: String,
age: u32,
}
let client = Client::from_env()?;
let agent = client
.agent(GPT_5_MINI)
.preamble("You are a helpful assistant that extracts personal details.")
.max_tokens(100)
.output_schema::<Person>()
.build();
let result: Person = agent
.prompt_typed("Hello! My name is John Doe and I'm 54 years old.")
.await?;
anyhow::ensure!(
result.name == "John Doe",
"expected name John Doe, got {}",
result.name
);
anyhow::ensure!(result.age == 54, "expected age 54, got {}", result.age);
tracing::info!("Extracted person: {:?}", result);
Ok(())
}
#[tokio::test]
async fn test_client_initialization() {
let _client = crate::providers::azure::Client::builder()
.api_key("test")
.azure_endpoint("test".to_string()) .build()
.expect("Client::builder() failed");
}
}