smolagents_rs/tools/
final_answer.rs

1//! This module contains the final answer tool. The model uses this tool to provide a final answer to the problem.
2
3use serde::{Deserialize, Serialize};
4use schemars::JsonSchema;
5
6use super::base::BaseTool;
7use super::tool_traits::Tool;
8use anyhow::Result;
9
10
11#[derive(Debug, Deserialize, JsonSchema)]
12#[schemars(title = "FinalAnswerToolParams")]
13pub struct FinalAnswerToolParams {
14    #[schemars(description = "The final answer to the problem")]
15    answer: String,
16}
17
18#[derive(Debug, Serialize, Default, Clone)]
19pub struct FinalAnswerTool {
20    pub tool: BaseTool,
21}
22
23impl FinalAnswerTool {
24    pub fn new() -> Self {
25        FinalAnswerTool {
26            tool: BaseTool {
27                name: "final_answer",
28                description: "Provides a final answer to the given problem.",
29            },
30        }
31    }
32}
33
34impl Tool for FinalAnswerTool {
35    type Params = FinalAnswerToolParams;
36    fn name(&self) -> &'static str {
37        self.tool.name
38    }
39    fn description(&self) -> &'static str {
40        self.tool.description
41    }
42
43    fn forward(&self, arguments: FinalAnswerToolParams) -> Result<String> {
44        Ok(arguments.answer)
45    }
46}
47
48
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_final_answer_tool() {
56        let tool = FinalAnswerTool::new();
57    let arguments = FinalAnswerToolParams {
58        answer: "The answer is 42".to_string(),
59        };
60        let result = tool.forward(arguments).unwrap();
61        assert_eq!(result, "The answer is 42");
62    }
63}