สร้างเอาต์พุตที่มีโครงสร้าง (เช่น JSON) โดยใช้ Gemini API

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

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

โดยตัวอย่างมีดังนี้

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

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

คู่มือนี้แสดงวิธีสร้างเอาต์พุต JSON โดยระบุ responseSchema ในการเรียก generateContent โดยมุ่งเน้นที่อินพุตที่เป็นข้อความเท่านั้น แต่ Gemini ยังสามารถสร้างคำตอบที่มีโครงสร้างสำหรับคำขอแบบมัลติโมเดลที่มีรูปภาพ วิดีโอ และเสียงเป็นอินพุตได้ด้วย

ด้านล่างของหน้านี้มีตัวอย่างเพิ่มเติม เช่น วิธีสร้างค่า Enum เป็นเอาต์พุต หากต้องการดูตัวอย่างเพิ่มเติมเกี่ยวกับวิธีสร้างเอาต์พุตที่มีโครงสร้าง ให้ดูรายการสคีมาตัวอย่างและการตอบกลับรูปแบบในเอกสารประกอบ Google Cloud

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

หากยังไม่ได้ดำเนินการ ให้อ่านคู่มือเริ่มต้นใช้งาน ซึ่งอธิบายวิธีตั้งค่าโปรเจ็กต์ Firebase, เชื่อมต่อแอปกับ Firebase, เพิ่ม SDK, เริ่มต้นบริการ Vertex AI และสร้างอินสแตนซ์ GenerativeModel

ขั้นตอนที่ 1: กําหนดสคีมาการตอบกลับ

กําหนดสคีมาการตอบกลับเพื่อระบุโครงสร้างของเอาต์พุตของโมเดล ชื่อช่อง และประเภทข้อมูลที่คาดไว้สําหรับแต่ละช่อง

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

ข้อควรพิจารณาสำหรับสคีมาคำตอบ

โปรดคำนึงถึงสิ่งต่อไปนี้เมื่อเขียนสคีมาคำตอบ

  • ขนาดของสคีมาการตอบกลับจะนับรวมในขีดจํากัดของโทเค็นอินพุต

  • ฟีเจอร์สคีมาคำตอบรองรับประเภท MIME ของคำตอบต่อไปนี้

    • application/json: เอาต์พุต JSON ตามที่ระบุไว้ในสคีมาการตอบกลับ (มีประโยชน์สำหรับข้อกำหนดเกี่ยวกับเอาต์พุตที่มีโครงสร้าง)

    • text/x.enum: แสดงผลค่า enum ตามที่ระบุไว้ในสคีมาการตอบกลับ (มีประโยชน์สำหรับงานการจัดประเภท)

  • ฟีเจอร์สคีมาคำตอบรองรับฟิลด์สคีมาต่อไปนี้

    enum
    items
    maxItems
    nullable
    properties
    required

    หากคุณใช้ฟิลด์ที่ระบบไม่รองรับ โมเดลจะยังคงจัดการคําขอได้ แต่จะละเว้นฟิลด์นั้น โปรดทราบว่ารายการข้างต้นคือชุดย่อยของออบเจ็กต์สคีมา OpenAPI 3.0 (ดูข้อมูลอ้างอิงสคีมา Vertex AI)

  • โดยค่าเริ่มต้น ระบบจะถือว่าช่องทั้งหมดใน Vertex AI in Firebase SDK ต้องกรอก เว้นแต่คุณจะระบุว่าเป็นช่องที่ไม่บังคับในอาร์เรย์ optionalProperties สําหรับช่องที่ไม่บังคับเหล่านี้ โมเดลจะป้อนข้อมูลในช่องหรือข้ามช่องก็ได้

    โปรดทราบว่าการดำเนินการนี้ตรงข้ามกับลักษณะการทำงานเริ่มต้นของ Gemini API in Vertex AI

ขั้นตอนที่ 2: ส่งพรอมต์ที่มีสคีมาการตอบกลับเพื่อสร้าง JSON

ตัวอย่างต่อไปนี้แสดงวิธีสร้างเอาต์พุต JSON ที่มีโครงสร้าง

หากต้องการสร้างเอาต์พุตที่มีโครงสร้าง คุณต้องระบุresponseMimeTypeที่เหมาะสม (ในตัวอย่างนี้คือ application/json) ในระหว่างการเริ่มต้นใช้งานโมเดล รวมถึงresponseSchemaที่คุณต้องการให้โมเดลใช้

โมเดล Gemini ทั้งหมดรองรับการใช้ responseSchema (ยกเว้นรุ่น Gemini 1.0)

Swift

import FirebaseVertexAI

// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
let jsonSchema = Schema.object(
  properties: [
    "characters": Schema.array(
      items: .object(
        properties: [
          "name": .string(),
          "age": .integer(),
          "species": .string(),
          "accessory": .enumeration(values: ["hat", "belt", "shoes"]),
        ],
        optionalProperties: ["accessory"]
      )
    ),
  ]
)

// Initialize the Vertex AI service and the generative model.
let model = VertexAI.vertexAI().generativeModel(
  modelName: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `application/json`
  // and pass the JSON schema object into `responseSchema`.
  generationConfig: GenerationConfig(
    responseMIMEType: "application/json",
    responseSchema: jsonSchema
  )
)

let prompt = "For use in a children's card game, generate 10 animal-based characters."

let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")

Kotlin

สำหรับ Kotlin เมธอดใน SDK นี้เป็นฟังก์ชันที่ระงับและต้องมีการเรียกใช้จากขอบเขต Coroutine
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
val jsonSchema = Schema.obj(
    mapOf("characters" to Schema.array(
        Schema.obj(
            mapOf(
                "name" to Schema.string(),
                "age" to Schema.integer(),
                "species" to Schema.string(),
                "accessory" to Schema.enumeration(listOf("hat", "belt", "shoes")),
            ),
            optionalProperties = listOf("accessory")
        )
    ))
)

// Initialize the Vertex AI service and the generative model.
val generativeModel = Firebase.vertexAI.generativeModel(
    modelName = "gemini-2.0-flash",
    // In the generation config, set the `responseMimeType` to `application/json`
    // and pass the JSON schema object into `responseSchema`.
    generationConfig = generationConfig {
        responseMimeType = "application/json"
        responseSchema = jsonSchema
    })

val prompt = "For use in a children's card game, generate 10 animal-based characters."
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

สําหรับ Java เมธอดสตรีมมิงใน SDK นี้จะแสดงผลประเภท Publisher จากไลบรารี Reactive Streams
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
Schema jsonSchema = Schema.obj(
        /* properties */
        Map.of(
                "characters", Schema.array(
                        /* items */ Schema.obj(
                                /* properties */
                                Map.of("name", Schema.str(),
                                        "age", Schema.numInt(),
                                        "species", Schema.str(),
                                        "accessory",
                                        Schema.enumeration(
                                                List.of("hat", "belt", "shoes")))
                        ))),
        List.of("accessory"));

// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "application/json";
configBuilder.responseSchema = jsonSchema;

GenerationConfig generationConfig = configBuilder.build();

// Initialize the Vertex AI service and the generative model.
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
  /* modelName */ "gemini-2.0-flash",
  /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content content = new Content.Builder()
    .addText("For use in a children's card game, generate 10 animal-based characters.")
    .build();

// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
    response,
    new FutureCallback<GenerateContentResponse>() {
      @Override
      public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
      }

      @Override
      public void onFailure(Throwable t) {
        t.printStackTrace();
      }
    },
    executor);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel, Schema } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service.
const vertexAI = getVertexAI(firebaseApp);

// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
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"],
      }),
    }),
  }
});

// Initialize the generative model.
const model = getGenerativeModel(vertexAI, {
  model: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `application/json`
  // and pass the JSON schema object into `responseSchema`.
  generationConfig: {
    responseMimeType: "application/json",
    responseSchema: jsonSchema
  },
});


let prompt = "For use in a children's card game, generate 10 animal-based characters.";

let result = await model.generateContent(prompt)
console.log(result.response.text());

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
final jsonSchema = Schema.object(
        properties: {
          'characters': Schema.array(
            items: Schema.object(
              properties: {
                'name': Schema.string(),
                'age': Schema.integer(),
                'species': Schema.string(),
                'accessory':
                    Schema.enumString(enumValues: ['hat', 'belt', 'shoes']),
              },
            ),
          ),
        },
        optionalProperties: ['accessory'],
      );

await Firebase.initializeApp();
// Initialize the Vertex AI service and the generative model.
final model =
      FirebaseVertexAI.instance.generativeModel(
        model: 'gemini-2.0-flash',
        // In the generation config, set the `responseMimeType` to `application/json`
        // and pass the JSON schema object into `responseSchema`.
        generationConfig: GenerationConfig(
            responseMimeType: 'application/json', responseSchema: jsonSchema));

final prompt = "For use in a children's card game, generate 10 animal-based characters.";
final response = await model.generateContent([Content.text(prompt)]);
print(response.text);

ดูวิธีเลือกโมเดลและตำแหน่ง (ไม่บังคับ) ที่เหมาะสมกับกรณีการใช้งานและแอป

ตัวอย่างเพิ่มเติม

หากต้องการดูตัวอย่างเพิ่มเติมเกี่ยวกับวิธีใช้และสร้างเอาต์พุตที่มีโครงสร้าง ให้ดูรายการสคีมาตัวอย่างและคำตอบจำลองในเอกสารประกอบ Google Cloud

สร้างค่า enum เป็นเอาต์พุต

ตัวอย่างต่อไปนี้แสดงวิธีใช้สคีมาคำตอบสำหรับงานการจัดประเภท ระบบจะขอให้โมเดลระบุประเภทของภาพยนตร์ตามคำอธิบาย เอาต์พุตคือค่าแบบข้อความล้วนแบบ Enum รายการเดียวที่โมเดลเลือกจากรายการค่าที่กําหนดไว้ในสคีมาการตอบกลับที่ระบุ

หากต้องการทํางานการแยกประเภทที่มีโครงสร้างนี้ คุณต้องระบุ responseMimeType ที่เหมาะสม (ในตัวอย่างนี้คือ text/x.enum) และ responseSchema ที่ต้องการให้โมเดลใช้ในระหว่างการเริ่มต้นใช้งานโมเดล

Swift

import FirebaseVertexAI

// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
let enumSchema = Schema.enumeration(values: ["drama", "comedy", "documentary"])

// Initialize the Vertex AI service and the generative model.
let model = VertexAI.vertexAI().generativeModel(
  modelName: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `text/x.enum`
  // and pass the enum schema object into `responseSchema`.
  generationConfig: GenerationConfig(
    responseMIMEType: "text/x.enum",
    responseSchema: enumSchema
  )
)

let prompt = """
The film aims to educate and inform viewers about real-life subjects, events, or people.
It offers a factual record of a particular topic by combining interviews, historical footage,
and narration. The primary purpose of a film is to present information and provide insights
into various aspects of reality.
"""

let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")

Kotlin

สำหรับ Kotlin เมธอดใน SDK นี้เป็นฟังก์ชันที่ระงับและต้องมีการเรียกใช้จากขอบเขต Coroutine
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
val enumSchema = Schema.enumeration(listOf("drama", "comedy", "documentary"))

// Initialize the Vertex AI service and the generative model.
val generativeModel = Firebase.vertexAI.generativeModel(
    modelName = "gemini-2.0-flash",
    // In the generation config, set the `responseMimeType` to `text/x.enum`
    // and pass the enum schema object into `responseSchema`.
    generationConfig = generationConfig {
        responseMimeType = "text/x.enum"
        responseSchema = enumSchema
    })

val prompt = """
    The film aims to educate and inform viewers about real-life subjects, events, or people.
    It offers a factual record of a particular topic by combining interviews, historical footage, 
    and narration. The primary purpose of a film is to present information and provide insights 
    into various aspects of reality.
    """
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

สําหรับ Java เมธอดสตรีมมิงใน SDK นี้จะแสดงผลประเภท Publisher จากไลบรารี Reactive Streams
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
Schema enumSchema = Schema.enumeration(List.of("drama", "comedy", "documentary"));

// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the enum schema object into `responseSchema`.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "text/x.enum";
configBuilder.responseSchema = enumSchema;

GenerationConfig generationConfig = configBuilder.build();

// Initialize the Vertex AI service and the generative model.
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
  /* modelName */ "gemini-2.0-flash",
  /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

String prompt = "The film aims to educate and inform viewers about real-life subjects," +
                " events, or people. It offers a factual record of a particular topic by" +
                " combining interviews, historical footage, and narration. The primary purpose" +
                " of a film is to present information and provide insights into various aspects" +
                " of reality.";

Content content = new Content.Builder().addText(prompt).build();

// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
    response,
    new FutureCallback<GenerateContentResponse>() {
      @Override
      public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
      }

      @Override
      public void onFailure(Throwable t) {
        t.printStackTrace();
      }
    },
    executor);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel, Schema } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
 // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service.
const vertexAI = getVertexAI(firebaseApp);

// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
const enumSchema = Schema.enumString({
  enum: ["drama", "comedy", "documentary"],
});

// Initialize the generative model.
const model = getGenerativeModel(vertexAI, {
  model: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `text/x.enum`
  // and pass the JSON schema object into `responseSchema`.
  generationConfig: {
    responseMimeType: "text/x.enum",
    responseSchema: enumSchema,
  },
});

let prompt = `The film aims to educate and inform viewers about real-life
subjects, events, or people. It offers a factual record of a particular topic
by combining interviews, historical footage, and narration. The primary purpose
of a film is to present information and provide insights into various aspects
of reality.`;

let result = await model.generateContent(prompt);
console.log(result.response.text());

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
final enumSchema = Schema.enumString(enumValues: ['drama', 'comedy', 'documentary']);

await Firebase.initializeApp();
// Initialize the Vertex AI service and the generative model.
final model =
      FirebaseVertexAI.instance.generativeModel(
        model: 'gemini-2.0-flash',
        // In the generation config, set the `responseMimeType` to `text/x.enum`
        // and pass the enum schema object into `responseSchema`.
        generationConfig: GenerationConfig(
            responseMimeType: 'text/x.enum', responseSchema: enumSchema));

final prompt = """
      The film aims to educate and inform viewers about real-life subjects, events, or people.
      It offers a factual record of a particular topic by combining interviews, historical footage, 
      and narration. The primary purpose of a film is to present information and provide insights
      into various aspects of reality.
      """;
final response = await model.generateContent([Content.text(prompt)]);
print(response.text);

ดูวิธีเลือกโมเดลและตำแหน่ง (ไม่บังคับ) ที่เหมาะสมกับกรณีการใช้งานและแอป

ตัวเลือกอื่นๆ ในการควบคุมการสร้างเนื้อหา

  • ดูข้อมูลเพิ่มเติมเกี่ยวกับการออกแบบพรอมต์เพื่อให้คุณควบคุมโมเดลให้สร้างเอาต์พุตที่ตรงกับความต้องการของคุณได้
  • กำหนดค่าพารามิเตอร์โมเดลเพื่อควบคุมวิธีที่โมเดลสร้างคำตอบ สําหรับโมเดล Gemini พารามิเตอร์เหล่านี้ ได้แก่ โทเค็นเอาต์พุตสูงสุด, อุณหภูมิ, topK และ topP สำหรับโมเดล Imagen สิ่งเหล่านี้รวมถึงสัดส่วนการแสดงผล การสร้างบุคคล ลายน้ำ ฯลฯ
  • ใช้การตั้งค่าความปลอดภัยเพื่อปรับความเป็นไปได้ที่จะได้รับคำตอบที่อาจถือว่ามีอันตราย ซึ่งรวมถึงวาจาสร้างความเกลียดชังและเนื้อหาเกี่ยวกับเรื่องเพศอย่างโจ่งแจ้ง
  • ตั้งค่าคำสั่งของระบบเพื่อกำหนดลักษณะการทํางานของโมเดล ฟีเจอร์นี้เปรียบเสมือน "คํานํา" ที่คุณเพิ่มก่อนที่จะแสดงรูปแบบต่อผู้ใช้ปลายทาง


แสดงความคิดเห็นเกี่ยวกับประสบการณ์การใช้งาน Vertex AI in Firebase