Gemini Developer API

Gemini Developer API ให้คุณเข้าถึงโมเดล Gemini ของ Google ซึ่งช่วยให้คุณสร้างฟีเจอร์ Generative AI ที่ล้ำสมัยในแอป Android ได้ ซึ่งรวมถึงแชทแบบสนทนา การสร้างรูปภาพ (ด้วย Nano Banana) และการสร้างข้อความโดยอิงตามอินพุตข้อความ รูปภาพ เสียง และวิดีโอ

หากต้องการเข้าถึงโมเดล Gemini Pro และ Flash คุณสามารถใช้ Gemini Developer API กับ Firebase AI Logic ซึ่งจะช่วยให้คุณเริ่มต้นใช้งานได้โดยไม่ต้องระบุวิธีการชำระเงิน (เช่น บัตรเครดิต) และมีรุ่นฟรีที่ครอบคลุมการใช้งานอย่างมาก เมื่อตรวจสอบการผสานรวมกับฐานผู้ใช้ขนาดเล็กแล้ว คุณสามารถปรับขนาดได้โดยอัปเกรดเป็นระดับที่ต้องชำระเงิน

ภาพที่แสดงแอป Android ที่ใช้ Firebase Android SDK เพื่อส่งคำขอไปยังแบ็กเอนด์ Firebase ในระบบคลาวด์ ระบบจะกำหนดเส้นทางคำขอไปยัง Gemini Developer API ซึ่งสามารถใช้ประโยชน์จากทั้งโมเดล Gemini Pro และ Flash
รูปที่ 1 สถาปัตยกรรมการผสานรวม Firebase AI Logic เพื่อเข้าถึง Gemini Developer API

เริ่มต้นใช้งาน

ก่อนที่จะโต้ตอบกับ Gemini API โดยตรงจากแอป คุณจะต้องทำสิ่งต่อไปนี้ก่อน ซึ่งรวมถึงการทำความคุ้นเคยกับการเขียนพรอมต์ ตลอดจนการตั้งค่า Firebase และแอปเพื่อใช้ SDK

ทดลองใช้พรอมต์

การทดลองใช้พรอมต์จะช่วยให้คุณค้นหาการใช้ถ้อยคำ เนื้อหา และรูปแบบที่ดีที่สุดสำหรับแอป Android ได้ Google AI Studio เป็นสภาพแวดล้อมในการพัฒนาซอฟต์แวร์แบบผสานรวม (IDE) ที่คุณสามารถใช้เพื่อสร้างต้นแบบและออกแบบพรอมต์สำหรับ Use Case ของแอปได้

การสร้างพรอมต์ที่มีประสิทธิภาพสำหรับ Use Case ของคุณเกี่ยวข้องกับการทดสอบอย่างครอบคลุม ซึ่งเป็นส่วนสำคัญของกระบวนการ ดูข้อมูลเพิ่มเติมเกี่ยวกับการเขียนพรอมต์ได้ในเอกสารประกอบของ Firebase

เมื่อพอใจกับพรอมต์แล้ว ให้คลิกปุ่ม <> เพื่อรับข้อมูลโค้ดที่สามารถเพิ่มลงในโค้ดของคุณได้

ตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase

เมื่อพร้อมที่จะเรียกใช้ API จากแอปแล้ว ให้ทำตามวิธีการใน "ขั้นตอนที่ 1" ของคู่มือเริ่มต้นใช้งาน Firebase AI Logic เพื่อตั้งค่า Firebase และเปิดใช้ API และบริการที่จำเป็น

เพิ่มทรัพยากร Dependency ของ Gradle

เพิ่มทรัพยากร Dependency ของ Gradle ต่อไปนี้ลงในโมดูลแอป

Kotlin

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:35.1.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")
}

Java

dependencies {
  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:35.1.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")

  // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Reactive Streams)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

กำหนดค่าผู้ให้บริการแก้ไขข้อบกพร่องของ App Check สำหรับการพัฒนาในเครื่อง

ตั้งแต่ต้นเดือนกรกฎาคม 2026 เป็นต้นไป Firebase App Check จะได้รับการบังคับใช้โดยอัตโนมัติเพื่อปกป้อง Gemini API โดยเป็นส่วนหนึ่งของเวิร์กโฟลว์การตั้งค่าที่มีคำแนะนำสำหรับ AI Logic ในคอนโซล Firebase สำหรับการพัฒนาในเครื่อง คุณต้องกำหนดค่าผู้ให้บริการแก้ไขข้อบกพร่องของ App Check เพื่อข้ามการรับรองในขณะที่ยังคงบังคับใช้ App Check อยู่

  1. ในบิลด์การแก้ไขข้อบกพร่อง ให้กำหนดค่า App Check เพื่อใช้โรงงานผู้ให้บริการแก้ไขข้อบกพร่อง ดังนี้

    Kotlin

    Firebase.initialize(context = this)
    Firebase.appCheck.installAppCheckProviderFactory(
        DebugAppCheckProviderFactory.getInstance(),
    )
    

    Java

    FirebaseApp.initializeApp(/*context=*/ this);
    FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
    firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
    
  2. รับโทเค็นการแก้ไขข้อบกพร่องโดยทำดังนี้

    1. เรียกใช้แอปในโปรแกรมจำลองหรือในอุปกรณ์ทดสอบ

    2. มองหาโทเค็นการแก้ไขข้อบกพร่องของ App Check ในบันทึก เช่น

      D DebugAppCheckProvider: Enter this debug secret into the allow list
      in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
      
    3. คัดลอกโทเค็น (เช่น 123a4567-b89c-12d3-e456-789012345678)

  3. ลงทะเบียนโทเค็นการแก้ไขข้อบกพร่องด้วย App Check โดยทำดังนี้

    1. ในคอนโซล Firebase ให้ไปที่แท็บความปลอดภัย > App Check > แอป

    2. ค้นหาแอปของคุณ คลิกเมนูรายการเพิ่มเติม () แล้วเลือกจัดการโทเค็นการแก้ไขข้อบกพร่อง

    3. ทำตามวิธีการบนหน้าจอเพื่อลงทะเบียนโทเค็นการแก้ไขข้อบกพร่อง

ดูรายละเอียดเกี่ยวกับผู้ให้บริการแก้ไขข้อบกพร่อง (รวมถึงวิธีรับโทเค็นการแก้ไขข้อบกพร่องใหม่) ได้ที่เอกสารประกอบอย่างเป็นทางการของ App Check

เริ่มต้นโมเดล Generative

เริ่มต้นด้วยการสร้างอินสแตนซ์ GenerativeModel และระบุชื่อโมเดล

Kotlin

// Start by instantiating a GenerativeModel and specifying the model name:
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel("gemini-3.5-flash")

Java

GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.5-flash");

GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);

ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดลที่พร้อมใช้งานกับ Gemini Developer API นอกจากนี้ คุณยังดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่าพารามิเตอร์โมเดลได้ด้วย

โต้ตอบกับ Gemini Developer API จากแอปของคุณ

เมื่อตั้งค่า Firebase และแอปให้ใช้ SDK แล้ว คุณก็พร้อมที่จะโต้ตอบกับ Gemini Developer API จากแอปของคุณ

สร้างข้อความ

หากต้องการสร้างการตอบกลับแบบข้อความ ให้เรียกใช้ generateContent() พร้อมพรอมต์ของคุณ

Kotlin

scope.launch {
    val response = model.generateContent("Write a story about a magic backpack.")
}

Java

Content prompt = new Content.Builder()
        .addText("Write a story about a magic backpack.")
        .build();

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

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

สร้างข้อความจากรูปภาพและสื่ออื่นๆ

นอกจากนี้ คุณยังสร้างข้อความจากพรอมต์ที่มีข้อความและรูปภาพหรือสื่ออื่นๆ ได้ด้วย เมื่อเรียกใช้ generateContent() คุณจะส่งสื่อเป็นข้อมูลในบรรทัดได้

เช่น หากต้องการใช้บิตแมป ให้ใช้ประเภทเนื้อหา image ดังนี้

Kotlin

scope.launch {
    val response = model.generateContent(
        content {
            image(bitmap)
            text("what is the object in the picture?")
        }
    )
}

Java

Content content = new Content.Builder()
        .addImage(bitmap)
        .addText("what is the object in the picture?")
        .build();

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

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

หากต้องการส่งไฟล์เสียง ให้ใช้ประเภทเนื้อหา inlineData ดังนี้

Kotlin

scope.launch {
    val contentResolver = applicationContext.contentResolver
    contentResolver.openInputStream(audioUri).use { stream ->
        stream?.let {
            val bytes = it.readBytes()

            val prompt = content {
                inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type
                text("Transcribe this audio recording.")
            }

            val response = model.generateContent(prompt)
        }
    }
}

Java

ContentResolver resolver = applicationContext.getContentResolver();

try (InputStream stream = resolver.openInputStream(audioUri)) {
    File audioFile = new File(new URI(audioUri.toString()));
    int audioSize = (int) audioFile.length();
    byte[] audioBytes = new byte[audioSize];
    if (stream != null) {
        stream.read(audioBytes, 0, audioBytes.length);
        stream.close();

        // Provide a prompt that includes audio specified earlier and text
        Content prompt = new Content.Builder()
                .addInlineData(audioBytes, "audio/mpeg")  // Specify the appropriate audio MIME type
                .addText("Transcribe what's said in this audio recording.")
                .build();

        // To generate text output, call `generateContent` with the prompt
        ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
        Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse result) {
                String text = result.getText();
                Log.d(TAG, (text == null) ? "" : text);
            }
            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "Failed to generate a response", t);
            }
        }, executor);
    } else {
        Log.e(TAG, "Error getting input stream for file.");
        // Handle the error appropriately
    }
} catch (IOException e) {
    Log.e(TAG, "Failed to read the audio file", e);
} catch (URISyntaxException e) {
    Log.e(TAG, "Invalid audio file", e);
}

และหากต้องการระบุไฟล์วิดีโอ ให้ใช้ประเภทเนื้อหา inlineData ต่อไปนี้

Kotlin

scope.launch {
    val contentResolver = applicationContext.contentResolver
    contentResolver.openInputStream(videoUri).use { stream ->
        stream?.let {
            val bytes = it.readBytes()

            val prompt = content {
                inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type
                text("Describe the content of this video")
            }

            val response = model.generateContent(prompt)
        }
    }
}

Java

ContentResolver resolver = applicationContext.getContentResolver();

try (InputStream stream = resolver.openInputStream(videoUri)) {
    File videoFile = new File(new URI(videoUri.toString()));
    int videoSize = (int) videoFile.length();
    byte[] videoBytes = new byte[videoSize];
    if (stream != null) {
        stream.read(videoBytes, 0, videoBytes.length);
        stream.close();

        // Provide a prompt that includes video specified earlier and text
        Content prompt = new Content.Builder()
                .addInlineData(videoBytes, "video/mp4")
                .addText("Describe the content of this video")
                .build();

        // To generate text output, call generateContent with the prompt
        ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
        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);
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (URISyntaxException e) {
    e.printStackTrace();
}

ในทำนองเดียวกัน คุณยังสามารถส่งเอกสาร PDF (application/pdf) และข้อความธรรมดา (text/plain) ได้โดยส่งประเภท MIME ที่เกี่ยวข้องเป็นพารามิเตอร์

แชทแบบต่อเนื่อง

นอกจากนี้ คุณยังรองรับการสนทนาแบบหลายเทิร์นได้ด้วย เริ่มต้นแชทด้วยฟังก์ชัน startChat() คุณสามารถระบุประวัติข้อความให้กับโมเดลได้ (ไม่บังคับ) จากนั้นเรียกใช้ฟังก์ชัน sendMessage() เพื่อส่งข้อความแชท

Kotlin

val chat = model.startChat(
    history = listOf(
        content(role = "user") { text("Hello, I have 2 dogs in my house.") },
        content(role = "model") { text("Great to meet you. What would you like to know?") }
    )
)

scope.launch {
    val response = chat.sendMessage("How many paws are in my house?")
}

Java

Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();

Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = modelContentBuilder.build();

List<Content> history = Arrays.asList(userContent, modelContent);

// Initialize the chat
ChatFutures chat = model.startChat(history);

// Create a new user message
Content.Builder messageBuilder = new Content.Builder();
messageBuilder.setRole("user");
messageBuilder.addText("How many paws are in my house?");

Content message = messageBuilder.build();

// Send the message
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(message);
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);

สร้างรูปภาพบน Android ด้วย Nano Banana

โมเดลรูปภาพ Gemini (หรือที่เรียกว่า โมเดล Nano Banana) สามารถสร้างและแก้ไขรูปภาพโดยใช้ประโยชน์จากความรู้เกี่ยวกับโลกและการให้เหตุผล โดยจะสร้างรูปภาพที่เกี่ยวข้องตามบริบท ผสมผสานหรือแทรกสลับเอาต์พุตข้อความและรูปภาพได้อย่างราบรื่น นอกจากนี้ ยังสามารถสร้างภาพที่ถูกต้องแม่นยำด้วยลำดับข้อความที่ยาว และรองรับการแก้ไขรูปภาพแบบสนทนาโดยยังคงบริบทไว้

คู่มือนี้อธิบายวิธีใช้โมเดลรูปภาพ Gemini (โมเดล Nano Banana) โดยใช้ Firebase AI Logic SDK สำหรับ Android ดูรายละเอียดเพิ่มเติมเกี่ยวกับการสร้างรูปภาพด้วย Gemini ในเอกสารประกอบของ Firebase

อินเทอร์เฟซของ Google AI Studio ที่แสดงช่องป้อนข้อความพร้อมพรอมต์ 'รูปภาพทีเร็กซ์ที่สมจริงสุดๆ พร้อมกระเป๋าเป้สีน้ำเงินที่เดินเตร่อยู่ในป่ายุคก่อนประวัติศาสตร์' และรูปภาพจาก Generative AI ของทีเร็กซ์ในป่าพร้อมกระเป๋าเป้สีน้ำเงินที่สร้างขึ้น
รูปที่ 2 ใช้ Google AI Studio เพื่อปรับแต่งพรอมต์การสร้างรูปภาพ Nano Banana สำหรับแอป Android ของคุณ

เริ่มต้นโมเดล Generative

สร้างอินสแตนซ์ GenerativeModel และระบุชื่อโมเดล Gemini Image (เช่น gemini-3.1-flash-image) จากนั้นกำหนดค่า responseModalities ให้รวมทั้ง TEXT และ IMAGE เพื่อสร้างข้อความและรูปภาพแบบแทรกสลับ หากคุณยกเว้น TEXT โมเดลจะแสดงเฉพาะรูปภาพเท่านั้น

Kotlin

val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "gemini-3.1-flash-image",
    // Configure the model to respond with images (required) and text (optional)
    generationConfig = generationConfig {
        responseModalities = listOf(
            ResponseModality.TEXT,
            ResponseModality.IMAGE
        )
    }
)

Java

GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()).generativeModel(
        "gemini-3.1-flash-image",
        // Configure the model to respond with text and images (required)
        new GenerationConfig.Builder()
                .setResponseModalities(Arrays.asList(ResponseModality.TEXT, ResponseModality.IMAGE))
                .build()
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

สร้างรูปภาพ (อินพุตแบบข้อความเท่านั้น)

คุณสามารถสั่งให้โมเดล Gemini สร้างรูปภาพได้โดยระบุพรอมต์แบบข้อความเท่านั้น

Kotlin

scope.launch {
    // Provide a text prompt instructing the model to generate an image
    val prompt =
        "A hyper realistic picture of a t-rex with a blue bag pack roaming a pre-historic forest."
    // To generate image output, call `generateContent` with the text input
    val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
        .candidates.first().content.parts.filterIsInstance<ImagePart>()
        .firstOrNull()?.image
}

Java

// Provide a text prompt instructing the model to generate an image
Content prompt = new Content.Builder()
        .addText("Generate an image of the Eiffel Tower with fireworks in the background.")
        .build();
// To generate an image, call `generateContent` with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        // iterate over all the parts in the first candidate in the result object
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                // The returned image as a bitmap
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

แก้ไขรูปภาพ (อินพุตข้อความและรูปภาพ)

คุณขอให้โมเดล Gemini แก้ไขรูปภาพที่มีอยู่ได้โดยระบุทั้งข้อความและรูปภาพอย่างน้อย 1 รูปในพรอมต์ โดยทำดังนี้

Kotlin

scope.launch {
    // Provide a text prompt instructing the model to edit the image
    val prompt = content {
        image(bitmap)
        text("Edit this image to make it look like a cartoon")
    }
    // To edit the image, call `generateContent` with the prompt (image and text input)
    val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
    // Handle the generated text and image
}

Java

// Provide an image for the model to edit
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.scones);
// Provide a text prompt instructing the model to edit the image
Content promptcontent = new Content.Builder()
        .addImage(bitmap)
        .addText("Edit this image to make it look like a cartoon")
        .build();
// To edit the image, call `generateContent` with the prompt (image and text input)
ListenableFuture<GenerateContentResponse> response = model.generateContent(promptcontent);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        // iterate over all the parts in the first candidate in the result object
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

ปรับแต่งและแก้ไขรูปภาพผ่านการแชทแบบหลายเทิร์น

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

ก่อนอื่น ให้เริ่มต้นแชทกับ startChat() โดยระบุประวัติข้อความ (ไม่บังคับ) จากนั้นใช้ sendMessage() สำหรับข้อความถัดไป

Kotlin

scope.launch {
    // Create the initial prompt instructing the model to edit the image
    val prompt = content {
        image(bitmap)
        text("Edit this image to make it look like a cartoon")
    }
    // Initialize the chat
    val chat = model.startChat()
    // To generate an initial response, send a user message with the image and text prompt
    var response = chat.sendMessage(prompt)
    // Inspect the returned image
    var generatedImageAsBitmap: Bitmap? = response
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
    // Follow up requests do not need to specify the image again
    response = chat.sendMessage("But make it old-school line drawing style")
    generatedImageAsBitmap = response
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
}

Java

// Provide an image for the model to edit
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.scones);
// Initialize the chat
ChatFutures chat = model.startChat();
// Create the initial prompt instructing the model to edit the image
Content prompt = new Content.Builder()
        .setRole("user")
        .addImage(bitmap)
        .addText("Edit this image to make it look like a cartoon")
        .build();
// To generate an initial response, send a user message with the image and text prompt
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(prompt);
// Extract the image from the initial response
ListenableFuture<Bitmap> initialRequest = Futures.transform(response,
        result -> {
            for (Part part : result.getCandidates().get(0).getContent().getParts()) {
                if (part instanceof ImagePart) {
                    ImagePart imagePart = (ImagePart) part;
                    return imagePart.getImage();
                }
            }
            return null;
        }, executor);
// Follow up requests do not need to specify the image again
ListenableFuture<GenerateContentResponse> modelResponseFuture = Futures.transformAsync(
        initialRequest,
        generatedImage -> {
            Content followUpPrompt = new Content.Builder()
                    .addText("But make it old-school line drawing style")
                    .build();
            return chat.sendMessage(followUpPrompt);
        }, executor);
// Add a final callback to check the reworked image
Futures.addCallback(modelResponseFuture, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

ข้อควรพิจารณาและข้อจำกัด

โปรดทราบข้อควรพิจารณาและข้อจำกัดต่อไปนี้

  • รูปแบบเอาต์พุต: ระบบจะสร้างรูปภาพเป็น PNG โดยมีขนาดสูงสุด 1024 พิกเซล
  • ประเภทอินพุต: โมเดลไม่รองรับอินพุตเสียงหรือวิดีโอสำหรับการสร้างรูปภาพ
  • การรองรับภาษา: เพื่อประสิทธิภาพที่ดีที่สุด ให้ใช้ภาษาต่อไปนี้ อังกฤษ (en), สเปนเม็กซิโก (es-mx), ญี่ปุ่น (ja-jp), จีนตัวย่อ (zh-cn) และฮินดี (hi-in)
  • ปัญหาเกี่ยวกับการสร้าง
    • การสร้างรูปภาพอาจไม่ทริกเกอร์เสมอไป ซึ่งบางครั้งอาจส่งผลให้ได้เอาต์พุตแบบข้อความเท่านั้น ลองขอเอาต์พุตรูปภาพอย่างชัดเจน (เช่น "สร้างรูปภาพ" "แสดงรูปภาพตามที่ทำไป" "อัปเดตรูปภาพ")
    • โมเดลอาจหยุดสร้างกลางคัน ลองอีกครั้งหรือลองใช้พรอมต์อื่น
    • โมเดลอาจสร้างข้อความเป็นรูปภาพ ลองขอเอาต์พุตข้อความอย่างชัดเจน (เช่น "สร้างข้อความบรรยายพร้อมภาพประกอบ")

ดูรายละเอียดเพิ่มเติมได้ในเอกสารประกอบของ Firebase

ขั้นตอนถัดไป

หลังจากตั้งค่าแอปแล้ว ให้พิจารณาขั้นตอนถัดไปดังนี้