ट्यूटोरियल: Gemini API का इस्तेमाल शुरू करना


इस ट्यूटोरियल में बताया गया है कि Android के लिए Google के एआई क्लाइंट SDK टूल का इस्तेमाल करके, सीधे अपने Android ऐप्लिकेशन से Gemini API को कैसे ऐक्सेस किया जा सकता है. अगर आपको Android ऐप्लिकेशन में Gemini के मॉडल ऐक्सेस करने के लिए, सीधे तौर पर REST API या सर्वर साइड कोड (जैसे Python) के साथ काम नहीं करना है, तो इस क्लाइंट SDK टूल का इस्तेमाल किया जा सकता है.

इस ट्यूटोरियल में आप इन कामों को करने का तरीका जानेंगे:

इसके अलावा, इस ट्यूटोरियल में बेहतर इस्तेमाल के उदाहरणों (जैसे, टोकन की गिनती करना) के सेक्शन के साथ-साथ कॉन्टेंट जनरेट करने की प्रोसेस को कंट्रोल करने के विकल्प भी दिए गए हैं.

डिवाइस पर Gemini को ऐक्सेस करें

इस ट्यूटोरियल में Android के क्लाइंट SDK टूल के बारे में बताया गया है. इससे Gemini Pro के उन मॉडल को ऐक्सेस किया जा सकता है जो Google के सर्वर पर चलते हैं. अगर आपको इनमें से किसी भी काम के लिए, संवेदनशील डेटा को प्रोसेस करना, ऑफ़लाइन उपलब्धता या अक्सर इस्तेमाल होने वाले यूज़र फ़्लो से होने वाला खर्च कम करना है, तो Gemini Nano का इस्तेमाल करें. यह डिवाइस पर काम करता है. ज़्यादा जानकारी के लिए, Android (डिवाइस पर) ट्यूटोरियल देखें.

ज़रूरी शर्तें

इस ट्यूटोरियल में हम यह मानकर चलते हैं कि आपने Android ऐप्लिकेशन डेवलप करने के लिए Android Studio का इस्तेमाल करने के बारे में अच्छी तरह से जान लिया है.

इस ट्यूटोरियल को पूरा करने के लिए, पक्का करें कि आपका डेवलपमेंट एनवायरमेंट और Android ऐप्लिकेशन इन ज़रूरी शर्तों को पूरा करते हों:

  • Android Studio (नया वर्शन)
  • आपके Android ऐप्लिकेशन को, एपीआई लेवल 21 या उसके बाद के लेवल को टारगेट करना होगा.

अपना प्रोजेक्ट सेट अप करना

Gemini API को कॉल करने से पहले, आपको अपना Android प्रोजेक्ट सेट अप करना होगा. इसमें, एपीआई पासकोड सेट अप करना, अपने Android प्रोजेक्ट में SDK डिपेंडेंसी जोड़ना, और मॉडल को शुरू करना शामिल है.

एपीआई पासकोड सेट अप करना

Gemini API का इस्तेमाल करने के लिए, आपको एपीआई पासकोड की ज़रूरत होगी. अगर आपके पास पहले से कोई पासकोड नहीं है, तो Google AI Studio में जाकर पासकोड बनाएं.

एपीआई पासकोड पाएं

एपीआई पासकोड को सुरक्षित रखना

हमारा सुझाव है कि आप अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच करें. इसके बजाय, आपको इसे किसी local.properties फ़ाइल में सेव करना चाहिए (जो आपके प्रोजेक्ट की रूट डायरेक्ट्री में मौजूद होती है, लेकिन वर्शन कंट्रोल में शामिल नहीं होती). इसके बाद, अपनी एपीआई कुंजी को बिल्ड कॉन्फ़िगरेशन वैरिएबल के रूप में पढ़ने के लिए, Android के लिए सीक्रेट ग्रेडल प्लगिन का इस्तेमाल करें.

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;

इस ट्यूटोरियल में दिए गए सभी स्निपेट, इस सबसे सही तरीके का इस्तेमाल करते हैं. साथ ही, अगर आपको Secrets Gradle प्लग इन लागू होने का विकल्प देखना है, तो इस SDK टूल के लिए सैंपल ऐप्लिकेशन देखें या Android Studio Iguana की नई झलक का इस्तेमाल करें. इसमें Gemini API Starter का टेंप्लेट (इसमें local.properties फ़ाइल, इस्तेमाल शुरू करने के लिए, इस्तेमाल किया गया है).

अपने प्रोजेक्ट में SDK टूल डिपेंडेंसी जोड़ना

  1. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) Gradle कॉन्फ़िगरेशन फ़ाइल (जैसे, <project>/<app-module>/build.gradle.kts) में, Android के लिए Google के एआई SDK टूल के लिए डिपेंडेंसी जोड़ें:

    Kotlin

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

    Java

    Java के लिए, आपको दो अतिरिक्त लाइब्रेरी जोड़नी होंगी.

    dependencies {
        // ... other androidx dependencies
    
        // add the dependency for the Google AI client SDK for Android
        implementation("com.google.ai.client.generativeai:generativeai:0.9.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 फ़ाइलों के साथ सिंक करें.

जनरेटिव मॉडल शुरू करें

एपीआई कॉल करने से पहले, आपको जनरेटिव मॉडल शुरू करना होगा:

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

Java के लिए, आपको 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);

मॉडल तय करते समय, इन बातों का ध्यान रखें:

  • उस मॉडल का इस्तेमाल करें जो आपके इस्तेमाल के उदाहरण के लिए खास हो. उदाहरण के लिए, gemini-1.5-flash मल्टीमॉडल इनपुट के लिए है. इस गाइड में, हर लागू करने के निर्देशों में, इस्तेमाल के हर उदाहरण के लिए सुझाए गए मॉडल की सूची दी गई है.

इस्तेमाल के सामान्य उदाहरण लागू करना

आपका प्रोजेक्ट सेट अप हो गया है. इसलिए, अब Gemini API के इस्तेमाल के अलग-अलग उदाहरण लागू किए जा सकते हैं:

सिर्फ़ टेक्स्ट इनपुट से टेक्स्ट जनरेट करें

अगर प्रॉम्प्ट इनपुट में सिर्फ़ टेक्स्ट शामिल है, तो टेक्स्ट आउटपुट जनरेट करने के लिए, Gemini के 1.5 मॉडल या generateContent के साथ Gemini 1.0 Pro मॉडल का इस्तेमाल करें:

Kotlin

ध्यान दें कि generateContent() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

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

ध्यान दें कि generateContent(), ListenableFuture दिखाता है. अगर आपको इस एपीआई के बारे में जानकारी नहीं है, तो ListenableFuture का इस्तेमाल करने के बारे में Android का दस्तावेज़ देखें.

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

टेक्स्ट और इमेज इनपुट से टेक्स्ट जनरेट करें (मल्टीमॉडल)

Gemini में ऐसे कई मॉडल उपलब्ध हैं जो मल्टीमोडल इनपुट (Gemini 1.5 मॉडल) को हैंडल कर सकते हैं, ताकि टेक्स्ट और इमेज, दोनों इनपुट किए जा सकें. प्रॉम्प्ट के लिए इमेज से जुड़ी ज़रूरी शर्तें ज़रूर देखें.

अगर प्रॉम्प्ट वाले इनपुट में टेक्स्ट और इमेज, दोनों शामिल हैं, तो टेक्स्ट आउटपुट जनरेट करने के लिए, generateContent के साथ Gemini 1.5 मॉडल का इस्तेमाल करें:

Kotlin

ध्यान दें कि generateContent() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

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

ध्यान दें कि generateContent(), ListenableFuture दिखाता है. अगर आपको इस एपीआई के बारे में जानकारी नहीं है, तो ListenableFuture का इस्तेमाल करने के बारे में Android का दस्तावेज़ देखें.

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

बार-बार होने वाली बातचीत (चैट) की सुविधा तैयार करें

Gemini की मदद से, अलग-अलग मौकों पर फ़्रीफ़ॉर्म तरीके से बातचीत की जा सकती है. SDK टूल में बातचीत की स्थिति को मैनेज करके, इस प्रक्रिया को आसान बनाया जाता है. इसलिए, generateContent की तरह इसमें आपको बातचीत का इतिहास खुद सेव करने की ज़रूरत नहीं पड़ती.

बार-बार होने वाली बातचीत (जैसे, चैट) शुरू करने के लिए, Gemini के 1.5 मॉडल या Gemini 1.0 Pro मॉडल का इस्तेमाल करें. इसके बाद, startChat() पर कॉल करके चैट शुरू करें. इसके बाद, उपयोगकर्ता को नया मैसेज भेजने के लिए sendMessage() का इस्तेमाल करें. इसमें मैसेज के साथ-साथ चैट के इतिहास में जवाब भी शामिल हो जाएगा.

किसी बातचीत के कॉन्टेंट से जुड़े role के लिए, ये दो विकल्प हो सकते हैं:

  • user: वह भूमिका जो प्रॉम्प्ट देती है. यह वैल्यू sendMessage कॉल के लिए डिफ़ॉल्ट तौर पर सेट है.

  • model: वह भूमिका जो जवाब देती है. मौजूदा history के साथ startChat() को कॉल करते समय, इस भूमिका का इस्तेमाल किया जा सकता है.

Kotlin

ध्यान दें कि generateContent() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

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

ध्यान दें कि generateContent(), ListenableFuture दिखाता है. अगर आपको इस एपीआई के बारे में जानकारी नहीं है, तो ListenableFuture का इस्तेमाल करने के बारे में Android का दस्तावेज़ देखें.

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

तेज़ी से इंटरैक्शन करने के लिए स्ट्रीमिंग का इस्तेमाल करें

डिफ़ॉल्ट रूप से, मॉडल जनरेट होने की पूरी प्रोसेस पूरी होने के बाद रिस्पॉन्स दिखाता है. आप पूरे नतीजे का इंतज़ार किए बिना तेज़ी से इंटरैक्शन कर सकते हैं. इसके बजाय, आंशिक नतीजों को मैनेज करने के लिए स्ट्रीमिंग का इस्तेमाल करें.

इस उदाहरण में, टेक्स्ट और इमेज इनपुट प्रॉम्प्ट से टेक्स्ट जनरेट करने के लिए, generateContentStream की मदद से स्ट्रीमिंग लागू करने का तरीका बताया गया है.

Kotlin

ध्यान दें कि generateContentStream() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

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

इस SDK टूल में मौजूद Java स्ट्रीमिंग के तरीकों से, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाया जाता है.

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

सिर्फ़ टेक्स्ट इनपुट और चैट के इस्तेमाल के लिए, इसी तरीके का इस्तेमाल किया जा सकता है:

Kotlin

ध्यान दें कि generateContentStream() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

// 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

इस SDK टूल में मौजूद Java स्ट्रीमिंग के तरीकों से, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाया जाता है.

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

इस्तेमाल के बेहतर उदाहरण लागू करना

इस ट्यूटोरियल के पिछले सेक्शन में, इस्तेमाल के जिन सामान्य उदाहरणों के बारे में बताया गया है उनसे Gemini API को इस्तेमाल करने में मदद मिलती है. इस सेक्शन में, इस्तेमाल के कुछ ऐसे उदाहरणों के बारे में बताया गया है जिन्हें ज़्यादा बेहतर माना जा सकता है.

फ़ंक्शन कॉलिंग

फ़ंक्शन कॉलिंग की मदद से, जनरेटिव मॉडल से स्ट्रक्चर्ड डेटा का आउटपुट पाना आसान हो जाता है. इसके बाद, इन आउटपुट का इस्तेमाल करके अन्य एपीआई को कॉल किया जा सकता है और मॉडल को रिस्पॉन्स का सही डेटा दिया जा सकता है. दूसरे शब्दों में कहें, तो फ़ंक्शन कॉलिंग की मदद से जनरेटिव मॉडल को बाहरी सिस्टम से जोड़ा जा सकता है, ताकि जनरेट किए गए कॉन्टेंट में अप-टू-डेट और सटीक जानकारी हो. फ़ंक्शन कॉलिंग का ट्यूटोरियल में ज़्यादा जानें.

टोकन गिनें

लंबे प्रॉम्प्ट का इस्तेमाल करते समय, मॉडल को कोई भी कॉन्टेंट भेजने से पहले टोकन की गिनती करना मददगार हो सकता है. यहां दिए गए उदाहरणों में, अलग-अलग कामों के लिए countTokens() इस्तेमाल करने का तरीका बताया गया है:

Kotlin

ध्यान दें कि countTokens() एक सस्पेंड फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आपको कोरूटीन के बारे में नहीं पता है, तो Android पर Kootlin कोरूटीन पढ़ें.

// 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 दिखाता है. अगर आपको इस एपीआई के बारे में जानकारी नहीं है, तो ListenableFuture का इस्तेमाल करने के बारे में Android का दस्तावेज़ देखें.

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

सुरक्षा सेटिंग का इस्तेमाल करना

सुरक्षा सेटिंग का इस्तेमाल करके, ऐसे जवाब मिलने की संभावना को कम या ज़्यादा किया जा सकता है जिन्हें नुकसान पहुंचाने वाला माना जा सकता हो. डिफ़ॉल्ट रूप से, सुरक्षा सेटिंग सभी डाइमेंशन में मीडियम और/या असुरक्षित कॉन्टेंट वाले कॉन्टेंट को ब्लॉक कर देती हैं. सुरक्षा की सेटिंग के बारे में ज़्यादा जानें.

यहां एक सुरक्षा सेटिंग सेट करने का तरीका बताया गया है:

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

आप एक से ज़्यादा सुरक्षा सेटिंग भी सेट कर सकते हैं:

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

आगे क्या करना है

  • प्रॉम्प्ट डिज़ाइन में प्रॉम्प्ट बनाने की प्रोसेस होती है, जो लैंग्वेज मॉडल से मनचाहा रिस्पॉन्स देती है. अच्छी तरह से स्ट्रक्चर किए गए प्रॉम्प्ट लिखना, किसी लैंग्वेज मॉडल से सटीक और अच्छी क्वालिटी वाले जवाब पाने का एक ज़रूरी हिस्सा है. प्रॉम्प्ट लिखने के सबसे सही तरीके जानें.

  • अलग-अलग कामों को पूरा करने के लिए, Gemini में कई तरह के मॉडल उपलब्ध हैं. जैसे, इनपुट का टाइप और जटिलता, चैट या डायलॉग की भाषा के अन्य टास्क लागू करना, और साइज़ से जुड़ी शर्तें. Gemini के उपलब्ध मॉडल के बारे में जानें.

  • इस ट्यूटोरियल में Android के क्लाइंट SDK टूल के बारे में बताया गया है. इससे Gemini Pro के उन मॉडल को ऐक्सेस किया जा सकता है जो Google के सर्वर पर चलते हैं. अगर आपको इनमें से किसी भी काम के लिए, संवेदनशील डेटा को प्रोसेस करना, ऑफ़लाइन उपलब्धता या अक्सर इस्तेमाल होने वाले यूज़र फ़्लो से होने वाला खर्च कम करना है, तो Gemini Nano का इस्तेमाल करें. यह डिवाइस पर काम करता है. ज़्यादा जानकारी के लिए, Android (डिवाइस पर) ट्यूटोरियल देखें.