আমরা যখন জেমিনি 2.0 মডেল পরিবার প্রবর্তন করি, তখন আমরা Gemini API-এর সাথে কাজ করার জন্য Google Gen AI SDK-এর একটি নতুন সেটও প্রকাশ করেছি:
এই আপডেট হওয়া SDKগুলি লাইভ API এবং Veo-এর মতো সাম্প্রতিক সংযোজন সহ সমস্ত Gemini API মডেল এবং বৈশিষ্ট্যগুলির সাথে সম্পূর্ণরূপে সামঞ্জস্যপূর্ণ হবে৷
আমরা সুপারিশ করছি যে আপনি আপনার প্রকল্পগুলিকে পুরানো জেমিনি SDK থেকে নতুন Gen AI SDKগুলিতে স্থানান্তরিত করা শুরু করুন৷ এই নির্দেশিকা আপনাকে শুরু করতে সাহায্য করার জন্য স্থানান্তরিত কোডের আগে-পরে উদাহরণ প্রদান করে। আপনাকে নতুন SDK-এর সাহায্যে উঠতে এবং চালানোর জন্য আমরা এখানে উদাহরণ যোগ করতে থাকব।
SDK ইনস্টল করুন
আগে
পাইথন
pip install -U -q "google-generativeai"
জাভাস্ক্রিপ্ট
npm install @google/generative-ai
পরে
পাইথন
pip install -U -q "google-genai"
জাভাস্ক্রিপ্ট
npm install @google/genai
প্রমাণীকরণ
একটি API কী ব্যবহার করে প্রমাণীকরণ করুন। আপনি Google AI স্টুডিওতে আপনার API কী তৈরি করতে পারেন।
আগে
পাইথন
পুরানো SDK এপিআই ক্লায়েন্ট অবজেক্টকে স্পষ্টভাবে পরিচালনা করে। নতুন SDK-এ আপনি API ক্লায়েন্ট তৈরি করেন এবং API কল করতে এটি ব্যবহার করেন। মনে রাখবেন, উভয় ক্ষেত্রেই আপনি যদি ক্লায়েন্টকে না দেন তাহলে SDK আপনার API কী GOOGLE_API_KEY
এনভায়রনমেন্ট ভেরিয়েবল থেকে তুলে নেবে।
import google.generativeai as genai
genai.configure(api_key=...)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
পরে
পাইথন
export GOOGLE_API_KEY="YOUR_API_KEY"
from google import genai
client = genai.Client() # Set the API key using the GOOGLE_API_KEY env var.
# Alternatively, you could set the API key explicitly:
# client = genai.Client(api_key="your_api_key")
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({apiKey: "GEMINI_API_KEY"});
সামগ্রী তৈরি করুন
আগে
পাইথন
নতুন SDK Client
অবজেক্টের মাধ্যমে সমস্ত API পদ্ধতিতে অ্যাক্সেস প্রদান করে। কয়েকটি রাষ্ট্রীয় বিশেষ ক্ষেত্রে ( chat
এবং লাইভ-এপিআই session
) ব্যতীত, এগুলি সমস্ত রাষ্ট্রহীন ফাংশন। ইউটিলিটি এবং অভিন্নতার জন্য, ফিরে আসা বস্তুগুলি হল pydantic
ক্লাস।
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
'Tell me a story in 300 words'
)
print(response.text)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = "Tell me a story in 300 words";
const result = await model.generateContent(prompt);
console.log(result.response.text());
পরে
পাইথন
from google import genai
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='Tell me a story in 300 words.'
)
print(response.text)
print(response.model_dump_json(
exclude_none=True, indent=4))
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "Tell me a story in 300 words.",
});
console.log(response.text);
আগে
পাইথন
নতুন SDK-তে একই সুবিধার অনেক বৈশিষ্ট্য বিদ্যমান। উদাহরণস্বরূপ, PIL.Image
অবজেক্টগুলি স্বয়ংক্রিয়ভাবে রূপান্তরিত হয়।
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content([
'Tell me a story based on this image',
Image.open(image_path)
])
print(response.text)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
mimeType,
},
};
}
const prompt = "Tell me a story based on this image";
const imagePart = fileToGenerativePart(
`path/to/organ.jpg`,
"image/jpeg",
);
const result = await model.generateContent([prompt, imagePart]);
console.log(result.response.text());
পরে
পাইথন
from google import genai
from PIL import Image
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=[
'Tell me a story based on this image',
Image.open(image_path)
]
)
print(response.text)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const organ = await ai.files.upload({
file: "path/to/organ.jpg",
});
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: [
createUserContent([
"Tell me a story based on this image",
createPartFromUri(organ.uri, organ.mimeType)
]),
],
});
console.log(response.text);
স্ট্রিমিং
আগে
পাইথন
import google.generativeai as genai
response = model.generate_content(
"Write a cute story about cats.",
stream=True)
for chunk in response:
print(chunk.text)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = "Write a story about a magic backpack.";
const result = await model.generateContentStream(prompt);
// Print text as it comes in.
for await (const chunk of result.stream) {
const chunkText = chunk.text();
process.stdout.write(chunkText);
}
পরে
পাইথন
from google import genai
client = genai.Client()
for chunk in client.models.generate_content_stream(
model='gemini-2.0-flash',
contents='Tell me a story in 300 words.'
):
print(chunk.text)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const response = await ai.models.generateContentStream({
model: "gemini-2.0-flash",
contents: "Write a story about a magic backpack.",
});
let text = "";
for await (const chunk of response) {
console.log(chunk.text);
text += chunk.text;
}
ঐচ্ছিক যুক্তি
আগে
পাইথন
নতুন SDK-এ সমস্ত পদ্ধতির জন্য, প্রয়োজনীয় আর্গুমেন্টগুলি কীওয়ার্ড আর্গুমেন্ট হিসাবে দেওয়া হয়। config
আর্গুমেন্টে সমস্ত ঐচ্ছিক ইনপুট দেওয়া হয়। কনফিগার আর্গুমেন্ট google.genai.types
নামস্থানে Python অভিধান বা Config
ক্লাস হিসাবে নির্দিষ্ট করা যেতে পারে। উপযোগিতা এবং অভিন্নতার জন্য, types
মডিউলের মধ্যে সমস্ত সংজ্ঞা হল pydantic
ক্লাস।
import google.generativeai as genai
model = genai.GenerativeModel(
'gemini-1.5-flash',
system_instruction='you are a story teller for kids under 5 years old',
generation_config=genai.GenerationConfig(
max_output_tokens=400,
top_k=2,
top_p=0.5,
temperature=0.5,
response_mime_type='application/json',
stop_sequences=['\n'],
)
)
response = model.generate_content('tell me a story in 100 words')
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
generationConfig: {
candidateCount: 1,
stopSequences: ["x"],
maxOutputTokens: 20,
temperature: 1.0,
},
});
const result = await model.generateContent(
"Tell me a story about a magic backpack.",
);
console.log(result.response.text())
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='Tell me a story in 100 words.',
config=types.GenerateContentConfig(
system_instruction='you are a story teller for kids under 5 years old',
max_output_tokens= 400,
top_k= 2,
top_p= 0.5,
temperature= 0.5,
response_mime_type= 'application/json',
stop_sequences= ['\n'],
seed=42,
),
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "Tell me a story about a magic backpack.",
config: {
candidateCount: 1,
stopSequences: ["x"],
maxOutputTokens: 20,
temperature: 1.0,
},
});
console.log(response.text);
নিরাপত্তা সেটিংস
নিরাপত্তা সেটিংস সহ একটি প্রতিক্রিয়া তৈরি করুন:
আগে
পাইথন
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
'say something bad',
safety_settings={
'HATE': 'BLOCK_ONLY_HIGH',
'HARASSMENT': 'BLOCK_ONLY_HIGH',
}
)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
});
const unsafePrompt =
"I support Martians Soccer Club and I think " +
"Jupiterians Football Club sucks! Write an ironic phrase telling " +
"them how I feel about them.";
const result = await model.generateContent(unsafePrompt);
try {
result.response.text();
} catch (e) {
console.error(e);
console.log(result.response.candidates[0].safetyRatings);
}
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='say something bad',
config=types.GenerateContentConfig(
safety_settings= [
types.SafetySetting(
category='HARM_CATEGORY_HATE_SPEECH',
threshold='BLOCK_ONLY_HIGH'
),
]
),
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const unsafePrompt =
"I support Martians Soccer Club and I think " +
"Jupiterians Football Club sucks! Write an ironic phrase telling " +
"them how I feel about them.";
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: unsafePrompt,
config: {
safetySettings: [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_ONLY_HIGH",
},
],
},
});
console.log("Finish reason:", response.candidates[0].finishReason);
console.log("Safety ratings:", response.candidates[0].safetyRatings);
অ্যাসিঙ্ক
আগে
পাইথন
asyncio
সাথে নতুন SDK ব্যবহার করার জন্য, client.aio
অধীনে প্রতিটি পদ্ধতির জন্য একটি পৃথক async
বাস্তবায়ন রয়েছে।
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content_async(
'tell me a story in 100 words'
)
পরে
পাইথন
from google import genai
client = genai.Client()
response = await client.aio.models.generate_content(
model='gemini-2.0-flash',
contents='Tell me a story in 300 words.'
)
চ্যাট
একটি চ্যাট শুরু করুন এবং মডেলকে একটি বার্তা পাঠান:
আগে
পাইথন
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
chat = model.start_chat()
response = chat.send_message(
"Tell me a story in 100 words")
response = chat.send_message(
"What happened after that?")
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const chat = model.startChat({
history: [
{
role: "user",
parts: [{ text: "Hello" }],
},
{
role: "model",
parts: [{ text: "Great to meet you. What would you like to know?" }],
},
],
});
let result = await chat.sendMessage("I have 2 dogs in my house.");
console.log(result.response.text());
result = await chat.sendMessage("How many paws are in my house?");
console.log(result.response.text());
পরে
পাইথন
from google import genai
client = genai.Client()
chat = client.chats.create(model='gemini-2.0-flash')
response = chat.send_message(
message='Tell me a story in 100 words')
response = chat.send_message(
message='What happened after that?')
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const chat = ai.chats.create({
model: "gemini-2.0-flash",
history: [
{
role: "user",
parts: [{ text: "Hello" }],
},
{
role: "model",
parts: [{ text: "Great to meet you. What would you like to know?" }],
},
],
});
const response1 = await chat.sendMessage({
message: "I have 2 dogs in my house.",
});
console.log("Chat response 1:", response1.text);
const response2 = await chat.sendMessage({
message: "How many paws are in my house?",
});
console.log("Chat response 2:", response2.text);
ফাংশন কলিং
আগে
পাইথন
নতুন SDK-এ, স্বয়ংক্রিয় ফাংশন কলিং ডিফল্ট। এখানে, আপনি এটি নিষ্ক্রিয় করুন.
import google.generativeai as genai
from enum import Enum
def get_current_weather(location: str) -> str:
"""Get the current whether in a given location.
Args:
location: required, The city and state, e.g. San Franciso, CA
unit: celsius or fahrenheit
"""
print(f'Called with: {location=}')
return "23C"
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
tools=[get_current_weather]
)
response = model.generate_content("What is the weather in San Francisco?")
function_call = response.candidates[0].parts[0].function_call
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
def get_current_weather(location: str) -> str:
"""Get the current whether in a given location.
Args:
location: required, The city and state, e.g. San Franciso, CA
unit: celsius or fahrenheit
"""
print(f'Called with: {location=}')
return "23C"
response = client.models.generate_content(
model='gemini-2.0-flash',
contents="What is the weather like in Boston?",
config=types.GenerateContentConfig(
tools=[get_current_weather],
automatic_function_calling={'disable': True},
),
)
function_call = response.candidates[0].content.parts[0].function_call
স্বয়ংক্রিয় ফাংশন কলিং
আগে
পাইথন
পুরানো SDK শুধুমাত্র চ্যাটে স্বয়ংক্রিয় ফাংশন কলিং সমর্থন করে। নতুন SDK-এ এটি generate_content
এ ডিফল্ট আচরণ।
import google.generativeai as genai
def get_current_weather(city: str) -> str:
return "23C"
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
tools=[get_current_weather]
)
chat = model.start_chat(
enable_automatic_function_calling=True)
result = chat.send_message("What is the weather in San Francisco?")
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
def get_current_weather(city: str) -> str:
return "23C"
response = client.models.generate_content(
model='gemini-2.0-flash',
contents="What is the weather like in Boston?",
config=types.GenerateContentConfig(
tools=[get_current_weather]
),
)
কোড এক্সিকিউশন
কোড এক্সিকিউশন হল একটি টুল যা মডেলটিকে পাইথন কোড তৈরি করতে, এটি চালাতে এবং ফলাফল ফেরত দিতে দেয়।
আগে
পাইথন
import google.generativeai as genai
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
tools="code_execution"
)
result = model.generate_content(
"What is the sum of the first 50 prime numbers? Generate and run code for "
"the calculation, and make sure you get all 50.")
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
tools: [{ codeExecution: {} }],
});
const result = await model.generateContent(
"What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get " +
"all 50.",
);
console.log(result.response.text());
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='What is the sum of the first 50 prime numbers? Generate and run '
'code for the calculation, and make sure you get all 50.',
config=types.GenerateContentConfig(
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
),
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const response = await ai.models.generateContent({
model: "gemini-2.0-pro-exp-02-05",
contents: `Write and execute code that calculates the sum of the first 50 prime numbers.
Ensure that only the executable code and its resulting output are generated.`,
});
// Each part may contain text, executable code, or an execution result.
for (const part of response.candidates[0].content.parts) {
console.log(part);
console.log("\n");
}
console.log("-".repeat(80));
// The `.text` accessor concatenates the parts into a markdown-formatted text.
console.log("\n", response.text);
গ্রাউন্ডিং অনুসন্ধান করুন
GoogleSearch
(Jemini>=2.0) এবং GoogleSearchRetrieval
(Gemini <2.0) হল এমন টুল যা মডেলটিকে Google দ্বারা চালিত গ্রাউন্ডিংয়ের জন্য সর্বজনীন ওয়েব ডেটা পুনরুদ্ধার করার অনুমতি দেয়।
আগে
পাইথন
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
contents="what is the Google stock price?",
tools='google_search_retrieval'
)
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='What is the Google stock price?',
config=types.GenerateContentConfig(
tools=[
types.Tool(
google_search=types.GoogleSearch()
)
]
)
)
JSON প্রতিক্রিয়া
JSON ফর্ম্যাটে উত্তর তৈরি করুন।
আগে
পাইথন
একটি response_schema
নির্দিষ্ট করে এবং response_mime_type="application/json"
সেট করে ব্যবহারকারীরা একটি প্রদত্ত কাঠামো অনুসরণ করে একটি JSON
প্রতিক্রিয়া তৈরি করতে মডেলকে সীমাবদ্ধ করতে পারে। নতুন SDK স্কিমা প্রদান করতে pydantic
ক্লাস ব্যবহার করে (যদিও আপনি একটি genai.types.Schema
, বা সমতুল্য dict
পাস করতে পারেন)। যখন সম্ভব, SDK প্রত্যাবর্তিত JSON-কে পার্স করবে, এবং ফলাফল response.parsed
.parsed-এ ফেরত দেবে। আপনি যদি স্কিমা হিসাবে একটি pydantic
ক্লাস প্রদান করেন তাহলে SDK সেই JSON
ক্লাসের একটি উদাহরণে রূপান্তর করবে।
import google.generativeai as genai
import typing_extensions as typing
class CountryInfo(typing.TypedDict):
name: str
population: int
capital: str
continent: str
major_cities: list[str]
gdp: int
official_language: str
total_area_sq_mi: int
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
result = model.generate_content(
"Give me information of the United States",
generation_config=genai.GenerationConfig(
response_mime_type="application/json",
response_schema = CountryInfo
),
)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const schema = {
description: "List of recipes",
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
recipeName: {
type: SchemaType.STRING,
description: "Name of the recipe",
nullable: false,
},
},
required: ["recipeName"],
},
};
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
generationConfig: {
responseMimeType: "application/json",
responseSchema: schema,
},
});
const result = await model.generateContent(
"List a few popular cookie recipes.",
);
console.log(result.response.text());
পরে
পাইথন
from google import genai
from pydantic import BaseModel
client = genai.Client()
class CountryInfo(BaseModel):
name: str
population: int
capital: str
continent: str
major_cities: list[str]
gdp: int
official_language: str
total_area_sq_mi: int
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='Give me information of the United States.',
config={
'response_mime_type': 'application/json',
'response_schema': CountryInfo,
},
)
response.parsed
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "List a few popular cookie recipes.",
config: {
responseMimeType: "application/json",
responseSchema: {
type: "array",
items: {
type: "object",
properties: {
recipeName: { type: "string" },
ingredients: { type: "array", items: { type: "string" } },
},
required: ["recipeName", "ingredients"],
},
},
},
});
console.log(response.text);
ফাইল
আপলোড করুন
একটি ফাইল আপলোড করুন:
আগে
পাইথন
import requests
import pathlib
import google.generativeai as genai
# Download file
response = requests.get(
'https://storage.googleapis.com/generativeai-downloads/data/a11.txt')
pathlib.Path('a11.txt').write_text(response.text)
file = genai.upload_file(path='a11.txt')
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content([
'Can you summarize this file:',
my_file
])
print(response.text)
পরে
পাইথন
import requests
import pathlib
from google import genai
client = genai.Client()
# Download file
response = requests.get(
'https://storage.googleapis.com/generativeai-downloads/data/a11.txt')
pathlib.Path('a11.txt').write_text(response.text)
my_file = client.files.upload(file='a11.txt')
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=[
'Can you summarize this file:',
my_file
]
)
print(response.text)
তালিকা এবং পেতে
আপলোড করা ফাইলগুলি তালিকাভুক্ত করুন এবং একটি ফাইলের নাম সহ একটি আপলোড করা ফাইল পান:
আগে
পাইথন
import google.generativeai as genai
for file in genai.list_files():
print(file.name)
file = genai.get_file(name=file.name)
পরে
পাইথন
from google import genai
client = genai.Client()
for file in client.files.list():
print(file.name)
file = client.files.get(name=file.name)
মুছুন
একটি ফাইল মুছুন:
আগে
পাইথন
import pathlib
import google.generativeai as genai
pathlib.Path('dummy.txt').write_text(dummy)
dummy_file = genai.upload_file(path='dummy.txt')
file = genai.delete_file(name=dummy_file.name)
পরে
পাইথন
import pathlib
from google import genai
client = genai.Client()
pathlib.Path('dummy.txt').write_text(dummy)
dummy_file = client.files.upload(file='dummy.txt')
response = client.files.delete(name=dummy_file.name)
প্রসঙ্গ ক্যাশিং
কনটেক্সট ক্যাশিং ব্যবহারকারীকে একবার মডেলে কন্টেন্ট পাঠাতে, ইনপুট টোকেন ক্যাশ করতে এবং তারপর খরচ কমাতে পরবর্তী কলগুলিতে ক্যাশে করা টোকেনগুলি উল্লেখ করতে দেয়।
আগে
পাইথন
import requests
import pathlib
import google.generativeai as genai
from google.generativeai import caching
# Download file
response = requests.get(
'https://storage.googleapis.com/generativeai-downloads/data/a11.txt')
pathlib.Path('a11.txt').write_text(response.text)
# Upload file
document = genai.upload_file(path="a11.txt")
# Create cache
apollo_cache = caching.CachedContent.create(
model="gemini-1.5-flash-001",
system_instruction="You are an expert at analyzing transcripts.",
contents=[document],
)
# Generate response
apollo_model = genai.GenerativeModel.from_cached_content(
cached_content=apollo_cache
)
response = apollo_model.generate_content("Find a lighthearted moment from this transcript")
জাভাস্ক্রিপ্ট
import { GoogleAICacheManager, GoogleAIFileManager } from "@google/generative-ai/server";
import { GoogleGenerativeAI } from "@google/generative-ai";
const cacheManager = new GoogleAICacheManager("GOOGLE_API_KEY");
const fileManager = new GoogleAIFileManager("GOOGLE_API_KEY");
const uploadResult = await fileManager.uploadFile("path/to/a11.txt", {
mimeType: "text/plain",
});
const cacheResult = await cacheManager.create({
model: "models/gemini-1.5-flash",
contents: [
{
role: "user",
parts: [
{
fileData: {
fileUri: uploadResult.file.uri,
mimeType: uploadResult.file.mimeType,
},
},
],
},
],
});
console.log(cacheResult);
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModelFromCachedContent(cacheResult);
const result = await model.generateContent(
"Please summarize this transcript.",
);
console.log(result.response.text());
পরে
পাইথন
import requests
import pathlib
from google import genai
from google.genai import types
client = genai.Client()
# Check which models support caching.
for m in client.models.list():
for action in m.supported_actions:
if action == "createCachedContent":
print(m.name)
break
# Download file
response = requests.get(
'https://storage.googleapis.com/generativeai-downloads/data/a11.txt')
pathlib.Path('a11.txt').write_text(response.text)
# Upload file
document = client.files.upload(file='a11.txt')
# Create cache
model='gemini-1.5-flash-001'
apollo_cache = client.caches.create(
model=model,
config={
'contents': [document],
'system_instruction': 'You are an expert at analyzing transcripts.',
},
)
# Generate response
response = client.models.generate_content(
model=model,
contents='Find a lighthearted moment from this transcript',
config=types.GenerateContentConfig(
cached_content=apollo_cache.name,
)
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const filePath = path.join(media, "a11.txt");
const document = await ai.files.upload({
file: filePath,
config: { mimeType: "text/plain" },
});
console.log("Uploaded file name:", document.name);
const modelName = "gemini-1.5-flash";
const contents = [
createUserContent(createPartFromUri(document.uri, document.mimeType)),
];
const cache = await ai.caches.create({
model: modelName,
config: {
contents: contents,
systemInstruction: "You are an expert analyzing transcripts.",
},
});
console.log("Cache created:", cache);
const response = await ai.models.generateContent({
model: modelName,
contents: "Please summarize this transcript",
config: { cachedContent: cache.name },
});
console.log("Response text:", response.text);
টোকেন গণনা করুন
একটি অনুরোধে টোকেনের সংখ্যা গণনা করুন।
আগে
পাইথন
import google.generativeai as genai
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.count_tokens(
'The quick brown fox jumps over the lazy dog.')
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY+);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
});
// Count tokens in a prompt without calling text generation.
const countResult = await model.countTokens(
"The quick brown fox jumps over the lazy dog.",
);
console.log(countResult.totalTokens); // 11
const generateResult = await model.generateContent(
"The quick brown fox jumps over the lazy dog.",
);
// On the response for `generateContent`, use `usageMetadata`
// to get separate input and output token counts
// (`promptTokenCount` and `candidatesTokenCount`, respectively),
// as well as the combined token count (`totalTokenCount`).
console.log(generateResult.response.usageMetadata);
// candidatesTokenCount and totalTokenCount depend on response, may vary
// { promptTokenCount: 11, candidatesTokenCount: 124, totalTokenCount: 135 }
পরে
পাইথন
from google import genai
client = genai.Client()
response = client.models.count_tokens(
model='gemini-2.0-flash',
contents='The quick brown fox jumps over the lazy dog.',
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const prompt = "The quick brown fox jumps over the lazy dog.";
const countTokensResponse = await ai.models.countTokens({
model: "gemini-2.0-flash",
contents: prompt,
});
console.log(countTokensResponse.totalTokens);
const generateResponse = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: prompt,
});
console.log(generateResponse.usageMetadata);
ছবি তৈরি করুন
ছবি তৈরি করুন:
আগে
পাইথন
#pip install https://github.com/google-gemini/generative-ai-python@imagen
import google.generativeai as genai
imagen = genai.ImageGenerationModel(
"imagen-3.0-generate-001")
gen_images = imagen.generate_images(
prompt="Robot holding a red skateboard",
number_of_images=1,
safety_filter_level="block_low_and_above",
person_generation="allow_adult",
aspect_ratio="3:4",
)
পরে
পাইথন
from google import genai
client = genai.Client()
gen_images = client.models.generate_images(
model='imagen-3.0-generate-001',
prompt='Robot holding a red skateboard',
config=types.GenerateImagesConfig(
number_of_images= 1,
safety_filter_level= "BLOCK_LOW_AND_ABOVE",
person_generation= "ALLOW_ADULT",
aspect_ratio= "3:4",
)
)
for n, image in enumerate(gen_images.generated_images):
pathlib.Path(f'{n}.png').write_bytes(
image.image.image_bytes)
বিষয়বস্তু এম্বেড করুন
কন্টেন্ট এম্বেডিং তৈরি করুন।
আগে
পাইথন
import google.generativeai as genai
response = genai.embed_content(
model='models/text-embedding-004',
content='Hello world'
)
জাভাস্ক্রিপ্ট
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("GOOGLE_API_KEY");
const model = genAI.getGenerativeModel({
model: "text-embedding-004",
});
const result = await model.embedContent("Hello world!");
console.log(result.embedding);
পরে
পাইথন
from google import genai
client = genai.Client()
response = client.models.embed_content(
model='text-embedding-004',
contents='Hello world',
)
জাভাস্ক্রিপ্ট
import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const text = "Hello World!";
const result = await ai.models.embedContent({
model: "text-embedding-004",
contents: text,
config: { outputDimensionality: 10 },
});
console.log(result.embeddings);
একটি মডেল টিউন
একটি টিউন করা মডেল তৈরি করুন এবং ব্যবহার করুন।
নতুন SDK client.tunings.tune
এর সাথে টিউনিং সহজ করে, যা টিউনিং কাজ চালু করে এবং কাজটি সম্পূর্ণ না হওয়া পর্যন্ত ভোট দেয়৷
আগে
পাইথন
import google.generativeai as genai
import random
# create tuning model
train_data = {}
for i in range(1, 6):
key = f'input {i}'
value = f'output {i}'
train_data[key] = value
name = f'generate-num-{random.randint(0,10000)}'
operation = genai.create_tuned_model(
source_model='models/gemini-1.5-flash-001-tuning',
training_data=train_data,
id = name,
epoch_count = 5,
batch_size=4,
learning_rate=0.001,
)
# wait for tuning complete
tuningProgress = operation.result()
# generate content with the tuned model
model = genai.GenerativeModel(model_name=f'tunedModels/{name}')
response = model.generate_content('55')
পরে
পাইথন
from google import genai
from google.genai import types
client = genai.Client()
# Check which models are available for tuning.
for m in client.models.list():
for action in m.supported_actions:
if action == "createTunedModel":
print(m.name)
break
# create tuning model
training_dataset=types.TuningDataset(
examples=[
types.TuningExample(
text_input=f'input {i}',
output=f'output {i}',
)
for i in range(5)
],
)
tuning_job = client.tunings.tune(
base_model='models/gemini-1.5-flash-001-tuning',
training_dataset=training_dataset,
config=types.CreateTuningJobConfig(
epoch_count= 5,
batch_size=4,
learning_rate=0.001,
tuned_model_display_name="test tuned model"
)
)
# generate content with the tuned model
response = client.models.generate_content(
model=tuning_job.tuned_model.model,
contents='55',
)
ব্রাউজারে জাভাস্ক্রিপ্ট
ব্রাউজারে Gemini API ব্যবহার শুরু করতে, আপনি একটি CDN থেকে জাভাস্ক্রিপ্টের জন্য Gen AI SDK আমদানি করতে পারেন, যেমনটি নিম্নলিখিত উদাহরণে দেখানো হয়েছে:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Using My Package</title>
</head>
<body>
<script type="module">
import {GoogleGenAI} from 'https://cdn.jsdelivr.net/npm/@google/genai@latest/+esm'
const ai = new GoogleGenAI({apiKey: "GOOGLE_API_KEY"});
async function main() {
const response = await ai.models.generateContent({
model: 'gemini-2.0-flash-001',
contents: 'Why is the sky blue?',
});
console.log(response.text);
}
main();
</script>
</body>
</html>
স্থানীয়ভাবে এই কোডটি চালানোর জন্য, আপনাকে http-server এর মত একটি সার্ভার ব্যবহার করা উচিত। আপনি যদি স্থানীয় ফাইল সিস্টেম থেকে কোড চালানোর চেষ্টা করেন, তাহলে আপনি একটি CORS ত্রুটির সম্মুখীন হতে পারেন।