File size: 2,303 Bytes
c2446d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ํŒŒ์ด์ฌ ์ฝ”๋“œ ์‹คํ–‰ ๋„๊ตฌ.

GAIA์— "attached Python code"์˜ ์ถœ๋ ฅ์„ ๋ฌป๋Š” ์งˆ๋ฌธ(์˜ˆ: f918266a)์ด ๋“ฑ์žฅ.
์ฒจ๋ถ€ ํŒŒ์ผ์€ ํ…์ŠคํŠธ๋กœ ๋ฐ›์ง€๋งŒ ์ถœ๋ ฅ์„ ์•Œ๋ ค๋ฉด ์‹ค์ œ ์‹คํ–‰์ด ํ•„์š”. smolagents์˜ ์ž์ฒด
์ฝ”๋“œ sandbox๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋ช…์‹œ ๋„๊ตฌ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด (a) LLM์ด ์˜๋„๋ฅผ ๋ช…ํ™•ํžˆ
ํ‘œํ˜„ (b) stdout ์บก์ฒ˜๋ฅผ ๋‹จ์ˆœํ™”ํ•œ๋‹ค.

์•ˆ์ „: ์ฒจ๋ถ€๋œ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์‹คํ–‰ํ•˜๋ฏ€๋กœ ์‹ ๋ขฐ๋˜์ง€ ์•Š์€ ์ž…๋ ฅ์— ๋…ธ์ถœ. GAIA ์ฑ„์ 
์ปจํ…์ŠคํŠธ ํ•œ์ •์œผ๋กœ ์‚ฌ์šฉ. ์‹ค์„œ๋น„์Šค์—๋Š” sandbox ๊ฐ•ํ™” ํ•„์š”.
"""
import os
import subprocess
import sys
import tempfile
from smolagents import tool


@tool
def exec_python_code(code: str) -> str:
    """Execute Python source code and return its captured stdout.
    Use this when the question asks for the output of a piece of attached or referenced
    Python code (e.g., "What is the final numeric output of the code?"). Pass the code
    body verbatim. Captures both stdout and stderr; returns up to ~12k characters.

    Args:
        code: The Python source code to execute.
    """
    if len(code) > 50000:
        return "exec_python_code error: code is too large to execute safely"

    try:
        with tempfile.TemporaryDirectory(prefix="gaia_exec_") as tmpdir:
            script_path = os.path.join(tmpdir, "snippet.py")
            with open(script_path, "w", encoding="utf-8") as f:
                f.write(code)
            result = subprocess.run(
                [sys.executable, "-I", script_path],
                cwd=tmpdir,
                capture_output=True,
                text=True,
                timeout=8,
            )
        out = (result.stdout or "") + (result.stderr or "")
        if result.returncode != 0:
            out = f"exec_python_code exited with status {result.returncode}\n{out}"
    except subprocess.TimeoutExpired as e:
        partial = ((e.stdout or "") + (e.stderr or ""))[:4000]
        return f"exec_python_code error: TimeoutExpired after 8s\n--- partial output ---\n{partial}"
    except Exception as e:
        return (
            f"exec_python_code error: {type(e).__name__}: {e}\n"
            f"--- partial output ---\n"
        )

    if len(out) > 12000:
        out = out[:12000] + "\n...[truncated]"
    return out or "(no stdout output)"