เริ่มต้นใช้งาน Gemini API ในแอป Android (SDK ของไคลเอ็นต์)

บทแนะนำนี้จะสาธิตวิธีเข้าถึง Gemini API จากแอป Android ของคุณโดยตรงโดยใช้ SDK ของไคลเอ็นต์ Google AI สำหรับ Android คุณใช้ SDK ของไคลเอ็นต์นี้ได้หากไม่ต้องการทำงานกับ REST API หรือโค้ดฝั่งเซิร์ฟเวอร์โดยตรง (เช่น Python) สำหรับการเข้าถึงโมเดล Gemini ในแอป Android

ในบทแนะนำนี้ คุณจะได้เรียนรู้วิธีทำสิ่งต่อไปนี้

บทแนะนำนี้ยังประกอบด้วยส่วนต่างๆ เกี่ยวกับกรณีการใช้งานขั้นสูง (เช่น การนับโทเค็น) รวมถึงตัวเลือกสำหรับการควบคุมการสร้างเนื้อหา

พิจารณาการเข้าถึง Gemini ในอุปกรณ์

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

ข้อกำหนดเบื้องต้น

บทแนะนำนี้จะถือว่าคุณคุ้นเคยกับการใช้ Android Studio เพื่อพัฒนาแอป Android

ในบทแนะนำนี้ โปรดตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Android ของคุณเป็นไปตามข้อกำหนดต่อไปนี้

  • Android Studio (เวอร์ชันล่าสุด)
  • แอป Android ต้องกำหนดเป้าหมายเป็น API ระดับ 21 ขึ้นไป

ตั้งค่าโปรเจ็กต์

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

ตั้งค่าคีย์ API

หากต้องการใช้ Gemini API คุณจะต้องมีคีย์ API หากยังไม่มี ให้ สร้างคีย์ใน Google AI Studio

รับคีย์ API

รักษาความปลอดภัยของคีย์ API

ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน แต่ควรเก็บไว้ในไฟล์ local.properties (ซึ่งอยู่ในไดเรกทอรีรูทของโปรเจ็กต์ แต่ไม่รวมอยู่ในการควบคุมเวอร์ชัน) แล้วใช้ปลั๊กอิน Secret Gradle สำหรับ Android เพื่ออ่านคีย์ API เป็นตัวแปรการกําหนดค่าบิลด์

Kotlin

// Access your API key as a Build Configuration variable
val apiKey = BuildConfig.apiKey

Java

// Access your API key as a Build Configuration variable
String apiKey = BuildConfig.apiKey;

ตัวอย่างทั้งหมดในบทแนะนำนี้จะใช้แนวทางปฏิบัติแนะนำนี้ นอกจากนี้ หากต้องการดูการติดตั้งใช้งานปลั๊กอิน Secret Gradle โปรดดูแอปตัวอย่าง สำหรับ SDK นี้ หรือใช้ตัวอย่างล่าสุดของ Android Studio Iguana ซึ่งมี เทมเพลต Gemini API Starter (ซึ่งมีไฟล์ local.properties ให้คุณเริ่มต้นใช้งาน)

เพิ่มทรัพยากร Dependency ของ SDK ไปยังโปรเจ็กต์ของคุณ

  1. ในโมดูล (ระดับแอป) ไฟล์การกำหนดค่า Gradle (เช่น <project>/<app-module>/build.gradle.kts) ให้เพิ่มทรัพยากร Dependency สำหรับ Google AI SDK สำหรับ Android ดังนี้

    Kotlin

    dependencies {
      // ... other androidx dependencies
    
      // add the dependency for the Google AI client SDK for Android
      implementation("com.google.ai.client.generativeai:generativeai:0.3.0")
    }
    

    Java

    สำหรับ Java คุณต้องเพิ่มไลบรารีอีก 2 รายการ

    dependencies {
        // ... other androidx dependencies
    
        // add the dependency for the Google AI client SDK for Android
        implementation("com.google.ai.client.generativeai:generativeai:0.3.0")
    
        // 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")
    }
    
  2. ซิงค์โปรเจ็กต์ Android กับไฟล์ Gradle

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

ก่อนที่จะเรียก API คุณต้องเริ่มต้นออบเจ็กต์ GenerativeModel โดยทำดังนี้

Kotlin

val generativeModel = GenerativeModel(
    // Use a model that's applicable for your use case (see "Implement basic use cases" below)
    modelName = "MODEL_NAME",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

Java

สำหรับ Java คุณต้องเริ่มต้นออบเจ็กต์ GenerativeModelFutures ด้วย

// Use a model that's applicable for your use case (see "Implement basic use cases" below)
GenerativeModel gm = new GenerativeModel(/* modelName */ "MODEL_NAME",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

เมื่อระบุโมเดล โปรดทราบข้อมูลต่อไปนี้

  • ใช้รูปแบบที่เหมาะกับ Use Case ของคุณโดยเฉพาะ (เช่น gemini-pro-vision สำหรับอินพุตหลายรูปแบบ) ในคู่มือนี้ วิธีการใช้รายการโมเดลที่แนะนำ สำหรับแต่ละกรณีการใช้งาน

ใช้ Use Case ที่พบบ่อย

เมื่อตั้งค่าโปรเจ็กต์แล้ว คุณจะสํารวจโดยใช้ Gemini API เพื่อนํากรณีการใช้งานต่างๆ ไปใช้ได้ ดังนี้

สร้างข้อความจากการป้อนข้อความเท่านั้น

เมื่ออินพุตของพรอมต์มีเฉพาะข้อความ ให้ใช้โมเดล gemini-pro กับ generateContent เพื่อสร้างเอาต์พุตข้อความ

Kotlin

โปรดทราบว่า generateContent() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

val generativeModel = GenerativeModel(
    // For text-only input, use the gemini-pro model
    modelName = "gemini-pro",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

val prompt = "Write a story about a magic backpack."
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

โปรดทราบว่า generateContent() แสดงผล ListenableFuture หากคุณไม่คุ้นเคยกับ API นี้ โปรดดูเอกสารประกอบของ Android เกี่ยวกับการใช้ ListenableFuture

// For text-only input, use the gemini-pro model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

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

Executor executor = // ...

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);

สร้างข้อความจากการป้อนข้อความและรูปภาพ (มัลติโมดัล)

Gemini มีโมเดลแบบหลายโมดัล (gemini-pro-vision) เพื่อให้คุณป้อนทั้งข้อความและรูปภาพ โปรดอ่านข้อกำหนดเกี่ยวกับรูปภาพสำหรับข้อความแจ้ง

เมื่ออินพุตของพรอมต์มีทั้งข้อความและรูปภาพ ให้ใช้โมเดล gemini-pro-vision ที่มี generateContent เพื่อสร้างเอาต์พุตข้อความ

Kotlin

โปรดทราบว่า generateContent() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

val generativeModel = GenerativeModel(
    // For text-and-images input (multimodal), use the gemini-pro-vision model
    modelName = "gemini-pro-vision",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

val image1: Bitmap = // ...
val image2: Bitmap = // ...

val inputContent = content {
    image(image1)
    image(image2)
    text("What's different between these pictures?")
}

val response = generativeModel.generateContent(inputContent)
print(response.text)

Java

โปรดทราบว่า generateContent() แสดงผล ListenableFuture หากคุณไม่คุ้นเคยกับ API นี้ โปรดดูเอกสารประกอบของ Android เกี่ยวกับการใช้ ListenableFuture

// For text-and-images input (multimodal), use the gemini-pro-vision model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro-vision",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Bitmap image1 = // ...
Bitmap image2 = // ...

Content content = new Content.Builder()
    .addText("What's different between these pictures?")
    .addImage(image1)
    .addImage(image2)
    .build();

Executor executor = // ...

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);

สร้างการสนทนาแบบหลายมุมมอง (แชท)

การใช้ Gemini จะช่วยให้คุณสร้างการสนทนารูปแบบอิสระได้ในหลายๆ จุด SDK ช่วยลดความซับซ้อนของกระบวนการด้วยการจัดการสถานะของการสนทนา จึงต่างจาก generateContent ตรงที่คุณไม่ต้องจัดเก็บประวัติการสนทนาเอง

หากต้องการสร้างการสนทนาแบบมัลติเทิร์น (เช่น แชท) ให้ใช้โมเดล gemini-pro และเริ่มต้นแชทโดยเรียกใช้ startChat() จากนั้นใช้ sendMessage() เพื่อส่งข้อความของผู้ใช้ใหม่ ซึ่งจะต่อท้ายข้อความและการตอบกลับประวัติการแชทด้วย

มี 2 ตัวเลือกที่เป็นไปได้สำหรับ role ที่เชื่อมโยงกับเนื้อหาในการสนทนา ดังนี้

  • user: บทบาทที่แสดงข้อความแจ้ง ค่านี้เป็นค่าเริ่มต้นสำหรับการเรียก sendMessage

  • model: บทบาทที่ระบุคำตอบ บทบาทนี้สามารถใช้เมื่อเรียกใช้ startChat() ด้วย history ที่มีอยู่

Kotlin

โปรดทราบว่า generateContent() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

val generativeModel = GenerativeModel(
    // For text-only input, use the gemini-pro model
    modelName = "gemini-pro",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

val chat = generativeModel.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?") }
    )
)

chat.sendMessage("How many paws are in my house?")

Java

โปรดทราบว่า generateContent() แสดงผล ListenableFuture หากคุณไม่คุ้นเคยกับ API นี้ โปรดดูเอกสารประกอบของ Android เกี่ยวกับการใช้ ListenableFuture

// For text-only input, use the gemini-pro model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// (optional) Create previous chat history for context
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 = userContentBuilder.build();

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

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

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

Executor executor = // ...

// Send the message
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(userMessage);

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);

ใช้สตรีมมิงเพื่อให้โต้ตอบได้เร็วขึ้น

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

ตัวอย่างต่อไปนี้แสดงวิธีใช้งานสตรีมมิงด้วย generateContentStream เพื่อสร้างข้อความจากพรอมต์ข้อความและรูปภาพ

Kotlin

โปรดทราบว่า generateContentStream() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

val generativeModel = GenerativeModel(
    // For text-and-image input (multimodal), use the gemini-pro-vision model
    modelName = "gemini-pro-vision",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

val image1: Bitmap = // ...
val image2: Bitmap = // ...

val inputContent = content {
    image(image1)
    image(image2)
    text("What's the difference between these pictures?")
}

var fullResponse = ""
generativeModel.generateContentStream(inputContent).collect { chunk ->
    print(chunk.text)
    fullResponse += chunk.text
}

Java

เมธอดสตรีมมิง Java ใน SDK นี้แสดงผลประเภท Publisher จากไลบรารี Reactive Streams

// For text-and-images input (multimodal), use the gemini-pro-vision model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro-vision",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
    /* apiKey */ BuildConfig.apiKey);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Bitmap image1 = // ...
Bitmap image2 = // ...

Content content = new Content.Builder()
    .addText("What's different between these pictures?")
    .addImage(image1)
    .addImage(image2)
    .build();

Publisher<GenerateContentResponse> streamingResponse =
    model.generateContentStream(content);

final String[] fullResponse = {""};

streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
    @Override
    public void onNext(GenerateContentResponse generateContentResponse) {
        String chunk = generateContentResponse.getText();
        fullResponse[0] += chunk;
    }

    @Override
    public void onComplete() {
        System.out.println(fullResponse[0]);
    }

    @Override
    public void onError(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onSubscribe(Subscription s) { }
});

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

Kotlin

โปรดทราบว่า generateContentStream() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

// Use streaming with text-only input
generativeModel.generateContentStream(inputContent).collect { chunk ->
    print(chunk.text)
}
// Use streaming with multi-turn conversations (like chat)
val chat = generativeModel.startChat()
chat.sendMessageStream(inputContent).collect { chunk ->
    print(chunk.text)
}

Java

เมธอดสตรีมมิง Java ใน SDK นี้แสดงผลประเภท Publisher จากไลบรารี Reactive Streams

// Use streaming with text-only input
Publisher<GenerateContentResponse> streamingResponse =
    model.generateContentStream(inputContent);

final String[] fullResponse = {""};

streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
    @Override
    public void onNext(GenerateContentResponse generateContentResponse) {
        String chunk = generateContentResponse.getText();
        fullResponse[0] += chunk;
    }

    @Override
    public void onComplete() {
        System.out.println(fullResponse[0]);
    }

    // ... other methods omitted for brevity
});
// Use streaming with multi-turn conversations (like chat)
ChatFutures chat = model.startChat(history);

Publisher<GenerateContentResponse> streamingResponse =
    chat.sendMessageStream(inputContent);

final String[] fullResponse = {""};

streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
    @Override
    public void onNext(GenerateContentResponse generateContentResponse) {
        String chunk = generateContentResponse.getText();
        fullResponse[0] += chunk;
    }

    @Override
    public void onComplete() {
        System.out.println(fullResponse[0]);
    }

    // ... other methods omitted for brevity
});

นำกรณีการใช้งานขั้นสูงไปใช้

กรณีการใช้งานทั่วไปที่อธิบายไว้ในส่วนก่อนหน้าของบทแนะนำนี้ช่วยให้คุณคุ้นเคยกับการใช้ Gemini API ได้ ส่วนนี้จะอธิบายกรณีการใช้งานที่อาจถือว่ามีขั้นสูงกว่า

นับโทเค็น

เมื่อใช้พรอมต์แบบยาว การนับโทเค็นก่อนที่จะส่งเนื้อหาไปยังโมเดลอาจเป็นประโยชน์ ตัวอย่างต่อไปนี้แสดงวิธีใช้ countTokens() สำหรับ Use Case ต่างๆ

Kotlin

โปรดทราบว่า countTokens() เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน Kotlin Coroutines บน Android

// For text-only input
val (totalTokens) = generativeModel.countTokens("Write a story about a magic backpack.")

// For text-and-image input (multi-modal)
val multiModalContent = content {
    image(image1)
    image(image2)
    text("What's the difference between these pictures?")
}

val (totalTokens) = generativeModel.countTokens(multiModalContent)

// For multi-turn conversations (like chat)
val history = chat.history
val messageContent = content { text("This is the message I intend to send")}
val (totalTokens) = generativeModel.countTokens(*history.toTypedArray(), messageContent)

Java

โปรดทราบว่า countTokens() แสดงผล ListenableFuture หากคุณไม่คุ้นเคยกับ API นี้ โปรดดูเอกสารประกอบของ Android เกี่ยวกับการใช้ ListenableFuture

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

Executor executor = // ...

// For text-only input
ListenableFuture<CountTokensResponse> countTokensResponse = model.countTokens(text);

Futures.addCallback(countTokensResponse, new FutureCallback<CountTokensResponse>() {
    @Override
    public void onSuccess(CountTokensResponse result) {
        int totalTokens = result.getTotalTokens();
        System.out.println("TotalTokens = " + totalTokens);
    }

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

// For text-and-image input
Bitmap image1 = // ...
Bitmap image2 = // ...

Content multiModalContent = new Content.Builder()
    .addImage(image1)
    .addImage(image2)
    .addText("What's different between these pictures?")
    .build();

ListenableFuture<CountTokensResponse> countTokensResponse = model.countTokens(multiModalContent);

// For multi-turn conversations (like chat)
List<Content> history = chat.getChat().getHistory();

Content messageContent = new Content.Builder()
    .addText("This is the message I intend to send")
    .build();

Collections.addAll(history, messageContent);

ListenableFuture<CountTokensResponse> countTokensResponse = model.countTokens(history.toArray(new Content[0]));

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

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

กำหนดค่าพารามิเตอร์โมเดล

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

Kotlin

val config = generationConfig {
    temperature = 0.9f
    topK = 16
    topP = 0.1f
    maxOutputTokens = 200
    stopSequences = listOf("red")
}

val generativeModel = GenerativeModel(
    modelName = "MODEL_NAME",
    apiKey = BuildConfig.apiKey,
    generationConfig = config
)

Java

GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.temperature = 0.9f;
configBuilder.topK = 16;
configBuilder.topP = 0.1f;
configBuilder.maxOutputTokens = 200;
configBuilder.stopSequences = Arrays.asList("red");

GenerationConfig generationConfig = configBuilder.build();

GenerativeModel gm = new GenerativeModel(
    "MODEL_NAME",
    BuildConfig.apiKey,
    generationConfig
);

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

ใช้การตั้งค่าความปลอดภัย

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

วิธีตั้งค่าความปลอดภัย 1 รายการมีดังนี้

Kotlin

val generativeModel = GenerativeModel(
    modelName = "MODEL_NAME",
    apiKey = BuildConfig.apiKey,
    safetySettings = listOf(
        SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH)
    )
)

Java

SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
    BlockThreshold.ONLY_HIGH);

GenerativeModel gm = new GenerativeModel(
    "MODEL_NAME",
    BuildConfig.apiKey,
    null, // generation config is optional
    Collections.singletonList(harassmentSafety)
);

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

คุณยังกำหนดการตั้งค่าความปลอดภัยได้มากกว่า 1 รายการดังนี้

Kotlin

val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH)

val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE)

val generativeModel = GenerativeModel(
    modelName = "MODEL_NAME",
    apiKey = BuildConfig.apiKey,
    safetySettings = listOf(harassmentSafety, hateSpeechSafety)
)

Java

SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
    BlockThreshold.ONLY_HIGH);

SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH,
    BlockThreshold.MEDIUM_AND_ABOVE);

GenerativeModel gm = new GenerativeModel(
    "MODEL_NAME",
    BuildConfig.apiKey,
    null, // generation config is optional
    Arrays.asList(harassmentSafety, hateSpeechSafety)
);

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

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

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

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

  • Gemini มีตัวเลือกในการขอเพิ่มขีดจำกัดอัตราคำขอ ขีดจำกัดอัตราคำขอสำหรับรุ่น Gemini Pro คือ 60 คำขอต่อนาที (RPM)

  • SDK ของไคลเอ็นต์สำหรับ Android ที่อธิบายไว้ในบทแนะนำนี้ช่วยให้คุณเข้าถึงโมเดล Gemini Pro ที่ทำงานบนเซิร์ฟเวอร์ของ Google ได้ สำหรับกรณีการใช้งานที่เกี่ยวข้องกับการประมวลผลข้อมูลที่ละเอียดอ่อน การใช้งานแบบออฟไลน์ หรือการประหยัดค่าใช้จ่ายสำหรับการไหลเวียนของผู้ใช้ที่ใช้บ่อย คุณอาจต้องพิจารณาเข้าถึง Gemini Nano ซึ่งทำงานในอุปกรณ์ ดูรายละเอียดเพิ่มเติมได้ที่บทแนะนำของ Android (ในอุปกรณ์)