openai_test/
openai_test.rs1use 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 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 let client = AiClient::new(Provider::OpenAI)?;
22 println!("β
OpenAI client created successfully");
23
24 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 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}