openai_test/
openai_test.rs

1/// OpenAI Provider ζ΅‹θ―•η€ΊδΎ‹ - OpenAI Provider test example
2use ai_lib::types::common::Content;
3use ai_lib::{AiClient, ChatCompletionRequest, Message, Provider, Role};
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("πŸ€– OpenAI Provider Test");
8    println!("======================");
9
10    // Check API key
11    match std::env::var("OPENAI_API_KEY") {
12        Ok(_) => println!("βœ… OPENAI_API_KEY detected"),
13        Err(_) => {
14            println!("❌ OPENAI_API_KEY environment variable not set");
15            println!("   Please set: export OPENAI_API_KEY=your_api_key");
16            return Ok(());
17        }
18    }
19
20    // Create OpenAI client
21    let client = AiClient::new(Provider::OpenAI)?;
22    println!("βœ… OpenAI client created successfully");
23
24    // Get model list
25    println!("\nπŸ“‹ Getting OpenAI model list...");
26    match client.list_models().await {
27        Ok(models) => {
28            println!("βœ… Successfully got {} models", models.len());
29            println!("   Common models:");
30            for model in models.iter().filter(|m| m.contains("gpt")) {
31                println!("   β€’ {}", model);
32            }
33        }
34        Err(e) => println!("❌ Failed to get model list: {}", e),
35    }
36
37    // Test chat completion
38    println!("\nπŸ’¬ Testing chat completion...");
39    let request = ChatCompletionRequest::new(
40        "gpt-3.5-turbo".to_string(),
41        vec![Message {
42            role: Role::User,
43            content: Content::Text(
44                "Hello! Please respond with 'Hello from OpenAI!' to confirm the connection."
45                    .to_string(),
46            ),
47            function_call: None,
48        }],
49    )
50    .with_max_tokens(20)
51    .with_temperature(0.7);
52
53    match client.chat_completion(request).await {
54        Ok(response) => {
55            println!("βœ… Chat completion successful!");
56            println!("   Model: {}", response.model);
57            println!(
58                "   Response: {}",
59                response.choices[0].message.content.as_text()
60            );
61            println!(
62                "   Token usage: {} (prompt: {}, completion: {})",
63                response.usage.total_tokens,
64                response.usage.prompt_tokens,
65                response.usage.completion_tokens
66            );
67        }
68        Err(e) => println!("❌ Chat completion failed: {}", e),
69    }
70
71    println!("\n🎯 OpenAI config-driven test completed!");
72    println!("   This demonstrates the power of config-driven architecture:");
73    println!("   β€’ No need to write OpenAI-specific code");
74    println!("   β€’ Just add configuration in ProviderConfigs");
75    println!("   β€’ Automatically supports all OpenAI-compatible features");
76
77    Ok(())
78}