สร้างเอาต์พุตที่มีโครงสร้าง (เช่น JSON และลิสต์แบบจำกัด) โดยใช้ Gemini API

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

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

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

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

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

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

ด้านล่างของหน้านี้มีตัวอย่างเพิ่มเติม เช่น วิธีสร้างค่า Enum เป็นเอาต์พุต

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

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะผู้ให้บริการในหน้านี้

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

เราขอแนะนําให้ใช้ Google AI Studio ในการทดสอบและปรับปรุงพรอมต์ รวมถึงรับข้อมูลโค้ดที่สร้างขึ้น

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

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

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

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

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

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

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

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

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

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

    enum
    items
    maxItems
    nullable
    properties
    required

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

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

ขั้นตอนที่ 2: สร้างเอาต์พุต JSON โดยใช้สคีมาการตอบกลับ

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

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

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

Swift


import FirebaseAI

// 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 Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel(
            /* modelName */ "gemini-2.0-flash",
            /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

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 { getAI, getGenerativeModel, GoogleAIBackend, Schema } from "firebase/ai";

// 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 Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// 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"],
      }),
    }),
  }
});

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, {
  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_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.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'],
      );


// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.googleAI().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);

Unity


using Firebase;
using Firebase.AI;

// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
var jsonSchema = Schema.Object(
  properties: new System.Collections.Generic.Dictionary<string, Schema> {
    { "characters", Schema.Array(
      items: Schema.Object(
        properties: new System.Collections.Generic.Dictionary<string, Schema> {
          { "name", Schema.String() },
          { "age", Schema.Int() },
          { "species", Schema.String() },
          { "accessory", Schema.Enum(new string[] { "hat", "belt", "shoes" }) },
        },
        optionalProperties: new string[] { "accessory" }
      )
    ) },
  }
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
var model = FirebaseAI.DefaultInstance.GetGenerativeModel(
  modelName: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `application/json`
  // and pass the JSON schema object into `responseSchema`.
  generationConfig: new GenerationConfig(
    responseMimeType: "application/json",
    responseSchema: jsonSchema
  )
);

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

var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

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

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

ตัวอย่างเพิ่มเติมเกี่ยวกับวิธีใช้และสร้าง Structured Output มีดังนี้

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

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

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

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

Swift


import FirebaseAI

// 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 Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel(
            /* modelName */ "gemini-2.0-flash",
            /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

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 { getAI, getGenerativeModel, GoogleAIBackend, Schema } from "firebase/ai";

// 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 Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// 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"],
});

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, {
  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_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.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']);

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.googleAI().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);

Unity


using Firebase;
using Firebase.AI;

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

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
var model = FirebaseAI.DefaultInstance.GetGenerativeModel(
  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: new GenerationConfig(
    responseMimeType: "text/x.enum",
    responseSchema: enumSchema
  )
);

var 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.
";

var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

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

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

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


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