ในคำขอแต่ละรายการที่ส่งไปยังโมเดล คุณจะส่งพรอมต์ รวมถึงสคีมาและการกำหนดค่า (ไม่บังคับ) เพื่อควบคุมการตอบสนองของโมเดล เมื่อใช้ Firebase AI Logic คุณจะส่งข้อมูลทั้งหมดนี้จาก โค้ดฝั่งไคลเอ็นต์ได้โดยตรง หรือจะระบุข้อมูลนี้ฝั่งเซิร์ฟเวอร์โดยใช้ เทมเพลตพรอมต์ของเซิร์ฟเวอร์ก็ได้
เมื่อใช้เทมเพลตพรอมต์ของเซิร์ฟเวอร์ คุณจะจัดเก็บพรอมต์ สคีมา และการกำหนดค่าไว้ฝั่งเซิร์ฟเวอร์ และฐานของโค้ดของแอปจะส่งจากไคลเอ็นต์ไปยังเซิร์ฟเวอร์เฉพาะคีย์ (รหัสเทมเพลต) ที่อ้างอิงเทมเพลตที่เฉพาะเจาะจง รวมถึงอินพุตที่จำเป็นสำหรับเทมเพลตนั้น แนวทางนี้มีประโยชน์ดังนี้
ป้องกันการเปิดเผยพรอมต์ฝั่งไคลเอ็นต์
อัปเดตพรอมต์และการกำหนดค่าโดยไม่ต้องเผยแพร่แอปเวอร์ชันใหม่
คู่มือนี้จะอธิบายวิธีเริ่มต้นใช้งานเทมเพลตพรอมต์ของเซิร์ฟเวอร์
ข้ามไปที่ภาพรวมระดับสูง ข้ามไปที่วิธีการโดยละเอียด
โมเดลและความสามารถที่รองรับ
ใช้เทมเพลตพรอมต์ของเซิร์ฟเวอร์กับโมเดล Geminiใดก็ได้ที่Firebase AI Logic รองรับ ยกเว้นโมเดลGemini Live API
ภาพรวมระดับสูง
เวิร์กโฟลว์พื้นฐานสำหรับการใช้เทมเพลตพรอมต์ของเซิร์ฟเวอร์มีดังนี้
สร้างเทมเพลตโดยใช้ UI ที่แนะนำใน Firebase คอนโซล
ทดสอบเทมเพลตในคำขอจริงโดยใช้ประสบการณ์การทดสอบใน Firebase คอนโซล
เข้าถึงเทมเพลตจากโค้ดของแอปโดยใช้
templateGenerativeModel
รูปแบบพื้นฐานของเทมเพลตพรอมต์ของเซิร์ฟเวอร์
สำหรับ Firebase AI Logic Firebaseคอนโซล จะมี UI ที่แนะนำเพื่อให้คุณระบุส่วนหัวและเนื้อหาของเทมเพลต
เทมเพลตพรอมต์ของเซิร์ฟเวอร์ใช้ไวยากรณ์และรูปแบบที่อิงตาม Dotprompt ดูรายละเอียดเพิ่มเติมได้ที่ รูปแบบ ไวยากรณ์ และตัวอย่างของเทมเพลต
เทมเพลตตัวอย่างด้านล่างแสดงคอมโพเนนต์ที่สำคัญที่สุดของเทมเพลตสำหรับ คำขอตัวอย่างที่ส่งไปยังโมเดล Gemini:
---
model: 'gemini-3.8-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}}.
ส่วนบนสุดภายในขีดกลาง 3 ขีดจะมีชื่อโมเดล รวมถึงการกำหนดค่าโมเดล การตรวจสอบอินพุต หรือสคีมาที่คุณต้องการส่งในคำขอ (ไม่บังคับ) โดยจะเขียนเป็นคู่คีย์-ค่าและมักเรียกว่า ส่วนหัว YAML
เนื้อหาของเทมเพลตจะมีพรอมต์ นอกจากนี้ยังอาจมี คำแนะนำของระบบและค่าอินพุต (ใช้ ไวยากรณ์ Handlebars) (ไม่บังคับ)
ใช้เทมเพลตในโค้ด
|
คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาเฉพาะของผู้ให้บริการ และโค้ดในหน้านี้ |
โค้ดไคลเอ็นต์ตัวอย่างด้านล่างแสดงวิธีใช้เทมเพลตในโค้ด
Swift
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).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 Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
TemplateGenerativeModel generativeModel = FirebaseAI.getInstance().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 Gemini Developer API backend service.
const ai = getAI(app, { backend: new GoogleAIBackend() });
// Create a `TemplateGenerativeModel` instance.
const model = getTemplateGenerativeModel(ai);
const customerName = 'Jane';
const result = 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.
templateVariables: {
customerName: customerName,
},
});
const response = result.response;
const text = response.text();
Dart
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
var _model = FirebaseAI.googleAI().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 Gemini Developer API backend service.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// 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ตรวจสอบว่าคุณมีสิทธิ์ที่จำเป็น ในการสร้างและจัดการเทมเพลตพรอมต์ของเซิร์ฟเวอร์ โดยค่าเริ่มต้น สิทธิ์ทั้งหมดนี้จะรวมอยู่ในบทบาทเจ้าของ
หากคุณใช้ Agent Platform Gemini API (formerly Vertex AI) และ กรณีการใช้งานของคุณต้องมี ข้อจำกัดตามสถานที่ตั้ง เราจะรองรับ เวิร์กโฟลว์ขั้นสูงสำหรับเทมเพลต
ขั้นตอนที่ 1: สร้างเทมเพลตพรอมต์ของเซิร์ฟเวอร์
สำหรับกรณีการใช้งานส่วนใหญ่ คุณจะสร้างและจัดการเทมเพลตพรอมต์ของเซิร์ฟเวอร์ใน Firebase คอนโซล
ในคอนโซล Firebase ให้ไปที่แท็บ AI Services > AI Logic > Prompt templates
คลิกสร้างเทมเพลตใหม่ แล้วเลือกตัวเลือกเทมเพลตเริ่มต้น
เทมเพลตเริ่มต้นเหล่านี้มีรูปแบบและไวยากรณ์สำหรับกรณีการใช้งานทั่วไปบางอย่าง ไม่ว่าคุณจะเลือกตัวเลือกใดก็ตาม คุณสามารถเปลี่ยนเทมเพลตได้อย่างสมบูรณ์เพื่อให้ตรงกับความต้องการ
คู่มือเริ่มต้นใช้งานนี้จะถือว่าคุณเลือกตัวเลือก
Input + System Instructions
ป้อนตัวระบุของเทมเพลต
ชื่อเทมเพลต: นี่คือชื่อที่แสดงของเทมเพลต (เช่น
My First Template) ซึ่งจะแสดงเฉพาะในอินเทอร์เฟซ Firebase เช่น คอนโซล Firebaseรหัสเทมเพลต: รหัสนี้ต้องเป็นรหัสที่ไม่ซ้ำกันสำหรับเทมเพลตภายในโปรเจ็กต์ Firebase (เช่น
) คุณจะอ้างอิง รหัสนี้ในคำขอจากแอปmy-first-template-v1-0-0เราขอแนะนำให้ ใช้ระบบควบคุมเวอร์ชัน สำหรับรหัสเทมเพลต
รหัสเทมเพลตมีความยาวได้สูงสุด 63 อักขระ และมีตัวอักษรพิมพ์เล็ก ตัวเลข และขีดกลางได้
แก้ไขส่วนการกำหนดค่า (ส่วนหัว) ของเทมเพลตตามที่จำเป็น
ส่วนนี้ต้องมีชื่อโมเดลอย่างน้อย 1 ชื่อ เช่น
--- model: 'gemini-3.8-flash' ---นอกจากนี้ คุณยังระบุการกำหนดค่าของโมเดล รวมถึงการควบคุมอินพุต & เอาต์พุต ฯลฯ ได้ด้วย (ไม่บังคับ) ดูรายละเอียดและตัวเลือกเพิ่มเติมได้ที่ รูปแบบ ไวยากรณ์ และตัวอย่างของเทมเพลต
แก้ไขส่วนพรอมต์และ (หากมี) คำแนะนำของระบบ ของเทมเพลตตามที่จำเป็น
ส่วนนี้ต้องมีพรอมต์ข้อความอย่างน้อย 1 รายการเพื่อส่งไปยังโมเดล
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
คอนโซล Firebase มีประสบการณ์การทดสอบสำหรับเทมเพลต ประสบการณ์นี้ช่วยให้คุณเห็นสิ่งที่เกิดขึ้นเมื่อใช้เทมเพลต ทั้งรูปแบบของคำขอและเอาต์พุตของคำขอจริง
คลิกบันทึกเทมเพลต เพื่อให้คุณทดสอบเทมเพลตได้
คุณสามารถแก้ไขหรือแม้แต่ลบเทมเพลตได้ทุกเมื่อ ค่าเดียวที่คุณเปลี่ยนไม่ได้ในภายหลังคือรหัสเทมเพลต
หากพรอมต์ใช้ตัวแปรอินพุต ให้ใส่ค่าทดสอบในช่องทดสอบอินพุต เช่น
{ "customerName": "Jane" }หากคุณเปิดใช้ผู้ให้บริการ Gemini API หลายรายในโปรเจ็กต์ Firebase คุณสามารถเลือกผู้ให้บริการที่จะใช้สำหรับคำขอทดสอบได้ หากตัวเลือกนี้แสดงในคอนโซล ให้เลือก
Gemini Developer APIหรือAgent Platform Gemini API(เดิมชื่อ Vertex AI)โปรดทราบว่าการเลือกนี้ใช้ได้กับคำขอที่ส่งผ่าน Firebaseประสบการณ์การทดสอบของคอนโซล ในคำขอจริงจากแอป คุณจะระบุผู้ให้บริการ Gemini API ที่เลือกไว้เช่นเดียวกับคำขออื่นๆ
คลิกปุ่มสร้างคำขอทดสอบที่จัดรูปแบบแล้ว
ตรวจสอบ คำขอทดสอบที่จัดรูปแบบแล้ว ที่ได้ในด้านขวาของหน้าจอ และทำซ้ำในช่องใดก็ได้ของเทมเพลต
เมื่อพอใจกับคำขอทดสอบที่จัดรูปแบบแล้ว ให้คลิกปุ่มเรียกใช้การทดสอบพรอมต์
ตรวจสอบ การตอบกลับทดสอบ ที่ได้ในด้านขวาของหน้าจอ และทำซ้ำในช่องใดก็ได้ของเทมเพลต
หากพร้อมที่จะเข้าถึงเทมเพลตจากโค้ดของแอป ล็อกเทมเพลต โดยคลิกไอคอน
ล็อก ที่มุมขวาบนของเทมเพลตคลิกปิด เพื่อออกจากประสบการณ์การแก้ไข
ขั้นตอนที่ 3: เข้าถึงเทมเพลตจากโค้ด
|
คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาเฉพาะของผู้ให้บริการ และโค้ดในหน้านี้ |
คำขอที่ใช้เทมเพลตพรอมต์ของเซิร์ฟเวอร์จะมีลักษณะคล้ายกับคำขออื่นๆ โดยมีการปรับเปลี่ยนดังนี้
- ใช้
templateGenerativeModel - ระบุรหัสเทมเพลต
- ระบุค่าของอินพุตตัวแปรที่เทมเพลตกำหนด
โปรดทราบว่าหลังจากสร้างหรืออัปเดตเทมเพลต คุณอาจต้องรอ 2-3 นาทีก่อนที่เทมเพลตจะเผยแพร่ไปยังเซิร์ฟเวอร์ Firebase ทั้งหมดก่อนที่จะเข้าถึงเทมเพลตจากโค้ด
Swift
สร้างอินสแตนซ์ templateGenerativeModel เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).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 เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
TemplateGenerativeModel generativeModel = FirebaseAI.getInstance().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 เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
const ai = getAI(app, { backend: new GoogleAIBackend() });
// Create a `TemplateGenerativeModel` instance.
const model = getTemplateGenerativeModel(ai);
const customerName = 'Jane';
const result = 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.
templateVariables: {
customerName: customerName,
},
});
const response = result.response;
const text = response.text();
Dart
ปลั๊กอิน Flutter จะรองรับเทมเพลตพรอมต์ของเซิร์ฟเวอร์ในเร็วๆ นี้
สร้างอินสแตนซ์ templateGenerativeModel เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
// Create a `TemplateGenerativeModel` instance.
var _model = FirebaseAI.googleAI().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 เพื่อใช้เทมเพลตในคำขอ
// ...
// Initialize the Gemini Developer API backend service.
var firebaseAI = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// 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 API หรือการทำให้ เทมเพลตใช้งานได้ในสถานที่ตั้งที่เฉพาะเจาะจง