Content-Length: 822154 | pFad | https://github.com/googleapis/google-cloud-java/commit/04e9574a223cd9c42615bfde18aa6ad3dc551251

02 feat: [vertexai] add GenerateContentConfig class (#10413) · googleapis/google-cloud-java@04e9574 · GitHub
Skip to content

Commit 04e9574

Browse files
feat: [vertexai] add GenerateContentConfig class (#10413)
PiperOrigin-RevId: 610472076 Co-authored-by: Jaycee Li <jayceeli@google.com>
1 parent a241393 commit 04e9574

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2024 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+
package com.google.cloud.vertexai.generativeai;
18+
19+
import com.google.api.core.BetaApi;
20+
import com.google.cloud.vertexai.api.GenerationConfig;
21+
import com.google.cloud.vertexai.api.SafetySetting;
22+
import com.google.cloud.vertexai.api.Tool;
23+
import com.google.common.collect.ImmutableList;
24+
import java.util.List;
25+
26+
/** This class holds all the configs when making a generate content API call */
27+
public class GenerateContentConfig {
28+
private GenerationConfig generationConfig;
29+
private ImmutableList<SafetySetting> safetySettings;
30+
private ImmutableList<Tool> tools;
31+
32+
/** Creates a builder for the GenerateContentConfig. */
33+
public static Builder newBuilder() {
34+
return new Builder();
35+
}
36+
37+
private GenerateContentConfig(Builder builder) {
38+
if (builder.generationConfig != null) {
39+
this.generationConfig = builder.generationConfig;
40+
} else {
41+
this.generationConfig = null;
42+
}
43+
if (builder.safetySettings != null) {
44+
this.safetySettings = builder.safetySettings;
45+
} else {
46+
this.safetySettings = ImmutableList.of();
47+
}
48+
if (builder.tools != null) {
49+
this.tools = builder.tools;
50+
} else {
51+
this.tools = ImmutableList.of();
52+
}
53+
}
54+
55+
/** Builder class for {@link GenerateContentConfig}. */
56+
public static class Builder {
57+
private GenerationConfig generationConfig;
58+
private ImmutableList<SafetySetting> safetySettings;
59+
private ImmutableList<Tool> tools;
60+
61+
private Builder() {}
62+
63+
/** Builds a GenerateContentConfig instance. */
64+
public GenerateContentConfig build() {
65+
return new GenerateContentConfig(this);
66+
}
67+
68+
/**
69+
* Set {@link com.google.cloud.vertexai.api.GenerationConfig} that will be used in the generate
70+
* content API call.
71+
*
72+
* @return builder for the GenerateContentConfig
73+
*/
74+
@BetaApi
75+
public Builder setGenerationConfig(GenerationConfig generationConfig) {
76+
this.generationConfig = generationConfig;
77+
return this;
78+
}
79+
80+
/**
81+
* Set a list of {@link com.google.cloud.vertexai.api.SafetySetting} that will be used in the
82+
* generate content API call.
83+
*
84+
* @return builder for the GenerateContentConfig
85+
*/
86+
@BetaApi
87+
public Builder setSafetySettings(List<SafetySetting> safetySettings) {
88+
ImmutableList.Builder builder = ImmutableList.builder();
89+
for (SafetySetting safetySetting : safetySettings) {
90+
if (safetySetting != null) {
91+
builder.add(safetySetting);
92+
}
93+
}
94+
this.safetySettings = builder.build();
95+
96+
return this;
97+
}
98+
99+
/**
100+
* Set a list of {@link com.google.cloud.vertexai.api.Tool} that will be used in the generate
101+
* content API call.
102+
*
103+
* @return builder for the GenerateContentConfig
104+
*/
105+
@BetaApi
106+
public Builder setTools(List<Tool> tools) {
107+
ImmutableList.Builder builder = ImmutableList.builder();
108+
for (Tool tool : tools) {
109+
if (tool != null) {
110+
builder.add(tool);
111+
}
112+
}
113+
this.tools = builder.build();
114+
115+
return this;
116+
}
117+
}
118+
119+
/**
120+
* Sets the value for {@link #getGenerationConfig}, which will be used in the generate content API
121+
* call.
122+
*/
123+
@BetaApi
124+
public void setGenerationConfig(GenerationConfig generationConfig) {
125+
this.generationConfig = generationConfig;
126+
}
127+
128+
/**
129+
* Sets the value for {@link #getSafetySettings}, which will be used in the generate content API
130+
* call.
131+
*/
132+
@BetaApi("safetySettings is a preview feature.")
133+
public void setSafetySettings(List<SafetySetting> safetySettings) {
134+
ImmutableList.Builder builder = ImmutableList.builder();
135+
for (SafetySetting safetySetting : safetySettings) {
136+
if (safetySetting != null) {
137+
builder.add(safetySetting);
138+
}
139+
}
140+
this.safetySettings = builder.build();
141+
}
142+
143+
/** Sets the value for {@link #getTools}, which will be used in the generate content API call. */
144+
@BetaApi("tools is a preview feature.")
145+
public void setTools(List<Tool> tools) {
146+
ImmutableList.Builder builder = ImmutableList.builder();
147+
for (Tool tool : tools) {
148+
if (tool != null) {
149+
builder.add(tool);
150+
}
151+
}
152+
this.tools = builder.build();
153+
}
154+
155+
/** Returns the {@link com.google.cloud.vertexai.api.GenerationConfig} of this config. */
156+
@BetaApi
157+
public GenerationConfig getGenerationConfig() {
158+
return this.generationConfig;
159+
}
160+
161+
/** Returns a list of {@link com.google.cloud.vertexai.api.SafetySettings} of this config. */
162+
@BetaApi("safetySettings is a preview feature.")
163+
public ImmutableList<SafetySetting> getSafetySettings() {
164+
return this.safetySettings;
165+
}
166+
167+
/** Returns a list of {@link com.google.cloud.vertexai.api.Tool} of this config. */
168+
@BetaApi("tools is a preview feature.")
169+
public ImmutableList<Tool> getTools() {
170+
return this.tools;
171+
}
172+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2024 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+
package com.google.cloud.vertexai.generativeai;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.vertexai.api.FunctionDeclaration;
22+
import com.google.cloud.vertexai.api.GenerationConfig;
23+
import com.google.cloud.vertexai.api.HarmCategory;
24+
import com.google.cloud.vertexai.api.SafetySetting;
25+
import com.google.cloud.vertexai.api.SafetySetting.HarmBlockThreshold;
26+
import com.google.cloud.vertexai.api.Schema;
27+
import com.google.cloud.vertexai.api.Tool;
28+
import com.google.cloud.vertexai.api.Type;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
public final class GenerateContentConfigTest {
37+
private static final GenerationConfig GENERATION_CONFIG =
38+
GenerationConfig.newBuilder().setCandidateCount(1).build();
39+
private static final SafetySetting SAFETY_SETTING =
40+
SafetySetting.newBuilder()
41+
.setCategory(HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT)
42+
.setThreshold(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE)
43+
.build();
44+
private static final Tool TOOL =
45+
Tool.newBuilder()
46+
.addFunctionDeclarations(
47+
FunctionDeclaration.newBuilder()
48+
.setName("getCurrentWeather")
49+
.setDescription("Get the current weather in a given location")
50+
.setParameters(
51+
Schema.newBuilder()
52+
.setType(Type.OBJECT)
53+
.putProperties(
54+
"location",
55+
Schema.newBuilder()
56+
.setType(Type.STRING)
57+
.setDescription("location")
58+
.build())
59+
.addRequired("location")))
60+
.build();
61+
62+
private List<SafetySetting> safetySettings = Arrays.asList(SAFETY_SETTING);
63+
private List<Tool> tools = Arrays.asList(TOOL);
64+
65+
private GenerateContentConfig config;
66+
67+
@Test
68+
public void testInstantiateGenerateContentConfigWithBuilder() {
69+
config =
70+
GenerateContentConfig.newBuilder()
71+
.setGenerationConfig(GENERATION_CONFIG)
72+
.setSafetySettings(safetySettings)
73+
.setTools(tools)
74+
.build();
75+
assertThat(config.getGenerationConfig()).isEqualTo(GENERATION_CONFIG);
76+
assertThat(config.getSafetySettings()).isEqualTo(safetySettings);
77+
assertThat(config.getTools()).isEqualTo(tools);
78+
}
79+
80+
@Test
81+
public void testGenerateContentConfigSetters() {
82+
config = GenerateContentConfig.newBuilder().build();
83+
84+
assertThat(config.getGenerationConfig()).isNull();
85+
assertThat(config.getSafetySettings()).isEmpty();
86+
assertThat(config.getTools()).isEmpty();
87+
88+
config.setGenerationConfig(GENERATION_CONFIG);
89+
config.setSafetySettings(safetySettings);
90+
config.setTools(tools);
91+
92+
assertThat(config.getGenerationConfig()).isEqualTo(GENERATION_CONFIG);
93+
assertThat(config.getSafetySettings()).isEqualTo(safetySettings);
94+
assertThat(config.getTools()).isEqualTo(tools);
95+
}
96+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/googleapis/google-cloud-java/commit/04e9574a223cd9c42615bfde18aa6ad3dc551251

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy