This is my first attempt at a code interpreter using the API.
{
"name": "run_python_code",
"description": "Executes any given Python code string with exec(code_string,{}{})",
"parameters": {
"type": "object",
"properties": {
"code_string": {
"type": "string",
"description": "The Python code string to execute."
}
},
"required": ["code_string"]
}
,
"returns": {
"type": "object",
"properties": {
"function_response": {
"type": "string",
"description": "The redirected stdout result from the exec function."
},
"details": {
"type": "string",
"description": "Exception details in case of an error."
}
}
}
}
Here is the function. It is part of a bigger source code.
def run_python_code(kwargs):
try:
response = {}
rpc_globals = {}
rpc_locals = {}
code_string = kwargs.get("code_string", "function_response = 'No code given.'")
original_stdout = sys.stdout
new_output = io.StringIO()
sys.stdout = new_output
try:
exec(code_string, rpc_globals, rpc_locals)
function_response = new_output.getvalue().strip()
finally:
sys.stdout = original_stdout
if function_response:
response["function_response"] = function_response
response_string = jsonpickle.encode(response)
except Exception as e:
response = {
"function_response": "Error executing code.",
"details": str(e)
}
response_string = jsonpickle.encode(response)
return response_string
jsonpickle is a somewhat more powerful replacement for json.
i’m using this system prompt:
You are a python code interpreter.
Results of the code or any formula must always be output with print().
With the help of the run_python_code function, you can execute any Python code, including those that display graphical elements.
You do not produce code that could harm the user or their hardware.
You describe the code before you execute it.
Example:
U = R * I
U = 10 Ohm * 1 A
Print(U)
Conversation:
# user #: Calculate the resistance of a 10m copper cable with a cross-sectional area of 4mm^2.
# assistant #: To calculate the resistance of a copper cable, we use the formula:
R = ρ * (L/A)
where:
- R is the resistance,
- ρ is the resistivity of the material (for copper it’s 1.68 x 10^-8 Ω.m),
- L is the length of the wire,
- A is the cross-sectional area of the wire.
Given:
- L = 10m,
- A = 4mm^2 = 4 x 10^-6 m^2 (since 1mm^2 = 10^-6 m^2).
Let’s calculate the resistance.
# run_python_code (request) #:
{
“code_string”: “rho = 1.68 * 10**-8\nL = 10\nA = 4 * 10**-6\nR = rho * (L/A)\nprint(R)”
}
# run_python_code (function) #: {“function_response”: “0.041999999999999996”}
# assistant #: The resistance of a 10m copper cable with a cross-sectional area of 4mm^2 is approximately 0.042 Ohms.
For smaller codes, it works quite well. I’ve built in a query before each execution so that one can review the code and possibly decline its execution.
I consider this more of a study. Because exec has some significant drawbacks. Using exec ties the execution to the program in which it is run. So, one would have to lock the entire application in a sandbox to avoid any problems. Also, the use of the globals and locals parameters is a bit tricky. Especially when GPT thinks it needs to create functions. At least one learns a lot about the exec command if one didn’t already have this knowledge. 
In the next step, I want to use a Jupyter kernel. One could lock it separately in a Docker container. But I still need to familiarize myself with it a bit.