সার্ভার প্রম্পট টেমপ্লেটের সাথে একাধিক-পালা মিথস্ক্রিয়া


এই নির্দেশিকাটি এমন যেকোনো সক্ষমতার ক্ষেত্রে প্রযোজ্য যা একাধিক পালাভিত্তিক মিথস্ক্রিয়ার ওপর নির্ভরশীল, যার মধ্যে অন্তর্ভুক্ত রয়েছে:

উচ্চ-স্তরের সংক্ষিপ্ত বিবরণ

একাধিক পালাবিশিষ্ট কথোপকথনের ক্ষেত্রে, Firebase AI Logic SDK-গুলো কথোপকথনের অবস্থা পরিচালনা করে। সার্ভার প্রম্পট টেমপ্লেট ব্যবহার করার ক্ষেত্রেও এটি প্রযোজ্য।

একাধিক টার্নের ইন্টারঅ্যাকশন এবং সার্ভার প্রম্পট টেমপ্লেটের মৌলিক কার্যপ্রবাহ একক টার্নের অনুরোধের মতোই, তবে কিছু গুরুত্বপূর্ণ পার্থক্য রয়েছে:

  1. Firebase কনসোলে নির্দেশিত UI ব্যবহার করে টেমপ্লেটটি তৈরি করুন।

    একাধিক পালাব্যাপী মিথস্ক্রিয়ার জন্য, আপনাকে অবশ্যই {% verbatim %}{% endverbatim %} যোগ করতে হবে। {% verbatim %}{% endverbatim %} টেমপ্লেটের কন্টেন্টে থাকা {% verbatim %}{% endverbatim %} ট্যাগ, যা টেমপ্লেটকে বলে দেয় ক্লায়েন্ট SDK দ্বারা পরিচালিত কথোপকথনের পালাগুলো কোথায় যুক্ত করতে হবে।

  2. Firebase কনসোলের টেস্টিং অভিজ্ঞতা ব্যবহার করে একটি বাস্তব অনুরোধে টেমপ্লেটটি পরীক্ষা করুন।

    একাধিক পালাবিশিষ্ট ইন্টারঅ্যাকশনের ক্ষেত্রে, কনসোল টেস্টিং অভিজ্ঞতা শুধুমাত্র প্রথম পালাটি পরীক্ষা করতে সাহায্য করতে পারে। পরবর্তী পালাগুলো (হিস্ট্রি) কীভাবে পরিচালিত হয়, তা আপনি আপনার আসল অ্যাপের সাথে টেমপ্লেটটি ব্যবহার করে পরীক্ষা করতে পারেন।

  3. আপনার অ্যাপের কোড থেকে templateGenerativeModel ব্যবহার করে টেমপ্লেটটি অ্যাক্সেস করুন।

    একাধিক পালাবিশিষ্ট আলাপচারিতার জন্য, আপনাকে অবশ্যই startChat এবং sendMessage ব্যবহার করতে হবে (ঠিক যেমনটি আপনি সার্ভার প্রম্পট টেমপ্লেট ব্যবহার না করার সময় একাধিক পালাবিশিষ্ট আলাপচারিতার জন্য করে থাকেন)।

উল্লেখ্য যে, ফাংশন কল করার ক্ষেত্রে আরও কয়েকটি পার্থক্য রয়েছে, যা এই পৃষ্ঠার পরবর্তী অংশে বর্ণনা করা হয়েছে।



একাধিক পালায় কথোপকথন (চ্যাট)

আপনি যদি ইতিমধ্যে তা না করে থাকেন, তাহলে সার্ভার প্রম্পট টেমপ্লেট ব্যবহার না করার ক্ষেত্রে একাধিক পালায় কথোপকথন (চ্যাট) তৈরির সাধারণ নির্দেশিকাটি পর্যালোচনা করুন।

সার্ভার প্রম্পট টেমপ্লেটের মৌলিক বিন্যাস

Firebase AI Logic- এর জন্য, Firebase কনসোল আপনাকে টেমপ্লেটের ফ্রন্টম্যাটার এবং বিষয়বস্তু নির্দিষ্ট করার জন্য একটি নির্দেশিত ইউজার ইন্টারফেস প্রদান করে।

সার্ভার প্রম্পট টেমপ্লেটগুলো ডটপ্রম্পট-ভিত্তিক সিনট্যাক্স এবং ফরম্যাট ব্যবহার করে। আরও বিস্তারিত জানতে, টেমপ্লেট ফরম্যাট, সিনট্যাক্স এবং উদাহরণসমূহ দেখুন।

নিচের উদাহরণ টেমপ্লেটটি একটি মাল্টি-টার্ন কথোপকথন (চ্যাট) তৈরির জন্য সবচেয়ে গুরুত্বপূর্ণ উপাদানগুলো দেখায়। {% verbatim %}{% endverbatim %} এর সংযোজনটি লক্ষ্য করুন। {% verbatim %}{% endverbatim %} টেমপ্লেটের কন্টেন্টে থাকা {% verbatim %}{% endverbatim %} ট্যাগ, যা টেমপ্লেটকে বলে দেয় ক্লায়েন্ট SDK দ্বারা পরিচালিত কথোপকথনের পালাগুলো কোথায় যুক্ত করতে হবে।

---
model: 'gemini-3-flash-preview'
---

{{role "system"}}
You help customers with their invoices, including answering questions or providing their invoices to them.
If an invoice is requested, it must be a clearly structured invoice document that uses a tabular or clearly delineated list format for line items.

{{history}}

কোডে আপনার টেমপ্লেট ব্যবহার করুন

এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট বিষয়বস্তু এবং কোড দেখতে আপনার জেমিনি এপিআই প্রদানকারীর উপর ক্লিক করুন।

নিচের উদাহরণ ক্লায়েন্ট কোডটি দেখায় কিভাবে আপনার কোডে টেমপ্লেটটি ব্যবহার করতে হয়। একাধিক টার্নের ইন্টারঅ্যাকশন তৈরি করার সময় startChat এবং sendMessage এর সাথে templateGenerativeModel এর ব্যবহার লক্ষ্য করুন।

সুইফট

For Swift, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Kotlin


// ...

// Initialize the Gemini Developer API backend service
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
                        .templateGenerativeModel()

// Start a chat session with a template.
val chatSession = model.startChat(
  // Specify your template ID
  templateId= "my-chat-template-v1-0-0",
  inputs = emptyMap()
)

// Send messages.
// The template's system instructions and model config apply to every turn automatically.
// The SDK automatically manages the state of the conversation.
val response = chatSession.sendMessage(
  content("user") { text("I need a copy of my invoice.") }
)

val text = response.text
println(text)

Java

For Java, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Web


// ...

// Initialize the Gemini Developer API backend service.
const ai = getAI(app, { backend: new GoogleAIBackend() });

// Create a `TemplateGenerativeModel` instance.
const model = getTemplateGenerativeModel(ai);

// Start a chat session with a template.
const chatSession = model.startChat({
  // Specify your template ID.
  templateId: 'my-chat-template-v1-0-0',
});

// Send messages.
// The template's system instructions and model config apply to every turn automatically.
// The SDK automatically manages the state of the conversation.
const result = await chatSession.sendMessage("I need a copy of my invoice.");

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

Dart


// ...

// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
final model = FirebaseAI.googleAI().templateGenerativeModel();

// Start a chat session with a template.
final chatSession = model.startChat(
  // Specify your template ID.
  templateId: 'my-chat-template-v1-0-0',
);

// Send messages.
// The template's system instructions and model config apply to every turn automatically.
// The SDK automatically manages the state of the conversation.
final response = await chatSession.sendMessage(
  Content.text('I need a copy of my invoice.'),
);

final text = response.text;
print(text);

ঐক্য


// ...

// Initialize the Gemini Developer API backend service.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Create a `TemplateGenerativeModel` instance.
var model = firebaseAI.GetTemplateGenerativeModel();

// Start a chat session with a template.
var chatSession = model.StartChat(
    // Specify your template ID.
    "my-chat-template-v1-0-0"
);

// Send messages.
// The template's system instructions and model config apply to every turn automatically.
// The SDK automatically manages the state of the conversation.
try
{
   var response = await chatSession.SendMessageAsync(ModelContent.Text("I need a copy of my invoice."));
   Debug.Log($"Response Text: {response.Text}");
}
catch (Exception e) {
  Debug.LogError($"An error occurred: {e.Message}");
}



ফাংশন কলিং

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

সার্ভার প্রম্পট টেমপ্লেটের মৌলিক বিন্যাস

Firebase AI Logic- এর জন্য, Firebase কনসোল আপনাকে টেমপ্লেটের ফ্রন্টম্যাটার এবং বিষয়বস্তু নির্দিষ্ট করার জন্য একটি নির্দেশিত ইউজার ইন্টারফেস প্রদান করে।

সার্ভার প্রম্পট টেমপ্লেটগুলো ডটপ্রম্পট-ভিত্তিক সিনট্যাক্স এবং ফরম্যাট ব্যবহার করে। আরও বিস্তারিত জানতে, টেমপ্লেট ফরম্যাট, সিনট্যাক্স এবং উদাহরণসমূহ দেখুন।

নিচের উদাহরণ টেমপ্লেটটি ফাংশন কলিং ব্যবহার করার ক্ষেত্রে একটি টেমপ্লেটের সবচেয়ে গুরুত্বপূর্ণ উপাদানগুলো দেখায়। নিম্নলিখিত বিষয়গুলো লক্ষ্য করুন:

  • টেমপ্লেটের ফ্রন্টম্যাটারে, tools অবজেক্টে ফাংশন ডিক্লারেশন প্রদান করে মডেলটি যে ফাংশনগুলো অ্যাক্সেস করতে পারে, সেগুলোর তালিকা তৈরি করুন।

    • মডেলটি যে প্রতিটি ফাংশন ব্যবহার করতে পারবে, তার জন্য name (আবশ্যক) এবং description (ঐচ্ছিক) নির্ধারণ করুন।

    • মডেলটি যে প্রতিটি ফাংশনে প্রবেশাধিকার রাখে, তার জন্য স্কিমা নির্ধারণ করুন।

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

  • টেমপ্লেটের কন্টেন্টে {% verbatim %}{% endverbatim %} যোগ করুন। {% verbatim %}{% endverbatim %} ট্যাগ, যা টেমপ্লেটকে বলে দেয় ক্লায়েন্ট SDK দ্বারা পরিচালিত কথোপকথনের পালাগুলো কোথায় যুক্ত করতে হবে।

টেমপ্লেটে সংজ্ঞায়িত ফাংশন স্কিমা সহ উদাহরণ টেমপ্লেট

---
model: gemini-3-flash-preview
tools:
  - name: fetchWeather
    description: Get the weather conditions for a specific city on a specific date.
    input:
      schema:
        location(object, The name of the city and its state for which to get the weather. Only cities in the USA are supported.):
          city: string, The city of the location.
          state: string, The state of the location.
        date: string, The date for which to get the weather. Date must be in the format YYYY-MM-DD.

---

What was the weather like in Boston, Massachusetts on 10/17 in year 2024?

{{history}}

কোডে আপনার টেমপ্লেট ব্যবহার করুন

এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট বিষয়বস্তু এবং কোড দেখতে আপনার জেমিনি এপিআই প্রদানকারীর উপর ক্লিক করুন।

নিচের উদাহরণ ক্লায়েন্ট কোডটি দেখায় কিভাবে আপনার কোডে টেমপ্লেটটি ব্যবহার করতে হয়। নিম্নলিখিত বিষয়গুলো লক্ষ্য করুন:

  • একাধিক পালায় ইন্টারঅ্যাকশন ব্যবহার করার সময় startChat এবং sendMessage এর সাথে templateGenerativeModel ব্যবহার করুন।

  • আপনার ক্লায়েন্ট কোডে মডেলটি ইনিশিয়ালাইজ করার সময়, মডেলটি যে ফাংশনগুলো অ্যাক্সেস করতে পারে সেগুলোর তালিকা করবেন না । এর পরিবর্তে, ফাংশনগুলোকে টেমপ্লেটের ফ্রন্টম্যাটারের tools অবজেক্টে তালিকাভুক্ত করতে হবে (উপরে দেখুন)।

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

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

টেমপ্লেটে ফাংশন স্কিমা সংজ্ঞায়িত সহ ক্লায়েন্ট কোডের উদাহরণ।

সুইফট

For Swift, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Kotlin


// ...

// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .templateGenerativeModel()

// Start a chat session with a template that has functions listed as tools.
val chatSession = model.startChat(
    // Specify your template ID
    templateId = "my-function-calling-template-v1-0-0",
    inputs = emptyMap()
)

// Send a message that might trigger a function call.
val response = chatSession.sendMessage(
    content("user") { text(userMessage) }
)

// When the model responds with one or more function calls, invoke the function(s).
// Note that this is the same as when *not* using server prompt templates.
val functionCalls = response.functionCalls
val fetchWeatherCall = functionCalls.find { it.name == "fetchWeather" }

// Forward the structured input data from the model to the hypothetical external API.
val functionResponse = fetchWeatherCall?.let {
  // Alternatively, if your `Location` class is marked as @Serializable, you can use
  // val location = Json.decodeFromJsonElement(it.args["location"]!!)
  val location = Location(
      it.args["location"]!!.jsonObject["city"]!!.jsonPrimitive.content,
      it.args["location"]!!.jsonObject["state"]!!.jsonPrimitive.content
  )
  val date = it.args["date"]!!.jsonPrimitive.content
  fetchWeather(location, date)
}

Java

For Java, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Web


// ...

// Initialize the Gemini Developer API backend service.
const ai = getAI(app, { backend: new GoogleAIBackend() });

// Create a `TemplateGenerativeModel` instance.
const model = getTemplateGenerativeModel(ai);

// Start a chat session with a template that has functions listed as tools.
const chatSession = model.startChat({
  // Specify your template ID
  templateId: 'my-function-calling-template-v1-0-0',
});

// Send a message that might trigger a function call.
const result = await chatSession.sendMessage(userMessage);

// When the model responds with one or more function calls, invoke the function(s).
// Note that this is the same as when *not* using server prompt templates.
const functionCalls = result.response.functionCalls();
let functionCall;
let functionResult;

if (functionCalls.length > 0) {
  for (const call of functionCalls) {
    if (call.name === "fetchWeather") {
      // Forward the structured input data prepared by the model
      // to the hypothetical external API.
      functionResult = await fetchWeather(call.args);
      functionCall = call;
    }
  }
}

Dart


// ...

// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
final model = FirebaseAI.googleAI().templateGenerativeModel()

// Start a chat session with a template that has functions listed as tools.
var chatSession = model.startChat(
    // Specify your template ID
    'my-function-calling-template-v1-0-0',
);

// Send a message that might trigger a function call.
var response = await chatSession.sendMessage(
    Content.text(userMessage),
);

// Check if the model wants to call a function.
// Note that this is the same as when *not* using server prompt templates.
final functionCalls = response?.functionCalls.toList();
// When the model responds with one or more function calls, invoke the function(s).
if (functionCalls != null && functionCalls.isNotEmpty) {
  for (final functionCall in functionCalls) {
    if (functionCall.name == 'fetchWeather') {
      Map<String, dynamic> location =
          functionCall.args['location']! as Map<String, dynamic>;
      var date = functionCall.args['date']! as String;
      var city = location['city'] as String;
      var state = location['state'] as String;
      final functionResult =
          await fetchWeather(Location(city, state), date);
      // Send the response to the model so that it can use the result to
      // generate text for the user.
      response = await chatSession.sendMessage(
        Content.functionResponse(functionCall.name, functionResult),
      );
    }
  }
}

ঐক্য


// ...

// Initialize the Gemini Developer API backend service.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Create a `TemplateGenerativeModel` instance.
var model = firebaseAI.GetTemplateGenerativeModel();

// Start a chat session with a template that has functions listed as tools.
var chatSession = model.StartChat(
    // Specify your template ID
    "my-function-calling-template-v1-0-0"
);

try
{
   // Send a message that might trigger a function call.
   var response = await chatSession.SendMessageAsync(ModelContent.Text(userMessage));
   var functionResponses = new List();

  // When the model responds with one or more function calls, invoke the function(s).
  // Note that this is the same as when *not* using server prompt templates.
  foreach (var functionCall in response.FunctionCalls) {
    if (functionCall.Name == "fetchWeather") {
      // TODO(developer): Handle invalid arguments.
      var location = functionCall.Args["location"] as Dictionary<string, object>;
      var city = location["city"] as string;
      var state = location["state"] as string;
      var date = functionCall.Args["date"] as string;

      functionResponses.Add(ModelContent.FunctionResponse(
        name: functionCall.Name,
        // Forward the structured input data prepared by the model
        // to the hypothetical external API.
        response: FetchWeather(city: city, state: state, date: date)
      ));
    }
    // TODO(developer): Handle other potential function calls, if any.
  }

  // Send the function responses back to the model.
  var functionResponseResult = await chatSession.SendMessageAsync(functionResponses);
}
catch (Exception e) {
  Debug.LogError($"An error occurred: {e.Message}");
}


ফাংশন কলিং - ক্লায়েন্ট কোডে স্কিমা নির্ধারণ করুন

সার্ভার প্রম্পট টেমপ্লেটের সাথে ফাংশন কলিং কীভাবে কাজ করে (বিশেষ করে, {% verbatim %}{% endverbatim %} এর ব্যবহার) সে সম্পর্কে উপরের বিভাগগুলি পর্যালোচনা করতে ভুলবেন না। {% verbatim %}{% endverbatim %} টেমপ্লেটের কন্টেন্টে {% verbatim %}{% endverbatim %} ট্যাগ)। আপনি যদি আপনার ফাংশন স্কিমা টেমপ্লেটের পরিবর্তে ক্লায়েন্ট কোডে সংজ্ঞায়িত করতে চান, তাহলে এই বিভাগে একটি উদাহরণ টেমপ্লেট এবং ক্লায়েন্ট কোড দেওয়া হয়েছে।

ক্লায়েন্ট কোডে ফাংশন স্কিমা নির্ধারণ করার বিষয়ে নিম্নলিখিত বিষয়গুলো লক্ষ্য করুন:

  • যদি আপনি আপনার ক্লায়েন্ট কোডে কোনো ফাংশনের স্কিমা নির্ধারণ করেন (যেমনটি নিচের উদাহরণে দেখানো হয়েছে), তাহলে ক্লায়েন্ট-সাইড স্কিমাটি সেই ফাংশনের জন্য টেমপ্লেটে নির্ধারিত যেকোনো স্কিমাকে ওভাররাইড করবে

  • ক্লায়েন্ট কোডে ফাংশন স্কিমা সংজ্ঞায়িত করতে, ফাংশন ডিক্লারেশনটি লিখুন এবং তারপর startChat এ সেই ডিক্লারেশনটি প্রদান করুন , মডেল ইনিশিয়ালাইজেশনের সময় নয় (যা আপনি সার্ভার প্রম্পট টেমপ্লেট ব্যবহার না করার সময় করে থাকেন)।

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

ক্লায়েন্ট কোডে ফাংশন স্কিমা সংজ্ঞায়িত সহ উদাহরণ টেমপ্লেট

---
model: gemini-3-flash-preview
tools:
  - name: fetchWeather
    description: Get the weather conditions for a specific city on a specific date.
---

What was the weather like in Boston, Massachusetts on 10/17 in year 2024, formatted in CELSIUS?

{{history}}

ক্লায়েন্ট কোডে ফাংশন স্কিমা সংজ্ঞায়িত সহ উদাহরণ ক্লায়েন্ট কোড।
(এই উদাহরণে বাদ পড়া বিবরণের জন্য, উপরের উদাহরণ ক্লায়েন্ট কোডটি দেখুন)

সুইফট

For Swift, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Kotlin

// ...

// Initialize your desired Gemini API backend service.
// Create a `TemplateGenerativeModel` instance.
...

// Define the schema for any functions listed in your template.
val fetchWeatherTool = functionDeclarations(
    functionDeclarations = listOf(
        FunctionDeclaration(
            name = "fetchWeather",
            description = "Returns the weather for a given location at a given time",
            parameters = mapOf(
                "location" to Schema.obj(
                    description = "The name of the city and its state for which to get the weather. Only cities in the USA are supported.",
                    properties = mapOf(
                        "city" to Schema.string(
                            description = "The city of the location."
                        ),
                        "state" to Schema.string(
                            description = "The state of the location."
                        ),
                        "zipCode" to Schema.string(
                            description = "Optional zip code of the location.",
                            nullable = true
                        )
                    ),
                    optionalProperties = listOf("zipCode")
                ),
                "date" to Schema.string(
                    description = "The date for which to get the weather. Date must be in the format: YYYY-MM-DD."
                ),
                "unit" to Schema.enumeration(
                    description = "The temperature unit.",
                    values = listOf("CELSIUS", "FAHRENHEIT"),
                    nullable = true
                )

            ),
            optionalParameters = listOf("unit"),
        )
    )
)

// Start a chat session with a template that has functions listed as tools.
var chatSessionWithSchemaOverride = model.startChat(
    // Specify your template ID.
    templateId = "my-function-calling-template-with-no-function-schema-v1-0-0",
    // In `startChat`, provide the schema for any functions listed in your template.
    // This client-side schema will override any schema defined in the template.
    tools = listOf(fetchWeatherTool)
)

// Send a message that might trigger a function call.
...

// When the model responds with one or more function calls, invoke the function(s).
// Note that this is the same as when *not* using server prompt templates.
...

// Forward the structured input data from the model to the hypothetical external API.
...

Java

For Java, using server prompt templates with multi-turn interactions is not yet supported. Check back soon!

Web

// ...

// Initialize your desired Gemini API backend service.
...

// Create a `TemplateGenerativeModel` instance.
...

// Start a chat session with a template that has functions listed as tools.
const chatSessionWithSchemaOverride = model.startChat({
  // Specify your template ID.
  templateId: 'my-function-calling-template-with-no-function-schema-v1-0-0',
  // In `startChat`, provide the schema for any functions listed in your template.
  // This client-side schema will override any schema defined in the template.
  tools: [
    {
      functionDeclarations: [
        {
          name: "fetchWeather",
          parameters: {
            type: Type.OBJECT,
            properties: {
              location: {
                type: Type.OBJECT,
                description: "The name of the city and its state for which to get the weather. Only cities in the USA are supported.",
                properties: {
                  city: {
                    type: Type.STRING,
                    description: "The city of the location."
                  },
                  state: {
                    type: Type.STRING,
                    description: "The state of the location."
                  },
                  zipCode: {
                    type: Type.INTEGER,
                    description: "Optional zip code of the location.",
                    nullable: true
                  },
                },
                required: ["city", "state"],
              },
              date: {
                type: Type.STRING,
                description: "The date for which to get the weather. Date must be in the format: YYYY-MM-DD.",
              },
              unit: {
                type: Type.STRING,
                description: "The temperature unit.",
                enum: ["CELSIUS", "FAHRENHEIT"],
                nullable: true,
              },
            },
            required: ["location", "date"],
          },
        },
      ],
    }
  ],
});

// Send a message that might trigger a function call.
...

// When the model responds with one or more function calls, invoke the function(s).
// Note that this is the same as when *not* using server prompt templates.
...

Dart

// ...

// Initialize your desired Gemini API backend service.
// Create a `TemplateGenerativeModel` instance.
...

// Start a chat session with a template that has functions listed as tools.
final chatSessionWithSchemaOverride = model?.startChat(
      // Specify your template ID.
      'my-function-calling-template-with-no-function-schema-v1-0-0',
      inputs: {},
      // In `startChat`, provide the schema for any functions listed in your template.
      // This client-side schema will override any schema defined in the template.
      tools: [
        TemplateTool.functionDeclarations(
          [
            TemplateFunctionDeclaration(
              'fetchWeather',
              parameters: {
                'location': JSONSchema.object(
                  description:
                      'The name of the city and its state for which to get '
                      'the weather. Only cities in the USA are supported.',
                  properties: {
                    'city': JSONSchema.string(
                      description: 'The city of the location.',
                    ),
                    'state': JSONSchema.string(
                      description: 'The state of the location.',
                    ),
                    'zipCode': JSONSchema.integer(
                      description: 'Optional zip code of the location.',
                      nullable: true,
                    ),
                  },
                  optionalProperties: ['zipCode'],
                ),
                'date': JSONSchema.string(
                  description: 'The date for which to get the weather. '
                      'Date must be in the format: YYYY-MM-DD.',
                ),
                'unit': JSONSchema.enumString(
                  enumValues: ['CELSIUS', 'FAHRENHEIT'],
                  description: 'The temperature unit.',
                  nullable: true,
                ),
              },
              optionalParameters: ['unit'],
            ),
          ],
        ),
      ],
    );

// Send a message that might trigger a function call.
...

// Check if the model wants to call a function.
// Note that this is the same as when *not* using server prompt templates.
...

ঐক্য

// ...

// Initialize your desired Gemini API backend service.
...

// Create a `TemplateGenerativeModel` instance.
...

// Define the schema for any functions listed in your template.
var fetchWeatherTool = new TemplateTool.FunctionDeclaration(
    name: "fetchWeather",
    parameters: new Dictionary<string, JsonSchema>() {
        { "location", JsonSchema.Object(
            description: "The name of the city and its state for which to get the weather. Only cities in the USA are supported.",
            properties: new Dictionary<string, JsonSchema>() {
                { "city", JsonSchema.String(description: "The city of the location.") },
                { "state", JsonSchema.String(description: "The state of the location.") },
                { "zipCode", JsonSchema.Int(description: "Optional zip code of the location.", nullable: true) }
            },
            optionalProperties: new[] { "zipCode" })
        },
        { "date", JsonSchema.String(description: "The date for which to get the weather. Date must be in the format: YYYY-MM-DD.")},
        { "unit", JsonSchema.Enum(
            values: new[] { "CELSIUS", "FAHRENHEIT" },
            description: "The temperature unit.",
            nullable: true)
        }
    },
    optionalParameters: new[] { "unit" }
);

// Start a chat session with a template that has functions listed as tools.
var chatSessionWithSchemaOverride = model.StartChat(
    // Specify your template ID.
    templateId: "my-function-calling-template-with-no-function-schema-v1-0-0",
    // In `startChat`, provide the schema for any functions listed in your template.
    // This client-side schema will override any schema defined in the template.
    tools: new[] { fetchWeatherTool }
);

try
{
   // Send a message that might trigger a function call.
   ...

  // When the model responds with one or more function calls, invoke the function(s).
  // Note that this is the same as when *not* using server prompt templates.
  ...
}

// ...



এরপর কী?