यह गाइड, मल्टी-टर्न इंटरैक्शन पर निर्भर करने वाली किसी भी सुविधा पर लागू होती है. इनमें ये शामिल हैं:
मल्टी-टर्न बातचीत (चैट), जिसमें सिर्फ़ टेक्स्ट आउटपुट वाली चैट और बार-बार इमेज में बदलाव करने की सुविधा शामिल है
खास जानकारी
मल्टी-टर्न इंटरैक्शन के लिए, Firebase AI Logic SDK, बातचीत की स्थिति को मैनेज करते हैं. यह बात, सर्वर प्रॉम्प्ट टेंप्लेट का इस्तेमाल करने पर भी लागू होती है.
मल्टी-टर्न इंटरैक्शन और सर्वर प्रॉम्प्ट टेंप्लेट के लिए, बुनियादी वर्कफ़्लो, सिंगल-टर्न अनुरोधों के लिए इस्तेमाल होने वाले वर्कफ़्लो जैसा ही होता है. हालांकि, इन दोनों में कुछ अहम अंतर हैं:
Firebase console में, गाइड की मदद से यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करके टेंप्लेट बनाएं.
मल्टी-टर्न इंटरैक्शन के लिए, आपको टेंप्लेट के कॉन्टेंट में
{{history}}टैग जोड़ना होगा, इससे टेंप्लेट को यह पता चलता है कि क्लाइंट SDK टूल से मैनेज किए जाने वाले बातचीत के टर्न कहां डालने हैं.consoleFirebase में, टेस्टिंग की सुविधा का इस्तेमाल करके, असली अनुरोध में टेंप्लेट की जांच करें.
मल्टी-टर्न इंटरैक्शन के लिए, कंसोल में टेस्टिंग की सुविधा से सिर्फ़ शुरुआती टर्न की जांच की जा सकती है. अपने असली ऐप्लिकेशन के साथ टेंप्लेट का इस्तेमाल करके, यह जांच की जा सकती है कि बाद के टर्न (इतिहास) को कैसे मैनेज किया जाता है.
templateGenerativeModelका इस्तेमाल करके, अपने ऐप्लिकेशन के कोड से टेंप्लेट ऐक्सेस करें.मल्टी-टर्न इंटरैक्शन के लिए, आपको
startChatऔरsendMessageका इस्तेमाल करना होगा. ठीक उसी तरह जैसे सर्वर प्रॉम्प्ट टेंप्लेट का इस्तेमाल न करने पर, मल्टी-टर्न इंटरैक्शन के लिए किया जाता है.
ध्यान दें कि फ़ंक्शन कॉल करने की सुविधा के लिए, कुछ और अंतर भी हैं. इनके बारे में इस पेज पर बाद में बताया गया है.
मल्टी-टर्न बातचीत (चैट)
अगर आपने अभी तक ऐसा नहीं किया है, तो सर्वर प्रॉम्प्ट टेंप्लेट का इस्तेमाल न करने पर, मल्टी-टर्न बातचीत (चैट) बनाने के लिए सामान्य गाइड देखें.
सर्वर प्रॉम्प्ट टेंप्लेट का सामान्य फ़ॉर्मैट
Firebase AI Logic के लिए, Firebase console में गाइड की मदद से यूज़र इंटरफ़ेस (यूआई) उपलब्ध है. इसकी मदद से, टेंप्लेट का फ़्रंटमैटर और कॉन्टेंट तय किया जा सकता है.
सर्वर प्रॉम्प्ट टेंप्लेट, Dotprompt पर आधारित सिंटैक्स और फ़ॉर्मैट का इस्तेमाल करते हैं. ज़्यादा जानकारी के लिए, टेंप्लेट का फ़ॉर्मैट, सिंटैक्स, और उदाहरण देखें.
नीचे दिए गए उदाहरण टेंप्लेट में, मल्टी-टर्न बातचीत (चैट) बनाने के लिए, टेंप्लेट के सबसे अहम कॉम्पोनेंट दिखाए गए हैं. ध्यान दें कि टेंप्लेट के कॉन्टेंट में
{{history}} टैग जोड़ा गया है. इससे टेंप्लेट को यह पता चलता है कि क्लाइंट 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}}
कोड में अपने टेंप्लेट का इस्तेमाल करना
|
इस पेज पर, Gemini API प्रोवाइडर के हिसाब से कॉन्टेंट और कोड देखने के लिए, उस पर क्लिक करें. |
नीचे दिए गए क्लाइंट कोड के उदाहरण में, अपने कोड में टेंप्लेट का इस्तेमाल करने का तरीका बताया गया है. मल्टी-टर्न इंटरैक्शन बनाते समय, startChat और sendMessage के साथ templateGenerativeModel का इस्तेमाल करने के बारे में ध्यान दें.
Swift
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);
Unity
// ...
// 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 console में गाइड की मदद से यूज़र इंटरफ़ेस (यूआई) उपलब्ध है. इसकी मदद से, टेंप्लेट का फ़्रंटमैटर और कॉन्टेंट तय किया जा सकता है.
सर्वर प्रॉम्प्ट टेंप्लेट, Dotprompt पर आधारित सिंटैक्स और फ़ॉर्मैट का इस्तेमाल करते हैं. ज़्यादा जानकारी के लिए, टेंप्लेट का फ़ॉर्मैट, सिंटैक्स, और उदाहरण देखें.
नीचे दिए गए उदाहरण टेंप्लेट में, फ़ंक्शन कॉल करने की सुविधा का इस्तेमाल करते समय, टेंप्लेट के सबसे अहम कॉम्पोनेंट दिखाए गए हैं. इन बातों का ध्यान रखें:
टेंप्लेट के फ़्रंटमैटर में, उन फ़ंक्शन की सूची बनाएं जिनका ऐक्सेस मॉडल के पास है. इसके लिए,
toolsऑब्जेक्ट में फ़ंक्शन के एलान दें.मॉडल के पास जिन फ़ंक्शन का ऐक्सेस है उनके लिए,
name(ज़रूरी है) औरdescription(ज़रूरी नहीं है) तय करें.मॉडल के पास जिन फ़ंक्शन का ऐक्सेस है उनके लिए, स्कीमा तय करें.
नीचे दिए गए टेंप्लेट के उदाहरण में यह माना गया है कि टेंप्लेट में फ़ंक्शन स्कीमा तय किए जा रहे हैं. हालांकि, क्लाइंट कोड में फ़ंक्शन का स्कीमा भी दिया जा सकता है. क्लाइंट कोड में तय किया गया स्कीमा, टेंप्लेट में तय किए गए किसी भी स्कीमा को ओवरराइड कर देगा. इस पेज पर बाद में, क्लाइंट कोड में स्कीमा तय करने के लिए, टेंप्लेट और क्लाइंट कोड का उदाहरण देखें.
टेम्प्लेट के कॉन्टेंट में,
{{history}}टैग जोड़ें. इससे टेम्प्लेट को यह पता चलता है कि क्लाइंट 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}}
कोड में अपने टेंप्लेट का इस्तेमाल करना
|
इस पेज पर, Gemini API प्रोवाइडर के हिसाब से कॉन्टेंट और कोड देखने के लिए, उस पर क्लिक करें. |
नीचे दिए गए क्लाइंट कोड के उदाहरण में, अपने कोड में टेंप्लेट का इस्तेमाल करने का तरीका बताया गया है. इन बातों का ध्यान रखें:
मल्टी-टर्न इंटरैक्शन का इस्तेमाल करते समय,
startChatऔरsendMessageके साथtemplateGenerativeModelका इस्तेमाल करें.अपने क्लाइंट कोड में मॉडल को शुरू करते समय, उन फ़ंक्शन की सूची न बनाएं जिनका ऐक्सेस मॉडल के पास है. इसके बजाय, फ़ंक्शन को टेंप्लेट के फ़्रंटमैटर के
toolsऑब्जेक्ट में शामिल किया जाना चाहिए (ऊपर देखें).नीचे दिए गए क्लाइंट कोड के उदाहरण में यह माना गया है कि टेंप्लेट में फ़ंक्शन स्कीमा तय किए जा रहे हैं. अगर आपने क्लाइंट कोड में स्कीमा तय करने का फ़ैसला किया है, तो यह टेंप्लेट में तय किए गए स्कीमा को ओवरराइड कर देगा. इस पेज पर बाद में, क्लाइंट कोड में स्कीमा तय करने के लिए, टेंप्लेट और क्लाइंट कोड का उदाहरण देखें.
यह देखें कि मॉडल, अनुरोध को पूरा करने के दौरान, फ़ंक्शन कॉल दिखाता है या नहीं. अगर ऐसा होता है, तो आपके ऐप्लिकेशन को स्थानीय लॉजिक को लागू करना होगा. इसके बाद, नतीजे को मॉडल पर वापस भेजना होगा.
टेंप्लेट में तय किए गए फ़ंक्शन स्कीमा वाला क्लाइंट कोड का उदाहरण
Swift
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),
);
}
}
}
Unity
// ...
// 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}");
}
फ़ंक्शन कॉल करने की सुविधा - क्लाइंट कोड में स्कीमा तय करना
सर्वर प्रॉम्प्ट टेंप्लेट के साथ फ़ंक्शन कॉल करने की सुविधा के काम करने के तरीके के बारे में ऊपर दिए गए सेक्शन ज़रूर देखें. खास तौर पर, टेंप्लेट के कॉन्टेंट में
{{history}} टैग का इस्तेमाल करने के बारे इस सेक्शन में, टेंप्लेट और क्लाइंट कोड का उदाहरण दिया गया है. इसका इस्तेमाल, क्लाइंट कोड में फ़ंक्शन स्कीमा तय करने के लिए किया जा सकता है. इसके लिए, टेंप्लेट में स्कीमा तय करने की ज़रूरत नहीं होती.
क्लाइंट कोड में फ़ंक्शन स्कीमा तय करने के बारे में इन बातों का ध्यान रखें:
अगर क्लाइंट कोड में किसी फ़ंक्शन का स्कीमा तय किया जाता है (जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है), तो क्लाइंट-साइड स्कीमा, उस फ़ंक्शन के लिए टेंप्लेट में तय किए गए किसी भी स्कीमा को ओवरराइड कर देगा.
क्लाइंट कोड में फ़ंक्शन स्कीमा तय करने के लिए, फ़ंक्शन का एलान लिखें और फिर मॉडल को शुरू करते समय नहीं, बल्कि
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}}
क्लाइंट कोड में तय किए गए फ़ंक्शन स्कीमा वाला क्लाइंट कोड का उदाहरण
(इस उदाहरण में शामिल न की गई जानकारी के लिए, ऊपर दिया गया
क्लाइंट कोड का उदाहरण देखें)
Swift
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.
...
Unity
// ...
// 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.
...
}
// ...
आगे क्या करना है?
सर्वर प्रॉम्प्ट टेंप्लेट का इस्तेमाल करने के सबसे सही तरीकों और ज़रूरी बातों के बारे में जानें.
टेंप्लेट के फ़ॉर्मैट और सिंटैक्स के बारे में ज़्यादा जानें. साथ ही, उदाहरण देखें.
टेंप्लेट मैनेज करें. इनमें, टेंप्लेट में बदलाव करना, उन्हें लॉक करना, और वर्शन कंट्रोल करना शामिल है.