Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,13 @@ public class ComputeTokens {
#### Embed Content

The `embedContent` method allows you to generate embeddings for words, phrases,
sentences, and code. Note that only text embedding is supported in this method.
sentences, and code, as well as multimodal content like images or videos via Vertex AI.

```java
package <your package name>;

import com.google.genai.Client;
import com.google.genai.types.EmbedContentConfig;
import com.google.genai.types.EmbedContentResponse;

public class EmbedContent {
Expand All @@ -602,6 +603,24 @@ public class EmbedContent {
client.models.embedContent("gemini-embedding-001", "why is the sky blue?", null);

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

// Multimodal embedding with Vertex AI
Client vertexClient = Client.builder().vertexAI(true).build();
EmbedContentConfig config =
EmbedContentConfig.builder()
.outputDimensionality(10)
.title("test_title")
.taskType("RETRIEVAL_DOCUMENT")
.build();

EmbedContentResponse mmResponse =
vertexClient.models.embedContent(
"gemini-embedding-2-exp-11-2025",
Content.fromParts(
Part.fromText("Hello"),
Part.fromUri("gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png", "image/png")),
config);
System.out.println("Multimodal embedding response: " + mmResponse);
}
}
```
Expand Down
20 changes: 20 additions & 0 deletions examples/src/main/java/com/google/genai/examples/EmbedContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.types.Content;
import com.google.genai.types.EmbedContentResponse;
import com.google.genai.types.FileData;
import com.google.genai.types.Part;
import java.util.Arrays;

/** An example of using the Unified Gen AI Java SDK to embed content. */
public final class EmbedContent {
Expand Down Expand Up @@ -73,6 +77,22 @@ public static void main(String[] args) {
client.models.embedContent(modelId, "why is the sky blue?", null);

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

if (client.vertexAI()) {
System.out.println("Embed content with GCS image example.");
Part textPart = Part.builder().text("What is in this image?").build();
Part imagePart =
Part.builder()
.fileData(
FileData.builder()
.fileUri("gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png")
.mimeType("image/png")
.build())
.build();
Content content = Content.builder().parts(Arrays.asList(textPart, imagePart)).build();
response = client.models.embedContent("gemini-embedding-2-exp-11-2025", content, null);
System.out.println("Embedding response with GCS image: " + response);
}
}

private EmbedContent() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.types.Content;
import com.google.genai.types.EmbedContentResponse;
import com.google.genai.types.FileData;
import com.google.genai.types.Part;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/** An example of using the Unified Gen AI Java SDK to embed content asynchronously. */
Expand Down Expand Up @@ -79,6 +83,28 @@ public static void main(String[] args) {
System.out.println("Async embedding response: " + response);
})
.join();

// Vertex Multimodal embedding.
if (client.vertexAI()) {
System.out.println("Embed content with GCS image example.");
Part textPart = Part.builder().text("What is in this image?").build();
Part imagePart =
Part.builder()
.fileData(
FileData.builder()
.fileUri("gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png")
.mimeType("image/png")
.build())
.build();
Content content = Content.builder().parts(Arrays.asList(textPart, imagePart)).build();
responseFuture =
client.async.models.embedContent("gemini-embedding-2-exp-11-2025", content, null);
responseFuture
.thenAccept(
response ->
System.out.println("Async embedding response with GCS image: " + response))
.join();
}
}

private EmbedContentAsync() {}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/google/genai/AsyncBatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public AsyncBatches(ApiClient apiClient) {

CompletableFuture<BatchJob> privateCreate(
String model, BatchJobSource src, CreateBatchJobConfig config) {

BuiltRequest builtRequest = batches.buildRequestForPrivateCreate(model, src, config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -62,6 +63,7 @@ CompletableFuture<BatchJob> privateCreate(

CompletableFuture<BatchJob> privateCreateEmbeddings(
String model, EmbeddingsBatchJobSource src, CreateEmbeddingsBatchJobConfig config) {

BuiltRequest builtRequest = batches.buildRequestForPrivateCreateEmbeddings(model, src, config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -83,6 +85,7 @@ CompletableFuture<BatchJob> privateCreateEmbeddings(
* @return A {@link BatchJob} object that contains the info of the batch job.
*/
public CompletableFuture<BatchJob> get(String name, GetBatchJobConfig config) {

BuiltRequest builtRequest = batches.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -103,6 +106,7 @@ public CompletableFuture<BatchJob> get(String name, GetBatchJobConfig config) {
* @param config A {@link CancelBatchJobConfig} for configuring the cancel request.
*/
public CompletableFuture<Void> cancel(String name, CancelBatchJobConfig config) {

BuiltRequest builtRequest = batches.buildRequestForCancel(name, config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -113,6 +117,7 @@ public CompletableFuture<Void> cancel(String name, CancelBatchJobConfig config)
}

CompletableFuture<ListBatchJobsResponse> privateList(ListBatchJobsConfig config) {

BuiltRequest builtRequest = batches.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -133,6 +138,7 @@ CompletableFuture<ListBatchJobsResponse> privateList(ListBatchJobsConfig config)
* @param config A {@link DeleteBatchJobConfig} for configuring the delete request.
*/
public CompletableFuture<DeleteResourceJob> delete(String name, DeleteBatchJobConfig config) {

BuiltRequest builtRequest = batches.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest(
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/google/genai/AsyncCaches.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public AsyncCaches(ApiClient apiClient) {
* @return A {@link CachedContent} object that contains the info of the created resource.
*/
public CompletableFuture<CachedContent> create(String model, CreateCachedContentConfig config) {

BuiltRequest builtRequest = caches.buildRequestForCreate(model, config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -71,6 +72,7 @@ public CompletableFuture<CachedContent> create(String model, CreateCachedContent
* @return A {@link CachedContent} object that contains the info of the cached content.
*/
public CompletableFuture<CachedContent> get(String name, GetCachedContentConfig config) {

BuiltRequest builtRequest = caches.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -90,6 +92,7 @@ public CompletableFuture<CachedContent> get(String name, GetCachedContentConfig
*/
public CompletableFuture<DeleteCachedContentResponse> delete(
String name, DeleteCachedContentConfig config) {

BuiltRequest builtRequest = caches.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest(
Expand All @@ -110,6 +113,7 @@ public CompletableFuture<DeleteCachedContentResponse> delete(
* @return A {@link CachedContent} object that contains the info of the updated resource.
*/
public CompletableFuture<CachedContent> update(String name, UpdateCachedContentConfig config) {

BuiltRequest builtRequest = caches.buildRequestForUpdate(name, config);
return this.apiClient
.asyncRequest("patch", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -122,6 +126,7 @@ public CompletableFuture<CachedContent> update(String name, UpdateCachedContentC
}

CompletableFuture<ListCachedContentsResponse> privateList(ListCachedContentsConfig config) {

BuiltRequest builtRequest = caches.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/google/genai/AsyncDocuments.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public AsyncDocuments(ApiClient apiClient) {
}

public CompletableFuture<Document> get(String name, GetDocumentConfig config) {

BuiltRequest builtRequest = documents.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -54,6 +55,7 @@ public CompletableFuture<Document> get(String name, GetDocumentConfig config) {
}

public CompletableFuture<Void> delete(String name, DeleteDocumentConfig config) {

BuiltRequest builtRequest = documents.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest(
Expand All @@ -65,6 +67,7 @@ public CompletableFuture<Void> delete(String name, DeleteDocumentConfig config)
}

CompletableFuture<ListDocumentsResponse> privateList(String parent, ListDocumentsConfig config) {

BuiltRequest builtRequest = documents.buildRequestForPrivateList(parent, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/google/genai/AsyncFileSearchStores.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public AsyncFileSearchStores(ApiClient apiClient) {
}

public CompletableFuture<FileSearchStore> create(CreateFileSearchStoreConfig config) {

BuiltRequest builtRequest = fileSearchStores.buildRequestForCreate(config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -71,6 +72,7 @@ public CompletableFuture<FileSearchStore> create(CreateFileSearchStoreConfig con
}

public CompletableFuture<FileSearchStore> get(String name, GetFileSearchStoreConfig config) {

BuiltRequest builtRequest = fileSearchStores.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -83,6 +85,7 @@ public CompletableFuture<FileSearchStore> get(String name, GetFileSearchStoreCon
}

public CompletableFuture<Void> delete(String name, DeleteFileSearchStoreConfig config) {

BuiltRequest builtRequest = fileSearchStores.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest(
Expand All @@ -94,6 +97,7 @@ public CompletableFuture<Void> delete(String name, DeleteFileSearchStoreConfig c
}

CompletableFuture<ListFileSearchStoresResponse> privateList(ListFileSearchStoresConfig config) {

BuiltRequest builtRequest = fileSearchStores.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -107,6 +111,7 @@ CompletableFuture<ListFileSearchStoresResponse> privateList(ListFileSearchStores

CompletableFuture<UploadToFileSearchStoreResumableResponse> privateUploadToFileSearchStore(
String fileSearchStoreName, UploadToFileSearchStoreConfig config) {

BuiltRequest builtRequest =
fileSearchStores.buildRequestForPrivateUploadToFileSearchStore(fileSearchStoreName, config);
return this.apiClient
Expand All @@ -122,6 +127,7 @@ CompletableFuture<UploadToFileSearchStoreResumableResponse> privateUploadToFileS

public CompletableFuture<ImportFileOperation> importFile(
String fileSearchStoreName, String fileName, ImportFileConfig config) {

BuiltRequest builtRequest =
fileSearchStores.buildRequestForImportFile(fileSearchStoreName, fileName, config);
return this.apiClient
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/google/genai/AsyncFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public AsyncFiles(ApiClient apiClient) {
}

CompletableFuture<ListFilesResponse> privateList(ListFilesConfig config) {

BuiltRequest builtRequest = files.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -62,6 +63,7 @@ CompletableFuture<ListFilesResponse> privateList(ListFilesConfig config) {
}

CompletableFuture<CreateFileResponse> privateCreate(File file, CreateFileConfig config) {

BuiltRequest builtRequest = files.buildRequestForPrivateCreate(file, config);
return this.apiClient
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -81,6 +83,7 @@ CompletableFuture<CreateFileResponse> privateCreate(File file, CreateFileConfig
* @return A File object representing the file.
*/
public CompletableFuture<File> get(String name, GetFileConfig config) {

BuiltRequest builtRequest = files.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
Expand All @@ -100,6 +103,7 @@ public CompletableFuture<File> get(String name, GetFileConfig config) {
* @return The DeleteFileResponse, the response for the delete method.
*/
public CompletableFuture<DeleteFileResponse> delete(String name, DeleteFileConfig config) {

BuiltRequest builtRequest = files.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest(
Expand Down
Loading