কোড এক্সিকিউশন

জেমিনি এপিআই একটি কোড এক্সিকিউশন টুল প্রদান করে যা মডেলটিকে পাইথন কোড তৈরি ও রান করতে সক্ষম করে। এরপর মডেলটি কোড এক্সিকিউশনের ফলাফল থেকে পুনরাবৃত্তিমূলকভাবে শিখতে থাকে যতক্ষণ না এটি একটি চূড়ান্ত আউটপুটে পৌঁছায়। আপনি কোড-ভিত্তিক যুক্তির সুবিধা নিতে পারে এমন অ্যাপ্লিকেশন তৈরি করতে কোড এক্সিকিউশন ব্যবহার করতে পারেন। উদাহরণস্বরূপ, আপনি সমীকরণ সমাধান করতে বা টেক্সট প্রসেস করতে কোড এক্সিকিউশন ব্যবহার করতে পারেন। আরও বিশেষায়িত কাজ সম্পাদনের জন্য আপনি কোড এক্সিকিউশন এনভায়রনমেন্টে অন্তর্ভুক্ত লাইব্রেরিগুলোও ব্যবহার করতে পারেন।

জেমিনি শুধুমাত্র পাইথনে কোড চালাতে সক্ষম। আপনি জেমিনিকে অন্য কোনো ভাষায় কোড তৈরি করতে বলতে পারেন, কিন্তু মডেলটি সেটি চালানোর জন্য কোড এক্সিকিউশন টুল ব্যবহার করতে পারে না।

কোড এক্সিকিউশন সক্ষম করুন

কোড এক্সিকিউশন সক্ষম করতে, মডেলে কোড এক্সিকিউশন টুলটি কনফিগার করুন। এটি মডেলকে কোড তৈরি ও রান করার সুযোগ দেয়।

পাইথন

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)

জাভাস্ক্রিপ্ট

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);
    }
}

বিশ্রাম

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 : নির্বাহযোগ্য কোডের ফলাফল

ছবির সাহায্যে কোড নির্বাহ (জেমিনি ৩)

জেমিনি ৩ ফ্ল্যাশ মডেলটি এখন সক্রিয়ভাবে ছবি পরিবর্তন ও পরীক্ষা করার জন্য পাইথন কোড লিখতে এবং চালাতে পারে।

ব্যবহারের ক্ষেত্র

  • জুম এবং পরিদর্শন : মডেলটি স্বয়ংক্রিয়ভাবে শনাক্ত করে যখন কোনো বিবরণ খুব ছোট হয় (যেমন, দূরবর্তী কোনো গেজ পড়া), এবং সেই অংশটি ক্রপ করে উচ্চতর রেজোলিউশনে পুনরায় পরীক্ষা করার জন্য কোড লেখে।
  • দৃশ্যমান গণিত : মডেলটি কোড ব্যবহার করে একাধিক ধাপের গণনা করতে পারে (যেমন, একটি রসিদের প্রতিটি আইটেমের যোগফল বের করা)।
  • চিত্র টীকা : মডেলটি প্রশ্নের উত্তর দেওয়ার জন্য ছবিতে টীকা যোগ করতে পারে, যেমন সম্পর্ক দেখানোর জন্য তীরচিহ্ন আঁকা।

ছবির সাহায্যে কোড এক্সিকিউশন সক্ষম করুন

জেমিনি ৩ ফ্ল্যাশে ছবিসহ কোড এক্সিকিউশন আনুষ্ঠানিকভাবে সমর্থিত। আপনি ‘কোড এক্সিকিউশন অ্যাজ এ টুল’ এবং ‘থিংকিং’ উভয়ই সক্রিয় করার মাধ্যমে এই বৈশিষ্ট্যটি চালু করতে পারেন।

পাইথন

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)

জাভাস্ক্রিপ্ট

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();

বিশ্রাম

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 ব্যবহার করে একাধিক পালাবিশিষ্ট কথোপকথনের অংশ হিসেবেও কোড এক্সিকিউশন ব্যবহার করতে পারেন।

পাইথন

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)

জাভাস্ক্রিপ্ট

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);
    }
}

বিশ্রাম

# 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)

জেমিনি ২.০ ফ্ল্যাশ থেকে শুরু করে, কোড এক্সিকিউশন ফাইল ইনপুট এবং গ্রাফ আউটপুট সমর্থন করে। এই ইনপুট এবং আউটপুট ক্ষমতা ব্যবহার করে, আপনি CSV এবং টেক্সট ফাইল আপলোড করতে পারেন, ফাইলগুলো সম্পর্কে প্রশ্ন করতে পারেন, এবং প্রতিক্রিয়ার অংশ হিসেবে ম্যাটপ্লটলিব গ্রাফ তৈরি করিয়ে নিতে পারেন। আউটপুট ফাইলগুলো প্রতিক্রিয়ার মধ্যে ইনলাইন ইমেজ হিসেবে ফেরত আসে।

ইনপুট/আউটপুট মূল্য নির্ধারণ

কোড এক্সিকিউশন I/O ব্যবহার করার সময়, আপনাকে ইনপুট টোকেন এবং আউটপুট টোকেনের জন্য চার্জ করা হয়:

ইনপুট টোকেন:

  • ব্যবহারকারীর অনুরোধ

আউটপুট টোকেন:

  • মডেল দ্বারা তৈরি কোড
  • কোড পরিবেশে কোড নির্বাহের আউটপুট
  • চিন্তার টোকেন
  • মডেল দ্বারা তৈরি সারাংশ

ইনপুট/আউটপুট বিবরণ

কোড এক্সিকিউশন I/O নিয়ে কাজ করার সময় নিম্নলিখিত প্রযুক্তিগত বিবরণগুলো সম্পর্কে সচেতন থাকুন:

  • কোড এনভায়রনমেন্টের সর্বোচ্চ কার্যকাল ৩০ সেকেন্ড।
  • যদি কোড এনভায়রনমেন্টে কোনো ত্রুটি তৈরি হয়, তাহলে মডেলটি কোডের আউটপুট পুনরায় তৈরি করার সিদ্ধান্ত নিতে পারে। এটি সর্বোচ্চ ৫ বার পর্যন্ত ঘটতে পারে।
  • মডেল টোকেন উইন্ডো দ্বারা সর্বোচ্চ ফাইল ইনপুট সাইজ সীমিত থাকে। AI Studio-তে, Gemini Flash 2.0 ব্যবহার করে, সর্বোচ্চ ইনপুট ফাইলের আকার হলো ১০ লক্ষ টোকেন (সমর্থিত ইনপুট ধরনের টেক্সট ফাইলের জন্য যা প্রায় ২ মেগাবাইট)। আপনি যদি খুব বড় আকারের কোনো ফাইল আপলোড করেন, AI Studio আপনাকে সেটি পাঠাতে দেবে না।
  • টেক্সট এবং CSV ফাইলের ক্ষেত্রে কোড এক্সিকিউশন সবচেয়ে ভালোভাবে কাজ করে।
  • ইনপুট ফাইলটি ইনলাইন ডেটা হিসেবে দেওয়া যেতে পারে অথবা ফাইলস এপিআই (Files API) ব্যবহার করে আপলোড করা যেতে পারে, এবং আউটপুট ফাইলটি সর্বদা ইনলাইন ডেটা হিসেবেই ফেরত দেওয়া হয়।

বিলিং

জেমিনি এপিআই থেকে কোড এক্সিকিউশন চালু করার জন্য কোনো অতিরিক্ত চার্জ নেই। আপনার ব্যবহৃত জেমিনি মডেলের উপর ভিত্তি করে ইনপুট এবং আউটপুট টোকেনের বর্তমান হারে আপনাকে বিল করা হবে।

কোড এক্সিকিউশনের বিলিং সম্পর্কে আরও কিছু বিষয় জেনে রাখা ভালো:

  • মডেলে আপনি যে ইনপুট টোকেনগুলো দেন, তার জন্য আপনাকে শুধুমাত্র একবার বিল করা হয় এবং মডেল থেকে ফেরত আসা চূড়ান্ত আউটপুট টোকেনগুলোর জন্যও বিল করা হয়।
  • জেনারেট করা কোডের প্রতিনিধিত্বকারী টোকেনগুলোকে আউটপুট টোকেন হিসেবে গণনা করা হয়। জেনারেট করা কোডে টেক্সট এবং ছবির মতো মাল্টিমোডাল আউটপুট অন্তর্ভুক্ত থাকতে পারে।
  • কোড নির্বাহের ফলাফলও আউটপুট টোকেন হিসেবে গণনা করা হয়।

বিলিং মডেলটি নিম্নলিখিত ডায়াগ্রামে দেখানো হয়েছে:

কোড এক্সিকিউশন বিলিং মডেল

  • আপনার ব্যবহৃত জেমিনি মডেল অনুযায়ী ইনপুট এবং আউটপুট টোকেনের বর্তমান হারে আপনাকে বিল করা হয়।
  • আপনার প্রতিক্রিয়া তৈরি করার সময় যদি জেমিনি কোড এক্সিকিউশন ব্যবহার করে, তাহলে মূল প্রম্পট, তৈরি হওয়া কোড এবং এক্সিকিউট করা কোডের ফলাফলকে ইন্টারমিডিয়েট টোকেন হিসেবে চিহ্নিত করা হয় এবং এগুলোর বিল ইনপুট টোকেন হিসেবে করা হয়।
  • এরপর জেমিনি একটি সারাংশ তৈরি করে এবং উৎপন্ন কোড, সম্পাদিত কোডের ফলাফল ও চূড়ান্ত সারাংশ ফেরত দেয়। এগুলোকে আউটপুট টোকেন হিসেবে বিল করা হয়।
  • জেমিনি এপিআই তার রেসপন্সে একটি অন্তর্বর্তীকালীন টোকেন সংখ্যা অন্তর্ভুক্ত করে, ফলে আপনি জানতে পারেন কেন আপনার প্রাথমিক প্রম্পটের বাইরেও অতিরিক্ত ইনপুট টোকেন পাচ্ছেন।

সীমাবদ্ধতা

  • মডেলটি শুধুমাত্র কোড তৈরি ও কার্যকর করতে পারে। এটি মিডিয়া ফাইলের মতো অন্য কোনো আর্টিফ্যাক্ট ফেরত দিতে পারে না।
  • কিছু ক্ষেত্রে, কোড এক্সিকিউশন চালু করলে মডেল আউটপুটের অন্যান্য অংশে (যেমন, স্টোরি লেখা) রিগ্রেশন দেখা দিতে পারে।
  • সফলভাবে কোড এক্সিকিউশন ব্যবহার করার ক্ষেত্রে বিভিন্ন মডেলের সক্ষমতায় কিছু ভিন্নতা রয়েছে।

সমর্থিত সরঞ্জামগুলির সংমিশ্রণ

আরও জটিল ব্যবহারের ক্ষেত্রগুলিকে শক্তিশালী করার জন্য কোড এক্সিকিউশন টুলকে গ্রাউন্ডিং উইথ গুগল সার্চ-এর সাথে একত্রিত করা যেতে পারে।

জেমিনি ৩ মডেলগুলো বিল্ট-ইন টুল (যেমন কোড এক্সিকিউশন) এবং কাস্টম টুল (ফাংশন কলিং) একত্রিত করার সুবিধা দেয়।

সমর্থিত গ্রন্থাগার

কোড এক্সিকিউশন এনভায়রনমেন্টে নিম্নলিখিত লাইব্রেরিগুলো অন্তর্ভুক্ত রয়েছে:

  • অ্যাট্রস
  • দাবা
  • কনট্যুরপি
  • fpdf
  • জিওপান্ডাস
  • ইমেজআইও
  • জিনজা২
  • জবলিব
  • jsonschema
  • jsonschema-স্পেসিফিকেশন
  • lxml
  • ম্যাটপ্লটলিব
  • এমপিম্যাথ
  • নাম্পাই
  • ওপেনসিভি-পাইথন
  • ওপেনপাইক্সল
  • প্যাকেজিং
  • পান্ডা
  • বালিশ
  • প্রোটোবাফ
  • পাইলাটেক্স
  • পাইপার্সিং
  • পাইপিডিএফ২
  • পাইথন-ডেটইউটিল
  • পাইথন-ডক্স
  • পাইথন-পিপিটিএক্স
  • রিপোর্টল্যাব
  • স্কিকিট-লার্ন
  • সাইপি
  • সমুদ্রজাত
  • ছয়
  • স্ট্রিপআরটিএফ
  • সিম্পি
  • সারণিবদ্ধ করুন
  • টেনসরফ্লো
  • টুলজ
  • এক্সএলআরডি

আপনি আপনার নিজের লাইব্রেরি ইনস্টল করতে পারবেন না।

এরপর কী?