Firebase-এর জন্য Vertex AI SDK ব্যবহার করে Gemini API দিয়ে শুরু করুন


Firebase-এর জন্য Vertex AI SDK ব্যবহার করে কীভাবে সরাসরি আপনার অ্যাপ থেকে Gemini API-তে কল করা শুরু করবেন তা এই নির্দেশিকা আপনাকে দেখায়।

পূর্বশর্ত

এই গাইডটি অনুমান করে যে আপনি ওয়েব অ্যাপস ডেভেলপ করার জন্য জাভাস্ক্রিপ্ট ব্যবহার করার সাথে পরিচিত। এই গাইড ফ্রেমওয়ার্ক-স্বাধীন।

  • নিশ্চিত করুন যে আপনার ডেভেলপমেন্ট এনভায়রনমেন্ট এবং ওয়েব অ্যাপ নিম্নলিখিত প্রয়োজনীয়তাগুলি পূরণ করে:

    • (ঐচ্ছিক) Node.js
    • আধুনিক ওয়েব ব্রাউজার
  • (ঐচ্ছিক) নমুনা অ্যাপটি দেখুন।

    নমুনা অ্যাপ্লিকেশন ডাউনলোড করুন

    আপনি দ্রুত SDK ব্যবহার করে দেখতে পারেন, বিভিন্ন ব্যবহারের ক্ষেত্রে সম্পূর্ণ বাস্তবায়ন দেখতে পারেন, অথবা আপনার নিজস্ব ওয়েব অ্যাপ না থাকলে নমুনা অ্যাপটি ব্যবহার করতে পারেন। নমুনা অ্যাপটি ব্যবহার করতে, আপনাকে এটি একটি Firebase প্রকল্পের সাথে সংযুক্ত করতে হবে।

ধাপ 1 : একটি Firebase প্রকল্প সেট আপ করুন এবং আপনার অ্যাপটিকে Firebase-এর সাথে সংযুক্ত করুন

আপনার যদি ইতিমধ্যেই একটি Firebase প্রকল্প এবং Firebase এর সাথে সংযুক্ত একটি অ্যাপ থাকে

  1. Firebase কনসোলে, Bild with Gemini পৃষ্ঠাতে যান এবং তারপরে একটি ওয়ার্কফ্লো চালু করতে দ্বিতীয় কার্ডে ক্লিক করুন যা আপনাকে নিম্নলিখিত কাজগুলি করতে সাহায্য করে। আপনি যদি একটি কার্ড লেআউট দেখতে না পান তবে এই কাজগুলি সম্পূর্ণ হয়েছে৷

  2. আপনার অ্যাপে SDK যোগ করতে এই গাইডের পরবর্তী ধাপে যান।

যদি আপনার কাছে ইতিমধ্যে একটি Firebase প্রকল্প এবং Firebase এর সাথে সংযুক্ত একটি অ্যাপ না থাকে

ধাপ 2 : SDK যোগ করুন

আপনার Firebase প্রকল্প সেট আপ এবং আপনার অ্যাপ Firebase-এর সাথে সংযুক্ত (আগের ধাপ দেখুন), আপনি এখন আপনার অ্যাপে Firebase-এর জন্য Vertex AI SDK যোগ করতে পারেন।

ওয়েবের জন্য Vertex AI SDK Gemini API-এ অ্যাক্সেস প্রদান করে।

  1. npm ব্যবহার করে SDK-এর ব্যক্তিগত পূর্বরূপ ইনস্টল করুন:

    npm install firebase@vertexai-preview
    
  2. আপনার অ্যাপে Firebase শুরু করুন:

    import { initializeApp } from "firebase/app";
    
    // 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);
    

ধাপ 3 : ভার্টেক্স এআই পরিষেবা এবং জেনারেটিভ মডেল শুরু করুন

আপনি যেকোনো API কল করার আগে, আপনাকে Vertex AI পরিষেবা এবং জেনারেটিভ মডেল শুরু করতে হবে।

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// 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 Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 Pro is versatile and can accept both text-only or multimodal prompt inputs
const model = getGenerativeModel(vertexAI, { model: "gemini-1.5-pro-preview-0409" });

আপনি যখন শুরু করার নির্দেশিকাটি শেষ করেছেন, তখন আপনার ব্যবহারের ক্ষেত্রে উপযুক্ত একটি মিথুন মডেল কীভাবে চয়ন করবেন তা শিখুন।

ধাপ 4 : Gemini API কল করুন

এখন যেহেতু আপনি আপনার অ্যাপটিকে Firebase-এ সংযুক্ত করেছেন, SDK যোগ করেছেন এবং Vertex AI পরিষেবা এবং জেনারেটিভ মডেল শুরু করেছেন, আপনি Gemini API কল করতে প্রস্তুত৷

আপনি প্রতিক্রিয়াটি স্ট্রিম করতে চান কিনা তা চয়ন করুন ( generateContentStream ) বা সম্পূর্ণ ফলাফল তৈরি না হওয়া পর্যন্ত প্রতিক্রিয়াটির জন্য অপেক্ষা করুন ( generateContent )৷

স্ট্রিমিং

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

আপনি generateContentStream() ব্যবহার করতে পারেন একটি প্রম্পট অনুরোধ থেকে জেনারেট করা টেক্সট স্ট্রিম করতে যাতে শুধুমাত্র টেক্সট থাকে:

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// 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 Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 Pro is versatile and can accept both text-only and multimodal prompt inputs
const model = getGenerativeModel(vertexAI, { model: "gemini-1.5-pro-preview-0409" });

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  const prompt = "Write a story about a magic backpack."

  // To stream generated text output, call generateContentStream with the text input
  const result = await model.generateContentStream(prompt);

  for await (const chunk of result.stream) {
    const chunkText = chunk.text();
    console.log(chunkText);
  }

  console.log('aggregated response: ', await result.response);
}

run();

স্ট্রিমিং ছাড়াই

বিকল্পভাবে, আপনি স্ট্রিমিংয়ের পরিবর্তে সম্পূর্ণ ফলাফলের জন্য অপেক্ষা করতে পারেন; মডেলটি পুরো প্রজন্মের প্রক্রিয়াটি সম্পূর্ণ করার পরেই ফলাফলটি ফিরে আসে।

আপনি একটি প্রম্পট অনুরোধ থেকে পাঠ্য তৈরি করতে generateContent() ব্যবহার করতে পারেন যাতে শুধুমাত্র পাঠ্য অন্তর্ভুক্ত থাকে:

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// 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 Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 Pro is versatile and can accept both text-only and multimodal prompt inputs
const model = getGenerativeModel(vertexAI, { model: "gemini-1.5-pro-preview-0409" });

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  const prompt = "Write a story about a magic backpack."

  // To generate text output, call generateContent with the text input
  const result = await model.generateContent(prompt);

  const response = result.response;
  const text = response.text();
  console.log(text);
}

run();

তুমি আর কি করতে পারো?

মিথুন মডেল সম্পর্কে আরও জানুন

বিভিন্ন ব্যবহারের ক্ষেত্রে উপলব্ধ মডেল এবং তাদের কোটা এবং মূল্য সম্পর্কে জানুন।

Gemini API-এর অন্যান্য ক্ষমতা ব্যবহার করে দেখুন

কন্টেন্ট জেনারেশন কিভাবে নিয়ন্ত্রণ করতে হয় তা জানুন

আপনি Vertex AI Studio ব্যবহার করে প্রম্পট এবং মডেল কনফিগারেশন নিয়ে পরীক্ষা করতে পারেন।


Firebase-এর জন্য Vertex AI SDK-এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন