llmgraph 0.1.1

This library provides a framework for building conversational AI applications with function calling capabilities using a graph-based architecture.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! LLMGraph - A Multi-Agent AI System Framework
//!
//! This library provides a framework for building conversational AI applications
//! with function calling capabilities using a graph-based architecture.

pub mod generate;
pub mod models;
pub mod errors;
pub mod agents;

// Re-export commonly used types for convenience
pub use errors::{LLMGraphError, LLMGraphResult};
pub use models::graph::{Agent, Graph};
pub use models::tools::{Tool, ToolRegistry, ToolRegistryTrait, Message};

// Include comprehensive test module
#[cfg(test)]
mod tests;

// Legacy function kept for compatibility
pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

// ================================
// EXISTING INTEGRATION TESTS
// ================================
// These tests demonstrate end-to-end functionality with real LLM integration

#[cfg(test)]
mod integration_tests {
    use super::*;
    use crate::models::graph::{Agent, Graph};
    use crate::models::tools::{
        Function, Message, Parameters, Property, Tool, ToolRegistryTrait
    };
    use async_trait::async_trait;
    use std::collections::HashMap;

    #[tokio::test]
    async fn graph_test() {
        pub struct ManagerAgent;

        #[async_trait]
        impl Agent for ManagerAgent {
            async fn run(
                &mut self,
                input: &str,
                _tool_registry: &(dyn ToolRegistryTrait + Send + Sync),
            ) -> (String, Option<i32>) {
                let response = format!("Manager received: '{}'. Delegating to developer.", input);
                (response, Some(1)) // Pass to developer (ID 1)
            }

            fn get_name(&self) -> &str {
                "Manager"
            }
        }

        pub struct DeveloperAgent;

        #[async_trait]
        impl Agent for DeveloperAgent {
            async fn run(
                &mut self,
                input: &str,
                _tool_registry: &(dyn ToolRegistryTrait + Send + Sync),
            ) -> (String, Option<i32>) {
                let response = format!(
                    "Developer working on: '{}'. Implementation complete.",
                    input
                );
                (response, None) // End of chain
            }

            fn get_name(&self) -> &str {
                "Developer"
            }
        }

        // Create a new graph
        let mut graph = Graph::new();

        // Add agents to the graph
        graph.add_node(0, Box::new(ManagerAgent));
        graph.add_node(1, Box::new(DeveloperAgent));

        // Connect the agents
        if let Err(e) = graph.add_edge(0, 1) {
            println!("Error adding edge: {}", e);
        }

        // Print the graph structure
        graph.print();

        // Run the graph starting from the manager
        println!("\nRunning the graph:");
        let output = graph.run(0, "Create a new feature").await;
        println!("{}", output);
    }

    #[tokio::test]
    async fn test_chain() {
        pub struct ManagerAgent;

        #[async_trait]
        impl Agent for ManagerAgent {
            async fn run(
                &mut self,
                input: &str,
                _tool_registry: &(dyn ToolRegistryTrait + Send + Sync),
            ) -> (String, Option<i32>) {
                let api_key =
                    "*"
                        .to_string();
                let base_url = "https://openrouter.ai/api/v1/chat/completions".to_string();
                let model = "z-ai/glm-4.5".to_string();
                let temperature = 0.1;
                let messages: Vec<Message> = vec![Message {
                    role: "system".to_string(),
                    content: Some(input.to_string()),
                    tool_calls: None,
                }];

                // Generate the response using the updated generate function
                let generated_response =
                    generate::generate::generate(base_url, api_key, model, temperature, messages)
                        .await
                        .unwrap_or_else(|_| "Failed to generate response".to_string());

                let response = format!(
                    "Manager received: '{}'. Response: '{}'. Delegating to developer.",
                    input, generated_response
                );
                (response, Some(1)) // Pass to developer (ID 1)
            }

            fn get_name(&self) -> &str {
                "Manager"
            }
        }

        pub struct DeveloperAgent;

        #[async_trait]
        impl Agent for DeveloperAgent {
            async fn run(
                &mut self,
                input: &str,
                _tool_registry: &(dyn ToolRegistryTrait + Send + Sync),
            ) -> (String, Option<i32>) {
                let response = format!(
                    "Developer working on: '{}'. Implementation complete.",
                    input
                );
                (response, None) // End of chain
            }

            fn get_name(&self) -> &str {
                "Developer"
            }
        }

        // Create a new graph
        let mut graph = Graph::new();

        // Add agents to the graph
        graph.add_node(0, Box::new(ManagerAgent));
        graph.add_node(1, Box::new(DeveloperAgent));

        // Connect the agents
        if let Err(e) = graph.add_edge(0, 1) {
            println!("Error adding edge: {}", e);
        }

        // Print the graph structure
        graph.print();

        // Run the graph starting from the manager
        println!("\nRunning the graph:");
        let output = graph
            .run(
                0,
                "what tools do you have list them and use the weather tool afterwards!",
            )
            .await;
        println!("{}", output);
    }

  #[tokio::test]
async fn test_generate_with_tools() {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    
    // Create flags to track if tools were called
    let weather_called = Arc::new(AtomicBool::new(false));
    let calculator_called = Arc::new(AtomicBool::new(false));
    
    // Clone the Arcs for the closures
    let weather_called_clone = weather_called.clone();
    let calculator_called_clone = calculator_called.clone();
    
    // Define weather tool creation function
    let create_weather_tool = || -> Tool {
        Tool {
            tool_type: "function".to_string(),
            function: Function {
                name: "get_weather".to_string(),
                description: "Get the current weather for a location".to_string(),
                parameters: Parameters {
                    param_type: "object".to_string(),
                    properties: {
                        let mut props = HashMap::new();
                        props.insert("location".to_string(), Property {
                            prop_type: "string".to_string(),
                            description: Some("The city and state, e.g. San Francisco, CA".to_string()),
                            items: None,
                        });
                        props
                    },
                    required: vec!["location".to_string()],
                },
            },
        }
    };
    
    // Define weather tool function
    let weather_tool_function = move |args: serde_json::Value| -> Result<serde_json::Value, String> {
        weather_called_clone.store(true, Ordering::SeqCst);
        let location = args["location"].as_str()
            .ok_or("Missing 'location' parameter")?;
        
        // Mock implementation
        Ok(serde_json::json!({
            "location": location,
            "temperature": 72,
            "condition": "Sunny",
            "humidity": 65
        }))
    };
    
    // Define calculator tool creation function
    let create_calculator_tool = || -> Tool {
        Tool {
            tool_type: "function".to_string(),
            function: Function {
                name: "calculate".to_string(),
                description: "Perform a mathematical calculation".to_string(),
                parameters: Parameters {
                    param_type: "object".to_string(),
                    properties: {
                        let mut props = HashMap::new();
                        props.insert("expression".to_string(), Property {
                            prop_type: "string".to_string(),
                            description: Some("The mathematical expression to evaluate, e.g. '2 + 2'".to_string()),
                            items: None,
                        });
                        props
                    },
                    required: vec!["expression".to_string()],
                },
            },
        }
    };
    
    // Define calculator tool function
    let calculator_tool_function = move |args: serde_json::Value| -> Result<serde_json::Value, String> {
        calculator_called_clone.store(true, Ordering::SeqCst);
        let expression = args["expression"].as_str()
            .ok_or("Missing 'expression' parameter")?;
        
        // Simple calculator implementation
        let tokens: Vec<&str> = expression.split_whitespace().collect();
        if tokens.len() != 3 {
            return Err("Expression must have exactly 3 parts: num1 operator num2".to_string());
        }

        let num1: f64 = tokens[0].parse().map_err(|_| "Invalid first number")?;
        let operator = tokens[1];
        let num2: f64 = tokens[2].parse().map_err(|_| "Invalid second number")?;

        let result = match operator {
            "+" => num1 + num2,
            "-" => num1 - num2,
            "*" => num1 * num2,
            "/" => {
                if num2 == 0.0 {
                    return Err("Division by zero".to_string());
                }
                num1 / num2
            }
            _ => return Err(format!("Unknown operator: {}", operator)),
        };

        Ok(serde_json::json!({
            "expression": expression,
            "result": result
        }))
    };

    // Define a ManagerAgent that properly handles tool calling
    pub struct ManagerAgent {
        api_key: String,
        model: String,
    }

    impl ManagerAgent {
        pub fn new(api_key: String, model: String) -> Self {
            Self { api_key, model }
        }
    }

#[async_trait]
impl Agent for ManagerAgent {
    async fn run(
        &mut self,
        input: &str,
        tool_registry: &(dyn ToolRegistryTrait + Send + Sync),
    ) -> (String, Option<i32>) {
            // Get tools from the registry
            let tools = tool_registry.get_tools();
            
            // Create initial messages
            let mut messages = vec![
                Message {
                    role: "system".to_string(),
                    content: Some("You are a helpful assistant. Use the available tools to answer questions.".to_string()),
                    tool_calls: None,
                },
                Message {
                    role: "user".to_string(),
                    content: Some(input.to_string()),
                    tool_calls: None,
                }
            ];
            
            // Maximum iterations to prevent infinite loops
            let max_iterations = 5;
            let mut iteration = 0;
            
            // Agentic loop for tool calling
            while iteration < max_iterations {
                iteration += 1;
                println!("Iteration {}", iteration);
                
                // Step 1: Send request with tools
                let response = generate::generate::generate_full_response(
                    "https://openrouter.ai/api/v1/chat/completions".to_string(),
                    self.api_key.clone(),
                    self.model.clone(),
                    0.1,
                    messages.clone(),
                    Some(tools.clone())
                ).await;
                
                match response {
                    Ok(llm_response) => {
                        let choice = &llm_response.choices[0];
                        let assistant_message = &choice.message;
                        
                        // Add assistant message to conversation
                        messages.push(Message {
                            role: "assistant".to_string(),
                            content: assistant_message.content.clone(),
                            tool_calls: assistant_message.tool_calls.clone(),
                        });
                        
                        // Check if there are tool calls
                        if let Some(tool_calls) = &assistant_message.tool_calls {
                            println!("LLM requested tool calls:");
                            
                            // Step 2: Execute tools and prepare results
                            for tool_call in tool_calls {
                                println!("- Tool: {}, Arguments: {}", tool_call.function.name, tool_call.function.arguments);
                                
                                // Execute the tool
                                let tool_result = tool_registry.execute_tool(
                                    &tool_call.function.name,
                                    &tool_call.function.arguments,
                                );
                                
                                let result_content = match tool_result {
                                    Ok(result) => {
                                        let result_str = serde_json::to_string(&result)
                                            .unwrap_or_else(|_| "Tool result".to_string());
                                        println!("Tool result: {}", result_str);
                                        result_str
                                    }
                                    Err(e) => {
                                        println!("Tool error: {}", e);
                                        format!("Error: {}", e)
                                    }
                                };
                                
                                // Step 3: Add tool result to conversation
                                messages.push(Message {
                                    role: "tool".to_string(),
                                    content: Some(result_content),
                                    tool_calls: None,
                                });
                            }
                            
                            // Continue the loop to let the model respond to tool results
                            continue;
                        } else if let Some(content) = &assistant_message.content {
                            println!("LLM responded without tool calls: {}", content);
                            
                            // No tool calls, we're done
                            return (format!("Manager processed: '{}'. Result: {}", input, content), Some(1));
                        } else {
                            return (format!("Manager received empty response for: '{}'", input), Some(1));
                        }
                    }
                    Err(e) => {
                        println!("Error generating response: {}", e);
                        return (format!("Error processing request: {}", e), Some(1));
                    }
                }
            }
            
            // If we reached max iterations, return the last response
            if let Some(last_message) = messages.last() {
                if let Some(content) = &last_message.content {
                    return (format!("Manager processed: '{}'. Result: {}", input, content), Some(1));
                }
            }
            
            (format!("Manager reached max iterations for: '{}'", input), Some(1))
        }

        fn get_name(&self) -> &str {
            "ManagerAgent"
        }
    }

    // Define a DeveloperAgent
    pub struct DeveloperAgent;

    #[async_trait]
    impl Agent for DeveloperAgent {
        async fn run(&mut self, input: &str, _tool_registry: &(dyn ToolRegistryTrait + Send + Sync)) -> (String, Option<i32>) {
            let response = format!("Developer received: '{}'. Processing complete.", input);
            (response, None) // End of chain
        }

        fn get_name(&self) -> &str {
            "DeveloperAgent"
        }
    }
    
    // Create a new graph
    let mut graph = Graph::new();
    
    // Register tools with the mock functions
    graph.register_tool(create_weather_tool(), weather_tool_function);
    graph.register_tool(create_calculator_tool(), calculator_tool_function);
    
    // Create agents
    let manager_agent = ManagerAgent::new(
        "{{api_key}}".to_string(), // Replace with your actual API key
        "model".to_string(),
    );
    
    // Add agents to the graph
    graph.add_node(0, Box::new(manager_agent));
    graph.add_node(1, Box::new(DeveloperAgent));
    
    // Connect the agents
    if let Err(e) = graph.add_edge(0, 1) {
        println!("Error adding edge: {}", e);
    }
    
    // Print the graph structure
    graph.print();
    
    // Run the graph with a prompt that requires tool usage
    println!("\nRunning the graph with tool calling:");
    let output = graph.run(0, "What's the weather in Boston and calculate 20 * 5?").await;
    println!("{}", output);
    
    // Assert that the output contains expected information
    assert!(output.contains("Boston") || output.contains("weather"));
    assert!(output.contains("20 * 5") || output.contains("100"));
    assert!(output.contains("Manager processed"));
    assert!(output.contains("Developer received"));
    
    // Check if the tools were called
    assert!(weather_called.load(Ordering::SeqCst), "Weather tool was not called");
    assert!(calculator_called.load(Ordering::SeqCst), "Calculator tool was not called");
    
    println!("✓ Weather tool was called: {}", weather_called.load(Ordering::SeqCst));
    println!("✓ Calculator tool was called: {}", calculator_called.load(Ordering::SeqCst));
}
}