Tutorial ini menunjukkan cara mengakses Gemini API langsung dari aplikasi Android Anda menggunakan SDK klien AI Google untuk Android. Anda dapat menggunakan SDK klien ini jika tidak ingin langsung menggunakan REST API atau kode sisi server (seperti Python) untuk mengakses model Gemini di aplikasi Android Anda.
Dalam tutorial ini, Anda akan mempelajari cara melakukan hal berikut:
- Menyiapkan project Anda, termasuk kunci API Anda
- Membuat teks dari input hanya teks
- Membuat teks dari input teks dan gambar (multimodal)
- Membuat percakapan bolak-balik (chat)
- Gunakan streaming untuk interaksi yang lebih cepat
Selain itu, tutorial ini berisi bagian tentang kasus penggunaan lanjutan (seperti menghitung token) serta opsi untuk mengontrol pembuatan konten.
Pertimbangkan untuk mengakses Gemini di perangkat
SDK klien untuk Android yang dijelaskan dalam tutorial ini memungkinkan Anda mengakses model Gemini Pro yang berjalan di server Google. Untuk kasus penggunaan yang melibatkan pemrosesan data sensitif, ketersediaan offline, atau penghematan biaya bagi alur penggunaan yang sering digunakan, sebaiknya pertimbangkan untuk mengakses Gemini Nano yang berjalan di perangkat. Untuk mengetahui detail selengkapnya, lihat tutorial Android (di perangkat).
Prasyarat
Tutorial ini mengasumsikan bahwa Anda sudah memahami penggunaan Android Studio untuk mengembangkan aplikasi Android.
Untuk menyelesaikan tutorial ini, pastikan lingkungan pengembangan dan aplikasi Android Anda memenuhi persyaratan berikut:
- Android Studio (versi terbaru)
- Aplikasi Android Anda harus menargetkan API level 21 atau yang lebih tinggi.
Menyiapkan project
Sebelum memanggil Gemini API, Anda perlu menyiapkan project Android, yang termasuk menyiapkan kunci API, menambahkan dependensi SDK ke project Android, dan menginisialisasi model.
Menyiapkan kunci API
Untuk menggunakan Gemini API, Anda memerlukan kunci API. Jika Anda belum memilikinya, buat kunci di Google AI Studio.
Mengamankan kunci API Anda
Anda sebaiknya tidak memeriksa kunci API di sistem kontrol
versi. Sebaiknya simpan kunci API dalam file local.properties
(yang terletak di direktori utama project, tetapi dikecualikan dari kontrol
versi), lalu gunakan
plugin Secrets Gradle untuk Android
untuk membaca kunci API Anda sebagai variabel Konfigurasi Build.
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;
Semua cuplikan dalam tutorial ini menggunakan praktik terbaik ini. Selain itu, jika Anda ingin melihat implementasi plugin Secrets Gradle, Anda dapat meninjau aplikasi contoh untuk SDK ini atau menggunakan pratinjau terbaru Android Studio Iguana yang memiliki template Gemini API Starter (yang mencakup file local.properties
untuk membantu Anda memulai).
Menambahkan dependensi SDK ke project
Dalam file konfigurasi Gradle modul (level aplikasi), (seperti
<project>/<app-module>/build.gradle.kts
), tambahkan dependensi untuk Google AI SDK untuk 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.7.0") }
Java
Untuk Java, Anda perlu menambahkan dua library tambahan.
dependencies { // ... other androidx dependencies // add the dependency for the Google AI client SDK for Android implementation("com.google.ai.client.generativeai:generativeai:0.7.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") }
Sinkronkan project Android Anda dengan file Gradle.
Melakukan inisialisasi model generatif
Sebelum dapat melakukan panggilan API, Anda perlu melakukan inisialisasi model generatif:
Kotlin
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with most use cases
modelName = "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey
)
Java
Untuk Java, Anda juga perlu melakukan inisialisasi objek GenerativeModelFutures
.
// Use a model that's applicable for your use case
// The Gemini 1.5 models are versatile and work with most use cases
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// 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);
Saat menentukan model, perhatikan hal-hal berikut:
Gunakan model yang dikhususkan untuk kasus penggunaan Anda (misalnya,
gemini-1.5-flash
untuk input multimodal). Dalam panduan ini, petunjuk untuk setiap penerapan mencantumkan model yang direkomendasikan untuk setiap kasus penggunaan.
Mengimplementasikan kasus penggunaan umum
Setelah project siap, Anda dapat mempelajari penggunaan Gemini API untuk menerapkan berbagai kasus penggunaan:
- Membuat teks dari input hanya teks
- Membuat teks dari input teks dan gambar (multimodal)
- Membuat percakapan bolak-balik (chat)
- Gunakan streaming untuk interaksi yang lebih cepat
Membuat teks dari input hanya teks
Jika input perintah hanya menyertakan teks, gunakan model Gemini 1.5 atau
model Gemini 1.0 Pro dengan generateContent
untuk menghasilkan output teks:
Kotlin
Perhatikan bahwa generateContent()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di Android.
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
modelName = "gemini-1.5-flash",
// 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
Perhatikan bahwa generateContent()
menampilkan ListenableFuture
. Jika
Anda tidak terbiasa dengan API ini, lihat dokumentasi Android tentang
Menggunakan ListenableFuture
.
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// 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);
Membuat teks dari input teks dan gambar (multimodal)
Gemini menyediakan berbagai model yang dapat menangani input multimodal (model Gemini 1.5) sehingga Anda dapat memasukkan teks dan gambar. Pastikan Anda meninjau persyaratan gambar untuk perintah.
Saat input perintah menyertakan teks dan gambar, gunakan model Gemini 1.5 dengan
generateContent
untuk menghasilkan output teks:
Kotlin
Perhatikan bahwa generateContent()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di Android.
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
modelName = "gemini-1.5-flash",
// 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
Perhatikan bahwa generateContent()
menampilkan ListenableFuture
. Jika
Anda tidak terbiasa dengan API ini, lihat dokumentasi Android tentang
Menggunakan ListenableFuture
.
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// 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);
Membuat percakapan bolak-balik (chat)
Dengan Gemini, Anda dapat membangun percakapan berformat bebas dalam beberapa giliran. SDK
menyederhanakan proses dengan mengelola status percakapan. Jadi, tidak seperti
generateContent
, Anda tidak perlu menyimpan histori percakapan
sendiri.
Untuk membuat percakapan multi-giliran (seperti chat), gunakan model Gemini 1.5 atau
model Gemini 1.0 Pro, lalu inisialisasi percakapan dengan memanggil startChat()
.
Kemudian, gunakan sendMessage()
untuk mengirim pesan pengguna baru, yang juga akan menambahkan
pesan dan respons ke histori chat.
Ada dua kemungkinan opsi untuk role
yang terkait dengan konten dalam
percakapan:
user
: peran yang memberikan perintah. Nilai ini adalah default untuk panggilansendMessage
.model
: peran yang memberikan respons. Peran ini dapat digunakan saat memanggilstartChat()
denganhistory
yang ada.
Kotlin
Perhatikan bahwa generateContent()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di Android.
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with multi-turn conversations (like chat)
modelName = "gemini-1.5-flash",
// 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
Perhatikan bahwa generateContent()
menampilkan ListenableFuture
. Jika
Anda tidak terbiasa dengan API ini, lihat dokumentasi Android tentang
Menggunakan ListenableFuture
.
// The Gemini 1.5 models are versatile and work with multi-turn conversations (like chat)
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// 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.Builder userMessageBuilder = new Content.Builder();
userMessageBuilder.setRole("user");
userMessageBuilder.addText("How many paws are in my house?");
Content userMessage = userMessageBuilder.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);
Gunakan streaming untuk interaksi yang lebih cepat
Secara default, model akan menampilkan respons setelah menyelesaikan seluruh proses pembuatan. Anda dapat mencapai interaksi yang lebih cepat dengan tidak menunggu seluruh hasil, dan sebagai gantinya menggunakan streaming untuk menangani hasil parsial.
Contoh berikut menunjukkan cara mengimplementasikan streaming dengan
generateContentStream
untuk membuat teks dari perintah input teks dan gambar.
Kotlin
Perhatikan bahwa generateContentStream()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di Android.
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
modelName = "gemini-1.5-flash",
// 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
Metode streaming Java di SDK ini menampilkan jenis Publisher
dari library Reactive Streams.
// The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// 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);
StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
outputContent.append(chunk);
}
@Override
public void onComplete() {
System.out.println(outputContent);
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
});
Anda dapat menggunakan pendekatan serupa untuk kasus penggunaan chat dan input hanya teks:
Kotlin
Perhatikan bahwa generateContentStream()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di 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
Metode streaming Java di SDK ini menampilkan jenis Publisher
dari library Reactive Streams.
// Use streaming with text-only input
Publisher<GenerateContentResponse> streamingResponse =
model.generateContentStream(inputContent);
StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
outputContent.append(chunk);
}
@Override
public void onComplete() {
System.out.println(outputContent);
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
// ... other methods omitted for brevity
});
// Use streaming with multi-turn conversations (like chat)
ChatFutures chat = model.startChat(history);
Publisher<GenerateContentResponse> streamingResponse =
chat.sendMessageStream(inputContent);
StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
outputContent.append(chunk);
}
@Override
public void onComplete() {
System.out.println(outputContent);
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
// ... other methods omitted for brevity
});
Mengimplementasikan kasus penggunaan lanjutan
Kasus penggunaan umum yang dijelaskan di bagian sebelumnya dalam tutorial ini membantu Anda memahami Gemini API. Bagian ini menjelaskan beberapa kasus penggunaan yang mungkin dianggap lebih lanjut.
Panggilan fungsi
Panggilan fungsi memudahkan Anda mendapatkan output data terstruktur dari model generatif. Selanjutnya, Anda dapat menggunakan output ini untuk memanggil API lain dan menampilkan data respons yang relevan ke model. Dengan kata lain, panggilan fungsi membantu Anda menghubungkan model generatif ke sistem eksternal sehingga konten yang dihasilkan berisi informasi terbaru dan akurat. Pelajari lebih lanjut di tutorial panggilan fungsi.
Hitung token
Saat menggunakan perintah panjang, sebaiknya hitung token sebelum mengirim konten apa pun ke model. Contoh berikut menunjukkan cara menggunakan countTokens()
untuk berbagai kasus penggunaan:
Kotlin
Perhatikan bahwa countTokens()
adalah fungsi penangguhan dan harus
dipanggil dari cakupan Coroutine. Jika Anda belum terbiasa dengan Coroutine, baca
Coroutine Kotlin di 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
Perhatikan bahwa countTokens()
menampilkan ListenableFuture
. Jika
Anda tidak terbiasa dengan API ini, lihat dokumentasi Android tentang
Menggunakan 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]));
Opsi untuk mengontrol pembuatan konten
Anda dapat mengontrol pembuatan konten dengan mengonfigurasi parameter model dan menggunakan setelan keamanan.
Mengonfigurasi parameter model
Setiap perintah yang Anda kirim ke model akan menyertakan parameter value yang mengontrol cara model menghasilkan respons. Model ini dapat memberikan hasil yang berbeda untuk parameter value yang berbeda. Pelajari Parameter model lebih lanjut.
Kotlin
val config = generationConfig {
temperature = 0.9f
topK = 16
topP = 0.1f
maxOutputTokens = 200
stopSequences = listOf("red")
}
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with most use cases
modelName = "gemini-1.5-flash",
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();
// The Gemini 1.5 models are versatile and work with most use cases
GenerativeModel gm = new GenerativeModel(
"gemini-1.5-flash",
BuildConfig.apiKey,
generationConfig
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Gunakan setelan keamanan
Anda dapat menggunakan setelan keamanan untuk menyesuaikan kemungkinan mendapatkan respons yang mungkin dianggap berbahaya. Secara default, setelan keamanan akan memblokir konten dengan probabilitas sedang dan/atau tinggi yang berisi konten yang tidak aman di semua dimensi. Pelajari Setelan keamanan lebih lanjut.
Berikut ini cara menetapkan satu setelan keamanan:
Kotlin
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with most use cases
modelName = "gemini-1.5-flash",
apiKey = BuildConfig.apiKey,
safetySettings = listOf(
SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH)
)
)
Java
SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
BlockThreshold.ONLY_HIGH);
// The Gemini 1.5 models are versatile and work with most use cases
GenerativeModel gm = new GenerativeModel(
"gemini-1.5-flash",
BuildConfig.apiKey,
null, // generation config is optional
Collections.singletonList(harassmentSafety)
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Anda juga dapat menetapkan lebih dari satu setelan keamanan:
Kotlin
val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH)
val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE)
val generativeModel = GenerativeModel(
// The Gemini 1.5 models are versatile and work with most use cases
modelName = "gemini-1.5-flash",
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);
// The Gemini 1.5 models are versatile and work with most use cases
GenerativeModel gm = new GenerativeModel(
"gemini-1.5-flash",
BuildConfig.apiKey,
null, // generation config is optional
Arrays.asList(harassmentSafety, hateSpeechSafety)
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Langkah selanjutnya
Desain prompt adalah proses pembuatan prompt yang mendapatkan respons yang diinginkan dari model bahasa. Menulis dialog yang terstruktur dengan baik adalah bagian penting untuk memastikan respons yang akurat dan berkualitas tinggi dari model bahasa. Pelajari praktik terbaik untuk menulis perintah.
Gemini menawarkan beberapa variasi model untuk memenuhi kebutuhan berbagai kasus penggunaan, seperti jenis input dan kompleksitas, implementasi untuk chat atau tugas bahasa dialog lainnya, serta batasan ukuran. Pelajari model Gemini yang tersedia.
SDK klien untuk Android yang dijelaskan dalam tutorial ini memungkinkan Anda mengakses model Gemini Pro yang berjalan di server Google. Untuk kasus penggunaan yang melibatkan pemrosesan data sensitif, ketersediaan offline, atau penghematan biaya bagi alur penggunaan yang sering digunakan, sebaiknya pertimbangkan untuk mengakses Gemini Nano yang berjalan di perangkat. Untuk mengetahui detail selengkapnya, lihat tutorial Android (di perangkat).