feat: Update execute_python_code tool to enforce explicit print() statements for calculations and add test_executor.py for validation
This commit is contained in:
Binary file not shown.
@@ -88,7 +88,7 @@ NORMAL_CHAT_PROMPT = """You're ChatGPT for Discord! You have access to powerful
|
|||||||
- scrape_webpage: Extract and analyze content from websites
|
- scrape_webpage: Extract and analyze content from websites
|
||||||
|
|
||||||
💻 **Programming & Data Tools:**
|
💻 **Programming & Data Tools:**
|
||||||
- execute_python_code: Run Python code for calculations, math problems, algorithms, and custom programming tasks
|
- execute_python_code: Run Python code for ALL calculations, math problems, algorithms, and custom programming tasks (ALWAYS use print() statements to display results)
|
||||||
- analyze_data_file: Analyze CSV/Excel files with pre-built templates when users request data analysis or insights
|
- analyze_data_file: Analyze CSV/Excel files with pre-built templates when users request data analysis or insights
|
||||||
|
|
||||||
🎨 **Creative Tools:**
|
🎨 **Creative Tools:**
|
||||||
@@ -103,6 +103,8 @@ NORMAL_CHAT_PROMPT = """You're ChatGPT for Discord! You have access to powerful
|
|||||||
- If they want custom programming or specific code → file path is provided to execute_python_code
|
- If they want custom programming or specific code → file path is provided to execute_python_code
|
||||||
- Both tools can install packages and create visualizations automatically displayed in Discord
|
- Both tools can install packages and create visualizations automatically displayed in Discord
|
||||||
|
|
||||||
|
**IMPORTANT FOR CALCULATIONS:** When using execute_python_code for mathematical calculations, ALWAYS write complete Python code with explicit print() statements. For example, for "calculate 2+2", write: print(2+2), not just: 2+2
|
||||||
|
|
||||||
Always explain your approach step-by-step and provide clear, Discord-friendly responses without excessive markdown. You MUST respond in the same language as the user."""
|
Always explain your approach step-by-step and provide clear, Discord-friendly responses without excessive markdown. You MUST respond in the same language as the user."""
|
||||||
|
|
||||||
SEARCH_PROMPT = "You are a Research Assistant with access to Google Search results. Your task is to synthesize information from search results to provide accurate, comprehensive answers. When analyzing search results: 1) Prioritize information from credible sources, 2) Compare and contrast different perspectives when available, 3) Acknowledge when information is limited or unclear, and 4) Cite specific sources when presenting facts. Structure your response in a clear, logical manner, focusing on directly answering the user's question while providing relevant context."
|
SEARCH_PROMPT = "You are a Research Assistant with access to Google Search results. Your task is to synthesize information from search results to provide accurate, comprehensive answers. When analyzing search results: 1) Prioritize information from credible sources, 2) Compare and contrast different perspectives when available, 3) Acknowledge when information is limited or unclear, and 4) Cite specific sources when presenting facts. Structure your response in a clear, logical manner, focusing on directly answering the user's question while providing relevant context."
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -31,7 +31,6 @@ def get_tools_for_model() -> List[Dict[str, Any]]:
|
|||||||
- Use when: math problems, programming tasks, custom scripts, algorithm implementations
|
- Use when: math problems, programming tasks, custom scripts, algorithm implementations
|
||||||
- Can create visualizations from scratch
|
- Can create visualizations from scratch
|
||||||
- Has sandboxed environment with package installation
|
- Has sandboxed environment with package installation
|
||||||
- Automatically gets context about uploaded files when available
|
|
||||||
|
|
||||||
2. analyze_data_file: For structured data analysis from CSV/Excel files
|
2. analyze_data_file: For structured data analysis from CSV/Excel files
|
||||||
- Use when: user explicitly requests data analysis, statistics, or insights from data files
|
- Use when: user explicitly requests data analysis, statistics, or insights from data files
|
||||||
@@ -329,9 +328,10 @@ def get_tools_for_model() -> List[Dict[str, Any]]:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"type": "function", "function": {
|
"type": "function",
|
||||||
|
"function": {
|
||||||
"name": "execute_python_code",
|
"name": "execute_python_code",
|
||||||
"description": "**GENERAL PYTHON EXECUTION TOOL** - Use this tool for general programming tasks, mathematical calculations, algorithm implementations, and custom Python scripts. Use this tool when: (1) User asks for calculations or math problems, (2) User wants to run custom Python code, (3) User needs algorithm implementations, (4) User requests programming solutions, (5) Creating custom visualizations or data processing, (6) User uploads data files but wants custom code rather than standard analysis. File paths for uploaded data files are automatically made available in the execution environment.",
|
"description": "**GENERAL PYTHON EXECUTION TOOL** - Use this tool for general programming tasks, mathematical calculations, algorithm implementations, and custom Python scripts. CRITICAL: You MUST write complete Python code with explicit print() statements to display any results. For calculations, write: print(your_calculation). Never write bare expressions without print(). Use this tool when: (1) User asks for calculations or math problems - ALWAYS wrap in print(), (2) User wants to run custom Python code, (3) User needs algorithm implementations, (4) User requests programming solutions, (5) Creating custom visualizations or data processing, (6) User uploads data files but wants custom code rather than standard analysis.",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|||||||
@@ -327,7 +327,7 @@ async def execute_code_safely(code: str, input_data: str, timeout: int) -> Dict[
|
|||||||
|
|
||||||
# Execute the code with timeout
|
# Execute the code with timeout
|
||||||
try:
|
try:
|
||||||
# Use asyncio.wait_for for timeout
|
# Execute the code as statements
|
||||||
await asyncio.wait_for(
|
await asyncio.wait_for(
|
||||||
asyncio.to_thread(exec, code, exec_globals),
|
asyncio.to_thread(exec, code, exec_globals),
|
||||||
timeout=timeout
|
timeout=timeout
|
||||||
|
|||||||
37
test_executor.py
Normal file
37
test_executor.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import asyncio
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Add the src directory to the path
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
||||||
|
|
||||||
|
from utils.python_executor import execute_python_code
|
||||||
|
|
||||||
|
async def test_calculation():
|
||||||
|
# Test with proper print statement (what the AI should generate now)
|
||||||
|
args = {
|
||||||
|
'code': 'print((3+2+1+1231231+2139018230912)/3+120/99+2012)'
|
||||||
|
}
|
||||||
|
print('Testing with proper print statement:', repr(args['code']))
|
||||||
|
|
||||||
|
result = await execute_python_code(args)
|
||||||
|
print('Success:', result.get('success', False))
|
||||||
|
print('Output:', repr(result.get('output', '')))
|
||||||
|
print('Expected result: 713006489396.2122')
|
||||||
|
|
||||||
|
# Test another calculation
|
||||||
|
args2 = {
|
||||||
|
'code': '''
|
||||||
|
result = (3+2+1+1231231+2139018230912)/3+120/99+2012
|
||||||
|
print(f"The calculation result is: {result}")
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
print('\nTesting with formatted output:', repr(args2['code']))
|
||||||
|
|
||||||
|
result2 = await execute_python_code(args2)
|
||||||
|
print('Success:', result2.get('success', False))
|
||||||
|
print('Output:', repr(result2.get('output', '')))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(test_calculation())
|
||||||
Reference in New Issue
Block a user