Google AI Android SDK for Gemini API

repository·main·Indexed 21 days ago

https://github.com/google-gemini/deprecated-generative-ai-android

Deprecated Android SDK for the Gemini API, superseded by the Firebase SDK for Vertex AI. This repository provides samples for text generation, photo reasoning, and multi-turn chat conversations, as well as documentation on handling GoogleGenerativeAIException and using the Chat class for context-aware interactions.

Tokens
8.7K
Snippets
33
Records
49
Agent score
75%

What's inside Google AI Android SDK (Deprecated)

  1. Explore Gemini API Android SDK samples

    main

    The samples/ directory provides implementation examples for key features of the Gemini API Android SDK. These samples are available in both Kotlin and Java and cover the following core functionalities:

    • Multi-turn chat conversations: Implementing interactive chat sessions.
    • Executing code: Using the code execution feature.
    • Setting model parameters: Configuring how the model behaves.
    • Controlled generation: Using output constraints like JSON mode.
    • Counting tokens: Measuring input and output token usage.
    • Function calling: Integrating external functions into the model workflow.
    • Safety settings: Configuring and applying safety controls.
    • System instructions: Providing high-level instructions to guide model behavior.
    • Text generation: Basic single-turn text generation.

    These samples are also integrated into the official Gemini API documentation and API reference.

  2. Explore Generative AI capabilities in the Android Sample

    main

    The sample app demonstrates three primary capabilities of the Google Generative AI SDK:

    • Generate Text: Demonstrates the core Text generation feature.
    • Photo Reasoning: Demonstrates MultiModal capabilities (processing images alongside text).
    • Chat: Demonstrates Multi-turn Conversations (maintaining context across multiple exchanges).
  3. Manage project changes with ChangelogPlugin

    main

    The ChangelogPlugin manages change files used to trigger releases and generate release notes. Change files are JSON-encoded instances of a Changelog object, stored by default in the .changes directory at the repo root, organized into subdirectories by project name. Each file specifies the version impact (patch, minor, or major) and an optional end-user message.

    Workflow:

    1. During development, create change files using makeChange.
    2. At release time, use makeReleaseNotes to aggregate all changes into a release_notes.md file.
    3. After release, use deleteChangeFiles to wipe the .changes directory.
  4. How to use the sample snippets for development

    main

    The generativeai-android-sample project is organized to allow you to use code snippets from the com.google.ai.client.generative.samples package as documentation and learning tools. To use them, follow this workflow:

    1. Import the project: Open the generativeai-android-sample project in Android Studio.
    2. Locate code: In the Android project view, navigate to the app module:
      • com.google.ai.client.generative.samples: Contains the individual code snippets.
      • com.google.ai.sample: Contains the actual quickstart application.
    3. Modify and Test: Make changes to the code within the com.google.ai.client.generative.samples package. To compile and verify these changes, compile the entire app module.

    Note on Parity: When adding new snippets, always include both the Kotlin and Java versions simultaneously to ensure language parity.

    1. In Android Studio, import the `generativeai-android-sample` project
    2. In the left-hand bar, using the "Android" perspective, you'll notice that within the `app` module, there are two packages:
       - `com.google.ai.client.generative.samples` which contains the snippets
       - `com.google.ai.sample` which contains the actual quickstart app
  5. Set up the Google AI API Key for the Android Sample

    main

    To use the sample app, you must obtain an API key from Google AI Studio. Once obtained, you must add it to your local environment configuration so the project can access it during development.

    1. Obtain a key from the Google AI Studio setup page.
    2. Open the local.properties file in the root directory of the project.
    3. Add the key using the apiKey property name.
    apiKey=YOUR_API_KEY
  6. Build and publish the Maven repository

    main

    You can publish the Maven repository locally or generate a full releasable version using Gradle tasks.

    • Locally: Use publishToMavenLocal to publish to your local machine's Maven repository.
    • Releasable: Use publishAllPublicationsToMavenRepository to generate a releasable repository. The output will be located in the /m2 directory.
    # Locally publish the m2 repo
    ./gradlew publishToMavenLocal
    
    # Generate a releasable m2 repo
    ./gradlew publishAllPublicationsToMavenRepository
  7. Record changes for release using the `change` command

    main

    The repository uses a custom plugin and a change bash script to manage changelogs. When making changes intended for a release, you must record them to ensure proper release notes and version bumping (major, minor, or patch).

    Changes are stored as files in the .changes directory at the root of the repo.

    Usage Patterns

    • Standard change: Provide a user-facing message. The tooling will automatically determine the API impact. change "your message here"
    • Change without release notes: Run the command without a message if you want to trigger a release without adding to the notes. change
    • Project-specific change: To target a specific project (e.g., generativeai or common) so that the change is only applied to that project's release notes, prefix the message with the project name. change <project_name> "your message here"
    # Record a change with a message
    change "hello world!"
    
    # Record a change without a message (no release notes)
    change
    
    # Record a change specifically for the generativeai project
    change generativeai "hello world!"
    
    # Record a change specifically for the common project
    change common "hello world!"
  8. Handle FunctionCallPart and FunctionResponsePart

    main

    Function calling allows the model to request client-side tool execution.

    • FunctionCallPart: Represents a requested function call from the model, containing the function name and a Map<String, String?>? of args.
    • FunctionResponsePart: Represents the result of a function call that you must return to the model, containing the function name and a JSONObject representing the response.
    // Receiving a call from the model
    if (part is FunctionCallPart) {
        val name = part.name
        val args = part.args
    }
    
    // Sending a response back to the model
    val responsePart = FunctionResponsePart("get_weather", JSONObject("{\"temp\": 72}"))