azure_openai/azure_openai.rs
1//! Azure `OpenAI` Integration Example
2//!
3//! This example demonstrates how to use the openai-ergonomic library with Azure `OpenAI`.
4//!
5//! # Azure `OpenAI` Configuration
6//!
7//! Azure `OpenAI` requires different configuration than standard `OpenAI`:
8//! 1. An Azure `OpenAI` endpoint (e.g., `<https://my-resource.openai.azure.com>`)
9//! 2. An API key from your Azure `OpenAI` resource
10//! 3. A deployment name (not a model name)
11//! 4. An API version (defaults to 2024-02-01)
12//!
13//! # Setup
14//!
15//! ## Option 1: Environment Variables
16//!
17//! ```bash
18//! export AZURE_OPENAI_ENDPOINT="https://my-resource.openai.azure.com"
19//! export AZURE_OPENAI_API_KEY="your-azure-api-key"
20//! export AZURE_OPENAI_DEPLOYMENT="gpt-4"
21//! export AZURE_OPENAI_API_VERSION="2024-02-01" # Optional, defaults to 2024-02-01
22//! ```
23//!
24//! ## Option 2: Manual Configuration
25//!
26//! See the examples below for programmatic configuration.
27//!
28//! # Run the Example
29//!
30//! ```bash
31//! cargo run --example azure_openai
32//! ```
33
34use openai_ergonomic::{Client, Config};
35
36#[tokio::main]
37async fn main() -> Result<(), Box<dyn std::error::Error>> {
38 // Initialize logging
39 tracing_subscriber::fmt::init();
40
41 println!("Azure OpenAI Integration Example");
42 println!("=================================\n");
43
44 // Example 1: Using environment variables
45 println!("Example 1: Using environment variables");
46 match Client::from_env() {
47 Ok(client) => {
48 let client = client.build();
49 println!("Client created from environment variables");
50
51 // Make a simple chat request
52 let builder = client.chat_simple("Hello from Azure OpenAI!");
53 match client.send_chat(builder).await {
54 Ok(response) => {
55 if let Some(content) = response.content() {
56 println!("Response: {content}");
57 }
58 }
59 Err(e) => {
60 println!("Error: {e}");
61 }
62 }
63 }
64 Err(e) => {
65 println!("Could not create client from environment: {e}");
66 println!("Make sure to set AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, and AZURE_OPENAI_DEPLOYMENT");
67 }
68 }
69
70 println!("\n---\n");
71
72 // Example 2: Manual configuration
73 println!("Example 2: Manual configuration");
74
75 // This example shows how to configure Azure `OpenAI` programmatically.
76 // Replace these values with your actual Azure `OpenAI` resource details.
77 let config = Config::builder()
78 .api_key("your-azure-api-key")
79 .api_base("https://my-resource.openai.azure.com")
80 .azure_deployment("gpt-4")
81 .azure_api_version("2024-02-01")
82 .build();
83
84 println!("Config: {config:?}");
85 println!("Is Azure: {}", config.is_azure());
86
87 // Note: This will fail unless you provide valid credentials above
88 // Uncomment the following to test with your actual credentials:
89 /*
90 let client = Client::builder(config)?.build();
91
92 // Simple chat completion
93 let response = client
94 .chat_simple("Tell me a short joke about Azure")
95 .await?;
96 println!("Response: {}", response);
97
98 // More advanced chat with custom parameters
99 let response = client
100 .chat()
101 .user("What are the main features of Azure OpenAI?")
102 .temperature(0.7)
103 .max_tokens(500)
104 .send()
105 .await?;
106
107 println!("\nAdvanced response:");
108 println!("{}", response.content());
109
110 // Streaming example
111 use futures::StreamExt;
112
113 println!("\nStreaming example:");
114 let mut stream = client
115 .chat()
116 .user("Count from 1 to 5")
117 .stream()
118 .await?;
119
120 while let Some(chunk) = stream.next().await {
121 print!("{}", chunk?.content());
122 }
123 println!();
124 */
125
126 println!("\n---\n");
127
128 // Example 3: Key differences between `OpenAI` and Azure `OpenAI`
129 println!("Example 3: Key differences between OpenAI and Azure OpenAI");
130 println!("\nOpenAI:");
131 println!(" - Endpoint: https://api.openai.com/v1");
132 println!(" - Authentication: Bearer token in Authorization header");
133 println!(" - Model specification: Use model names like 'gpt-4', 'gpt-3.5-turbo'");
134 println!(" - Example: client.chat().model('gpt-4').send().await?\n");
135
136 println!("Azure OpenAI:");
137 println!(" - Endpoint: https://{{{{resource-name}}}}.openai.azure.com");
138 println!(" - Authentication: api-key header");
139 println!(" - Deployment specification: Use your deployment name");
140 println!(" - API version required as query parameter");
141 println!(" - Example: Configure deployment in Config, then use client normally\n");
142
143 println!("With this library, you only need to configure the endpoint and deployment,");
144 println!("and the library handles all the differences automatically!");
145
146 Ok(())
147}