sapiens_tools 0.4.0

The tools - Sapiens
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
use std::cmp::Ordering;
use std::collections::HashMap;

use convert_case::{Case, Casing};
use pyo3::prelude::*;
use pyo3::types::{IntoPyDict, PyDict};
use sapiens::tools::{
    invoke_simple_from_toolbox, AdvancedTool, Describe, ProtoToolDescribe, ProtoToolInvoke,
    ToolDescription, ToolUseError, Toolbox,
};
use sapiens_derive::{Describe, ProtoToolDescribe};
use serde::{Deserialize, Serialize};
use serde_yaml::Value;

/// Conversion tools
pub(crate) mod utils;

use crate::python::utils::SimpleToolDescription;

const MAX_OUTPUT_SIZE: usize = 512;

/// A tool that runs sandboxed Python code. Use this to transform data.
///
/// - To use another Tool:
/// ```python
/// input = {'field': ...}
/// output = tools.ToolName(**input)
/// print(output['field'])
/// ```
/// - Only stdout and stderr are captured and made available (limited to 512B
///   total). If the output is larger, use `tools.Conclude` directly from the
///   code.
/// - List available tools with `tools.list()`. And returns a list of
///   `{'name':.., 'description':.., 'input':..,
/// 'output':.., 'description_context':.. }`.
/// - `open`|`exec` are forbidden.
/// - Limited libraries available: urllib3, requests, sympy, numpy,
/// BeautifulSoup4, feedparser, arxiv.
/// - No PIP.
#[derive(Default, ProtoToolDescribe)]
#[tool(
    name = "SandboxedPython",
    input = "PythonToolInput",
    output = "PythonToolOutput"
)]
pub struct PythonTool {}

/// The input of the Python tool
#[derive(Serialize, Deserialize, Describe)]
pub struct PythonToolInput {
    /// The Python code to run. MANDATORY
    pub code: String,
}

/// The output of the Python tool
#[derive(Serialize, Deserialize, Describe)]
pub struct PythonToolOutput {
    /// The stdout output of the Python code.
    pub stdout: String,
    /// The stderr output of the Python code.
    pub stderr: String,
}

#[pyclass]
#[derive(Default)]
struct Logging {
    output: String,
}

#[pymethods]
impl Logging {
    fn write(&mut self, data: &str) {
        self.output.push_str(data);
    }
}

#[pyclass(unsendable)]
struct ToolsWrapper {
    toolbox: Toolbox,
    tool_list: Vec<SimpleToolDescription>,
}

impl ToolsWrapper {
    async fn new(toolbox: Toolbox) -> Self {
        let tools = toolbox.describe().await;
        let tool_list = tools
            .into_values()
            .map(SimpleToolDescription::from)
            .collect::<Vec<_>>();

        ToolsWrapper { toolbox, tool_list }
    }
}

#[pymethods]
impl ToolsWrapper {
    // list all tools
    #[pyo3(signature = ())]
    fn list(&self, py: Python<'_>) -> PyResult<PyObject> {
        let tools = self.tool_list.to_object(py);
        Ok(tools)
    }

    // invoke a tool
    #[pyo3(signature = (tool_name, input))]
    fn invoke(
        &self,
        py: Python<'_>,
        tool_name: &str,
        input: Option<&PyDict>,
    ) -> PyResult<PyObject> {
        // convert PyDict to a serde_yaml::Value
        let input = if let Some(input) = input {
            let input: PyObject = input.into();

            utils::to_yaml(py, &input).map_err(|e| {
                pyo3::exceptions::PyException::new_err(format!("Invalid input: {}", e))
            })?
        } else {
            Value::default()
        };

        // println!("invoking tool {} with input {:?}", tool_name, input);

        // Build the runtime for the new thread.
        //
        // The runtime is created before spawning the thread
        // to more cleanly forward errors if the `unwrap()`
        // panics.
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();

        let (tx, mut rx) = tokio::sync::oneshot::channel::<Result<Value, ToolUseError>>();

        let toolbox = self.toolbox.clone();

        let tool_name = tool_name.to_string();

        std::thread::spawn(move || {
            rt.block_on(async move {
                let output = invoke_simple_from_toolbox(toolbox, &tool_name, input).await;

                match output {
                    Ok(output) => {
                        tx.send(Ok(output)).unwrap();
                    }
                    Err(e) => {
                        tx.send(Err(e)).unwrap();
                    }
                }
            });
        });

        // blockingly wait for the result
        loop {
            if let Ok(output) = rx.try_recv() {
                let output = output.map_err(|e| {
                    pyo3::exceptions::PyException::new_err(format!("Tool invocation failed: {}", e))
                })?;

                let output = utils::value_to_object(output, py);

                return Ok(output);
            }
        }
    }
}

impl PythonTool {
    async fn invoke_typed(
        &self,
        toolbox: Toolbox,
        input: &PythonToolInput,
    ) -> Result<PythonToolOutput, ToolUseError> {
        let code = input.code.clone();

        let tools = toolbox.describe().await;

        let code = Self::transform_code(code, tools)?;

        let toolwrapper = ToolsWrapper::new(toolbox).await;

        // print!("{}", code);

        let res: PyResult<(String, String)> = Python::with_gil(|py| {
            // println!("Python version: {}", py.version());

            let tools_cell = PyCell::new(py, toolwrapper)?;
            let globals = [("toolbox", tools_cell)].into_py_dict(py);

            // capture stdout and stderr
            let sys = py.import("sys")?;

            let stdout = Logging::default();
            let py_stdout_cell = PyCell::new(py, stdout)?;
            let py_stdout = py_stdout_cell.borrow_mut();
            sys.setattr("stdout", py_stdout.into_py(py))?;

            let stderr = Logging::default();
            let py_stderr_cell = PyCell::new(py, stderr)?;
            let py_stderr = py_stderr_cell.borrow_mut();
            sys.setattr("stderr", py_stderr.into_py(py))?;

            // FUTURE(ssoudan) pass something in

            // run code
            Python::run(py, &code, globals.into(), None)?;

            // NOFUTURE(ssoudan) get something out

            let stdout = py_stdout_cell.borrow().output.clone();
            let stderr = py_stderr_cell.borrow().output.clone();

            Ok((stdout, stderr))
        });

        let (stdout, stderr) = res.map_err(|e| {
            ToolUseError::ToolInvocationFailed(format!("Python code execution failed: {}", e))
        })?;

        Ok(PythonToolOutput { stdout, stderr })
    }

    fn transform_code(
        code: String,
        tools: HashMap<String, ToolDescription>,
    ) -> Result<String, ToolUseError> {
        lazy_static::lazy_static! {
            static ref EXEC_RE: regex::Regex = regex::Regex::new(r"(exec|pip)").unwrap();
            static ref IMPORT_RE: regex::Regex = regex::Regex::new(r"(?x)import \s+ tools.*").unwrap();
            static ref FROM_RE: regex::Regex =
                regex::Regex::new(r"(?x)from \s+ tools \s+ import .*").unwrap();
        }

        // check for forbidden keywords - with capture

        if let Some(caps) = EXEC_RE.captures(code.as_ref()) {
            return Err(ToolUseError::ToolInvocationFailed(format!(
                "Python code contains forbidden keywords such as {}",
                caps.get(0).unwrap().as_str()
            )));
        }

        // remove the `import tools` if present
        // remove the `from tools import xxx` if present
        let code = code
            .lines()
            .filter(|&l| !IMPORT_RE.is_match(l))
            .filter(|&l| !FROM_RE.is_match(l))
            // .map(|l| l.replace(r"^import tools.*$", ""))
            // .map(|l| l.replace(r"^from tools import.*", ""))
            .collect::<Vec<_>>()
            .join("\n");

        let mut tool_class_code = String::new();

        tool_class_code.push_str("class Tools:\n");
        tool_class_code.push_str("    def __init__(self, toolbox):\n");
        tool_class_code.push_str("        self.toolbox = toolbox\n");

        let mut binding_code = String::new();

        for (name, description) in tools {
            let inputs_parts = description.input_format.fields;
            // FUTURE(ssoudan) might want to add None only for optional inputs
            let mut inputs = inputs_parts.clone();

            // sort with the optional inputs at the end
            inputs.sort_by(|a, b| {
                if a.optional && !b.optional {
                    Ordering::Greater
                } else if !a.optional && b.optional {
                    Ordering::Less
                } else {
                    Ordering::Equal
                }
            });

            let inputs = inputs
                .into_iter()
                .map(|f| {
                    if f.optional {
                        format!("{}=None", f.name)
                    } else {
                        f.name
                    }
                })
                .collect::<Vec<_>>();

            let inputs = inputs.join(", ");
            let inputs = if inputs.is_empty() {
                "".to_string()
            } else {
                format!("(self, {})", inputs)
            };

            let dict = inputs_parts
                .iter()
                .map(|f| {
                    let name = &f.name;
                    format!("\"{}\": {}", name, name)
                })
                .collect::<Vec<_>>()
                .join(", ");

            // in snake case
            // tool_class_code.push_str(&format!(
            //     "    def {}{}:\n        return self.toolbox.invoke(\"{}\", {{{}}})\n",
            //     name.to_case(Case::Snake),
            //     inputs,
            //     name,
            //     dict
            // ));

            // in Pascal case
            tool_class_code.push_str(&format!(
                "    def {}{}:\n        return self.toolbox.invoke(\"{}\", {{{}}})\n",
                name.to_case(Case::Pascal),
                inputs,
                name,
                dict
            ));

            // add ToolName(..) to the binding code
            binding_code.push_str(&format!(
                "def {}{}:\n        return tools.toolbox.invoke(\"{}\", {{{}}})\n",
                name, inputs, name, dict
            ));

            // FUTURE(ssoudan) set input_format and output_format
        }

        // add list function
        tool_class_code.push_str("    def list(self):\n");
        tool_class_code.push_str("        return self.toolbox.list()\n");

        tool_class_code.push_str("tools = Tools(toolbox)\n");

        let code_to_prepend = format!("{}{}", tool_class_code, binding_code);

        // prepend the code to the user code
        let code = format!("{}\n# ======== user code\n{}", code_to_prepend, code);

        Ok(code)
    }

    fn invoke_sync_typed(&self, input: &PythonToolInput) -> Result<PythonToolOutput, ToolUseError> {
        let code = input.code.clone();

        // check for forbidden keywords - with capture
        let re = regex::Regex::new(r"(exec|pip)").unwrap();
        if let Some(caps) = re.captures(&code) {
            return Err(ToolUseError::ToolInvocationFailed(format!(
                "Python code contains forbidden keywords such as {}",
                caps.get(0).unwrap().as_str()
            )));
        }

        let res: PyResult<(String, String)> = Python::with_gil(|py| {
            // println!("Python version: {}", py.version());

            let globals = PyDict::new(py);

            // capture stdout and stderr
            let sys = py.import("sys")?;

            let stdout = Logging::default();
            let py_stdout_cell = PyCell::new(py, stdout)?;
            let py_stdout = py_stdout_cell.borrow_mut();
            sys.setattr("stdout", py_stdout.into_py(py))?;

            let stderr = Logging::default();
            let py_stderr_cell = PyCell::new(py, stderr)?;
            let py_stderr = py_stderr_cell.borrow_mut();
            sys.setattr("stderr", py_stderr.into_py(py))?;

            // run code
            Python::run(py, &code, globals.into(), None)?;

            let stdout = py_stdout_cell.borrow().output.clone();
            let stderr = py_stderr_cell.borrow().output.clone();

            Ok((stdout, stderr))
        });

        let (stdout, stderr) = res.map_err(|e| {
            ToolUseError::ToolInvocationFailed(format!("Python code execution failed: {}", e))
        })?;

        Ok(PythonToolOutput { stdout, stderr })
    }
}

#[async_trait::async_trait]
impl ProtoToolInvoke for PythonTool {
    async fn invoke(&self, input: serde_yaml::Value) -> Result<serde_yaml::Value, ToolUseError> {
        let input = serde_yaml::from_value(input)?;

        let output = self.invoke_sync_typed(&input)?;

        // check the size of the output (stdout and stderr)
        let l = output.stdout.len() + output.stderr.len();
        if l > MAX_OUTPUT_SIZE {
            return Err(ToolUseError::ToolInvocationFailed(format!(
                "Python code produced too much output on stdout and stderr
        combined ({} bytes) - max is {}",
                l, MAX_OUTPUT_SIZE
            )));
        }

        Ok(serde_yaml::to_value(output)?)
    }
}

#[async_trait::async_trait]
impl AdvancedTool for PythonTool {
    async fn invoke_with_toolbox(
        &self,
        toolbox: Toolbox,
        input: Value,
    ) -> Result<Value, ToolUseError> {
        let input = serde_yaml::from_value(input)?;
        let output = self.invoke_typed(toolbox, &input).await?;
        Ok(serde_yaml::to_value(output)?)
    }
}

#[cfg(test)]
mod tests {
    use indoc::indoc;
    use insta::assert_display_snapshot;
    use sapiens::tools::Toolbox;

    use crate::conclude::ConcludeTool;
    use crate::python::{PythonTool, PythonToolInput};

    #[tokio::test]
    async fn test_code_transformation() {
        let input = PythonToolInput {
            code: indoc! {
                r#"
                import tools
                from tools import Arxiv

                arxiv_results = Arxiv(
                  search_query='cat:cs.AI',
                  max_results=5,
                  sort_by='lastUpdatedDate',
                  sort_order='descending',
                  show_summary=True
                )
            
                formatted_results = []
                for result in arxiv_results['result']:
                    formatted_results.append(f"{result['title']} : {result['pdf_url']}")
                formatted_results = "\n".join(formatted_results)
            
                print({'formatted_results': formatted_results})
            "#}
            .to_string(),
        };

        let mut toolbox = Toolbox::default();
        // toolbox.add_tool(ArxivTool::new().await).await;
        toolbox.add_terminal_tool(ConcludeTool::default()).await;
        // toolbox.add_advanced_tool(PythonTool::default()).await;

        let tools = toolbox.describe().await;

        let code = PythonTool::transform_code(input.code, tools).unwrap();

        assert_display_snapshot!(code);
    }
}