জেমিনি API কুইকস্টার্ট

এই কুইকস্টার্টটি আপনাকে দেখাবে কীভাবে আমাদের লাইব্রেরিগুলো ইনস্টল করতে হয় এবং আপনার প্রথম অনুরোধটি করতে, প্রতিক্রিয়াগুলো স্ট্রিম করতে, একাধিক ধাপের কথোপকথন তৈরি করতে ও টুলগুলো ব্যবহার করতে হয়।

জেমিনি এপিআই-তে অনুরোধ পাঠানোর দুটি উপায় রয়েছে:

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

কুইকস্টার্টের এই সংস্করণটি জেমিনি এপিআই-তে অনুরোধ পাঠাতে ইন্টারঅ্যাকশনস এপিআই ব্যবহার করে।

শুরু করার আগে

জেমিনি এপিআই ব্যবহার করার জন্য আপনার একটি এপিআই কী প্রয়োজন, যা আপনার অনুরোধগুলোর সত্যতা যাচাই করতে, নিরাপত্তা সীমা প্রয়োগ করতে এবং আপনার অ্যাকাউন্টের ব্যবহার ট্র্যাক করতে ব্যবহৃত হয়।

শুরু করার জন্য এআই স্টুডিওতে বিনামূল্যে একটি তৈরি করুন:

একটি জেমিনি এপিআই কী তৈরি করুন

Google GenAI SDK ইনস্টল করুন

পাইথন

Python 3.9+ ব্যবহার করে, নিম্নলিখিত pip কমান্ডটির মাধ্যমে google-genai প্যাকেজটি ইনস্টল করুন:

pip install -q -U google-genai

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

Node.js v18+ ব্যবহার করে, নিম্নলিখিত npm কমান্ডটির মাধ্যমে TypeScript এবং JavaScript-এর জন্য Google Gen AI SDK ইনস্টল করুন:

npm install @google/genai

টেক্সট তৈরি করুন

টেক্সট প্রতিক্রিয়া তৈরি করতে interactions.create মেথডটি ব্যবহার করুন।

পাইথন

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Explain how AI works in a few words"
)

print(interaction.output_text)

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

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.5-flash",
    input: "Explain how AI works in a few words",
  });

  console.log(interaction.output_text);
}

main();

বিশ্রাম

curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Explain how AI works in a few words"
  }'

স্ট্রিম প্রতিক্রিয়া

ডিফল্টরূপে, সম্পূর্ণ জেনারেশন প্রক্রিয়াটি শেষ হওয়ার পরেই মডেলটি একটি প্রতিক্রিয়া ফেরত দেয়। আরও দ্রুত এবং ইন্টারেক্টিভ অভিজ্ঞতার জন্য, আপনি প্রতিক্রিয়া খণ্ডগুলি তৈরি হওয়ার সাথে সাথেই স্ট্রিম করতে পারেন।

পাইথন

stream = client.interactions.create(
    model="gemini-3.5-flash",
    input="Explain how AI works in detail",
    stream=True
)

for event in stream:
    if event.event_type == "step.delta":
        if event.delta.type == "text":
            print(event.delta.text, end="", flush=True)

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

async function main() {
  const stream = await ai.interactions.create({
    model: "gemini-3.5-flash",
    input: "Explain how AI works in detail",
    stream: true,
  });

  for await (const event of stream) {
    if (event.event_type === "step.delta") {
      if (event.delta.type === "text") {
        process.stdout.write(event.delta.text);
      }
    }
  }
}

main();

বিশ্রাম

# Use alt=sse for streaming
curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Api-Revision: 2026-05-20" \
  --no-buffer \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Explain how AI works in detail",
    "stream": true
  }'

একাধিক পালা কথোপকথন

জেমিনি এপিআই-তে একাধিক পালাক্রমে কথোপকথন তৈরির জন্য অন্তর্নির্মিত সমর্থন রয়েছে। পূর্ববর্তী ইন্টারঅ্যাকশন থেকে প্রাপ্ত id কেবল previous_interaction_id প্যারামিটার হিসাবে পাস করুন, এবং সার্ভার স্বয়ংক্রিয়ভাবে কথোপকথনের ইতিহাস পরিচালনা করবে।

পাইথন


interaction1 = client.interactions.create(
    model="gemini-3.5-flash",
    input="I have 2 dogs in my house."
)
print("Response 1:", interaction1.output_text)

interaction2 = client.interactions.create(
    model="gemini-3.5-flash",
    input="How many paws are in my house?",
    previous_interaction_id=interaction1.id
)
print("Response 2:", interaction2.output_text)

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

async function main() {
  const interaction1 = await ai.interactions.create({
    model: "gemini-3-flash-preview",
    input: "I have 2 dogs in my house.",
  });
  console.log("Response 1:", interaction1.output_text);

  const interaction2 = await ai.interactions.create({
    model: "gemini-3-flash-preview",
    input: "How many paws are in my house?",
    previous_interaction_id: interaction1.id,
  });
  console.log("Response 2:", interaction2.output_text);
}

main();

বিশ্রাম

# Turn 1: Start the conversation
RESPONSE1=$(curl -s -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Api-Revision: 2026-05-20" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "I have 2 dogs in my house."
  }')

# Extract the interaction ID
INTERACTION_ID=$(echo "$RESPONSE1" | jq -r '.id')

# Turn 2: Continue the conversation
curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Api-Revision: 2026-05-20" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d "{
    \"model\": \"gemini-3-flash-preview\",
    \"input\": \"How many paws are in my house?\",
    \"previous_interaction_id\": \"$INTERACTION_ID\"
  }"

সরঞ্জাম ব্যবহার করুন

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

নিম্নলিখিত উদাহরণটি দেখায় কিভাবে গুগল সার্চ সক্রিয় করতে হয়:

পাইথন

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Who won the euro 2024?",
    tools=[{"type": "google_search"}]
)

print(interaction.output_text)

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text" and content_block.annotations:
                print("\nCitations:")
                for annotation in content_block.annotations:
                    if annotation.type == "url_citation":
                        print(f"  - [{annotation.title}]({annotation.url})")

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

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Who won the euro 2024?",
    tools: [{ type: "google_search" }]
  });

  console.log(interaction.output_text);

  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text' && contentBlock.annotations) {
          console.log("\nCitations:");
          for (const annotation of contentBlock.annotations) {
            if (annotation.type === 'url_citation') {
              console.log(`  - [${annotation.title}](${annotation.url})`);
            }
          }
        }
      }
    }
  }
}

main();

বিশ্রাম

curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Api-Revision: 2026-05-20" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Who won the euro 2024?",
    "tools": [{"type": "google_search"}]
  }'

জেমিনি এপিআই অন্যান্য অন্তর্নির্মিত সরঞ্জামগুলিও সমর্থন করে:

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

কাস্টম ফাংশন কল করুন

আপনার কাস্টম টুল এবং এপিআই-এর সাথে মডেল সংযোগ করতে ফাংশন কলিং ব্যবহার করুন। মডেলটি নির্ধারণ করে কখন আপনার ফাংশনটি কল করতে হবে এবং আপনার অ্যাপ্লিকেশনটি কার্যকর করার জন্য আর্গুমেন্টসহ একটি function_call স্টেপ রিটার্ন করে।

এই উদাহরণটি একটি নকল তাপমাত্রা ফাংশন ঘোষণা করে এবং মডেলটি এটিকে কল করতে চায় কিনা তা পরীক্ষা করে।

পাইথন

import json

weather_function = {
    "type": "function",
    "name": "get_current_temperature",
    "description": "Gets the current temperature for a given location.",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city name, e.g. San Francisco",
            },
        },
        "required": ["location"],
    },
}

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="What's the temperature in London?",
    tools=[weather_function],
)

fc_step = None
for step in interaction.steps:
    if step.type == "function_call":
        fc_step = step
        break

if fc_step:
    print(f"Model requested function: {fc_step.name} with args {fc_step.arguments}")

    mock_result = {"temperature": "15C", "condition": "Cloudy"}

    final_interaction = client.interactions.create(
        model="gemini-3-flash-preview",
        input=[
            {
                "type": "function_result",
                "name": fc_step.name,
                "call_id": fc_step.id,
                "result": [{"type": "text", "text": json.dumps(mock_result)}],
            }
        ],
        tools=[weather_function],
        previous_interaction_id=interaction.id,
    )
    print("Final Response:", final_interaction.output_text)

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

async function main() {
  const weatherFunction = {
    type: 'function',
    name: 'get_current_temperature',
    description: 'Gets the current temperature for a given location.',
    parameters: {
      type: 'object',
      properties: {
        location: {
          type: 'string',
          description: 'The city name, e.g. San Francisco',
        },
      },
      required: ['location'],
    },
  };

  const interaction = await ai.interactions.create({
    model: 'gemini-3-flash-preview',
    input: "What's the temperature in London?",
    tools: [weatherFunction],
  });

  const fcStep = interaction.steps.find(s => s.type === 'function_call');
  if (fcStep) {
    console.log(`Model requested function: ${fcStep.name}`);

    const mockResult = { temperature: "15C", condition: "Cloudy" };

    const finalInteraction = await ai.interactions.create({
      model: 'gemini-3-flash-preview',
      input: [{
        type: 'function_result',
        name: fcStep.name,
        call_id: fcStep.id,
        result: [{ type: 'text', text: JSON.stringify(mockResult) }]
      }],
      tools: [weatherFunction],
      previous_interaction_id: interaction.id,
    });

    console.log("Final Response:", finalInteraction.output_text);
  }
}

main();

বিশ্রাম

curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Api-Revision: 2026-05-20" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "What'\''s the temperature in London?",
    "tools": [{
      "type": "function",
      "name": "get_current_temperature",
      "description": "Gets the current temperature for a given location.",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {"type": "string", "description": "The city name"}
        },
        "required": ["location"]
      }
    }]
  }'

এরপর কী?

এখন যেহেতু আপনি জেমিনি এপিআই (Gemini API) দিয়ে কাজ শুরু করেছেন, আরও উন্নত অ্যাপ্লিকেশন তৈরি করতে নিম্নলিখিত গাইডগুলো দেখুন: