Running Python Code
Overview
Witsy includes a built-in Python interpreter that allows the AI to perform complex calculations, manipulate data, and solve logic problems that are traditionally difficult for LLMs. Instead of "hallucinating" math results, the AI writes a Python script, executes it locally, and uses the output to answer your prompt.
Enabling the Python Plugin
Before the AI can run code, you must ensure the Python plugin is active for your current chat session or workspace.
- Open Settings: Navigate to the Witsy settings menu.
- Plugins Section: Look for the Plugins or Tools tab.
- Toggle Python: Ensure the Python interpreter is enabled.
- Chat Toolbar: In an active chat window, you can often toggle specific tools using the plugin icon (typically found near the message input area).
How to Use the Python Interpreter
You don't need to write the Python code yourself. Instead, describe the task to the AI. When Witsy identifies a task requiring computation, it will automatically:
- Generate the necessary Python script.
- Execute the script in a secure environment.
- Show you the code blocks and the resulting output.
Recipe: Advanced Mathematical Analysis
If you need to calculate compound interest over 20 years with varying rates, a standard LLM might struggle.
Prompt:
"Calculate the final balance of a $10,000 investment after 20 years with a 5% annual return, compounded monthly. Show the growth every 5 years."
What Witsy does: Witsy will generate a script using a loop or the compound interest formula and provide the exact figures based on the code's output.
Recipe: Data Manipulation and Transformation
You can paste raw data (like CSV or JSON) into the chat and ask the Python plugin to process it.
Prompt:
"I'm going to paste a list of transactions. Use Python to calculate the total spent on 'Groceries' and 'Rent', then tell me which category had the highest single transaction." [Paste your data here]
What Witsy does: The AI will write a script to parse the text, filter the categories, and perform the aggregation.
Recipe: Logic and String Operations
Python is excellent for complex text transformations that require strict rules, such as generating permutations or cleaning messy strings.
Prompt:
"Take the following list of names, remove any special characters, capitalize only the first letter, and sort them by the length of the name."
Using Python with the CLI
If you are using Witsy via the Command Line Interface (CLI), the Python interpreter is available as part of the CliPlugin. When you run Witsy in a specific directory using the --workDir flag, the AI can use Python to interact with files in that path (subject to the access levels defined).
# Example: Asking the CLI to analyze a local file using Python
witsy "Read 'data.csv' and use Python to find the average value in the 'Price' column"
Tips for Best Results
- Be Specific: If you want the AI to use a specific library (like
numpyorpandas), mention it in your prompt. - Check the Output: Witsy displays the code it ran. You can click on the code block to inspect the logic and ensure the AI's approach matches your requirements.
- Sandbox Awareness: For security, the Python environment is sandboxed. It is designed for computation and data processing rather than system-level operations or network requests.
- Error Handling: If the code execution fails, the AI will see the error trace and usually attempt to fix the script automatically and re-run it.