09_test_groq/
09_test_groq.rs

1use ceylon_next::agent::Agent;
2use ceylon_next::llm::LLMConfig;
3use ceylon_next::tasks::TaskRequest;
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("๐Ÿงช Testing Groq LLM Provider with API Key\n");
8
9    // Test 1: Using Agent::new_with_config() - Recommended approach
10    println!("{}", "=".repeat(60));
11    println!("Test 1: Using Agent::new_with_config() with explicit API key");
12    println!("{}", "=".repeat(60));
13
14    match std::env::var("GROQ_API_KEY") {
15        Ok(api_key) => {
16            println!("โœ“ GROQ_API_KEY found in environment\n");
17
18            let groq_config = LLMConfig::new("groq::llama-3.3-70b-versatile")
19                .with_api_key(api_key)
20                .with_temperature(0.7)
21                .with_max_tokens(100);
22
23            match Agent::new_with_config("Groq Agent", groq_config) {
24                Ok(mut agent) => {
25                    println!("โœ“ Groq agent created successfully!\n");
26
27                    println!("Running task: 'What is 2 + 2?'");
28                    let task = TaskRequest::new("What is 2 + 2? Answer in one short sentence.");
29                    println!("Sending request to Groq API...\n");
30
31                    let response = agent.run(task).await;
32
33                    println!("Response received:");
34                    println!("================");
35                    println!("{:?}", response.result());
36                    println!("================\n");
37
38                    println!("๐ŸŽ‰ Test 1 completed successfully!");
39                }
40                Err(e) => {
41                    println!("โœ— Failed to create Groq agent: {}\n", e);
42                }
43            }
44        }
45        Err(_) => {
46            println!("โœ— GROQ_API_KEY not found in environment");
47            println!("  Please set GROQ_API_KEY to run this test\n");
48        }
49    }
50
51    // Test 2: Using Agent::new() with environment variable - Simpler approach
52    println!("\n{}", "=".repeat(60));
53    println!("Test 2: Using Agent::new() with environment variable");
54    println!("{}", "=".repeat(60));
55
56    if std::env::var("GROQ_API_KEY").is_ok() {
57        println!("โœ“ GROQ_API_KEY is set, agent will use it automatically\n");
58
59        match std::panic::catch_unwind(|| {
60            Agent::new("Simple Groq Agent", "groq::llama-3.3-70b-versatile")
61        }) {
62            Ok(mut agent) => {
63                println!("โœ“ Groq agent created successfully with auto API key!\n");
64
65                println!("Running task: 'What is the capital of France?'");
66                let task = TaskRequest::new("What is the capital of France? Answer in one word.");
67
68                let response = agent.run(task).await;
69
70                println!("Response received:");
71                println!("================");
72                println!("{:?}", response.result());
73                println!("================\n");
74
75                println!("๐ŸŽ‰ Test 2 completed successfully!");
76            }
77            Err(_) => {
78                println!("โœ— Failed to create agent (unexpected panic)\n");
79            }
80        }
81    } else {
82        println!("โœ— GROQ_API_KEY not set, skipping this test\n");
83    }
84
85    println!("\n{}", "=".repeat(60));
86    println!("All tests completed!");
87    println!("{}", "=".repeat(60));
88
89    Ok(())
90}