Class TurCodeInterpreterToolService
java.lang.Object
com.viglet.turing.genai.tool.TurCodeInterpreterToolService
-
Constructor Summary
ConstructorsConstructorDescriptionTurCodeInterpreterToolService(TurGlobalSettingsService turGlobalSettingsService) -
Method Summary
-
Constructor Details
-
TurCodeInterpreterToolService
-
-
Method Details
-
executePython
@Tool(name="execute_python", description="Executes Python code and returns the output (stdout and stderr).\nUse this tool when the user asks you to:\n- Solve math or statistical problems\n- Process, analyze, or transform data (CSV, JSON, Excel)\n- Generate charts or graphs (use matplotlib/seaborn, save to 'output.png')\n- Perform calculations, conversions, or simulations\n- Run any computation that benefits from actual code execution\n\nThe code runs in a temporary sandbox directory. Any files generated (charts, CSVs, etc.) are saved there and accessible via URL.\n\nIMPORTANT:\n- Use print() to produce output that will be returned to you.\n- For charts: use plt.savefig('output.png', dpi=150, bbox_inches='tight') then print the filename. Do NOT call plt.show().\n- For data files: save to the current directory and print the filename.\n- Common libraries available: math, json, csv, datetime, os, sys, re, statistics, collections, itertools, functools, urllib.\n- Libraries that MAY be available (if installed): numpy, pandas, matplotlib, seaborn, scipy, openpyxl, requests, Pillow.\n- Python version is 3.12+.\n\nINSTALLING MISSING LIBRARIES:\n- If a library is missing (ModuleNotFoundError or ImportError), install it BEFORE running the actual code by calling execute_python with an install script:\n import subprocess, sys\n subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"matplotlib\", \"numpy\"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n- Always use sys.executable so pip installs into the same interpreter.\n- You may install multiple packages in a single call: [\"matplotlib\", \"numpy\", \"pandas\", \"seaborn\", \"scipy\", \"openpyxl\", \"Pillow\"].\n- After a successful install call execute_python again with the real code.\n- If pip itself fails (no internet, permissions), report the error and suggest a stdlib-only alternative when possible.\n\nCHART STYLING (mandatory for all charts):\n- Always start chart code with: plt.style.use('seaborn-v0_8-darkgrid')\n- Remove top and right spines: ax.spines['top'].set_visible(False); ax.spines['right'].set_visible(False)\n- Use colorblind-friendly palette: ['#0072B2', '#D55E00', '#009E73', '#CC79A7', '#F0E442', '#56B4E9', '#E69F00']\n- Set figure background to transparent or white: fig.patch.set_facecolor('white')\n- Use tight_layout() before savefig for clean margins.\n\nDISPLAYING GENERATED FILES:\n- The tool returns file URLs starting with /api/v2/code-interpreter/...\n- To display an image, use EXACTLY the URL returned, with NO prefix. Example: \n- NEVER add \"sandbox:\" or any other prefix to the URL.\n\nERROR HANDLING:\n- If the code fails (non-zero exit code) or produces warnings/errors in stderr, the output will include the error details.\n- When you receive a ModuleNotFoundError, run the pip install step first, then retry with the real code. Count install + retry as a single attempt.\n- For other errors, analyze and fix the code, then call execute_python again. You may retry up to 3 times before giving up.\n- Common fixes: missing imports, deprecated APIs, syntax errors.\n\nArgs:\n code (str): Python code to execute. Required.\nReturns:\n The stdout output of the code, any stderr messages, and URLs for generated files.") public String executePython(String code)
-