في كل طلب ترسله إلى نموذج، يمكنك إرسال طلب، بالإضافة إلى مخطط وإعدادات اختيارية للتحكّم في استجابة النموذج. عند استخدام Firebase AI Logic، يمكنك إرسال كل هذه المعلومات مباشرةً من رمز العميل، أو يمكنك تحديد هذه المعلومات من جهة الخادم باستخدام نماذج طلبات الخادم.
عند استخدام نماذج طلبات الخادم، يمكنك تخزين الطلب والمخطط والإعدادات من جهة الخادم، ولا يمرّر تطبيقك من العميل إلى الخادم سوى المفتاح (معرّف النموذج) الذي يشير إلى نموذج معيّن، بالإضافة إلى الإدخالات المطلوبة لهذا النموذج.
عند استخدام نماذج طلبات الخادم، يمكنك تخزين الطلب والإعدادات من جهة الخادم، ولا تقدّم سوى مفتاح (معرّف النموذج) في قاعدة رموز تطبيقك. في ما يلي بعض مزايا هذا النهج:
الحماية من عرض طلبك من جهة العميل
تعديل الطلب والإعدادات بدون إصدار إصدار جديد من التطبيق
يوضّح هذا الدليل كيفية البدء باستخدام نماذج طلبات الخادم.
الانتقال إلى نظرة عامة على مستوى عالٍ الانتقال إلى التعليمات التفصيلية
النماذج والإمكانات المتوافقة
يمكنك استخدام نماذج طلبات الخادم مع أي من Gemini وImagen النماذج التي تتيحها Firebase AI Logic، باستثناء نماذج Gemini Live.
نظرة عامة على مستوى عالٍ
في ما يلي سير العمل الأساسي لاستخدام نماذج طلبات الخادم:
أنشئ النموذج باستخدام واجهة المستخدم الإرشادية في الـ Firebase Console.
اختبِر النموذج في طلب حقيقي باستخدام تجربة الاختبار في الـ Firebase Console.
يمكنك الوصول إلى النموذج من رمز تطبيقك باستخدام
templateGenerativeModel.
التنسيق الأساسي لنموذج طلب خادم
بالنسبة إلى Firebase AI Logic، يوفّر Firebase Console واجهة مستخدم إرشادية لتحديد بيانات النموذج الأولية ومحتوياته.
تستخدِم نماذج طلبات الخادم بنية وتنسيقًا يستندان إلى Dotprompt. لمزيد من التفاصيل، يُرجى الاطّلاع على تنسيق النموذج وبنيته وأمثلة عليه.
يوضّح نموذج الطلب أدناه أهم مكوّنات نموذج لطلب مثال إلى نموذج Gemini:
---
model: 'gemini-3.5-flash'
---
{{role "system"}}
All output must be a clearly structured invoice document.
Use a tabular or clearly delineated list format for line items.
{{role "user"}}
Create an example customer invoice for a customer named {{customerName}}.
يحتوي القسم العلوي ضمن الشرطات الثلاثية على اسم النموذج، بالإضافة إلى أي إعدادات للنموذج أو عملية التحقق من صحة الإدخال أو مخطط تريد إرساله في الطلب (اختياري). تُكتب هذه المعلومات في صورة أزواج مفاتيح/قيم، ويُطلق عليها عادةً بيانات أولية بتنسيق YAML.
يحتوي نص النموذج على الطلب. ويمكن أن يتضمّن أيضًا تعليمات النظام وقيم الإدخال (باستخدام بنية Handlebars).
استخدام النموذج في الرمز
|
انقر على موفّر Gemini API لعرض المحتوى الخاصّ بالموفّر والرمز على هذه الصفحة. |
يوضّح رمز العميل المثال أدناه كيفية استخدام النموذج في الرمز:
Swift
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .vertexAI(location: "global")).templateGenerativeModel()
let customerName = "Jane"
do {
let response = try await model.generateContent(
// Specify your template ID
templateID: "my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
inputs: [
"customerName": customerName
]
)
if let text = response.text {
print("Response Text: \(text)")
}
} catch {
print("An error occurred: \(error)")
}
print("\n")
Kotlin
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.vertexAI(location = "global")).templateGenerativeModel()
val customerName = "Jane"
val response = model.generateContent(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
mapOf(
"customerName" to customerName
)
)
val text = response.text
println(text)
Java
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
TemplateGenerativeModel generativeModel = FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")).templateGenerativeModel();
TemplateGenerativeModelFutures model = TemplateGenerativeModelFutures.from(generativeModel);
String customerName = "Jane";
Future response = model.generateContent(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
mapOf("customerName", customerName)
);
addCallback(response,
new FutureCallback() {
public void onSuccess(GenerateContentResponse result) {
System.out.println(result.getText());
}
public void onFailure(Throwable t) {
reportError(t);
}
}
executor);
Web
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
const ai = getAI(app, { backend: new VertexAIBackend('global') });
// Create a `TemplateGenerativeModel` instance
const model = getTemplateGenerativeModel(ai);
const customerName = 'Jane';
const result = await model.generateContent(
// Specify your template ID
'my-first-template-v1-0-0',
// Provide the values for any input variables required by your template
{
customerName: customerName,
}
);
const response = result.response;
const text = response.text();
Dart
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
var _model = FirebaseAI.vertexAI(location: 'global').templateGenerativeModel()
var customerName = 'Jane';
var response = await _model.generateContent(
// Specify your template ID
'my-first-template-v1-0-0',
// Provide the values for any input variables required by your template
inputs: {
'customerName': customerName,
},
);
var text = response?.text;
print(text);
Unity
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.VertexAI(location: "global"));
// Create a `TemplateGenerativeModel` instance
var model = firebaseAI.GetTemplateGenerativeModel();
var customerName = "Jane";
try
{
var response = await model.GenerateContentAsync(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template
new Dictionary<string, object> {
{ "customerName", customerName },
}
);
Debug.Log($"Response Text: {response.Text}");
}
catch (Exception e) {
Debug.LogError($"An error occurred: {e.Message}");
}
تعليمات تفصيلية
يقدّم هذا القسم تعليمات تفصيلية لإنشاء نماذج طلبات الخادم واختبارها واستخدامها.
قبل البدء
إذا لم يسبق لك إجراء ذلك، أكمِل دليل البدء الذي يوضّح كيفية إعداد مشروعك على Firebase وربط تطبيقك بـ Firebase و إضافة حزمة تطوير البرامج (SDK) وتهيئة خدمة الخلفية لموفّر Gemini API الذي اخترته وإنشاء مثال
GenerativeModel.تأكَّد من حصولك على الأذونات المطلوبة لإنشاء نماذج طلبات الخادم وإدارتها. تتضمّن جميع هذه الأذونات دور "المالك" تلقائيًا.
إذا كنت تستخدِم Vertex AI Gemini API و إذا كانت حالة الاستخدام تتطلّب قيودًا تستند إلى الموقع الجغرافي، نتيح لك سير عمل متقدّمًا للنماذج.
الخطوة 1: إنشاء نموذج طلب خادم
في معظم حالات الاستخدام، يمكنك إنشاء نماذج طلبات الخادم وإدارتها في الـ Firebase Console.
في Firebase Console، انتقِل إلى خدمات الذكاء الاصطناعي > Firebase AI Logic > علامة التبويب نماذج الطلبات.
انقر على إنشاء نموذج جديد، واختَر نموذجًا أوليًا.
توفّر هذه النماذج الأولية التنسيق والبنية لبعض حالات الاستخدام الشائعة. ومع ذلك، بغض النظر عن الخيار الذي تحدّده، يمكنك تغيير النموذج بالكامل ليناسب احتياجاتك.
يفترض دليل البدء هذا أنّك اخترت الخيار
Input + System Instructions.
أدخِل معرّفات النموذج:
اسم النموذج: هذا هو اسم العرض للنموذج (على سبيل المثال،
My First Template). ولا يظهر إلا في واجهات Firebase، مثل وحدة تحكّم Firebase.معرّف النموذج: يجب أن يكون هذا المعرّف فريدًا للنموذج ضمن مشروعك على Firebase (على سبيل المثال،
). ستشير إلى هذا المعرّف في الطلب من تطبيقك.my-first-template-v1-0-0يمكن أن يصل طول معرّفات النماذج إلى 63 حرفًا، ويمكن أن تحتوي على أحرف صغيرة وأرقام وواصلات.
عدِّل قسم الإعدادات (البيانات الأولية) في النموذج حسب الحاجة.
يجب أن يتضمّن هذا القسم اسم نموذج على الأقل، على النحو التالي:
--- model: 'gemini-3.5-flash' ---يمكنك أيضًا تحديد إعدادات النموذج وأي عناصر تحكّم في الإدخال و& الإخراج وما إلى ذلك (اختياري). لمزيد من التفاصيل والخيارات، يُرجى الاطّلاع على تنسيق النموذج وبنيته وأمثلة عليه.
عدِّل قسم الطلب وتعليمات النظام (حسب الاقتضاء) في النموذج حسب الحاجة.
يجب أن يتضمّن هذا القسم نص الطلب على الأقل لإرساله إلى النموذج.
Write a story about a magic backpack.يمكنك أيضًا إنشاء طلبات أكثر تعقيدًا، مثل الخيارات التالية. لمزيد من التفاصيل والخيارات، يُرجى الاطّلاع على تنسيق النموذج وبنيته وأمثلة عليه.
(اختياري وحسب الاقتضاء) حدِّد تعليمات النظام باستخدام بنية
{{role "system"}}، ونص الطلب باستخدام بنية{{role "user"}}.(اختياري) حدِّد متغيّرات الإدخال باستخدام بنية Handlebars (مثل
{{customerName}}). يمكنك تقديم قيمة تلقائية في النموذج، ولكن عادةً ما يتم تمرير قيمة متغيّر الإدخال هذا في الطلب.
{{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer named {{customerName}}.
الخطوة 2: اختبار النموذج في الـ Firebase Console
يوفّر Firebase Console تجربة اختبار لنموذجك. تتيح لك هذه التجربة الاطّلاع على ما سيحدث عند استخدام النموذج، أي تنسيق الطلب واستجابة الطلب الفعلي.
انقر على حفظ النموذج لتتمكّن من اختباره.
يمكنك تعديل النموذج أو حتى حذفه في أي وقت لاحق. القيمة الوحيدة التي لا يمكنك تغييرها لاحقًا هي معرّف النموذج.
إذا كان طلبك يستخدم متغيّرات إدخال، أدرِج قيم الاختبار في حقل اختبار الإدخال. في هذا المثال:
{ "customerName": "Jane" }إذا كان لديك عدة موفّري Gemini API مفعّلين في مشروعك على Firebase ، يمكنك اختيار الموفّر الذي تريد استخدامه لطلب الاختبار. إذا ظهر هذا الخيار في وحدة التحكّم، اختَر
Gemini Developer APIأوVertex AI Gemini API.يُرجى العِلم أنّ هذا الاختيار لا ينطبق إلا على الطلبات المُرسَلة من خلال الـ Firebase تجربة الاختبار. في الطلب الفعلي من تطبيقك، يمكنك تحديد موفّر Gemini API الذي اخترته تمامًا كما تفعل مع أي طلب آخر.
انقر على الزر إنشاء طلب اختبار منسَّق.
راجِع طلب الاختبار المنسَّق الناتج في الجانب الأيسر من الشاشة، وعدِّل أيًا من حقول النموذج.
بعد التأكّد من أنّ طلب الاختبار المنسَّق مناسب، انقر على الزر تشغيل اختبار الطلب.
راجِع استجابة الاختبار الناتجة في الجانب الأيسر من الشاشة، وعدِّل أيًا من حقول النموذج.
إذا كنت مستعدًا للوصول إلى النموذج من رمز تطبيقك، يمكنك قفله من خلال النقر على
رمز القفل في أعلى يسار النموذج.انقر على إغلاق للخروج من تجربة التعديل.
الخطوة 3: الوصول إلى النموذج من الرمز
|
انقر على موفّر Gemini API لعرض المحتوى الخاصّ بالموفّر والرمز على هذه الصفحة. |
يبدو الطلب الذي يستخدم نموذج طلب خادم مشابهًا للطلبات الأخرى، مع إجراء التعديلات التالية:
- استخدِم
templateGenerativeModel(أوtemplateImagenModelحسب الحاجة). - قدِّم معرّف النموذج.
- قدِّم قيم أي إدخالات متغيّرة يتطلبها النموذج.
يُرجى العِلم أنّه بعد إنشاء النموذج أو تعديله، قد تحتاج إلى الانتظار بضع دقائق حتى يتم نشره على خوادم Firebase قبل الوصول إليه من الرمز.
Swift
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .vertexAI(location: "global")).templateGenerativeModel()
let customerName = "Jane"
do {
let response = try await model.generateContent(
// Specify your template ID
templateID: "my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
inputs: [
"customerName": customerName
]
)
if let text = response.text {
print("Response Text: \(text)")
}
} catch {
print("An error occurred: \(error)")
}
print("\n")
Kotlin
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.vertexAI(location = "global")).templateGenerativeModel()
val customerName = "Jane"
val response = model.generateContent(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
mapOf(
"customerName" to customerName
)
)
val text = response.text
println(text)
Java
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
TemplateGenerativeModel generativeModel = FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")).templateGenerativeModel();
TemplateGenerativeModelFutures model = TemplateGenerativeModelFutures.from(generativeModel);
String customerName = "Jane";
Future response = model.generateContent(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template.
mapOf("customerName", customerName)
);
addCallback(response,
new FutureCallback() {
public void onSuccess(GenerateContentResponse result) {
System.out.println(result.getText());
}
public void onFailure(Throwable t) {
reportError(t);
}
}
executor);
Web
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
const ai = getAI(app, { backend: new VertexAIBackend('global') });
// Create a `TemplateGenerativeModel` instance
const model = getTemplateGenerativeModel(ai);
const customerName = 'Jane';
const result = await model.generateContent(
// Specify your template ID
'my-first-template-v1-0-0',
// Provide the values for any input variables required by your template
{
customerName: customerName,
}
);
const response = result.response;
const text = response.text();
Dart
ستتيح إضافة Flutter نماذج طلبات الخادم قريبًا.
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
// Create a `TemplateGenerativeModel` instance.
var _model = FirebaseAI.vertexAI(location: 'global').templateGenerativeModel()
var customerName = 'Jane';
var response = await _model.generateContent(
// Specify your template ID
'my-first-template-v1-0-0',
// Provide the values for any input variables required by your template
inputs: {
'customerName': customerName,
},
);
var text = response?.text;
print(text);
Unity
أنشئ مثالاً templateGenerativeModel (أو templateImagenModel) لاستخدام نموذج في طلبك.
// ...
// Initialize the Vertex AI Gemini API backend service.
// Specify `global` as the location to access the model.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.VertexAI(location: "global"));
// Create a `TemplateGenerativeModel` instance
var model = firebaseAI.GetTemplateGenerativeModel();
var customerName = "Jane";
try
{
var response = await model.GenerateContentAsync(
// Specify your template ID
"my-first-template-v1-0-0",
// Provide the values for any input variables required by your template
new Dictionary<string, object> {
{ "customerName", customerName },
}
);
Debug.Log($"Response Text: {response.Text}");
}
catch (Exception e) {
Debug.LogError($"An error occurred: {e.Message}");
}
ما هي الخطوات التالية؟
تعرَّف على أفضل الممارسات والاعتبارات لاستخدام نماذج طلبات الخادم.
تعلَّم عن إدارة النماذج، بما في ذلك التعديل والقفل والتحكّم في الإصدارات.
تعرَّف على سير العمل المتقدّم، مثل استخدام النماذج آليًا باستخدام واجهة برمجة تطبيقات REST أو نشر النماذج في مواقع جغرافية معيّنة.