|
| 1 | +use std::error::Error; |
| 2 | + |
| 3 | +use turingflow::rchain::chat_models::{ChatFireworks, ChatMessage}; |
| 4 | +use turingflow::rchain::tools::{ToolDefinition, ToolFunction, ToolParam, ToolParamType}; |
| 5 | + |
| 6 | +fn multiply(a: i64, b: i64) -> i64 { |
| 7 | + a * b |
| 8 | +} |
| 9 | + |
| 10 | +pub fn run_calc( |
| 11 | + prompt: impl Into<String>, |
| 12 | + model: impl Into<String>, |
| 13 | + temperature: f64, |
| 14 | +) -> Result<(), Box<dyn Error>> { |
| 15 | + let tool = ToolDefinition::from_function( |
| 16 | + ToolFunction::new("multiply", "Multiply two integers.") |
| 17 | + .with_param(ToolParam::new( |
| 18 | + "a", |
| 19 | + ToolParamType::Integer, |
| 20 | + true, |
| 21 | + Some("First factor.".to_string()), |
| 22 | + )) |
| 23 | + .with_param(ToolParam::new( |
| 24 | + "b", |
| 25 | + ToolParamType::Integer, |
| 26 | + true, |
| 27 | + Some("Second factor.".to_string()), |
| 28 | + )), |
| 29 | + ); |
| 30 | + |
| 31 | + let llm = ChatFireworks::new(model, temperature)?.bind_tools(vec![tool]); |
| 32 | + let user_message = ChatMessage::user_text(prompt.into()); |
| 33 | + let response = llm.invoke_messages(&[user_message.clone()])?; |
| 34 | + |
| 35 | + if response.tool_calls.is_empty() { |
| 36 | + println!("Model answer: {}", response.content); |
| 37 | + return Ok(()); |
| 38 | + } |
| 39 | + |
| 40 | + for tool_call in &response.tool_calls { |
| 41 | + if tool_call.name != "multiply" { |
| 42 | + continue; |
| 43 | + } |
| 44 | + let a = tool_call |
| 45 | + .args |
| 46 | + .get("a") |
| 47 | + .and_then(|value| value.as_i64()) |
| 48 | + .ok_or("Missing integer argument 'a' for multiply")?; |
| 49 | + let b = tool_call |
| 50 | + .args |
| 51 | + .get("b") |
| 52 | + .and_then(|value| value.as_i64()) |
| 53 | + .ok_or("Missing integer argument 'b' for multiply")?; |
| 54 | + let result = multiply(a, b); |
| 55 | + |
| 56 | + println!("Tool result: {}", result); |
| 57 | + |
| 58 | + let final_response = llm.invoke_messages(&[ |
| 59 | + user_message.clone(), |
| 60 | + ChatMessage::assistant_from_ai(&response), |
| 61 | + ChatMessage::tool_result(tool_call.id.clone(), result.to_string()), |
| 62 | + ])?; |
| 63 | + |
| 64 | + println!("Final answer: {}", final_response.content); |
| 65 | + } |
| 66 | + |
| 67 | + Ok(()) |
| 68 | +} |
0 commit comments