API-ja Gemini ofron një mjet ekzekutimi kodi që i mundëson modelit të gjenerojë dhe ekzekutojë kod Python. Modeli më pas mund të mësojë në mënyrë iterative nga rezultatet e ekzekutimit të kodit derisa të arrijë në një rezultat përfundimtar. Ju mund të përdorni ekzekutimin e kodit për të ndërtuar aplikacione që përfitojnë nga arsyetimi i bazuar në kod. Për shembull, ju mund të përdorni ekzekutimin e kodit për të zgjidhur ekuacione ose për të përpunuar tekst. Ju gjithashtu mund të përdorni libraritë e përfshira në mjedisin e ekzekutimit të kodit për të kryer detyra më të specializuara.
Gemini është në gjendje të ekzekutojë kod vetëm në Python. Ju prapë mund t'i kërkoni Gemini-t të gjenerojë kod në një gjuhë tjetër, por modeli nuk mund ta përdorë mjetin e ekzekutimit të kodit për ta ekzekutuar atë.
Aktivizo ekzekutimin e kodit
Për të aktivizuar ekzekutimin e kodit, konfiguroni mjetin e ekzekutimit të kodit në model. Kjo i lejon modelit të gjenerojë dhe ekzekutojë kod.
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.7-flash",
input="What is the sum of the first 50 prime numbers? "
"Generate and run code for the calculation, and make sure you get all 50.",
tools=[{"type": "code_execution"}]
)
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print(content_block.text)
elif step.type == "code_execution_call":
print(step.arguments.code)
elif step.type == "code_execution_result":
print(step.result)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3.7-flash",
input: "What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50.",
tools: [{ type: "code_execution" }]
});
for (const step of interaction.steps) {
if (step.type === "model_output") {
for (const contentBlock of step.content) {
if (contentBlock.type === "text") {
console.log(contentBlock.text);
}
}
} else if (step.type === "code_execution_call") {
console.log(step.arguments.code);
} else if (step.type === "code_execution_result") {
console.log(step.result);
}
}