Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ If you're using Maven, add the following to your dependencies:
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.47.0</version>
<version>1.53.0</version>
</dependency>
</dependencies>
```
Expand All @@ -29,8 +29,8 @@ SDK for Java.

### Create a client
The Google Gen AI Java SDK provides a Client class, simplifying interaction
with both the Gemini API and Vertex AI API. With minimal configuration,
you can seamlessly switch between the 2 backends without rewriting
with both the Gemini API and Gemini Enterprise Agent Platform API. With minimal
configuration, you can seamlessly switch between the 2 backends without rewriting
your code.

#### Instantiate a client that uses Gemini API
Expand All @@ -42,40 +42,40 @@ import com.google.genai.Client;
Client client = Client.builder().apiKey("your-api-key").build();
```

#### Instantiate a client that uses Vertex AI API
#### Instantiate a client that uses Gemini Enterprise Agent Platform API

##### Using project and location

```java
import com.google.genai.Client;

// Use Builder class for instantiation. Explicitly set the project and location,
// and set `vertexAI(true)` to use Vertex AI backend.
// and set `enterprise(true)` to use Gemini Enterprise Agent Platform backend.
Client client = Client.builder()
.project("your-project")
.location("your-location")
.vertexAI(true)
.enterprise(true)
.build();
```

##### Using API key on Vertex AI (GCP Express Mode)
##### Using API key on Gemini Enterprise Agent Platform (GCP Express Mode)

```java
import com.google.genai.Client;

// Explicitly set the `apiKey` and `vertexAI(true)` to use Vertex AI backend
// Explicitly set the `apiKey` and `enterprise(true)` to use Gemini Enterprise Agent Platform backend
// in express mode.
Client client = Client.builder()
.apiKey("your-api-key")
.vertexAI(true)
.enterprise(true)
.build();
```

#### (Optional) Using environment variables:

You can create a client by configuring the necessary environment variables.
Configuration setup instructions depends on whether you're using the Gemini
Developer API or the Gemini API in Vertex AI.
Developer API or the Gemini API in Gemini Enterprise Agent Platform.

**Gemini Developer API:** Set the `GOOGLE_API_KEY`. It will automatically be
picked up by the client. Note that `GEMINI_API_KEY` is a legacy environment
Expand All @@ -86,15 +86,16 @@ variable, it's recommended to use `GOOGLE_API_KEY` only. But if both are set,
export GOOGLE_API_KEY='your-api-key'
```

**Gemini API on Vertex AI:** Set `GOOGLE_GENAI_USE_VERTEXAI`,
**Gemini API on Gemini Enterprise Agent Platform:** Set `GOOGLE_GENAI_USE_ENTERPRISE`,
`GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, or `GOOGLE_API_KEY` for
Vertex AI express mode. It's recommended that you set only project & location,
or API key. But if both are set, project & location takes precedence.
Gemini Enterprise Agent Platform express mode. It's recommended that you set
only project & location, or API key. But if both are set, project & location
takes precedence.

```bash
export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_GENAI_USE_ENTERPRISE=true

// Set project and location for Vertex AI authentication
// Set project and location for Gemini Enterprise Agent Platform authentication
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='us-central1'
// or API key for express mode
Expand All @@ -117,7 +118,7 @@ preview features in the APIs. The stable API endpoints can be selected by
setting the API version to `v1`.

To set the API version use `HttpOptions`. For example, to set the API version to
`v1` for Vertex AI:
`v1` for Gemini Enterprise Agent Platform:

```java
import com.google.genai.Client;
Expand All @@ -126,7 +127,7 @@ import com.google.genai.types.HttpOptions;
Client client = Client.builder()
.project("your-project")
.location("your-location")
.vertexAI(true)
.enterprise(true)
.httpOptions(HttpOptions.builder().apiVersion("v1"))
.build();
```
Expand Down Expand Up @@ -284,10 +285,10 @@ import com.google.genai.types.Part;

public class GenerateContentWithImageInput {
public static void main(String[] args) {
// Instantiate the client using Vertex API. The client gets the project and
// Instantiate the client using Gemini Enterprise Agent Platform API. The client gets the project and
// location from the environment variables `GOOGLE_CLOUD_PROJECT` and
// `GOOGLE_CLOUD_LOCATION`.
Client client = Client.builder().vertexAI(true).build();
Client client = Client.builder().enterprise(true).build();

// Construct a multimodal content with quick constructors
Content content =
Expand Down Expand Up @@ -563,7 +564,7 @@ public class CountTokens {
```

The `computeTokens` method returns the Tokens Info that contains tokens and
token IDs given your prompt. This method is only supported in Vertex AI.
token IDs given your prompt. This method is only supported in Gemini Enterprise Agent Platform.

```java
package <your package name>;
Expand All @@ -573,7 +574,7 @@ import com.google.genai.types.ComputeTokensResponse;

public class ComputeTokens {
public static void main(String[] args) {
Client client = Client.builder().vertexAI(true).build();
Client client = Client.builder().enterprise(true).build();

ComputeTokensResponse response =
client.models.computeTokens("gemini-2.5-flash", "What is your name?", null);
Expand All @@ -586,7 +587,7 @@ public class ComputeTokens {
#### Embed Content

The `embedContent` method allows you to generate embeddings for words, phrases,
sentences, and code, as well as multimodal content like images or videos via Vertex AI.
sentences, and code, as well as multimodal content like images or videos via Gemini Enterprise Agent Platform.

```java
package <your package name>;
Expand All @@ -604,8 +605,8 @@ public class EmbedContent {

System.out.println("Embedding response: " + response);

// Multimodal embedding with Vertex AI
Client vertexClient = Client.builder().vertexAI(true).build();
// Multimodal embedding with Gemini Enterprise Agent Platform
Client enterpriseClient = Client.builder().enterprise(true).build();
EmbedContentConfig config =
EmbedContentConfig.builder()
.outputDimensionality(10)
Expand All @@ -614,7 +615,7 @@ public class EmbedContent {
.build();

EmbedContentResponse mmResponse =
vertexClient.models.embedContent(
enterpriseClient.models.embedContent(
"gemini-embedding-2-exp-11-2025",
Content.fromParts(
Part.fromText("Hello"),
Expand Down Expand Up @@ -669,7 +670,7 @@ public class GenerateImages {
#### Upscale Image

The `upscaleImage` method allows you to upscale an image. This feature is only
supported in Vertex AI.
supported in Gemini Enterprise Agent Platform.

```java
package <your package name>;
Expand All @@ -681,7 +682,7 @@ import com.google.genai.types.UpscaleImageResponse;

public class UpscaleImage {
public static void main(String[] args) {
Client client = Client.builder().vertexAI(true).build();
Client client = Client.builder().enterprise(true).build();

Image image = Image.fromFile("path/to/your/image");

Expand Down Expand Up @@ -712,7 +713,7 @@ The `editImage` method lets you edit an image. You can input reference images
addition to a text prompt to guide the editing.

This feature uses a different model than `generateImages` and `upscaleImage`. It
is only supported in Vertex AI.
is only supported in Gemini Enterprise Agent Platform.

```java
package <your package name>;
Expand All @@ -731,7 +732,7 @@ import java.util.ArrayList;

public class EditImage {
public static void main(String[] args) {
Client client = Client.builder().vertexAI(true).build();
Client client = Client.builder().enterprise(true).build();

Image image = Image.fromFile("path/to/your/image");

Expand Down Expand Up @@ -986,10 +987,9 @@ The Google Gen AI Java SDK will accept contributions in the future.
Apache 2.0 - See [LICENSE][license] for more information.

[gemini-api-doc]: https://ai.google.dev/gemini-api/docs
[vertex-api-doc]: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview
[gemini-enterprise-agent-platform-doc]: https://docs.cloud.google.com/gemini-enterprise-agent-platform
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.genai/google-genai.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.genai/google-genai
[javadoc-image]: https://img.shields.io/badge/JavaDoc-Online-green
[javadoc-link]: https://googleapis.github.io/java-genai/javadoc/
[license]: https://github.com/googleapis/java-genai/blob/main/LICENSE

[license]: https://github.com/googleapis/java-genai/blob/main/LICENSE
Loading