java-genai  by googleapis

Java SDK for Google's generative AI models

Created 11 months ago
258 stars

Top 98.1% on SourcePulse

GitHubView on GitHub
Project Summary

This Java SDK provides a Java-idiomatic interface for integrating Google's generative AI models, specifically the Gemini Developer APIs and Vertex AI APIs, into Java applications. It targets Java developers looking to leverage advanced AI capabilities like text generation, multimodal understanding, image generation, and video generation. The primary benefit is a unified client that allows seamless switching between Gemini and Vertex AI backends without code refactoring.

How It Works

The SDK centers around a Client class, which acts as a facade for interacting with Google's generative AI services. Using a builder pattern, developers can configure the client to target either the Gemini API or Vertex AI, specifying authentication details like API keys or GCP project and location. This abstraction simplifies backend management and allows for flexible deployment strategies. The SDK handles underlying HTTP requests, retries, and API versioning, offering a streamlined developer experience.

Quick Start & Requirements

  • Installation: Add the following dependency to your Maven pom.xml:
    <dependency>
      <groupId>com.google.genai</groupId>
      <artifactId>google-genai</artifactId>
      <version>1.24.0</version>
    </dependency>
    
  • Prerequisites: A Java Development Kit (JDK) and a Maven build environment. API keys or GCP project/location credentials are required for authentication.
  • Authentication: Can be configured via direct builder parameters (apiKey, project, location, vertexAI(true)) or environment variables (GOOGLE_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_LOCATION).
  • API Versioning: By default, beta API endpoints are used. Stable (v1) or specific alpha versions can be selected via HttpOptions.

Highlighted Details

  • Unified Client: Seamlessly switch between Gemini API and Vertex AI backends.
  • Multimodal Support: Generate content using text and image inputs.
  • Advanced Configuration: Customize system instructions, safety settings, tools (including Google Search), and thinking configurations.
  • Automatic Function Calling (AFC): Enables models to invoke Java methods, requiring specific Maven compiler arguments (-parameters).
  • Streaming & Async: Supports streaming responses and asynchronous API calls for improved responsiveness.
  • Structured Output: Generate responses directly in JSON format using a defined schema.
  • Token Management: Utilities for counting tokens (countTokens) and computing token details (computeTokens, Vertex AI only).
  • Embeddings: Generate text embeddings using embedContent.
  • Imagen: Supports text-to-image generation, image upscaling (Vertex AI only), and image editing (Vertex AI only).
  • Veo: Provides video generation capabilities from text or images, returning an asynchronous operation.
  • HTTP Customization: Flexible configuration for HTTP requests, including timeouts, headers, base URLs, and automatic retry logic.

Maintenance & Community

The library follows Semantic Versioning. Contributions are expected to be accepted in the future. No specific community channels (e.g., Discord, Slack) or roadmap links are provided in the README.

Licensing & Compatibility

The SDK is licensed under the Apache 2.0 license, which is permissive for commercial use and integration into closed-source projects.

Limitations & Caveats

Certain advanced features like computeTokens, upscaleImage, and editImage are exclusively supported on Vertex AI. The SDK defaults to beta API endpoints, requiring explicit configuration for stable versions. Video generation and some image operations return asynchronous operations that necessitate polling for completion. Automatic Function Calling requires specific compiler configurations.

Health Check
Last Commit

20 hours ago

Responsiveness

Inactive

Pull Requests (30d)
64
Issues (30d)
6
Star History
25 stars in the last 30 days

Explore Similar Projects

Feedback? Help us improve.