कोड को चलाने की सुविधा

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

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

कोड चलाने की सुविधा चालू करना

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

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    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-flash-preview",
    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);
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "gemini-3-flash-preview",
    "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"}]
}'

आउटपुट कुछ इस तरह दिख सकता है. इसे पढ़ने में आसानी हो, इसलिए फ़ॉर्मैट किया गया है:

Okay, I need to calculate the sum of the first 50 prime numbers. Here's how I'll
approach this:

1.  **Generate Prime Numbers:** I'll use an iterative method to find prime
    numbers. I'll start with 2 and check if each subsequent number is divisible
    by any number between 2 and its square root. If not, it's a prime.
2.  **Store Primes:** I'll store the prime numbers in a list until I have 50 of
    them.
3.  **Calculate the Sum:**  Finally, I'll sum the prime numbers in the list.

Here's the Python code to do this:

def is_prime(n):
  """Efficiently checks if a number is prime."""
  if n <= 1:
    return False
  if n <= 3:
    return True
  if n % 2 == 0 or n % 3 == 0:
    return False
  i = 5
  while i * i <= n:
    if n % i == 0 or n % (i + 2) == 0:
      return False
    i += 6
  return True

primes = []
num = 2
while len(primes) < 50:
  if is_prime(num):
    primes.append(num)
  num += 1

sum_of_primes = sum(primes)
print(f'{primes=}')
print(f'{sum_of_primes=}')

primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]
sum_of_primes=5117

The sum of the first 50 prime numbers is 5117.

इस आउटपुट में, कॉन्टेंट के कई ऐसे हिस्से शामिल होते हैं जिन्हें कोड एक्ज़ीक्यूशन का इस्तेमाल करते समय मॉडल दिखाता है:

  • text: मॉडल से जनरेट किया गया इनलाइन टेक्स्ट
  • code_execution_call: मॉडल से जनरेट किया गया कोड, जिसे एक्ज़ीक्यूट किया जाना है
  • code_execution_result: एक्ज़ीक्यूटेबल कोड का नतीजा

इमेज के साथ कोड एक्ज़ीक्यूट करना (Gemini 3)

Gemini 3 Flash मॉडल अब Python कोड लिख सकता है और उसे चला सकता है. इससे वह इमेज में बदलाव कर सकता है और उनकी जाँच कर सकता है.

इस्तेमाल के उदाहरण

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

इमेज के साथ कोड चलाने की सुविधा चालू करना

Gemini 3 Flash में, इमेज के साथ कोड को एक्ज़ीक्यूट करने की सुविधा आधिकारिक तौर पर उपलब्ध है. इस सुविधा को चालू करने के लिए, टूल के तौर पर कोड एक्ज़ीक्यूशन और सोचने की क्षमता, दोनों को चालू करें.

Python

from google import genai
import requests
import base64
from PIL import Image
import io

image_path = "https://goo.gle/instrument-img"
image_bytes = requests.get(image_path).content

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=[
        {"type": "image", "data": base64.b64encode(image_bytes).decode('utf-8'), "mime_type": "image/jpeg"},
        {"type": "text", "text": "Zoom into the expression pedals and tell me how many pedals are there?"}
    ],
    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 content_block.type == "image":
                # Display generated image
                display(Image.open(io.BytesIO(base64.b64decode(content_block.data))))
    elif step.type == "code_execution_call":
        print(step.arguments.code)
    elif step.type == "code_execution_result":
        print(step.result)

JavaScript

async function main() {
  const client = new GoogleGenAI({});

  // 1. Prepare Image Data
  const imageUrl = "https://goo.gle/instrument-img";
  const response = await fetch(imageUrl);
  const imageArrayBuffer = await response.arrayBuffer();
  const base64ImageData = Buffer.from(imageArrayBuffer).toString('base64');

  // 2. Call the API with Code Execution enabled
  const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: [
      {
        type: "image",
        data: base64ImageData,
        mimeType: "image/jpeg"
      },
      { type: "text", text: "Zoom into the expression pedals and tell me how many pedals are there?" }
    ],
    tools: [{ type: "code_execution" }]
  });

  // 3. Process the response (Text, Code, and Execution Results)
  for (const step of interaction.steps) {
    if (step.type === "model_output") {
      for (const contentBlock of step.content) {
        if (contentBlock.type === "text") {
          console.log("Text:", contentBlock.text);
        }
      }
    } else if (step.type === "code_execution_call") {
      console.log(`\nGenerated Code:\n`, step.arguments.code);
    } else if (step.type === "code_execution_result") {
      console.log(`\nExecution Output:\n`, step.result);
    }
  }
}

main();

REST

IMG_URL="https://goo.gle/instrument-img"
MODEL="gemini-3-flash-preview"

MIME_TYPE=$(curl -sIL "$IMG_URL" | grep -i '^content-type:' | awk -F ': ' '{print $2}' | sed 's/\r$//' | head -n 1)
if [[ -z "$MIME_TYPE" || ! "$MIME_TYPE" == image/* ]]; then
  MIME_TYPE="image/jpeg"
fi

if [[ "$(uname)" == "Darwin" ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -b 0)
elif [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64)
else
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -w0)
fi

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3-flash-preview",
      "input": [
        {
          "type": "image",
          "data": "'"$IMAGE_B64"'",
          "mime_type": "'"$MIME_TYPE"'"
        },
        {"type": "text", "text": "Zoom into the expression pedals and tell me how many pedals are there?"}
      ],
      "tools": [{"type": "code_execution"}]
    }'

बार-बार किए जाने वाले इंटरैक्शन में कोड को चलाने की सुविधा का इस्तेमाल करना

previous_interaction_id का इस्तेमाल करके, एक से ज़्यादा बार की गई बातचीत में कोड को एक्ज़ीक्यूट किया जा सकता है.

Python

from google import genai

client = genai.Client()

# First turn
interaction1 = client.interactions.create(
    model="gemini-3-flash-preview",
    input="I have a math question for you.",
    tools=[{"type": "code_execution"}]
)
print(interaction1.steps[-1].content[0].text)

# Second turn - follow-up with code execution
interaction2 = client.interactions.create(
    model="gemini-3-flash-preview",
    previous_interaction_id=interaction1.id,
    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 interaction2.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({});

// First turn
const interaction1 = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "I have a math question for you.",
    tools: [{ type: "code_execution" }]
});
console.log(interaction1.steps.at(-1).content[0].text);

// Second turn - follow-up with code execution
const interaction2 = await client.interactions.create({
    model: "gemini-3-flash-preview",
    previousInteractionId: interaction1.id,
    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 interaction2.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);
    }
}

REST

# First turn
RESPONSE1=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "gemini-3-flash-preview",
    "input": "I have a math question for you.",
    "tools": [{"type": "code_execution"}]
}')

INTERACTION_ID=$(echo $RESPONSE1 | jq -r '.id')

# Second turn with previous_interaction_id
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "gemini-3-flash-preview",
    "previous_interaction_id": "'"$INTERACTION_ID"'",
    "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"}]
}'

इनपुट/आउटपुट (I/O)

Gemini 2.0 Flash से शुरू करके, कोड को एक्ज़ीक्यूट करने की सुविधा में फ़ाइल इनपुट और ग्राफ़ आउटपुट का इस्तेमाल किया जा सकता है. इनपुट और आउटपुट की इन सुविधाओं का इस्तेमाल करके, CSV और टेक्स्ट फ़ाइलें अपलोड की जा सकती हैं. साथ ही, फ़ाइलों के बारे में सवाल पूछे जा सकते हैं. इसके अलावा, जवाब के तौर पर Matplotlib ग्राफ़ जनरेट किए जा सकते हैं. आउटपुट फ़ाइलें, जवाब में इनलाइन इमेज के तौर पर दिखती हैं.

I/O की कीमत

कोड एक्ज़ीक्यूशन I/O का इस्तेमाल करने पर, आपसे इनपुट टोकन और आउटपुट टोकन के लिए शुल्क लिया जाता है:

इनपुट टोकन:

  • उपयोगकर्ता का प्रॉम्प्ट

आउटपुट टोकन:

  • मॉडल से जनरेट किया गया कोड
  • कोड एनवायरमेंट में कोड को चलाने का आउटपुट
  • सोच-विचार वाले टोकन
  • मॉडल से जनरेट की गई खास जानकारी

I/O की जानकारी

कोड एक्ज़ीक्यूशन I/O के साथ काम करते समय, इन तकनीकी बातों का ध्यान रखें:

  • कोड एनवायरमेंट का ज़्यादा से ज़्यादा रनटाइम 30 सेकंड होता है.
  • अगर कोड एनवायरमेंट में कोई गड़बड़ी होती है, तो मॉडल कोड आउटपुट को फिर से जनरेट करने का फ़ैसला ले सकता है. ऐसा पांच बार तक किया जा सकता है.
  • फ़ाइल के इनपुट का ज़्यादा से ज़्यादा साइज़, मॉडल के टोकन विंडो के हिसाब से तय होता है. AI Studio में, Gemini Flash 2.0 का इस्तेमाल करके, इनपुट फ़ाइल का साइज़ ज़्यादा से ज़्यादा 10 लाख टोकन हो सकता है. यह साइज़, इनपुट के तौर पर इस्तेमाल की जा सकने वाली टेक्स्ट फ़ाइलों के लिए करीब 2 एमबी होता है. अगर आपने बहुत बड़ी फ़ाइल अपलोड की है, तो AI Studio उसे भेजने की अनुमति नहीं देगा.
  • कोड को टेक्स्ट और CSV फ़ाइलों के साथ सबसे अच्छे तरीके से एक्ज़ीक्यूट किया जा सकता है.
  • इनपुट फ़ाइल को इनलाइन डेटा के तौर पर पास किया जा सकता है या Files API का इस्तेमाल करके अपलोड किया जा सकता है. साथ ही, आउटपुट फ़ाइल हमेशा इनलाइन डेटा के तौर पर दिखाई जाती है.

बिलिंग

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

कोड एक्ज़ीक्यूट करने के लिए बिलिंग के बारे में यहां कुछ और बातें बताई गई हैं:

  • मॉडल को दिए गए इनपुट टोकन के लिए, आपसे सिर्फ़ एक बार शुल्क लिया जाता है. साथ ही, मॉडल से मिले फ़ाइनल आउटपुट टोकन के लिए भी आपसे शुल्क लिया जाता है.
  • जनरेट किए गए कोड को दिखाने वाले टोकन, आउटपुट टोकन के तौर पर गिने जाते हैं. जनरेट किए गए कोड में टेक्स्ट और इमेज जैसे मल्टीमॉडल आउटपुट शामिल हो सकते हैं.
  • कोड चलाने के नतीजों को भी आउटपुट टोकन के तौर पर गिना जाता है.

बिलिंग मॉडल को इस डायग्राम में दिखाया गया है:

कोड को चलाने के लिए बिलिंग मॉडल

  • आपसे इनपुट और आउटपुट टोकन के लिए, Gemini मॉडल के मौजूदा रेट के हिसाब से शुल्क लिया जाता है.
  • अगर Gemini, जवाब जनरेट करते समय कोड एक्ज़ीक्यूशन का इस्तेमाल करता है, तो ओरिजनल प्रॉम्प्ट, जनरेट किया गया कोड, और एक्ज़ीक्यूट किए गए कोड के नतीजे को इंटरमीडिएट टोकन के तौर पर लेबल किया जाता है. साथ ही, इनका बिल इनपुट टोकन के तौर पर भेजा जाता है.
  • इसके बाद, Gemini एक खास जानकारी जनरेट करता है. साथ ही, जनरेट किया गया कोड, कोड को लागू करने का नतीजा, और खास जानकारी दिखाता है. इनके लिए, आउटपुट टोकन के तौर पर बिल भेजा जाता है.
  • Gemini API, एपीआई के जवाब में इंटरमीडिएट टोकन की संख्या शामिल करता है, ताकि आपको पता चल सके कि आपको अपने शुरुआती प्रॉम्प्ट के अलावा अतिरिक्त इनपुट टोकन क्यों मिल रहे हैं.

सीमाएं

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

इस्तेमाल किए जा सकने वाले टूल कॉम्बिनेशन

ज़्यादा मुश्किल सवालों के जवाब पाने के लिए, कोड एक्ज़ीक्यूशन टूल को Google Search से मिली जानकारी का इस्तेमाल करके जवाब देने की सुविधा के साथ इस्तेमाल किया जा सकता है.

Gemini 3 मॉडल, पहले से मौजूद टूल (जैसे, कोड एक्ज़ीक्यूशन) को कस्टम टूल (फ़ंक्शन कॉलिंग) के साथ इस्तेमाल करने की सुविधा देते हैं.

इस्तेमाल की जा सकने वाली लाइब्रेरी

कोड को चलाने वाले एनवायरमेंट में ये लाइब्रेरी शामिल हैं:

  • attrs
  • शतरंज
  • contourpy
  • fpdf
  • geopandas
  • imageio
  • jinja2
  • joblib
  • jsonschema
  • jsonschema-specifications
  • lxml
  • matplotlib
  • mpmath
  • numpy
  • opencv-python
  • openpyxl
  • पैकेजिंग
  • पांडा
  • तकिया
  • प्रोटोबफ़
  • pylatex
  • pyparsing
  • PyPDF2
  • python-dateutil
  • python-docx
  • python-pptx
  • reportlab
  • scikit-learn
  • scipy
  • seaborn
  • छह
  • striprtf
  • sympy
  • टेबल में व्यवस्थित करना
  • tensorflow
  • toolz
  • xlrd

अपनी लाइब्रेरी इंस्टॉल नहीं की जा सकतीं.

आगे क्या करना है