টিউটোরিয়াল: Gemini API দিয়ে শুরু করুন


এই টিউটোরিয়ালটি গুগল এআই সুইফট এসডিকে ব্যবহার করে সরাসরি আপনার সুইফট অ্যাপ থেকে সরাসরি জেমিনি এপিআই অ্যাক্সেস করতে পারে তা প্রমাণ করে। আপনি যদি আপনার সুইফট অ্যাপে জেমিনি মডেলগুলি অ্যাক্সেস করার জন্য REST এপিআই বা সার্ভার-সাইড কোড (পাইথন) এর সাথে সরাসরি কাজ করতে না চান তবে আপনি এই এসডিকে ব্যবহার করতে পারেন।

এই টিউটোরিয়ালে, আপনি নিম্নলিখিতগুলি কীভাবে করবেন তা শিখবেন:

এছাড়াও, এই টিউটোরিয়ালটিতে উন্নত ব্যবহারের কেসগুলি (যেমন গণনা টোকেনগুলির মতো) সম্পর্কিত বিভাগ রয়েছে পাশাপাশি সামগ্রী উত্পাদন নিয়ন্ত্রণের জন্য বিকল্প রয়েছে।

পূর্বশর্ত

এই টিউটোরিয়ালটি ধরে নিয়েছে যে আপনি সুইফট অ্যাপ্লিকেশনগুলি বিকাশের জন্য এক্সকোড ব্যবহারের সাথে পরিচিত।

এই টিউটোরিয়ালটি সম্পূর্ণ করতে, নিশ্চিত হয়ে নিন যে আপনার বিকাশের পরিবেশ এবং সুইফট অ্যাপটি নিম্নলিখিত প্রয়োজনীয়তাগুলি পূরণ করেছে:

  • এক্সকোড 15.0 বা উচ্চতর
  • আপনার সুইফট অ্যাপটি অবশ্যই আইওএস 15 বা উচ্চতর, বা ম্যাকোস 12 বা উচ্চতর লক্ষ্যবস্তু করতে হবে।

আপনার প্রকল্প সেট আপ করুন

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

আপনার এপিআই কী সেট আপ করুন

জেমিনি এপিআই ব্যবহার করতে আপনার একটি এপিআই কী দরকার। আপনার যদি ইতিমধ্যে একটি না থাকে তবে গুগল এআই স্টুডিওতে একটি কী তৈরি করুন।

একটি API কী পান

আপনার এপিআই কীটি সুরক্ষিত করুন

এটি দৃ strongly ়ভাবে সুপারিশ করা হয় যে আপনি আপনার সংস্করণ নিয়ন্ত্রণ সিস্টেমে কোনও এপিআই কী পরীক্ষা করবেন না । একটি বিকল্প বিকল্প হ'ল এটি একটি GenerativeAI-Info.plist ফাইলে সঞ্চয় করা এবং তারপরে .plist ফাইল থেকে এপিআই কীটি পড়ুন। আপনার অ্যাপ্লিকেশনটির মূল ফোল্ডারে এই .plist ফাইলটি রাখার বিষয়টি নিশ্চিত করুন এবং এটি সংস্করণ নিয়ন্ত্রণ থেকে বাদ দিন।

আপনার এপিআই কীটি একটি .plist ফাইলে কীভাবে সঞ্চয় করতে হয় তা শিখতে আপনি নমুনা অ্যাপ্লিকেশনটিও পর্যালোচনা করতে পারেন।

এই টিউটোরিয়ালের সমস্ত স্নিপেটগুলি ধরে নিয়েছে যে আপনি এই অন-ডিমান্ড রিসোর্স .plist ফাইল থেকে আপনার এপিআই কীটি অ্যাক্সেস করছেন।

আপনার প্রকল্পে এসডিকে প্যাকেজ যুক্ত করুন

আপনার নিজের সুইফট অ্যাপে জেমিনি এপিআই ব্যবহার করতে, আপনার অ্যাপ্লিকেশনটিতে GoogleGenerativeAI প্যাকেজ যুক্ত করুন:

  1. এক্সকোডে, প্রকল্প নেভিগেটরে আপনার প্রকল্পে ডান ক্লিক করুন।

  2. প্রসঙ্গ মেনু থেকে প্যাকেজ যুক্ত করুন নির্বাচন করুন।

  3. অ্যাড প্যাকেজ ডায়ালগে, অনুসন্ধান বারে প্যাকেজ URL পেস্ট করুন:

    https://github.com/google/generative-ai-swift
    
  4. প্যাকেজ যুক্ত করুন ক্লিক করুন। এক্সকোড এখন আপনার প্রকল্পে GoogleGenerativeAI প্যাকেজ যুক্ত করবে।

জেনারেটর মডেল আরম্ভ করুন

আপনি কোনও এপিআই কল করার আগে আপনাকে জেনারেটর মডেলটি আরম্ভ করতে হবে।

  1. GoogleGenerativeAI মডিউলটি আমদানি করুন:

    import GoogleGenerativeAI
    
  2. জেনারেটর মডেল শুরু করুন:

    // Access your API key from your on-demand resource .plist file
    // (see "Set up your API key" above)
    let model = GenerativeModel(name: "MODEL_NAME", apiKey: APIKey.default)
    

কোনও মডেল নির্দিষ্ট করার সময়, নিম্নলিখিতগুলি নোট করুন:

  • আপনার ব্যবহারের ক্ষেত্রে নির্দিষ্ট এমন একটি মডেল ব্যবহার করুন (উদাহরণস্বরূপ, gemini-pro-vision মাল্টিমোডাল ইনপুটটির জন্য)। এই গাইডের মধ্যে, প্রতিটি বাস্তবায়নের জন্য নির্দেশাবলী প্রতিটি ব্যবহারের ক্ষেত্রে প্রস্তাবিত মডেল তালিকাভুক্ত করে।

সাধারণ ব্যবহারের ক্ষেত্রে প্রয়োগ করুন

এখন আপনার প্রকল্পটি সেট আপ হয়েছে, আপনি বিভিন্ন ব্যবহারের ক্ষেত্রে বাস্তবায়নের জন্য জেমিনি এপিআই ব্যবহার করে অন্বেষণ করতে পারেন:

কেবল পাঠ্য-ইনপুট থেকে পাঠ্য তৈরি করুন

যখন প্রম্পট ইনপুটটিতে কেবল পাঠ্য অন্তর্ভুক্ত থাকে, তখন পাঠ্য আউটপুট উত্পন্ন করতে generateContent পদ্ধতি সহ gemini-pro মডেলটি ব্যবহার করুন:

import GoogleGenerativeAI

// For text-only input, use the gemini-pro model
// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(name: "gemini-pro", apiKey: APIKey.default)

let prompt = "Write a story about a magic backpack."
let response = try await model.generateContent(prompt)
if let text = response.text {
  print(text)
}

পাঠ্য এবং চিত্র ইনপুট থেকে পাঠ্য উত্পন্ন করুন (মাল্টিমোডাল)

জেমিনি একটি মাল্টিমোডাল মডেল ( gemini-pro-vision ) সরবরাহ করে, যাতে আপনি পাঠ্য এবং চিত্র উভয়ই ইনপুট করতে পারেন। প্রম্পটগুলির জন্য চিত্রের প্রয়োজনীয়তাগুলি পর্যালোচনা করার বিষয়টি নিশ্চিত করুন।

যখন প্রম্পট ইনপুটটিতে পাঠ্য এবং চিত্র উভয়ই অন্তর্ভুক্ত থাকে, তখন পাঠ্য আউটপুট উত্পন্ন করতে generateContent পদ্ধতির সাথে gemini-pro-vision মডেলটি ব্যবহার করুন:

import GoogleGenerativeAI

// For text-and-image input (multimodal), use the gemini-pro-vision model
// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(name: "gemini-pro-vision", apiKey: APIKey.default)

let image1 = UIImage(...)
let image2 = UIImage(...)

let prompt = "What's different between these pictures?"

let response = try await model.generateContent(prompt, image1, image2)
if let text = response.text {
  print(text)
}

মাল্টি-টার্ন কথোপকথন (চ্যাট) তৈরি করুন

জেমিনি ব্যবহার করে আপনি একাধিক টার্ন জুড়ে ফ্রিফর্ম কথোপকথন তৈরি করতে পারেন। The SDK simplifies the process by managing the state of the conversation, so unlike with generateContent , you don't have to store the conversation history yourself.

To build a multi-turn conversation (like chat), use the gemini-pro model, and initialize the chat by calling startChat() . Then use sendMessage() to send a new user message, which will also append the message and the response to the chat history.

There are two possible options for role associated with the content in a conversation:

  • user : the role which provides the prompts. This value is the default for sendMessage calls.

  • model : the role which provides the responses. This role can be used when calling startChat() with existing history .

import GoogleGenerativeAI

let config = GenerationConfig(
  maxOutputTokens: 100
)

// For text-only input, use the gemini-pro model
// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(
  name: "gemini-pro",
  apiKey: APIKey.default,
  generationConfig: config
)

let history = [
  ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."),
  ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"),
]

// Initialize the chat
let chat = model.startChat(history: history)
let response = try await chat.sendMessage("How many paws are in my house?")
if let text = response.text {
  print(text)
}

Use streaming for faster interactions

By default, the model returns a response after completing the entire generation process. You can achieve faster interactions by not waiting for the entire result, and instead use streaming to handle partial results.

The following example shows how to implement streaming with the generateContentStream method to generate text from a text-and-image input prompt.

import GoogleGenerativeAI

// For text-and-image input (multimodal), use the gemini-pro-vision model
// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(name: "gemini-pro-vision", apiKey: APIKey.default)

let image1 = UIImage(named: "")!
let image2 = UIImage(named: "")!

let prompt = "What's different between these pictures?"
var fullResponse = ""
let contentStream = model.generateContentStream(prompt, image1, image2)
for try await chunk in contentStream {
  if let text = chunk.text {
    print(text)
    fullResponse += text
  }
}
print(fullResponse)

You can use a similar approach for text-only input and chat use cases.

// Use streaming with text-only input
let contentStream = model.generateContentStream(prompt)
// Use streaming with multi-turn conversations (like chat)
let responseStream = chat.sendMessageStream(message)

Implement advanced use cases

The common use cases described in the previous section of this tutorial help you become comfortable with using the Gemini API. This section describes some use cases that might be considered more advanced.

Function calling

Function calling makes it easier for you to get structured data outputs from generative models. You can then use these outputs to call other APIs and return the relevant response data to the model. In other words, function calling helps you connect generative models to external systems so that the generated content includes the most up-to-date and accurate information. Learn more in the function calling tutorial .

Count tokens

When using long prompts, it might be useful to count tokens before sending any content to the model. The following examples show how to use countTokens() for various use cases:

// For text-only input
let response = try await model.countTokens("Why is the sky blue?")
print(response.totalTokens)
// For text-and-image input (multi-modal)
let response = try await model.countTokens(prompt, image1, image2)
print(response.totalTokens)
// For multi-turn conversations (like chat)
let chat = model.startChat()
let history = chat.history
let message = try ModelContent(role: "user", "Why is the sky blue?")
let contents = history + [message]
let response = try await model.countTokens(contents)
print(response.totalTokens)

Options to control content generation

You can control content generation by configuring model parameters and by using safety settings.

Configure model parameters

Every prompt you send to the model includes parameter values that control how the model generates a response. The model can generate different results for different parameter values. Learn more about Model parameters . The configuration is maintained for the lifetime of your model instance.

let config = GenerationConfig(
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
  maxOutputTokens: 200,
  stopSequences: ["red"]
)

// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(
  name: "MODEL_NAME",
  apiKey: APIKey.default,
  generationConfig: config
)

Use safety settings

You can use safety settings to adjust the likelihood of getting responses that may be considered harmful. By default, safety settings block content with medium and/or high probability of being unsafe content across all dimensions. Learn more about Safety settings .

Here's how to set one safety setting:

// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(
  name: "MODEL_NAME",
  apiKey: APIKey.default,
  safetySettings: [
    SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
  ]
)

You can also set more than one safety setting:

let harassmentSafety = SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
let hateSpeechSafety = SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove)

// Access your API key from your on-demand resource .plist file (see "Set up your API key" above)
let model = GenerativeModel(
  name: "MODEL_NAME",
  apiKey: APIKey.default,
    safetySettings: [harassmentSafety, hateSpeechSafety]
)

এরপর কি

  • Prompt design is the process of creating prompts that elicit the desired response from language models. Writing well structured prompts is an essential part of ensuring accurate, high quality responses from a language model. Learn about best practices for prompt writing .

  • Gemini offers several model variations to meet the needs of different use cases, such as input types and complexity, implementations for chat or other dialog language tasks, and size constraints. Learn about the available Gemini models .

  • Gemini offers options for requesting rate limit increases . The rate limit for Gemini Pro models is 60 requests per minute (RPM).