Skip to content

Commit 7583ddf

Browse files
jaycee-licopybara-github
authored andcommitted
docs: Add examples for embedContent
PiperOrigin-RevId: 743999288
1 parent 1f62aee commit 7583ddf

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Usage:
19+
*
20+
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
21+
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
22+
*
23+
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
24+
*
25+
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
26+
*
27+
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
28+
*
29+
* <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a
30+
* list of available API keys here: https://aistudio.google.com/app/apikey
31+
*
32+
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
33+
*
34+
* <p>2. Compile the java package and run the sample code.
35+
*
36+
* <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.EmbedContent"
37+
*/
38+
package com.google.genai.examples;
39+
40+
import com.google.genai.Client;
41+
import com.google.genai.types.EmbedContentResponse;
42+
43+
/** An example of using the Unified Gen AI Java SDK to embed content. */
44+
public class EmbedContent {
45+
public static void main(String[] args) {
46+
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
47+
// key from the environment variable `GOOGLE_API_KEY`.
48+
Client client = new Client();
49+
50+
EmbedContentResponse response =
51+
client.models.embedContent("text-embedding-004", "why is the sky blue?", null);
52+
53+
System.out.println("Embedding response: " + response);
54+
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Usage:
19+
*
20+
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
21+
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
22+
*
23+
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
24+
*
25+
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
26+
*
27+
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
28+
*
29+
* <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a
30+
* list of available API keys here: https://aistudio.google.com/app/apikey
31+
*
32+
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
33+
*
34+
* <p>2. Compile the java package and run the sample code.
35+
*
36+
* <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.EmbedContentAsync"
37+
*/
38+
package com.google.genai.examples;
39+
40+
import com.google.genai.Client;
41+
import com.google.genai.types.EmbedContentResponse;
42+
import java.util.concurrent.CompletableFuture;
43+
44+
/** An example of using the Unified Gen AI Java SDK to embed content asynchronously. */
45+
public class EmbedContentAsync {
46+
public static void main(String[] args) {
47+
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
48+
// key from the environment variable `GOOGLE_API_KEY`.
49+
Client client = new Client();
50+
51+
CompletableFuture<EmbedContentResponse> responseFuture =
52+
client.async.models.embedContent("text-embedding-004", "Why is the sky blue?", null);
53+
54+
responseFuture
55+
.thenAccept(
56+
response -> {
57+
System.out.println("Async embedding response: " + response);
58+
})
59+
.join();
60+
}
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Usage:
19+
*
20+
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
21+
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
22+
*
23+
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
24+
*
25+
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
26+
*
27+
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
28+
*
29+
* <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a
30+
* list of available API keys here: https://aistudio.google.com/app/apikey
31+
*
32+
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
33+
*
34+
* <p>2. Compile the java package and run the sample code.
35+
*
36+
* <p>mvn clean compile exec:java
37+
* -Dexec.mainClass="com.google.genai.examples.EmbedContentWithConfig"
38+
*/
39+
package com.google.genai.examples;
40+
41+
import com.google.genai.Client;
42+
import com.google.genai.types.EmbedContentConfig;
43+
import com.google.genai.types.EmbedContentResponse;
44+
import java.util.Arrays;
45+
import java.util.List;
46+
47+
/** An example of using the Unified Gen AI Java SDK to embed content with extra config. */
48+
public class EmbedContentWithConfig {
49+
public static void main(String[] args) {
50+
// Instantiate the client using Vertex API. The client gets the project and location from the
51+
// environment variables `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`.
52+
Client client = Client.builder().vertexAI(true).build();
53+
54+
List<String> texts = Arrays.asList("why is the sky blue?", "What is your age?");
55+
56+
EmbedContentConfig config = EmbedContentConfig.builder().outputDimensionality(10).build();
57+
58+
EmbedContentResponse response = client.models.embedContent("text-embedding-004", texts, config);
59+
60+
System.out.println("Embedding response: " + response);
61+
}
62+
}

0 commit comments

Comments
 (0)