Connect Riza’s code interpreter to Claude
anthropic
and rizaio
packages with pip
:
OB2XE4DMMUQG233ONNSXSIDENFZWQ53BONUGK4Q=
).
For a correct result, we can offer Claude a tool to execute Python.
execute_function()
helper, which we’ll use to handle any tool use calls. This function uses Riza to safely execute code.
execute_python_function
tool to execute Python code, the response will
include a tool_use
block with the execute_python_function
name.
This block will have a set of input parameters corresponding to the input properties
we specified in our tool definition. In this case, the parameters we care about are
code
and input
.
We take the code that Claude wants to run and execute it using Riza:
tool_result
message to send back.
This step is optional, as the code execution output might be all you need.
execute_python_function
tool.
But what happens if Claude’s first attempt at writing code results in an error? In our example so far, we don’t give Claude the chance to recover from that mistake.
Here’s one way to address this problem: we’ll adjust our script to keep calling Claude until Claude decides it no longer wants to use a tool. This solution is only appropriate because our prompt requires Claude to use a tool to produce a correct answer.
Here’s our revised script, which introduces a while loop. Notice that if execute_python_function
results in an error, we now pass the error to Claude, and Claude can respond to it. (Ideally, Claude would try calling the tool again with a different piece of code.)