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


কোড এক্সিকিউশন হল এমন একটি টুল যা মডেলটিকে পাইথন কোড তৈরি এবং চালাতে সক্ষম করে। মডেলটি একটি চূড়ান্ত আউটপুটে না আসা পর্যন্ত কোড এক্সিকিউশন ফলাফল থেকে পুনরাবৃত্তিমূলকভাবে শিখতে পারে।

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

আপনি মডেলকে প্রদান করেন এমন সমস্ত সরঞ্জামের মতো, মডেলটি সিদ্ধান্ত নেয় কখন কোড এক্সিকিউশন ব্যবহার করতে হবে।

কোড বাস্তবায়নে ঝাঁপ দাও

কোড এক্সিকিউশন বনাম ফাংশন কলিং এর তুলনা

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

এখানে কোড এক্সিকিউশন এবং ফাংশন কলিংয়ের মধ্যে কিছু অতিরিক্ত পার্থক্য রয়েছে:

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

সমর্থিত মডেল

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemini-2.0-flash-001 (এবং এর স্বয়ংক্রিয়-আপডেট উপনাম gemini-2.0-flash )
  • gemini-2.0-flash-live-preview-04-09

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

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

আপনি শুরু করার আগে

এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট সামগ্রী এবং কোড দেখতে আপনার Gemini API প্রদানকারীতে ক্লিক করুন।

যদি আপনি ইতিমধ্যে না করে থাকেন, শুরু করার নির্দেশিকাটি সম্পূর্ণ করুন, যা বর্ণনা করে যে কীভাবে আপনার Firebase প্রকল্প সেট আপ করবেন, আপনার অ্যাপকে Firebase-এ সংযুক্ত করবেন, SDK যোগ করবেন, আপনার নির্বাচিত Gemini API প্রদানকারীর জন্য ব্যাকএন্ড পরিষেবা শুরু করবেন এবং একটি GenerativeModel উদাহরণ তৈরি করবেন।

আপনার প্রম্পট পরীক্ষা এবং পুনরাবৃত্তি করার জন্য, আমরা Google AI Studio ব্যবহার করার পরামর্শ দিই।

কোড এক্সিকিউশন সক্রিয় করুন

এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন।
সেই বিভাগে, আপনি আপনার নির্বাচিত Gemini API প্রদানকারীর জন্য একটি বোতামে ক্লিক করবেন যাতে আপনি এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট সামগ্রী দেখতে পান

আপনি যখন GenerativeModel উদাহরণ তৈরি করেন, তখন CodeExecution একটি টুল হিসাবে প্রদান করুন যা মডেলটি তার প্রতিক্রিয়া তৈরি করতে ব্যবহার করতে পারে। এটি মডেলটিকে পাইথন কোড তৈরি এবং চালানোর অনুমতি দেয়।

সুইফট


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  // Provide code execution as a tool that the model can use to generate its response.
  tools: [.codeExecution()]
)

let prompt = """
What is the sum of the first 50 prime numbers?
Generate and run code for the calculation, and make sure you get all 50.
"""

let response = try await model.generateContent(prompt)

guard let candidate = response.candidates.first else {
  print("No candidates in response.")
  return
}
for part in candidate.content.parts {
  if let textPart = part as? TextPart {
    print("Text = \(textPart.text)")
  } else if let executableCode = part as? ExecutableCodePart {
    print("Code = \(executableCode.code), Language = \(executableCode.language)")
  } else if let executionResult = part as? CodeExecutionResultPart {
    print("Outcome = \(executionResult.outcome), Result = \(executionResult.output ?? "no output")")
  }
}

Kotlin


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    // Provide code execution as a tool that the model can use to generate its response.
    tools = listOf(Tool.codeExecution())
)

val prompt =  "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50."

val response = model.generateContent(prompt)

response.candidates.first().content.parts.forEach {
    if(it is TextPart) {
        println("Text = ${it.text}")
    }
    if(it is ExecutableCodePart) {
        println("Code = ${it.code}, Language = ${it.language}")
    }
    if(it is CodeExecutionResultPart) {
       println("Outcome = ${it.outcome}, Result = ${it.output}")
    }
}

Java


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel("GEMINI_MODEL_NAME",
                        null,
                        null,
                        // Provide code execution as a tool that the model can use to generate its response.
                        List.of(Tool.codeExecution()));

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

String text = "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50.";

Content prompt = new Content.Builder()
    .addText(text)
    .build();

ListenableFuture response = model.generateContent(prompt);

Futures.addCallback(response, new FutureCallback() {
   @Override
public void onSuccess(GenerateContentResponse response)   {
    // Access the first candidate's content parts
    List parts = response.getCandidates().get(0).getContent().getParts();
    for (Part part : parts) {
        if (part instanceof TextPart) {
            TextPart textPart = (TextPart) part;
            System.out.println("Text = " + textPart.getText());
        } else if (part instanceof ExecutableCodePart) {
            ExecutableCodePart codePart = (ExecutableCodePart) part;
            System.out.println("Code = " + codePart.getCode() + ", Language = " + codePart.getLanguage());
        } else if (part instanceof CodeExecutionResultPart) {
            CodeExecutionResultPart resultPart = (CodeExecutionResultPart) part;
            System.out.println("Outcome = " + resultPart.getOutcome() + ", Result = " + resultPart.getOutput());
        }
    }
}

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(
  ai,
  {
    model: "GEMINI_MODEL_NAME",
    // Provide code execution as a tool that the model can use to generate its response.
    tools: [{ codeExecution: {} }]
  }
);

const prompt =  "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50."

const result = await model.generateContent(prompt);
const response = await result.response;

const parts = response.candidates?.[0].content.parts;

if (parts) {
  parts.forEach((part) => {
    if (part.text) {
        console.log(`Text: ${part.text}`);
    } else if (part.executableCode) {
      console.log(
        `Code: ${part.executableCode.code}, Language: ${part.executableCode.language}`
      );
    } else if (part.codeExecutionResult) {
      console.log(
        `Outcome: ${part.codeExecutionResult.outcome}, Result: ${part.codeExecutionResult.output}`
      );
    }
  });
}

Dart

ফ্লটারের জন্য সমর্থন এর পরবর্তী রিলিজে আসছে।

ঐক্য

ঐক্যের জন্য সমর্থন তার পরবর্তী প্রকাশে আসছে।

একটি মডেল নির্বাচন কিভাবে শিখুনআপনার ব্যবহারের ক্ষেত্রে এবং অ্যাপের জন্য উপযুক্ত।

চ্যাটে কোড এক্সিকিউশন ব্যবহার করুন

আপনি একটি চ্যাটের অংশ হিসাবে কোড এক্সিকিউশন ব্যবহার করতে পারেন:

সুইফট


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  // Provide code execution as a tool that the model can use to generate its response.
  tools: [.codeExecution()]
)

let prompt = """
What is the sum of the first 50 prime numbers?
Generate and run code for the calculation, and make sure you get all 50.
"""
let chat = model.startChat()

let response = try await chat.sendMessage(prompt)

guard let candidate = response.candidates.first else {
  print("No candidates in response.")
  return
}
for part in candidate.content.parts {
  if let textPart = part as? TextPart {
    print("Text = \(textPart.text)")
  } else if let executableCode = part as? ExecutableCodePart {
    print("Code = \(executableCode.code), Language = \(executableCode.language)")
  } else if let executionResult = part as? CodeExecutionResultPart {
    print("Outcome = \(executionResult.outcome), Result = \(executionResult.output ?? "no output")")
  }
}

Kotlin


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    // Provide code execution as a tool that the model can use to generate its response.
    tools = listOf(Tool.codeExecution())
)

val prompt =  "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50."
val chat = model.startChat()
val response = chat.sendMessage(prompt)

response.candidates.first().content.parts.forEach {
    if(it is TextPart) {
        println("Text = ${it.text}")
    }
    if(it is ExecutableCodePart) {
        println("Code = ${it.code}, Language = ${it.language}")
    }
    if(it is CodeExecutionResultPart) {
       println("Outcome = ${it.outcome}, Result = ${it.output}")
    }
}

Java


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel("GEMINI_MODEL_NAME",
                        null,
                        null,
                        // Provide code execution as a tool that the model can use to generate its response.
                        List.of(Tool.codeExecution()));

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
String text = "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50.";

Content prompt = new Content.Builder()
    .addText(text)
    .build();

ChatFutures chat = model.startChat();
ListenableFuture response = chat.sendMessage(prompt);

Futures.addCallback(response, new FutureCallback() {
   @Override
public void onSuccess(GenerateContentResponse response)   {
    // Access the first candidate's content parts
    List parts = response.getCandidates().get(0).getContent().getParts();
    for (Part part : parts) {
        if (part instanceof TextPart) {
            TextPart textPart = (TextPart) part;
            System.out.println("Text = " + textPart.getText());
        } else if (part instanceof ExecutableCodePart) {
            ExecutableCodePart codePart = (ExecutableCodePart) part;
            System.out.println("Code = " + codePart.getCode() + ", Language = " + codePart.getLanguage());
        } else if (part instanceof CodeExecutionResultPart) {
            CodeExecutionResultPart resultPart = (CodeExecutionResultPart) part;
            System.out.println("Outcome = " + resultPart.getOutcome() + ", Result = " + resultPart.getOutput());
        }
    }
}

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(
  ai,
  {
    model: "GEMINI_MODEL_NAME",
    // Provide code execution as a tool that the model can use to generate its response.
    tools: [{ codeExecution: {} }]
  }
);

const prompt =  "What is the sum of the first 50 prime numbers? " +
        "Generate and run code for the calculation, and make sure you get all 50."
const chat = model.startChat()
const result = await chat.sendMessage(prompt);
const response = await result.response;

const parts = response.candidates?.[0].content.parts;

if (parts) {
  parts.forEach((part) => {
    if (part.text) {
        console.log(`Text: ${part.text}`);
    } else if (part.executableCode) {
      console.log(
        `Code: ${part.executableCode.code}, Language: ${part.executableCode.language}`
      );
    } else if (part.codeExecutionResult) {
      console.log(
        `Outcome: ${part.codeExecutionResult.outcome}, Result: ${part.codeExecutionResult.output}`
      );
    }
  });
}

Dart

ফ্লটারের জন্য সমর্থন এর পরবর্তী রিলিজে আসছে।

ঐক্য

ঐক্যের জন্য সমর্থন তার পরবর্তী প্রকাশে আসছে।

একটি মডেল নির্বাচন কিভাবে শিখুনআপনার ব্যবহারের ক্ষেত্রে এবং অ্যাপের জন্য উপযুক্ত।

মূল্য নির্ধারণ

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

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

একটি মডেল কোড এক্সিকিউশন ব্যবহার করলে কিভাবে টোকেন বিল করা হয় তা দেখানো ডায়াগ্রাম।

একটি মডেল যখন কোড এক্সিকিউশন ব্যবহার করে তখন কীভাবে টোকেন বিল করা হয় তার একটি সারসংক্ষেপ এখানে দেওয়া হল:

  • মূল প্রম্পট একবার বিল করা হয়. এর টোকেনগুলিকে মধ্যবর্তী টোকেন হিসাবে লেবেল করা হয়, যা ইনপুট টোকেন হিসাবে বিল করা হয়।

  • জেনারেট করা কোড এবং এক্সিকিউটেড কোডের ফলাফল এভাবে বিল করা হয়:

    • যখন সেগুলি কোড এক্সিকিউশনের সময় ব্যবহার করা হয় - সেগুলিকে অন্তর্বর্তী টোকেন হিসাবে লেবেল করা হয় যা ইনপুট টোকেন হিসাবে বিল করা হয়৷

    • যখন সেগুলি চূড়ান্ত প্রতিক্রিয়ার অংশ হিসাবে অন্তর্ভুক্ত করা হয় - তখন সেগুলিকে আউটপুট টোকেন হিসাবে বিল করা হয়৷

  • চূড়ান্ত প্রতিক্রিয়ার চূড়ান্ত সারাংশ আউটপুট টোকেন হিসাবে বিল করা হয়।

Gemini API এ API প্রতিক্রিয়াতে একটি মধ্যবর্তী টোকেন গণনা অন্তর্ভুক্ত করে, তাই আপনি জানেন কেন আপনার প্রাথমিক প্রম্পটের বাইরে ইনপুট টোকেনের জন্য আপনাকে চার্জ করা হচ্ছে।

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

সীমাবদ্ধতা এবং সর্বোত্তম অনুশীলন

  • মডেলটি শুধুমাত্র পাইথন কোড তৈরি এবং কার্যকর করতে পারে। এটি মিডিয়া ফাইলের মত অন্যান্য শিল্পকর্ম ফেরত দিতে পারে না।

  • টাইম আউট হওয়ার আগে কোড এক্সিকিউশন সর্বোচ্চ 30 সেকেন্ডের জন্য চলতে পারে।

  • কিছু ক্ষেত্রে, কোড এক্সিকিউশন সক্ষম করার ফলে মডেল আউটপুটের অন্যান্য ক্ষেত্রে রিগ্রেশন হতে পারে (উদাহরণস্বরূপ, একটি গল্প লেখা)।

  • কোড এক্সিকিউশন টুল ইনপুট/আউটপুট হিসাবে ফাইল URI-কে সমর্থন করে না। যাইহোক, কোড এক্সিকিউশন টুল ইনলাইনড বাইট হিসাবে ফাইল ইনপুট এবং গ্রাফ আউটপুট সমর্থন করে। এই ইনপুট এবং আউটপুট ক্ষমতাগুলি ব্যবহার করে, আপনি CSV এবং পাঠ্য ফাইলগুলি আপলোড করতে পারেন, ফাইলগুলি সম্পর্কে প্রশ্ন জিজ্ঞাসা করতে পারেন এবং কোড নির্বাহের ফলাফলের অংশ হিসাবে ম্যাটপ্লটলিব গ্রাফ তৈরি করতে পারেন৷ ইনলাইনড বাইটের জন্য সমর্থিত মাইম প্রকারগুলি হল .cpp , .csv , .java , .jpeg , .js , .png , .py , .ts , এবং .xml

সমর্থিত লাইব্রেরি

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


Firebase AI লজিকের সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন