Gemini Live API অডিও বা টেক্সটের একটানা স্ট্রিম প্রক্রিয়া করে যাকে সেশন বলা হয়। আপনি প্রাথমিক হ্যান্ডশেক থেকে শুরু করে সুন্দরভাবে সমাপ্তি পর্যন্ত সেশনের জীবনচক্র পরিচালনা করতে পারেন।
সেশনের সীমা
Live API ক্ষেত্রে, একটি সেশন বলতে একটি স্থায়ী সংযোগকে বোঝায় যেখানে ইনপুট এবং আউটপুট একই সংযোগের মাধ্যমে ক্রমাগত স্ট্রিম করা হয়।
যদি সেশনটি নিম্নলিখিত সীমা অতিক্রম করে, তাহলে সংযোগটি বন্ধ করে দেওয়া হবে।
সংযোগের দৈর্ঘ্য প্রায় ১০ মিনিটের মধ্যে সীমাবদ্ধ।
সেশনের দৈর্ঘ্য ইনপুট পদ্ধতির উপর নির্ভর করে:
- শুধুমাত্র অডিও ইনপুট সেশনগুলি ১৫ মিনিটের মধ্যে সীমাবদ্ধ।
- ভিডিও + অডিও ইনপুট 2 মিনিটের মধ্যে সীমাবদ্ধ।
সেশন কনটেক্সট উইন্ডো ১২৮,০০০ টোকেনের মধ্যে সীমাবদ্ধ।
সংযোগ শেষ হওয়ার আগে আপনি একটি বন্ধ থাকার বিজ্ঞপ্তি পাবেন, যা আপনাকে পরবর্তী পদক্ষেপ নেওয়ার অনুমতি দেবে।
একটি সেশন শুরু করুন
একটি সেশন কীভাবে শুরু করবেন তার সম্পূর্ণ স্নিপেটের জন্য Live API এর শুরু করার নির্দেশিকাটি দেখুন।
সেশনের মাঝামাঝি আপডেট করুন
Live API মডেলগুলি মধ্য-সেশন আপডেটের জন্য নিম্নলিখিত উন্নত ক্ষমতাগুলিকে সমর্থন করে:
সিস্টেম নির্দেশাবলী আপডেট করুন (শুধুমাত্র Vertex AI Gemini API এর জন্য)
ক্রমবর্ধমান কন্টেন্ট আপডেট যোগ করুন
একটি সক্রিয় সেশনের সময় আপনি ক্রমবর্ধমান আপডেট যোগ করতে পারেন। টেক্সট ইনপুট পাঠাতে, সেশনের প্রসঙ্গ স্থাপন করতে, অথবা সেশনের প্রসঙ্গ পুনরুদ্ধার করতে এটি ব্যবহার করুন।
দীর্ঘ প্রেক্ষাপটের জন্য, পরবর্তী ইন্টারঅ্যাকশনের জন্য প্রেক্ষাপট উইন্ডো খালি করার জন্য আমরা একটি একক বার্তার সারাংশ প্রদান করার পরামর্শ দিচ্ছি।
সংক্ষিপ্ত প্রসঙ্গের জন্য, আপনি ঘটনাগুলির সঠিক ক্রম উপস্থাপনের জন্য পর্যায়ক্রমে ইন্টারঅ্যাকশন পাঠাতে পারেন, যেমন নীচের স্নিপেট।
সুইফট
// Define initial turns (history/context).
let turns: [ModelContent] = [
ModelContent(role: "user", parts: [TextPart("What is the capital of France?")]),
ModelContent(role: "model", parts: [TextPart("Paris")]),
]
// Send history, keeping the conversational turn OPEN (false).
await session.sendContent(turns, turnComplete: false)
// Define the new user query.
let newTurn: [ModelContent] = [
ModelContent(role: "user", parts: [TextPart("What is the capital of Germany?")]),
]
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.sendContent(newTurn, turnComplete: true)
Kotlin
Not yet supported for Android apps - check back soon!
Java
Not yet supported for Android apps - check back soon!
Web
const turns = [{ text: "Hello from the user!" }];
await session.send(
turns,
false // turnComplete: false
);
console.log("Sent history. Waiting for next input...");
// Define the new user query.
const newTurn [{ text: "And what is the capital of Germany?" }];
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
newTurn,
true // turnComplete: true
);
console.log("Sent final query. Model response expected now.");
Dart
// Define initial turns (history/context).
final List turns = [
Content(
"user",
[Part.text("What is the capital of France?")],
),
Content(
"model",
[Part.text("Paris")],
),
];
// Send history, keeping the conversational turn OPEN (false).
await session.send(
input: turns,
turnComplete: false,
);
// Define the new user query.
final List newTurn = [
Content(
"user",
[Part.text("What is the capital of Germany?")],
),
];
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
input: newTurn,
turnComplete: true,
);
ঐক্য
// Define initial turns (history/context).
List turns = new List {
new ModelContent("user", new ModelContent.TextPart("What is the capital of France?") ),
new ModelContent("model", new ModelContent.TextPart("Paris") ),
};
// Send history, keeping the conversational turn OPEN (false).
foreach (ModelContent turn in turns)
{
await session.SendAsync(
content: turn,
turnComplete: false
);
}
// Define the new user query.
ModelContent newTurn = ModelContent.Text("What is the capital of Germany?");
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.SendAsync(
content: newTurn,
turnComplete: true
);
সেশনের মাঝামাঝি সময়ে সিস্টেম নির্দেশাবলী আপডেট করুন
| শুধুমাত্র আপনার API প্রদানকারী হিসেবে Vertex AI Gemini API ব্যবহার করলেই এটি উপলব্ধ। |
একটি সক্রিয় সেশনের সময় আপনি সিস্টেমের নির্দেশাবলী আপডেট করতে পারেন। মডেলের প্রতিক্রিয়াগুলিকে অভিযোজিত করতে এটি ব্যবহার করুন, উদাহরণস্বরূপ প্রতিক্রিয়া ভাষা পরিবর্তন করতে বা স্বর পরিবর্তন করতে।
সেশনের মাঝামাঝি সময়ে সিস্টেম নির্দেশাবলী আপডেট করার জন্য, আপনি system ভূমিকা সহ টেক্সট কন্টেন্ট পাঠাতে পারেন। আপডেট করা সিস্টেম নির্দেশাবলী সেশনের বাকি সময় ধরে কার্যকর থাকবে।
সুইফট
await session.sendContent(
[ModelContent(
role: "system",
parts: [TextPart("new system instruction")]
)],
turnComplete: false
)
Kotlin
Not yet supported for Android apps - check back soon!
Java
Not yet supported for Android apps - check back soon!
Web
Not yet supported for Web apps - check back soon!
Dart
try {
await _session.send(
input: Content(
'system',
[Part.text('new system instruction')],
),
turnComplete: false,
);
} catch (e) {
print('Failed to update system instructions: $e');
}
ঐক্য
try
{
await session.SendAsync(
content: new ModelContent(
"system",
new ModelContent.TextPart("new system instruction")
),
turnComplete: false
);
}
catch (Exception e)
{
Debug.LogError($"Failed to update system instructions: {e.Message}");
}
একটি সেশন কখন শেষ হবে তা সনাক্ত করুন
সেশন শেষ হওয়ার
নিম্নলিখিত উদাহরণে দেখানো হয়েছে কিভাবে একটি আসন্ন সেশন সমাপ্তি সনাক্ত করা যায়, একটি বন্ধ হয়ে যাওয়ার বিজ্ঞপ্তি শুনে:
সুইফট
for try await response in session.responses {
switch response.payload {
case .goingAwayNotice(let goingAwayNotice):
// Prepare for the session to close soon
if let timeLeft = goingAwayNotice.timeLeft {
print("Server going away in \(timeLeft) seconds")
}
}
}
Kotlin
for (response in session.responses) {
when (val message = response.payload) {
is LiveServerGoAway -> {
// Prepare for the session to close soon
val remaining = message.timeLeft
logger.info("Server going away in $remaining")
}
}
}
Java
session.getResponses().forEach(response -> {
if (response.getPayload() instanceof LiveServerResponse.GoingAwayNotice) {
LiveServerResponse.GoingAwayNotice notice = (LiveServerResponse.GoingAwayNotice) response.getPayload();
// Prepare for the session to close soon
Duration timeLeft = notice.getTimeLeft();
}
});
Web
for await (const message of session.receive()) {
switch (message.type) {
...
case "goingAwayNotice":
console.log("Server going away. Time left:", message.timeLeft);
break;
}
}
Dart
Future _handleLiveServerMessage(LiveServerResponse response) async {
final message = response.message;
if (message is GoingAwayNotice) {
// Prepare for the session to close soon
developer.log('Server going away. Time left: ${message.timeLeft}');
}
}
ঐক্য
foreach (var response in session.Responses) {
if (response.Payload is LiveSessionGoingAway notice) {
// Prepare for the session to close soon
TimeSpan timeLeft = notice.TimeLeft;
Debug.Log($"Server going away notice received. Remaining: {timeLeft}");
}
}