Android ऐप्लिकेशन (क्लाइंट SDK टूल) में Gemini API का इस्तेमाल शुरू करना

इस ट्यूटोरियल में बताया गया है कि Android के लिए, Google AI क्लाइंट SDK का इस्तेमाल करके, Gemini API को सीधे अपने Android ऐप्लिकेशन से कैसे ऐक्सेस किया जा सकता है. अगर आपको अपने 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 AI 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.3.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.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 फ़ाइलों के साथ सिंक करें.

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

कोई भी एपीआई कॉल करने से पहले, आपको 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);

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

  • ऐसे मॉडल का इस्तेमाल करें जो आपके इस्तेमाल के उदाहरण के हिसाब से हो. उदाहरण के लिए, gemini-pro-vision मल्टीमोडल इनपुट के लिए है. इस गाइड में, लागू करने के हर तरीके के निर्देश दिए गए हैं. इसमें बताया गया है कि इस्तेमाल के हर उदाहरण के लिए, किस मॉडल का सुझाव दिया गया है.

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

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

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

जब प्रॉम्प्ट इनपुट में सिर्फ़ टेक्स्ट शामिल हो, तो टेक्स्ट आउटपुट जनरेट करने के लिए, generateContent के साथ gemini-pro मॉडल का इस्तेमाल करें:

Kotlin

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

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

// 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) उपलब्ध कराता है, ताकि आप टेक्स्ट और इमेज दोनों इनपुट कर सकें. प्रॉम्प्ट के लिए इमेज से जुड़ी ज़रूरी शर्तों को ज़रूर देखें.

जब प्रॉम्प्ट इनपुट में टेक्स्ट और इमेज, दोनों शामिल हों, तो टेक्स्ट आउटपुट जनरेट करने के लिए, generateContent वाले gemini-pro-vision मॉडल का इस्तेमाल करें:

Kotlin

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

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

// 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() का इस्तेमाल करें. इससे, मैसेज और चैट का इतिहास भी जुड़ जाएगा.

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

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

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

Kotlin

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

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

// 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() एक निलंबित फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आप Coroutines के बारे में नहीं जानते, तो Android पर Kotlin Coroutines पढ़ें.

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

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

// 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() एक निलंबित फ़ंक्शन है और इसे कोरूटीन स्कोप से कॉल किया जाना चाहिए. अगर आप Coroutines के बारे में नहीं जानते, तो Android पर Kotlin Coroutines पढ़ें.

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

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() को कैसे इस्तेमाल किया जाता है:

Kotlin

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

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

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

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

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

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

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

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 अनुरोध प्रति मिनट (आरपीएम) की दर तय की जा सकती है.

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