Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3f8043a

Browse files
jdomingrJuan Dominguez
and
Juan Dominguez
authored
feat(genai): add samples for embeddings, provisioned throughput and safety (GoogleCloudPlatform#10188)
Co-authored-by: Juan Dominguez <jdomin@softserveinc.com>
1 parentd8ac9a5 commit3f8043a

File tree

6 files changed

+485
-0
lines changed

6 files changed

+485
-0
lines changed
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+
* http://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+
packagegenai.embeddings;
18+
19+
// [START googlegenaisdk_embeddings_docretrieval_with_txt]
20+
21+
importcom.google.genai.Client;
22+
importcom.google.genai.types.EmbedContentConfig;
23+
importcom.google.genai.types.EmbedContentResponse;
24+
importjava.util.List;
25+
26+
publicclassEmbeddingsDocRetrievalWithTxt {
27+
28+
publicstaticvoidmain(String[]args) {
29+
// TODO(developer): Replace these variables before running the sample.
30+
StringmodelId ="gemini-embedding-001";
31+
embedContent(modelId);
32+
}
33+
34+
// Shows how to embed content with text.
35+
publicstaticEmbedContentResponseembedContent(StringmodelId) {
36+
// Client Initialization. Once created, it can be reused for multiple requests.
37+
try (Clientclient =Client.builder().location("global").vertexAI(true).build()) {
38+
39+
EmbedContentResponseresponse =
40+
client.models.embedContent(
41+
modelId,
42+
List.of(
43+
"How do I get a driver's license/learner's permit?",
44+
"How long is my driver's license valid for?",
45+
"Driver's knowledge test study guide"),
46+
EmbedContentConfig.builder()
47+
.taskType("RETRIEVAL_DOCUMENT")
48+
.outputDimensionality(3072)
49+
.title("Driver's License")
50+
.build());
51+
52+
System.out.println(response);
53+
// Example response:
54+
// embeddings=Optional[[ContentEmbedding{values=Optional[[-0.035855383, 0.008127963, ... ]]
55+
// statistics=Optional[ContentEmbeddingStatistics{truncated=Optional[false],
56+
// tokenCount=Optional[11.0]}]}]],
57+
// metadata=Optional[EmbedContentMetadata{billableCharacterCount=Optional[153]}]}
58+
returnresponse;
59+
}
60+
}
61+
}
62+
// [END googlegenaisdk_embeddings_docretrieval_with_txt]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* http://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+
packagegenai.provisionedthroughput;
18+
19+
// [START googlegenaisdk_provisionedthroughput_with_txt]
20+
21+
importcom.google.genai.Client;
22+
importcom.google.genai.types.GenerateContentConfig;
23+
importcom.google.genai.types.GenerateContentResponse;
24+
importcom.google.genai.types.HttpOptions;
25+
importjava.util.Map;
26+
27+
publicclassProvisionedThroughputWithTxt {
28+
29+
publicstaticvoidmain(String[]args) {
30+
// TODO(developer): Replace these variables before running the sample.
31+
StringmodelId ="gemini-2.5-flash";
32+
generateContent(modelId);
33+
}
34+
35+
// Generates content with Provisioned Throughput.
36+
publicstaticStringgenerateContent(StringmodelId) {
37+
// Client Initialization. Once created, it can be reused for multiple requests.
38+
try (Clientclient =
39+
Client.builder()
40+
.location("us-central1")
41+
.vertexAI(true)
42+
.httpOptions(
43+
HttpOptions.builder()
44+
.apiVersion("v1")
45+
.headers(
46+
// Options:
47+
// - "dedicated": Use Provisioned Throughput
48+
// - "shared": Use pay-as-you-go
49+
// https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput
50+
Map.of("X-Vertex-AI-LLM-Request-Type","shared"))
51+
.build())
52+
.build()) {
53+
54+
GenerateContentResponseresponse =
55+
client.models.generateContent(
56+
modelId,"How does AI work?",GenerateContentConfig.builder().build());
57+
58+
System.out.println(response.text());
59+
// Example response:
60+
// At its core, **AI (Artificial Intelligence) works by enabling machines to learn,
61+
// reason, and make decisions in ways that simulate human intelligence.** Instead of being
62+
// explicitly programmed for every single task...
63+
returnresponse.text();
64+
}
65+
}
66+
}
67+
// [END googlegenaisdk_provisionedthroughput_with_txt]
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
* http://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+
packagegenai.safety;
18+
19+
// [START googlegenaisdk_safety_with_txt]
20+
21+
importcom.google.genai.Client;
22+
importcom.google.genai.types.Candidate;
23+
importcom.google.genai.types.Content;
24+
importcom.google.genai.types.GenerateContentConfig;
25+
importcom.google.genai.types.GenerateContentResponse;
26+
importcom.google.genai.types.HarmBlockThreshold;
27+
importcom.google.genai.types.HarmCategory;
28+
importcom.google.genai.types.HttpOptions;
29+
importcom.google.genai.types.Part;
30+
importcom.google.genai.types.SafetySetting;
31+
importjava.util.List;
32+
importjava.util.stream.Collectors;
33+
34+
publicclassSafetyWithTxt {
35+
36+
publicstaticvoidmain(String[]args) {
37+
// TODO(developer): Replace these variables before running the sample.
38+
StringmodelId ="gemini-2.5-flash";
39+
generateContent(modelId);
40+
}
41+
42+
// Shows how to generate content with safety settings.
43+
publicstaticGenerateContentResponsegenerateContent(StringmodelId) {
44+
// Client Initialization. Once created, it can be reused for multiple requests.
45+
try (Clientclient =
46+
Client.builder()
47+
.location("global")
48+
.vertexAI(true)
49+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
50+
.build()) {
51+
52+
StringsystemInstruction ="Be as mean as possible.";
53+
54+
Stringprompt =
55+
"Write a list of 5 disrespectful things that I might say"
56+
+" to the universe after stubbing my toe in the dark.";
57+
58+
// Set safety settings.
59+
List<HarmCategory.Known>categoriesToBlock =
60+
List.of(
61+
HarmCategory.Known.HARM_CATEGORY_DANGEROUS_CONTENT,
62+
HarmCategory.Known.HARM_CATEGORY_HARASSMENT,
63+
HarmCategory.Known.HARM_CATEGORY_HATE_SPEECH,
64+
HarmCategory.Known.HARM_CATEGORY_SEXUALLY_EXPLICIT);
65+
66+
List<SafetySetting>safetySettings =
67+
categoriesToBlock.stream()
68+
.map(
69+
category ->
70+
SafetySetting.builder()
71+
.category(category)
72+
.threshold(HarmBlockThreshold.Known.BLOCK_LOW_AND_ABOVE)
73+
.build())
74+
.collect(Collectors.toList());
75+
76+
GenerateContentResponseresponse =
77+
client.models.generateContent(
78+
modelId,
79+
prompt,
80+
GenerateContentConfig.builder()
81+
.systemInstruction(Content.fromParts(Part.fromText(systemInstruction)))
82+
.safetySettings(safetySettings)
83+
.build());
84+
85+
// Get response candidate.
86+
Candidatecandidate =
87+
response
88+
.candidates()
89+
.flatMap(candidates ->candidates.stream().findFirst())
90+
.orElseThrow(
91+
() ->newIllegalStateException("No response candidate generated by the model."));
92+
93+
// Finish Reason will be `SAFETY` if it is blocked.
94+
System.out.println(candidate.finishReason());
95+
// Example response:
96+
// Optional[SAFETY]
97+
98+
// For details on all the fields in the response.
99+
candidate
100+
.safetyRatings()
101+
.ifPresent(
102+
safetyRatings ->
103+
safetyRatings.forEach(
104+
safetyRating -> {
105+
System.out.println("\nCategory: " +safetyRating.category());
106+
System.out.println("Is Blocked: " +safetyRating.blocked());
107+
System.out.println("Probability: " +safetyRating.probability());
108+
System.out.println("Probability Score: " +safetyRating.probabilityScore());
109+
System.out.println("Severity: " +safetyRating.severity());
110+
System.out.println("Severity Score: " +safetyRating.severityScore());
111+
}));
112+
// Example response:
113+
// Category: Optional[HARM_CATEGORY_HATE_SPEECH]
114+
// Is Blocked: Optional.empty
115+
// Probability: Optional[NEGLIGIBLE]
116+
// Probability Score: Optional[1.9967922E-5]
117+
// Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
118+
// Severity Score: Optional[0.05732864]
119+
//
120+
// Category: Optional[HARM_CATEGORY_DANGEROUS_CONTENT]
121+
// Is Blocked: Optional.empty
122+
// Probability: Optional[NEGLIGIBLE]
123+
// Probability Score: Optional[2.9124324E-6]
124+
// Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
125+
// Severity Score: Optional[0.04544826]
126+
//
127+
// Category: Optional[HARM_CATEGORY_HARASSMENT]
128+
// Is Blocked: Optional[true]
129+
// Probability: Optional[MEDIUM]
130+
// Probability Score: Optional[0.4593908]
131+
// Severity: Optional[HARM_SEVERITY_MEDIUM]
132+
// Severity Score: Optional[0.22082388]
133+
//
134+
// Category: Optional[HARM_CATEGORY_SEXUALLY_EXPLICIT]
135+
// Is Blocked: Optional.empty
136+
// Probability: Optional[NEGLIGIBLE]
137+
// Probability Score: Optional[6.453211E-8]
138+
// Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
139+
// Severity Score: Optional[0.023201048]
140+
returnresponse;
141+
}
142+
}
143+
}
144+
// [END googlegenaisdk_safety_with_txt]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* http://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+
packagegenai.embeddings;
18+
19+
importstaticcom.google.common.truth.Truth.assertThat;
20+
importstaticcom.google.common.truth.Truth.assertWithMessage;
21+
22+
importcom.google.genai.types.EmbedContentResponse;
23+
importjava.io.ByteArrayOutputStream;
24+
importjava.io.PrintStream;
25+
importorg.junit.After;
26+
importorg.junit.Before;
27+
importorg.junit.BeforeClass;
28+
importorg.junit.Test;
29+
importorg.junit.runner.RunWith;
30+
importorg.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
publicclassEmbeddingsIT {
34+
35+
privatestaticfinalStringGEMINI_EMBEDDING ="gemini-embedding-001";
36+
privateByteArrayOutputStreambout;
37+
38+
// Check if the required environment variables are set.
39+
publicstaticvoidrequireEnvVar(StringenvVarName) {
40+
assertWithMessage(String.format("Missing environment variable '%s' ",envVarName))
41+
.that(System.getenv(envVarName))
42+
.isNotEmpty();
43+
}
44+
45+
@BeforeClass
46+
publicstaticvoidcheckRequirements() {
47+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
48+
}
49+
50+
@Before
51+
publicvoidsetUp() {
52+
bout =newByteArrayOutputStream();
53+
System.setOut(newPrintStream(bout));
54+
}
55+
56+
@After
57+
publicvoidtearDown() {
58+
System.setOut(null);
59+
}
60+
61+
@Test
62+
publicvoidtestEmbeddingsDocRetrievalWithTxt() {
63+
EmbedContentResponseresponse =EmbeddingsDocRetrievalWithTxt.embedContent(GEMINI_EMBEDDING);
64+
assertThat(response.toString()).isNotEmpty();
65+
assertThat(response.embeddings()).isPresent();
66+
assertThat(response.embeddings().get()).isNotEmpty();
67+
assertThat(response.metadata()).isPresent();
68+
69+
Stringoutput =bout.toString();
70+
assertThat(output).contains("statistics");
71+
assertThat(output).contains("tokenCount");
72+
}
73+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp