สร้างเอาต์พุตที่มีโครงสร้างสำหรับประสบการณ์การใช้งานแบบไฮบริดในเว็บแอป

Gemini โมเดลจะแสดงผลการตอบกลับเป็นข้อความที่ไม่มีโครงสร้างโดยค่าเริ่มต้น อย่างไรก็ตาม บางกรณีการใช้งานจำเป็นต้องใช้ข้อความที่มีโครงสร้าง เช่น JSON ตัวอย่างเช่น คุณอาจใช้การตอบกลับสำหรับงานอื่นๆ ที่ต้องใช้สคีมาข้อมูลที่กำหนดไว้

หากต้องการให้เอาต์พุตที่สร้างขึ้นของโมเดลเป็นไปตามสคีมาที่เฉพาะเจาะจงเสมอ คุณสามารถกำหนด สคีมา ซึ่งทำงานเหมือนพิมพ์เขียวสำหรับการตอบกลับของโมเดล จากนั้นคุณจะดึงข้อมูลจากเอาต์พุตของโมเดลได้โดยตรงโดยมีการประมวลผลภายหลังน้อยลง

ตัวอย่างเช่น

  • ตรวจสอบว่าการตอบกลับของโมเดลสร้าง JSON ที่ถูกต้องและเป็นไปตาม สคีมาที่คุณระบุ
    เช่น โมเดลสามารถสร้างรายการที่มีโครงสร้างสำหรับสูตรอาหารซึ่งรวมถึงชื่อสูตร รายการส่วนผสม และขั้นตอนต่างๆ เสมอ จากนั้นคุณจะแยกวิเคราะห์และแสดงข้อมูลนี้ใน UI ของแอปได้ง่ายขึ้น

  • จำกัดวิธีที่โมเดลสามารถตอบกลับระหว่างงานการจัดประเภท
    เช่น คุณสามารถให้โมเดลใส่คำอธิบายประกอบข้อความด้วยชุดป้ายกำกับที่เฉพาะเจาะจง (เช่น ชุด enum ที่เฉพาะเจาะจง เช่น positive และ negative) แทนที่จะใช้ป้ายกำกับที่โมเดลสร้างขึ้น (ซึ่งอาจมีความแปรปรวนในระดับหนึ่ง เช่น good, positive, negative หรือ bad)

หน้านี้อธิบายวิธีสร้างเอาต์พุตที่มีโครงสร้าง (เช่น JSON และ enum) ในประสบการณ์แบบไฮบริดสำหรับเว็บแอป

ก่อนเริ่มต้น

ตรวจสอบว่าคุณได้ทำตาม คู่มือเริ่มต้นใช้งานสำหรับการสร้างประสบการณ์แบบไฮบริดเรียบร้อยแล้ว

กำหนดการตั้งค่าสำหรับเอาต์พุตที่มีโครงสร้าง

ระบบรองรับการสร้างเอาต์พุตที่มีโครงสร้าง (เช่น JSON และ enum) สำหรับการอนุมานโดยใช้ทั้งโมเดลที่โฮสต์ในระบบคลาวด์และโมเดลในอุปกรณ์

สำหรับการอนุมานแบบไฮบริด ให้ใช้ทั้ง inCloudParams และ onDeviceParams เพื่อกำหนดค่าโมเดลให้ตอบกลับด้วยเอาต์พุตที่มีโครงสร้าง สำหรับโหมดอื่นๆ ให้ใช้เฉพาะการกำหนดค่าที่เกี่ยวข้อง

  • สำหรับ inCloudParams: ระบุ responseMimeType ที่เหมาะสม (เช่น application/json) รวมถึง responseSchema ที่ต้องการให้โมเดลใช้

  • สำหรับ onDeviceParams: ระบุ responseConstraint ที่ต้องการให้ โมเดลใช้

เอาต์พุต JSON

ตัวอย่างต่อไปนี้ปรับ ตัวอย่างเอาต์พุต JSON ทั่วไป ให้รองรับการอนุมานแบบไฮบริด (ในตัวอย่างนี้คือ PREFER_ON_DEVICE)

ตัวอย่างที่ 1: การใช้เมธอดตัวช่วย Schema ของ Firebase

ตัวอย่างนี้ใช้เมธอดตัวช่วยที่ Firebase AI Logic SDK มีให้เพื่อทำให้คำจำกัดความสคีมามีขนาดกะทัดรัดมากขึ้น

import {
  getAI,
  getGenerativeModel,
  Schema
} from "firebase/ai";

// This schema can also be defined as a plain JSON object.
// See next snippet for an example of a plain JSON schema.
const jsonSchema = Schema.object({
 properties: {
    characters: Schema.array({
      items: Schema.object({
        properties: {
          name: Schema.string(),
          accessory: Schema.string(),
          age: Schema.number(),
          species: Schema.string(),
        },
        optionalProperties: ["accessory"],
      }),
    }),
  }
});

const model = getGenerativeModel(ai, {
  mode: InferenceMode.PREFER_ON_DEVICE,
  inCloudParams: {
    generationConfig: {
      model: "gemini-3.1-flash-lite",
      responseMimeType: "application/json",
      responseSchema: jsonSchema
    },
  }
  onDeviceParams: {
    promptOptions: {
      responseConstraint: jsonSchema
    }
  }
});

const result = await model
  .generateContent(
    "Create profiles for some characters for a fantasy story.");

console.log(result.response.text());
console.log(JSON.parse(result.response.text()));

// ...

ตัวอย่างที่ 2: การใช้สคีมา JSON ธรรมดา

ตัวอย่างนี้กำหนดสคีมาโดยใช้ JSON เท่านั้นและแสดงผลลัพธ์เดียวกัน

import {
  getAI,
  getGenerativeModel,
  Schema
} from "firebase/ai";

// The JSON schema defined as a plain JSON object.
const jsonSchema = {
    "type": "object",
    "properties": {
        "characters": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "nullable": false
                    },
                    "accessory": {
                        "type": "string",
                        "nullable": false
                    },
                    "age": {
                        "type": "number",
                        "nullable": false
                    },
                    "species": {
                        "type": "string",
                        "nullable": false
                    }
                },
                "nullable": false,
                "required": [
                    "name",
                    "age",
                    "species"
                ]
            },
            "nullable": false
        }
    },
    "nullable": false,
    "required": [
        "characters"
    ]
};

const model = getGenerativeModel(ai, {
  mode: InferenceMode.PREFER_ON_DEVICE,
  inCloudParams: {
    generationConfig: {
      model: "gemini-3.1-flash-lite",
      responseMimeType: "application/json",
      responseSchema: jsonSchema
    },
  }
  onDeviceParams: {
    promptOptions: {
      responseConstraint: jsonSchema
    }
  }
});

const result = await model
  .generateContent(
    "Create profiles for some characters for a fantasy story.");

console.log(result.response.text());
console.log(JSON.parse(result.response.text()));

เอาต์พุต enum

ตัวอย่างต่อไปนี้ปรับ ตัวอย่างเอาต์พุต enum ทั่วไป ให้รองรับการอนุมานแบบไฮบริด (ในตัวอย่างนี้คือ PREFER_ON_DEVICE)

import {
  getAI,
  getGenerativeModel,
  Schema
} from "firebase/ai";

// enumSchema can also be defined as a plain JSON schema instead of
// using the `Schema` helper methods, just as in the JSON output example.
const enumSchema = Schema.enumString({
  enum: ["drama", "comedy", "documentary"],
});

const model = getGenerativeModel(ai, {
  mode: InferenceMode.PREFER_ON_DEVICE,
  inCloudParams: {
    generationConfig: {
      responseMimeType: "text/x.enum",
      responseSchema: enumSchema
    },
  }
  onDeviceParams: {
    promptOptions: {
      responseConstraint: enumSchema
    }
  }
});

// ...