|
| 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 | +} |
0 commit comments