कोड लागू करें

Gemini API, कोड एक्ज़ीक्यूशन टूल उपलब्ध कराता है. इसकी मदद से, मॉडल Python कोड जनरेट और रन कर सकता है. इसके बाद, मॉडल कोड के चलने पर मिलने वाले नतीजों से बार-बार सीख सकता है. यह प्रोसेस तब तक चलती है, जब तक मॉडल को फ़ाइनल आउटपुट नहीं मिल जाता. कोड एक्ज़ीक्यूशन का इस्तेमाल करके, ऐसे ऐप्लिकेशन बनाए जा सकते हैं जिनमें कोड के आधार पर गहराई से विश्लेषण करने की सुविधा होती है. उदाहरण के लिए, कोड एक्ज़ीक्यूशन का इस्तेमाल करके, समीकरण हल किए जा सकते हैं या टेक्स्ट को प्रोसेस किया जा सकता है. इसके अलावा, कोड एक्ज़ीक्यूशन एनवायरमेंट में शामिल लाइब्रेरी का इस्तेमाल करके, ज़्यादा खास टास्क पूरे किए जा सकते हैं.

Gemini, सिर्फ़ Python में कोड चला सकता है. हालांकि, Gemini से किसी दूसरी भाषा में कोड जनरेट करने के लिए कहा जा सकता है. हालांकि, मॉडल इसे रन करने के लिए, कोड एक्ज़ीक्यूशन टूल का इस्तेमाल नहीं कर सकता.

कोड एक्ज़ीक्यूशन चालू करें

कोड एक्ज़ीक्यूशन चालू करने के लिए, मॉडल पर कोड एक्ज़ीक्यूशन टूल को कॉन्फ़िगर करें. इससे मॉडल, कोड जनरेट और रन कर सकता है.

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-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.6-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