Example code
Get the full code for this example in our GitHub.Getting started
To generate Python with Gemini, you’ll need an API key from the Google. To execute Python using Riza’s Code Interpreter API, you’ll need an API key from the Riza Dashboard. Make these API keys available in your shell environment:Python environment setup
Create a virtualenv and activate it:google-generativeai
and rizaio
packages with pip
:
Scenario
We’ll create a message for Gemini that requires code execution to answer. Here, we ask Gemini to base32 encode the message “purple monkey dishwasher.”Response
OB2XE4DMMUQG233ONNSXSIDENFZWQ53BONUGK4Q=
.
For a correct result, we can offer Gemini a tool to execute Python.
Step 1: Import required libraries
First we import and initialize required libraries from Google and Riza.Step 2: Define a code execution tool
Before sending the message to Gemini we’ll describe the Riza Code Interpreter as a tool using a tool definition that Claude understands. In this example, we will use Riza’s Execute Code API to run the code. So first, we define anexecute_python
helper function in our script. This function uses Riza to execute arbitrary Python:
Step 3: Call Gemini with Riza as a tool
Now, we can makeexecute_python()
function available to Gemini as a tool, and call Gemini with our query:
enable_automatic_function_calling
setting to allow Gemini to call any function without us having to explicitly handle the tool use. If you’d like to explicitly handle the tool use, you can disable this setting.
Finally, let’s print out the full chat history so we can see the function call: