azure_comprehensive/
azure_comprehensive.rs

1//! Comprehensive Azure `OpenAI` API Test
2//!
3//! This example tests various Azure `OpenAI` endpoints to verify that the
4//! integration works correctly across different API features.
5
6use openai_ergonomic::Client;
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    tracing_subscriber::fmt::init();
11
12    println!("=== Azure OpenAI Comprehensive API Test ===\n");
13
14    let client = Client::from_env()?.build();
15
16    // Test 1: Simple chat completion
17    println!("1. Testing simple chat completion...");
18    let builder = client.chat_simple("What is 2+2? Answer in one word.");
19    match client.send_chat(builder).await {
20        Ok(response) => {
21            if let Some(content) = response.content() {
22                println!("   ✓ Chat completion: {content}");
23            }
24        }
25        Err(e) => println!("   ✗ Chat completion failed: {e}"),
26    }
27
28    // Test 2: Chat with system message
29    println!("\n2. Testing chat with system message...");
30    let builder = client.chat_with_system(
31        "You are a helpful assistant that responds in one sentence.",
32        "What is Rust?",
33    );
34    match client.send_chat(builder).await {
35        Ok(response) => {
36            if let Some(content) = response.content() {
37                println!("   ✓ System message chat: {content}");
38            }
39        }
40        Err(e) => println!("   ✗ System message chat failed: {e}"),
41    }
42
43    // Test 3: Chat with temperature
44    println!("\n3. Testing chat with custom parameters...");
45    let builder = client
46        .chat()
47        .user("Say 'test' in a creative way")
48        .temperature(0.7)
49        .max_tokens(50);
50    match client.send_chat(builder).await {
51        Ok(response) => {
52            if let Some(content) = response.content() {
53                println!("   ✓ Custom parameters: {content}");
54            }
55        }
56        Err(e) => println!("   ✗ Custom parameters failed: {e}"),
57    }
58
59    // Test 4: Multiple messages conversation
60    println!("\n4. Testing multi-message conversation...");
61    let builder = client
62        .chat()
63        .system("You are a helpful assistant")
64        .user("My name is Alice")
65        .assistant("Hello Alice! Nice to meet you.")
66        .user("What's my name?");
67    match client.send_chat(builder).await {
68        Ok(response) => {
69            if let Some(content) = response.content() {
70                println!("   ✓ Multi-message: {content}");
71            }
72        }
73        Err(e) => println!("   ✗ Multi-message failed: {e}"),
74    }
75
76    // Test 5: Chat with max_tokens limit
77    println!("\n5. Testing with max_tokens limit...");
78    let builder = client.chat().user("Explain quantum physics").max_tokens(20);
79    match client.send_chat(builder).await {
80        Ok(response) => {
81            if let Some(content) = response.content() {
82                println!("   ✓ Limited tokens: {content}");
83                println!("   (Note: response is truncated due to max_tokens=20)");
84            }
85        }
86        Err(e) => println!("   ✗ Max tokens test failed: {e}"),
87    }
88
89    // Test 6: Using responses API
90    println!("\n6. Testing responses API...");
91    let builder = client.responses().user("What is the capital of France?");
92    match client.send_responses(builder).await {
93        Ok(response) => {
94            if let Some(content) = response.content() {
95                println!("   ✓ Responses API: {content}");
96            }
97        }
98        Err(e) => println!("   ✗ Responses API failed: {e}"),
99    }
100
101    println!("\n=== Test Summary ===");
102    println!("Azure OpenAI integration tested across multiple endpoints!");
103    println!("\nNote: Some advanced features like embeddings, streaming, and");
104    println!("tool calling may require specific Azure OpenAI deployments.");
105
106    Ok(())
107}