1. ก่อนเริ่มต้น
การพัฒนาซอฟต์แวร์มีการเปลี่ยนแปลงไป นักพัฒนาซอฟต์แวร์ที่ประสบความสำเร็จมากที่สุดไม่ได้เขียนโค้ดเพียงอย่างเดียว แต่ยังจัดการเป็นกลุ่มเครื่องมือ AI และ Agent เพื่อสร้างโค้ดที่ดีขึ้นและเร็วขึ้นด้วย ระบบนิเวศเครื่องมือของ Google มีชุดเครื่องมือที่ครอบคลุมตลอดวงจรการพัฒนาทั้งหมด ไม่ว่าจะเป็นการสร้าง UI, การสร้างต้นแบบและการติดตั้งใช้งานแอป, การพัฒนาฟีเจอร์ AI รวมถึงการผสานรวมแบ็กเอนด์ที่ปลอดภัยและปรับขนาดได้ ใน Codelab นี้ คุณจะใช้ Google Stitch เพื่อออกแบบ UI, Google AI Studio เพื่อ Vibe Coding และติดตั้งใช้งานแอป และ Firebase เพื่อพัฒนาฟีเจอร์ AI และจัดเก็บข้อมูลในแบ็กเอนด์ที่ปลอดภัยและรองรับการปรับขนาดได้
แอปที่คุณจะสร้างใน Codelab นี้ชื่อว่า Decide For Me ซึ่งเป็นผู้ช่วยในการตัดสินใจสำหรับผู้ที่ลังเลเมื่อต้องเลือกทางเลือกในชีวิต (เช่น การย้ายอพาร์ตเมนต์ การเปลี่ยนอาชีพ หรือการเลือกงานอดิเรก) ผู้ใช้ป้อนปัญหาที่กลืนไม่เข้าคายไม่ออกและลำดับความสำคัญในชีวิต จากนั้นแอปจะใช้ AI เพื่อช่วยผู้ใช้ตัดสินใจโดยอิงตามสิ่งที่ลดความผิดหวังในระยะยาว นอกจากนี้ แอปยังให้ขั้นตอนแรกที่เป็นรูปธรรมแก่ผู้ใช้ด้วย แอปเวอร์ชันสุดท้ายควรมีลักษณะดังนี้

สิ่งที่คุณจะได้เรียนรู้
- ออกแบบ UI ด้วย Google Stitch
- Vibe Code เว็บแอปด้วย Google AI Studio
- ใช้ Firebase เพื่อพัฒนาฟีเจอร์ AI และจัดเก็บข้อมูลในระบบคลาวด์
- ทำให้เว็บแอปใช้งานได้จริงด้วย Google AI Studio (ขับเคลื่อนโดย Cloud Run)
2. ออกแบบ UI ด้วย Stitch
- ดาวน์โหลดรูปภาพจำลอง (
DecideForMeScreen.jpeg) บันทึกไว้ในโฟลเดอร์ในเครื่องที่คุณจะค้นหาได้ - ไปที่ Stitch ค้นหาช่องป้อนข้อมูล แล้วเลือกตัวเลือกเว็บ (สำหรับ Codelab นี้ คุณจะมุ่งเน้นที่ประสบการณ์การใช้งานบนเว็บ แต่คุณยังใช้ Stitch เพื่อสร้างหน้าจอมือถือได้ด้วย)
- อัปโหลดรูปภาพจำลองโดยทำดังนี้
- คลิก + (ปุ่มเพิ่ม)
- เลือกอัปโหลดไฟล์
- แนบรูปภาพที่คุณดาวน์โหลดในขั้นตอนการตั้งค่า (
DecideForMeScreen.jpeg) ภาพจำลองที่วาดด้วยมือนี้จะช่วยให้คุณทดสอบความสามารถแบบหลายรูปแบบของ Stitch ได้
- คัดลอกพรอมต์ต่อไปนี้แล้ววางลงในช่องป้อนข้อมูล
# General Guidance Create a modern, clean, and highly polished UI for a web application called "Decide For Me" - a hyper-rational decision assistant. CRITICAL: Replicate the exact layout, structure, and element placement shown in the hand-drawn mockup file DecideForMeScreen.jpeg. Do not add any extra menus, sidebars, buttons, or top/bottom navigation bars that are not explicitly drawn in the file. Stick strictly to the skeleton provided in the mockup. # Layout & Structure - Two-Column Split Screen: - Left Side (Main Chat Area): Takes up roughly 70-75% of the screen width. It should feel like a premium, focused workspace. - Right Side (Configuration Panel): A sidebar taking up 25-30% of the screen width with a distinct, subtle background color to separate it from the chat. # Right Panel: Configuration Screen - Header: Title it "My Priorities" with a clean, minimalist typography. - Content Card: A styled card containing a text box or paragraph area showcasing the user's priorities. - Interactions: Include a clear "Edit Priorities" button. When clicked, this area should visually toggle into an editable textarea or input field. # Left Panel: Chat & Dilemma Decision Screen - Chat History: A clean vertical flow of message bubbles. - The first message bubble must be aligned to the left (system message) and say: "Tell me your dilemma". Use a friendly, neutral background color for this bubble. - Bottom Input Area: Instead of a traditional single-line chat bar, create a unified, beautiful "Decision Submission" layout positioned at the bottom: - Dual Input Fields: Place two horizontal or cleanly stacked input fields. - Input 1 label: "Option A" - Input 2 label: "Option B" - Action Button: A prominent, styled "Go" action button to trigger the reasoning loop. # Theme & Aesthetic - Vibe: Hyper-rational, clean, trustworthy, and modern. Use a sophisticated color palette (e.g., deep slates, clean whites/grays, and a sharp accent color like a subtle indigo or teal for active states and buttons). - Spacing & Typography: Use generous whitespace, soft rounded corners (border-radius) for cards and buttons, and a highly legible sans-serif font. - คลิกลูกศรสร้างที่มุมขวาล่างของช่องป้อนข้อมูล Stitch จะสร้างหน้าจอและระบบการออกแบบทั้งหมด ซึ่งรวมถึง Color Palette ตัวเลือกแบบอักษร การใช้ไอคอน และความรู้สึกทั่วไปสำหรับองค์ประกอบ UI ดังนี้

3. สร้างแอปด้วย Google AI Studio
- สร้างโปรเจ็กต์ Firebase
- ลงชื่อเข้าใช้คอนโซล Firebase ด้วยบัญชี Google เดียวกับที่ใช้สำหรับ Stitch
- คลิกปุ่มเพื่อสร้างโปรเจ็กต์ใหม่ แล้วป้อนชื่อโปรเจ็กต์ (เช่น
Decider App)
- คลิกต่อไป
- หากได้รับข้อความแจ้ง ให้อ่านและยอมรับข้อกำหนดของ Firebase แล้วคลิกต่อไป
- (ไม่บังคับ) เปิดใช้ความช่วยเหลือจาก AI ในคอนโซล Firebase (เรียกว่า "Gemini ใน Firebase")
- สำหรับ Codelab นี้ คุณไม่จำเป็นต้องใช้ Google Analytics ดังนั้นให้ปิดตัวเลือก Google Analytics
- คลิกสร้างโปรเจ็กต์ รอให้ระบบจัดสรรโปรเจ็กต์ แล้วคลิกต่อไป
- ส่งออกหน้าจอที่คุณสร้างใน Stitch ไปยัง Google AI Studio โดยทำดังนี้
- คลิกการออกแบบที่สร้างขึ้นใน Stitch
- คลิกส่งออกที่มุมขวาบนของหน้าจอ
- ตรวจสอบว่าตัวเลือกที่เลือกคือ Google AI Studio
- เลื่อนลงไปที่ช่องคำอธิบาย
- คัดลอกพรอมต์ต่อไปนี้แล้ววางลงในช่องคำอธิบาย แทนที่ [FIREBASE_PROJECT_ID] ด้วยรหัสโปรเจ็กต์จริงจากขั้นตอนที่ 1 (หากจำรหัสโปรเจ็กต์ไม่ได้ คุณจะดูได้ในการตั้งค่าโปรเจ็กต์)
# General Guidance Build the "Decide For Me" app: A hyper-rational decision assistant for chronically indecisive people facing life choices (moving apartments, changing careers, picking a hobby). The user enters a dilemma and their top priorities in life. The app will help with the decision. Follow the instructions below to build it: # UI Instructions Using the design artifacts exported from Stitch (the PNG image and HTML file), build a fully functional web application that accurately mirrors the design and layout. # App Architecture & Initialization Instructions - Initialize the latest Firebase JS SDK for client-side services. Use the Firebase project with ID [FIREBASE_PROJECT_ID]. - Enable Firebase Authentication in this project using the Google provider. If a user is not logged in, show a clean, minimalist sign-in state. If the user is logged in, take them directly to the dilemma decision screen. - Initialize Firebase App Check in debug mode with the following code: `self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;` `initializeAppCheck(app, { /* App Check options */ });` # State & Database Management Instructions - User Priorities: - Once authenticated, pull the user's saved priorities from Firebase Firestore at the path: `users/{userId}`. Each document should contain one string field: 'priorities'. - Bind this data to the Right Panel ("My Priorities"). - When the user clicks the "Edit" button, toggle the view to a textarea, with a 'save' button. When saved, write the updated string back to `users/{userId}`. - Make sure to implement and deploy strong Security Rules for Firestore. # UI Stream Update & Display Instructions - Once the user submits option A and option B in the dilemma decision screen, display both options as a single message on the user side of the chat. Then, display a sleek loading state in the main chat panel. Leave the app in this "loading" state and do nothing else, we will process both options and build the server-side thinking later. - คลิกสร้างด้วย Google AI Studio ซึ่งจะเปิด Google AI Studio ในแท็บใหม่และนำไฟล์การออกแบบและพรอมต์ของคุณไปด้วย
- ตรวจสอบว่าพรอมต์ได้รับการส่งต่ออย่างสมบูรณ์
- หากเห็นพรอมต์เพียงบางส่วน ให้วางพรอมต์ทั้งหมดอีกครั้ง
- คลิกสร้าง Google AI Studio จะเริ่มสร้างแอป Decide For Me ตามพรอมต์และการออกแบบ UI ของคุณ
- ขณะสร้าง ระบบจะแนะนำการออกแบบ UI ทางเลือก (ไม่ต้องสนใจการออกแบบเหล่านี้สำหรับ Codelab นี้ แต่คุณสามารถเลือกการออกแบบอื่นๆ ได้ในอนาคต)
- เมื่อได้รับข้อความแจ้งให้เปิดใช้ Firebase ให้คลิกเปิดใช้
- ตรวจสอบและยืนยันแอปที่สร้างขึ้น
- ทดสอบแอปในแท็บแสดงตัวอย่าง
- เปลี่ยนไปที่แท็บโค้ดเพื่อตรวจสอบไฟล์และโค้ดทั้งหมดที่สร้างขึ้น
- Google AI Studio ยังได้สร้างโปรเจ็กต์ Firebase, เปิดใช้การตรวจสอบสิทธิ์ Firebase และ Firestore รวมถึงกำหนดค่า Firebase ในฐานของโค้ดด้วย คุณจะเข้าถึงและทำงานกับโปรเจ็กต์ Firebase นี้ในขั้นตอนถัดไปของ Codelab นี้
4. ลงทะเบียนโทเค็นการแก้ไขข้อบกพร่องของ App Check
เมื่อ Google AI Studio สร้างแอป ระบบจะสร้างโปรเจ็กต์ Firebase และเชื่อมต่อแอปของคุณกับโปรเจ็กต์นั้น คุณต้องเข้าถึงโปรเจ็กต์ Firebase นี้และลงทะเบียนสภาพแวดล้อมในการพัฒนาซอฟต์แวร์กับบริการรักษาความปลอดภัยที่สำคัญซึ่งเรียกว่า Firebase App Check
เข้าถึงโปรเจ็กต์ Firebase
- ค้นหารหัสโปรเจ็กต์ของโปรเจ็กต์ Firebase ที่สร้างโดย Google AI Studio โดยทำดังนี้
- เปลี่ยนไปที่แท็บโค้ดใน Google AI Studio
- เปิดไฟล์
firebase-applet-config.json - คัดลอกค่าของช่อง
projectId
- ไปที่คอนโซล Firebase แล้วค้นหาโปรเจ็กต์โดยใช้รหัสนี้
ลงทะเบียนโทเค็นการแก้ไขข้อบกพร่อง
แอปของคุณสามารถใช้ Firebase App Check เพื่อปกป้องบริการแบ็กเอนด์และโมเดล AI จากการละเมิดได้โดยตรวจสอบว่าการเข้าชมที่เข้ามานั้นมาจากแอปที่ถูกต้องเท่านั้น
เนื่องจากแอปของคุณทำงานในสภาพแวดล้อมเวอร์ชันตัวอย่างของ Google AI Studio แอปจึงใช้ผู้ให้บริการแก้ไขข้อบกพร่องของ App Check (self.FIREBASE_APPCHECK_DEBUG_TOKEN = true) เมื่อเว็บแอปโหลด Firebase จะแสดงผลโทเค็นการแก้ไขข้อบกพร่องไปยังคอนโซลนักพัฒนาซอฟต์แวร์ของเบราว์เซอร์โดยอัตโนมัติ
- วิธีค้นหาโทเค็นการแก้ไขข้อบกพร่อง
- ใน Google AI Studio ให้เปิดเครื่องมือสำหรับนักพัฒนาซอฟต์แวร์ของเบราว์เซอร์ขณะดูตัวอย่างแอป โดยทำดังนี้
- Chrome, Edge หรือ Firefox: กด F12 (หรือคลิกขวาที่ใดก็ได้ในตัวอย่าง แล้วเลือกตรวจสอบ) จากนั้นเลือกแท็บคอนโซล
- macOS: กด Cmd+Option+I แล้วเลือกแท็บคอนโซล
- ในบันทึกของคอนโซล ให้ค้นหาข้อความแก้ไขข้อบกพร่องของ App Check ดังนี้
AppCheck debug token: "123a4567-b89c-12d3-e456-789012345678". You will need to safelist it in the Firebase console for it to work. - คัดลอกสตริงโทเค็น (โดยไม่มีเครื่องหมายคำพูด)
- ใน Google AI Studio ให้เปิดเครื่องมือสำหรับนักพัฒนาซอฟต์แวร์ของเบราว์เซอร์ขณะดูตัวอย่างแอป โดยทำดังนี้
- ลงทะเบียนโทเค็นการแก้ไขข้อบกพร่องโดยทำดังนี้
- ในคอนโซล Firebase ให้ไปที่ความปลอดภัย > App Check > แท็บแอป
- ค้นหาเว็บแอปในรายการ คลิกเมนู 3 จุด (⋮) แล้วเลือกจัดการโทเค็นการแก้ไขข้อบกพร่อง
- คลิกเพิ่มโทเค็นการแก้ไขข้อบกพร่อง วางโทเค็นที่คัดลอกไว้ ป้อนชื่อ เช่น
Google AI Studio Previewแล้วคลิกบันทึก

5. ทดสอบแอป
เมื่อเชื่อมต่อโปรเจ็กต์และลงทะเบียนโทเค็นการแก้ไขข้อบกพร่องกับ App Check แล้ว ให้ทดสอบฟีเจอร์พื้นฐาน ซึ่งได้แก่ การตรวจสอบสิทธิ์และ Firestore
- ทดสอบการตรวจสอบสิทธิ์ผู้ใช้และบันทึกรายการสำคัญ
- ในตัวอย่างแอป Decide For Me ใน Google AI Studio ให้ทดสอบหน้าจอการตรวจสอบสิทธิ์โดยลงชื่อเข้าใช้ด้วยบัญชี Google
- บันทึกลำดับความสำคัญในชีวิตโดยใช้แผงด้านขวา (ลำดับความสำคัญของฉัน)
- ยืนยันการตรวจสอบสิทธิ์ในคอนโซล Firebase โดยทำดังนี้
- ไปที่คอนโซล Firebase แล้วเปิด Authentication
- ตรวจสอบว่าบัญชี Google ที่คุณใช้ตรวจสอบสิทธิ์ในแอป Decide For Me แสดงอยู่ในรายการ โดยทำดังนี้

- ยืนยันพื้นที่เก็บข้อมูล Firestore โดยทำดังนี้
- เปิด Firestore ในคอนโซล Firebase
- ตรวจสอบว่าลำดับความสำคัญของคุณจัดเก็บอยู่ในเอกสารภายใต้เส้นทาง
users/{userId}ดังนี้

6. สร้างโฟลว์ Decider ที่ทำงานด้วยระบบ AI โดยใช้ Firebase
แอปของคุณจะใช้ AI เพื่อขับเคลื่อนการดำเนินการตัดสินใจแทนฉัน คุณใช้ Firebase เพื่อสร้างโฟลว์การตัดสินใจที่ทำงานด้วยระบบ AI นี้ได้
ตั้งค่า Firebase AI Logic
- เปิดใช้ Gemini Developer API ใน Firebase AI Logic โดยทำดังนี้
- เปิด Firebase AI Logic ในคอนโซล Firebase แล้วคลิกเริ่มต้นใช้งาน
- เลือก Gemini Developer API แล้วทำตามวิธีการในคอนโซลเพื่อเปิดใช้ API
- กำหนดการตั้งค่า Firebase AI Logic ดังนี้
- ไปที่แท็บการตั้งค่า
- สลับตัวเลือกบังคับใช้โหมดเทมเพลตเท่านั้นเป็นบังคับใช้
- สลับตัวเลือกบังคับใช้โหมดผู้ใช้ที่ตรวจสอบสิทธิ์แล้วเป็นบังคับใช้
สร้างเทมเพลตพรอมต์เซิร์ฟเวอร์
ใน Codelab นี้ คุณจะใช้เทมเพลตพรอมต์ของเซิร์ฟเวอร์เพื่อโต้ตอบกับ Gemini API ผ่าน Firebase AI Logic
ก่อนดำเนินการต่อด้วยเทมเพลต โปรดอ่านข้อมูลต่อไปนี้เพื่อทำความเข้าใจว่าเหตุใดการจัดเก็บการกำหนดค่าโมเดล คำสั่งของระบบ และพรอมต์ฝั่งเซิร์ฟเวอร์จึงมีความสำคัญ
- การปกป้องทรัพย์สินทางปัญญา: ดึงพรอมต์ออกจากอุปกรณ์ไคลเอ็นต์และเก็บไว้อย่างปลอดภัยในแบ็กเอนด์
- การป้องกันการโจมตีแบบแทรกพรอมต์: ไคลเอ็นต์จะกรอกได้เฉพาะตัวแปรที่เฉพาะเจาะจงซึ่งมีการตรวจสอบสคีมาแบ็กเอนด์เท่านั้น
- การอัปเดตโดยไม่ต้องเผยแพร่แอป: คุณสามารถอัปเดตพรอมต์, คำสั่งของระบบ และการกำหนดค่าโมเดลในคอนโซลได้โดยไม่ต้องเผยแพร่แอปเวอร์ชันใหม่
วิธีสร้างและทดสอบเทมเพลตพรอมต์เซิร์ฟเวอร์มีดังนี้
- สร้างเทมเพลตพรอมต์ของเซิร์ฟเวอร์
- ไปที่แท็บเทมเพลตพรอมต์
- คลิกสร้างเทมเพลต
- เลือกเทมเพลตเปล่า
- สร้างเทมเพลตแรกโดยใช้ข้อมูลต่อไปนี้
- ชื่อเทมเพลต: Decider Flow
- รหัสเทมเพลต:
decider-flow - การกำหนดค่า (Frontmatter)
model: "gemini-3.5-flash-lite" input: schema: priorities: "string, user's core priorities" optionA: "string, option A under consideration" optionB: "string, option B under consideration" output: format: json schema: verdict: string justification: string firstStep: string - พรอมต์และคำสั่งของระบบ (ไม่บังคับ)
เพิ่ม{role "system"} You are the Decider Agent, a multi-stage reasoning engine that combines the roles of an honest and polite behavioral economist, a long-term strategic forecaster, and an executive decision coach. Your job is to take a user's dilemma and their core priorities, run them through a rigorous, adversarial 3-phase thinking process, and output a highly analytical, reasoning-backed final verdict. You must be direct, sharp, and realistic. Do not give generic platitudes or claim that "both options are great." You must force a clear choice. {role "user"} # Context & Inputs You are analyzing a critical life decision for the user. Base your entire multi-phase reasoning flow strictly on these three specific inputs: - User's Core Priorities: {{priorities}} - Option A under consideration: {{optionA}} - Option B under consideration: {{optionB}} Execute your reasoning flow through the following three phases: # Phase 1: Cynical Breakdown (Input-Driven Analysis) Analyze {{optionA}} and {{optionB}} strictly through the lens of the user's explicit {{priorities}}. Do not evaluate them in a vacuum. - Identify the hidden flaws, compromises, and psychological costs embedded in both choices. - Pinpoint exactly how {{optionA}} could directly conflict with or undermine their {{priorities}}. - Pinpoint exactly how {{optionB}} could directly conflict with or undermine their {{priorities}}. - Uncover any cognitive biases (like status quo bias or loss aversion) the user might be falling for in either option. # Phase 2: Long-Term Regret Projection Project the user 5 years into the future. Apply the Regret Minimization Framework to both paths: - The {{optionA}} Timeline: Quantify the absolute worst-case, deep, structural 5-year regret of choosing this path. Focus heavily on failures of courage vs. failures of action based on their {{priorities}}. - The {{optionB}} Timeline: Quantify the matching 5-year regret for this path. - Contrast these two timelines. Which option carries the kind of regret that fundamentally breaks the user's {{priorities}}? # Phase 3: Final Recommendation & Action Step Synthesize the cynical critiques and long-term regret scores. Weigh them against the user's {{priorities}} to determine which choice minimizes long-term disappointment. - Verdict: You must declare either {{optionA}} or {{optionB}} as the definitive winner. No compromise, no "it depends," and no hybrid options. - Justification: Provide a sharp, reasoning-backed explanation showing why the losing option was eliminated based on the Phase 1 and Phase 2 analysis. - First Step: Give one microscopic, low-friction task the user can execute within the next 15 minutes to commit to or move toward the chosen verdict.{}เพิ่มเติมรอบ{role "system"}และ{role "user"}เพื่อเขียนคำสั่งของระบบและพรอมต์ในไวยากรณ์ที่ถูกต้อง:
- อินพุตทดสอบ
{ "priorities": "Be financially stable, have a good work-life balance where I can travel twice a year", "optionA": "Buy a house for 615000 British pounds with a mortgage of 2800 per month for the next 35 years", "optionB": "Keep renting for 2400 per month for the foreseeable" }
- คลิกบันทึกเทมเพลต
- ทดสอบเทมเพลต
- คลิกเรียกใช้การทดสอบพรอมต์
- ค้นหาคำตอบทดสอบและตรวจสอบว่ามีฟิลด์ทั้งหมดที่คุณระบุไว้ในพรอมต์ (
verdict,justificationและfirstStep) - หากทุกอย่างตรงกับคำตอบที่คาดไว้ ให้ล็อกเทมเพลตโดยใช้ไอคอนแม่กุญแจที่มุมขวาบนของเทมเพลตเพื่อให้มั่นใจว่าเวอร์ชันจะเสถียรและป้องกันการแก้ไขโดยไม่ตั้งใจ
เทมเพลตพร้อมให้แอปของคุณใช้งานแล้ว
7. ใช้เทมเพลตพรอมต์ Decider ในแอป
ทำให้เว็บแอปใช้เทมเพลตโดยทำดังนี้
- กลับไปที่แอปของคุณใน Google AI Studio
- ขอให้ Agent ของ Google AI Studio ใช้การดำเนินการตัดสินใจให้ฉัน
- คัดลอกพรอมต์ต่อไปนี้
Now let's implement the "Decide For Me" button action. When clicked, it should call the Firebase AI Logic SDK using a Template Generative Model. The prompt is stored in a prompt template with the id "decider-flow". When calling the API, pass the following variables as the input payload object: - priorities - optionA - optionB The API will return a JSON object. Handle the response asynchronously and extract: - verdict - justification - firstStep Once the data is received, update the UI state to append a new message to the chat array. Ensure it renders as a system chat bubble positioned on the left side of the chat screen, mapping these three fields clearly into the bubble's layout. Please provide the necessary front-end component state updates and the Firebase SDK call. - วางพรอมต์ลงในช่องป้อนข้อมูลแชทของ Google AI Studio แล้วคลิกส่ง
- คัดลอกพรอมต์ต่อไปนี้
- ยืนยันการกำหนดค่าและทดสอบผู้ช่วยในการตัดสินใจ
- รอจนกว่า Google AI Studio จะเพิ่ม Firebase AI Logic ลงในแอปเสร็จ
- เปลี่ยนไปที่แท็บโค้ด และตรวจสอบว่ามีคีย์ API ในไฟล์
firebase-applet-config.jsonหากคีย์ Firebase API ไม่อยู่ในไฟล์การกำหนดค่า ให้ขอให้ Google AI Studio นำเข้าคีย์ดังกล่าว - เปลี่ยนไปที่แท็บตัวอย่าง
- ส่งตัวเลือก A และ B แล้วยืนยันว่า AI แสดงคำตัดสิน เหตุผล และขั้นตอนแรก
โค้ดที่ Google AI Studio สร้างขึ้นควรมีลักษณะคล้ายกับโค้ดต่อไปนี้
const aiInstance = getAI(app);
const templateModel = getTemplateGenerativeModel(aiInstance);
const result = await templateModel.generateContent('decider-flow', {
priorities: priorities,
optionA: optA,
optionB: optB
});
const rawText = result.response.text();
// Parse response text to extract fields
const cleaned = rawText.replace(/```json|```/g, '').trim();
const parsedData: { verdict?: string; justification?: string; firstStep?: string } = JSON.parse(cleaned);
const verdict = parsedData.verdict;
const justification = parsedData.justification;
const firstStep = parsedData.firstStep;
แก้ไขข้อบกพร่องและตรวจสอบคำขอ
หากแอปพบข้อผิดพลาด สร้างคำตอบไม่สำเร็จ หรือคุณต้องการตรวจสอบการเข้าชม API โดยละเอียด ให้ใช้เครื่องมือวิเคราะห์และตรวจสอบในตัวของ Firebase AI Logic ดังนี้
- ในคอนโซล Firebase ให้ไปที่บริการ AI > ตรรกะ AI > แท็บการตรวจสอบ AI
- ตรวจสอบเมตริกเชิงปริมาณในหน้าแดชบอร์ด
- ปริมาณและสถานะคำขอ: ตรวจสอบว่าคำขอขาเข้าถึงแบ็กเอนด์และแสดงผล
200 OKหรือไม่ หรือไม่สำเร็จเนื่องจากข้อผิดพลาดฝั่งไคลเอ็นต์หรือข้อผิดพลาดเกี่ยวกับเซิร์ฟเวอร์ - เวลาในการตอบสนอง: ตรวจสอบกราฟเวลาในการตอบสนองเพื่อดูเวลาในการตอบสนองตั้งแต่ต้นจนจบ
- จำนวนโทเค็น: ตรวจสอบการใช้งานโทเค็นอินพุตและเอาต์พุตต่อคำขอ
- ปริมาณและสถานะคำขอ: ตรวจสอบว่าคำขอขาเข้าถึงแบ็กเอนด์และแสดงผล
- ตรวจสอบการติดตามคำขอแต่ละรายการเพื่อแก้ไขข้อบกพร่องอย่างละเอียด:คลิกการติดตามคำขอที่เฉพาะเจาะจงในตารางการติดตามเพื่อตรวจสอบเพย์โหลด
- อินพุต: ยืนยันว่าตัวแปร (
priorities,optionAและoptionB) ได้รับการส่งผ่านจากแอปไคลเอ็นต์อย่างถูกต้อง - เอาต์พุต: ดูข้อความดิบที่โมเดลสร้างขึ้นเพื่อยืนยันว่าโมเดลแสดงผล JSON ที่ถูกต้องซึ่งตรงกับสคีมาหรือไม่
- รายละเอียดข้อผิดพลาด: หากคำขอล้มเหลว ให้ตรวจสอบบันทึกข้อผิดพลาดในการติดตามเพื่อหาสาเหตุ เช่น โทเค็น App Check ไม่ถูกต้อง การปฏิเสธการตรวจสอบสิทธิ์ ขีดจำกัดโควต้า หรือข้อผิดพลาดในการตรวจสอบพรอมต์
- อินพุต: ยืนยันว่าตัวแปร (
- แก้ปัญหาและแก้ไขปัญหาเกี่ยวกับ Google AI Studio
- หากพบข้อผิดพลาดในการติดตาม (เช่น การแยกวิเคราะห์การตอบกลับ JSON ที่มีรูปแบบไม่ถูกต้องหรือพารามิเตอร์ที่ขาดหายไป) ให้คัดลอกข้อความแสดงข้อผิดพลาดหรือเพย์โหลด
- กลับไปที่ Google AI Studio แล้วป้อนพรอมต์ให้ผู้ช่วยแก้ไขปัญหาที่เกี่ยวข้องในโค้ดของแอป
8. ไม่บังคับ: ทำให้ผู้ใช้ใช้งานแอปได้
เมื่อแอปทำงานในโหมดแสดงตัวอย่างแล้ว ให้ทำให้แอปใช้งานได้ในเวอร์ชันที่ใช้งานจริง
หากต้องการปกป้องปลายทาง Firebase ในเวอร์ชันที่ใช้งานจริง ให้กำหนดค่าผู้ให้บริการเอกสารรับรองเวอร์ชันที่ใช้งานจริงเพื่อใช้กับ Firebase App Check (ใน Codelab นี้ คุณจะใช้ reCAPTCHA v3)
คุณต้องทำให้แอปใช้งานได้ก่อนเพื่อให้ได้ URL ที่เผยแพร่อยู่ที่แน่นอน เพื่อให้คุณลงทะเบียนโดเมนที่เฉพาะเจาะจงกับ reCAPTCHA ได้ แทนที่จะอนุญาตแอป ai.studio ทั้งหมด
ติดตั้งใช้งานแอปและกำหนดค่า reCAPTCHA โดยทำดังนี้
- ติดตั้งใช้งานเว็บแอปด้วย Google AI Studio
- ใน Google AI Studio ให้คลิกเผยแพร่ในแถบนำทางด้านบน
- คลิกเริ่มต้นใช้งาน ตรวจสอบหรือปรับแต่งชื่อและคำอธิบายแอปพลิเคชัน แล้วคลิกเผยแพร่แอป
- รอให้ Google AI Studio สร้างและทำให้แอปใช้งานได้ (โดยปกติจะใช้เวลา 1-2 นาที)
- คัดลอก URL ที่กำหนดเองแบบสดที่สร้างขึ้น (เช่น
https://decide-for-me-123.ai.studio) และจดบันทึกโดเมน (เช่นdecide-for-me-123.ai.studio)
- สร้างคีย์ reCAPTCHA v3 ใน Google reCAPTCHA โดยทำดังนี้
- ไปที่คอนโซลผู้ดูแลระบบ Google reCAPTCHA
- ป้อนป้ายกำกับ (เช่น
Decide For Me - Production) - เลือกประเภท reCAPTCHA > reCAPTCHA v3 (อิงตามคะแนน)
- ในส่วนโดเมน ให้เพิ่มโดเมนที่ติดตั้งใช้งานจริงของแอป (เช่น
decide-for-me-123.ai.studioโดยละเว้นhttps://และเส้นทางต่างๆ) หรือโดเมนที่กำหนดเองหากคุณวางแผนที่จะใช้ - ยอมรับข้อกำหนดในการให้บริการ แล้วคลิกส่ง
- เปิดแท็บนี้ไว้ คุณจะต้องใช้ทั้งคีย์เว็บไซต์ (คีย์ไคลเอ็นต์สาธารณะ) และคีย์ลับ (คีย์เซิร์ฟเวอร์ส่วนตัว)
- ลงทะเบียนคีย์ลับในคอนโซล Firebase โดยทำดังนี้
- ไปที่ App Check ในคอนโซล Firebase
- ไปที่แท็บแอป
- ค้นหาเว็บแอปพลิเคชันในรายการ แล้วคลิก (หรือคลิกเมนู 3 จุด ⋮) แล้วเลือก reCAPTCHA v3
- วางคีย์ลับ reCAPTCHA ลงในช่อง แล้วคลิกบันทึก
- กำหนดค่าแอปไคลเอ็นต์ให้ใช้
ReCaptchaV3Provider:- ใน Google AI Studio ให้เปลี่ยนไปใช้แผงแชทและแจ้งให้ผู้ช่วยอัปเดตการกำหนดค่า App Check สำหรับเวอร์ชันที่ใช้งานจริง โดยทำดังนี้
Please update our Firebase App Check initialization to use ReCaptchaV3Provider with the reCAPTCHA site key "YOUR_RECAPTCHA_SITE_KEY", while retaining support for debug tokens in non-production environments. - หากคุณตรวจสอบโค้ดโดยตรงในแท็บโค้ด ให้ตรวจสอบว่าการเริ่มต้น App Check ตรงกับโครงสร้างต่อไปนี้
import { initializeAppCheck, ReCaptchaV3Provider } from "firebase/app-check"; const appCheck = initializeAppCheck(app, { provider: new ReCaptchaV3Provider("YOUR_RECAPTCHA_SITE_KEY"), isTokenAutoRefreshEnabled: true }); - หลังจากอัปเดตโค้ดแล้ว ให้เผยแพร่แอปซ้ำใน Google AI Studio เพื่อพุชการกำหนดค่า App Check ที่อัปเดตแล้วไปยังเว็บไซต์ที่เผยแพร่อยู่
- ใน Google AI Studio ให้เปลี่ยนไปใช้แผงแชทและแจ้งให้ผู้ช่วยอัปเดตการกำหนดค่า App Check สำหรับเวอร์ชันที่ใช้งานจริง โดยทำดังนี้
- ยืนยันการเข้าชมเวอร์ชันที่ใช้งานจริงและตรวจสอบ App Check
- เปิด URL ของแอปเวอร์ชันที่ใช้จริงในหน้าต่างเบราว์เซอร์ที่ไม่ระบุตัวตน ลงชื่อเข้าใช้ด้วย Google และทดสอบการส่งปัญหาที่กลืนไม่เข้าคายไม่ออก
- กลับไปที่ App Check ในคอนโซล Firebase แล้วตรวจสอบแท็บ API และการตรวจสอบเพื่อยืนยันว่าคำขอได้รับการลงทะเบียนสำหรับ Firestore และ Firebase AI Logic เรียบร้อยแล้ว
9. บทสรุป
ยินดีด้วย คุณสร้างแอปที่มีประโยชน์ซึ่งมีฟีเจอร์ AI ตั้งแต่ต้นโดยใช้เครื่องมือใน Builder Stack ของ Google นี่เป็นเพียงจุดเริ่มต้นเท่านั้น ยังมีสิ่งอื่นๆ อีกมากมายที่คุณทำได้ด้วยเครื่องมือของ Google ตอนนี้คุณมีโปรเจ็กต์ Firebase + Google Cloud แล้ว ลองสำรวจบริการทั้งหมดที่มีให้คุณ
ไปที่ลิงก์นี้เพื่อรับป้ายเส้นทางนักสร้างสรรค์ที่สมบูรณ์
หากต้องการเดินทางต่อ โปรดไปที่ goo.gle/builders เพื่อดูห้องทดลองถัดไป
หากคุณใช้ Antigravity 2.0 ซึ่งเป็นแพลตฟอร์มเฉพาะของ Google สำหรับจัดการ Agent อัตโนมัติหลายรายการพร้อมกันอยู่แล้ว คุณสามารถส่งออกโปรเจ็กต์จาก Google AI Studio และนำเข้าสู่ Antigravity ได้อย่างรวดเร็วเพื่อทำงานต่อในเครื่อง Antigravity มีทักษะ Agent สำหรับ Firebase ที่พร้อมให้ติดตั้งได้ในคลิกเดียว ทักษะเหล่านี้ยังให้การสนับสนุนสำหรับการพัฒนาแอปบนอุปกรณ์เคลื่อนที่ ซึ่งจะช่วยให้คุณจำลองแอปนี้ได้ทั้งในสภาพแวดล้อม Android และ iOS หากต้องการ