Genkit

Unlocking the power of code execution in Genkit

tanabee
Firebase Developers
3 min readSep 19, 2024

--

Learn how to seamlessly integrate Python code within your Genkit workflows for enhanced output generation and automation.

Code execution, introduced in Genkit 0.5.8, enables the execution of Python code during output generation. Let’s take a look at how this works.

Initializing a Genkit Project

Let’s first initialize a Genkit project. The Genkit version must be 0.5.8 or higher, so let’s start by installing Genkit.

% npm i -g genkit
% export GOOGLE_GENAI_API_KEY=<your API key>
% mkdir genkit-code-execution-sample
% cd genkit-code-execution-sample
% genkit init
? Select a runtime to initialize a Genkit project:
❯ Node.js
? Select a model provider:
❯ Google AI
? Select a model provider: Google AI
? Would you like to generate a sample flow? Yes
✔ Successfully generated sample file (src/index.ts)
Genkit successfully initialized.

Enabling Code Execution

You can enable code execution by simply adding codeExecution: true to the arguments of the generate method. Also, the prompt will be modified to pass the input text as is.

const llmResponse = await generate({
model: gemini15Flash,
prompt,
config: {
temperature: 1,
codeExecution: true, // Add this line
},
});

Actual Behavior

Run the following to launch the Genkit Developer UI.

% genkit start -o

Select the implemented flow from the Flow menu.

First, let’s try requesting the following prompt:
“Simulate the ratio of heads to tails after flipping a coin 100,000 times.”

By selecting View trace, you can see that the code has actually been executed.

Next, let’s try a more direct request:
“Execute the following code in Python: print(‘Hello World’)”

As with the previous request, selecting View trace shows that the code has been executed.

Finally, let’s try a general request unrelated to code.

You can see that no code was executed. This is because code execution is specified as one of the tool options, and the generative AI only uses code execution when necessary.

Summary

With just one additional line, we were able to use code execution in our Genkit flows. This expands the possibilities of what can be done with Genkit. Please give it a try.

Here is the code on GitHub.

References

--

--

tanabee
Firebase Developers

Vice President, RPG TEC. Google Developers Expert / Licensed Scrum Master / Website: https://tanabee.github.io