Anthropic
Add Riza as a Python code interpreter to Claude 3
LLMs like Anthropic’s Claude are good at lots of things, but executing code isn’t one of them. Fortunately, models like Claude 3 understand this and allow you to supplement their functionality with external “tools”.
In this guide we’ll use Python to hook up the Riza Code Interpreter API as a tool for Claude to use when it wants or needs to execute Python. Using Riza as the code execution environment keeps your local machine safe in case the model does something unexpected.
First we import and initialize required libraries from Anthropic and Riza.
We’ll create a message for Claude that requires code execution to answer. Here we ask Claude to base32 encode the message “purple monkey dishwasher.”
Sending this message to Claude without offering any additional tools results in unexpected hallucinated output. Here are two consecutive responses to the above prompt.
The results are inconsistent, and neither is correct (the correct result is
OB2XE4DMMUQG233ONNSXSIDENFZWQ53BONUGK4Q=
).
For a correct result, we can offer Claude a tool to execute Python.
Before sending the message to Claude, we’ll describe the Riza Code Interpreter API as a tool using a tool definition that Claude understands.
Now we can send the message and tool definition to Claude.
If Claude wants to use the available tool to execute Python code, the response will
include a tool_use
block with the execute_python
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 parameter we care about is
named code
.
We take the code that Claude wants to run and execute it via the Riza Code Interpreter API.
If the execution is successful,
we add the output as a tool_result
message to send back. This step is
optional, as the code execution output might be all you need.
If necessary, we can send the updated list of messages back to Claude to get a final answer incorporating the code execution output.
With the extra tool in its belt, Claude comes up with the correct response.
See the full example on GitHub.