harbor run -t openthoughts/tasktrove-mix-h8-adversarial-tests-v2-mix-h8-adversarial-1139harbor download openthoughts/tasktrove-mix-h8-adversarial-tests-v2-mix-h8-adversarial-1139solution file
"""Execute Python code to obtain information that otherwise hard for LLMs such as numeric compute or complex logic
"""
import io
import os
import ast
import time
import traceback as tb
from .command import Command
from ..models import CompletionModel
def exec_and_return(script, globals=None, locals=None):
'''Execute a script and return the value of the last expression
Note this was written by GPT and looks legit in a glance. Check more carefully when time permits
'''
stmts = list(ast.iter_child_nodes(ast.parse(script)))
if not stmts:
return None
if isinstance(stmts[-1], ast.Expr):
# the last one is an expression and we will try to return the results
# so we first execute the previous statements
if len(stmts) > 1:
exec(compile(ast.Module(body=stmts[:-1], type_ignores=[]), filename="<ast>", mode="exec"), globals, locals)
# then we eval the last one
return eval(compile(ast.Expression(body=stmts[-1].value), filename="<ast>", mode="eval"), globals, locals)
else:
# otherwise we just execute the entire code
return exec(script, globals, locals)
class PythonCommand(Command,
command='python',
description="""Submit Python code that perform complex tasks or computations. Printouts or error messages will be returned to you. If you do not explicitly create a file it will not be created. The content must be a Python dictionary with fields:
- code: Required field. A string containing the code snippet
- save_as: Optional. String. Filename for the code to be saved as; do not include path.
- return_variables: Optional. List of variable names that you need to be returned after the code is executed
- packages: Optional. A list of packages that need to be installed
- execute: Optional. If False, the code will be saved but not executed. Default is True.
"""
):
config = {
'fix_model': 'gpt-4',
}
def generate_prompt(self):
"""Take the python code it write and run them"""
stdout_buffer = io.StringIO()
if isinstance(self.content, list): # Need future work to handle multiple scripts in one command
if len(self.content) > 1:
self.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py