|
| 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 | +package com.google.adk.tools; |
| 18 | + |
| 19 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.google.adk.JsonBaseModel; |
| 24 | +import com.google.adk.agents.ConfigAgentUtils.ConfigurationException; |
| 25 | +import com.google.adk.examples.BaseExampleProvider; |
| 26 | +import com.google.adk.examples.Example; |
| 27 | +import com.google.adk.examples.ExampleUtils; |
| 28 | +import com.google.adk.models.LlmRequest; |
| 29 | +import com.google.common.collect.ImmutableList; |
| 30 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 31 | +import com.google.genai.types.Content; |
| 32 | +import io.reactivex.rxjava3.core.Completable; |
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.lang.reflect.Modifier; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Optional; |
| 39 | + |
| 40 | +/** |
| 41 | + * A tool that injects (few-shot) examples into the outgoing LLM request as system instructions. |
| 42 | + * |
| 43 | + * <p>Configuration (args) options for YAML: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li><b>examples</b>: Either a fully-qualified reference to a {@link BaseExampleProvider} |
| 47 | + * instance (e.g., <code>com.example.MyExamples.INSTANCE</code>) or a list of examples with |
| 48 | + * fields <code>input</code> and <code>output</code> (array of messages). |
| 49 | + * </ul> |
| 50 | + */ |
| 51 | +public final class ExampleTool extends BaseTool { |
| 52 | + |
| 53 | + private static final ObjectMapper MAPPER = JsonBaseModel.getMapper(); |
| 54 | + |
| 55 | + private final Optional<BaseExampleProvider> exampleProvider; |
| 56 | + private final Optional<List<Example>> examples; |
| 57 | + |
| 58 | + /** Single private constructor; create via builder or fromConfig. */ |
| 59 | + private ExampleTool(Builder builder) { |
| 60 | + super( |
| 61 | + isNullOrEmpty(builder.name) ? "example_tool" : builder.name, |
| 62 | + isNullOrEmpty(builder.description) |
| 63 | + ? "Adds few-shot examples to the request" |
| 64 | + : builder.description); |
| 65 | + this.exampleProvider = builder.provider; |
| 66 | + this.examples = builder.examples.isEmpty() ? Optional.empty() : Optional.of(builder.examples); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Completable processLlmRequest( |
| 71 | + LlmRequest.Builder llmRequestBuilder, ToolContext toolContext) { |
| 72 | + // Do not add anything if no user text |
| 73 | + String query = |
| 74 | + toolContext |
| 75 | + .userContent() |
| 76 | + .flatMap(content -> content.parts().flatMap(parts -> parts.stream().findFirst())) |
| 77 | + .flatMap(part -> part.text()) |
| 78 | + .orElse(""); |
| 79 | + if (query.isEmpty()) { |
| 80 | + return Completable.complete(); |
| 81 | + } |
| 82 | + |
| 83 | + final String examplesBlock; |
| 84 | + if (exampleProvider.isPresent()) { |
| 85 | + examplesBlock = ExampleUtils.buildExampleSi(exampleProvider.get(), query); |
| 86 | + } else if (examples.isPresent()) { |
| 87 | + // Adapter provider that returns a fixed list irrespective of query |
| 88 | + BaseExampleProvider provider = q -> examples.get(); |
| 89 | + examplesBlock = ExampleUtils.buildExampleSi(provider, query); |
| 90 | + } else { |
| 91 | + return Completable.complete(); |
| 92 | + } |
| 93 | + |
| 94 | + llmRequestBuilder.appendInstructions(ImmutableList.of(examplesBlock)); |
| 95 | + // Delegate to BaseTool to keep any declaration bookkeeping (none for this tool) |
| 96 | + return super.processLlmRequest(llmRequestBuilder, toolContext); |
| 97 | + } |
| 98 | + |
| 99 | + /** Factory from YAML tool args. */ |
| 100 | + public static ExampleTool fromConfig(ToolArgsConfig args, String configAbsPath) |
| 101 | + throws ConfigurationException { |
| 102 | + if (args == null || args.isEmpty()) { |
| 103 | + throw new ConfigurationException("ExampleTool requires 'examples' argument"); |
| 104 | + } |
| 105 | + Object examplesArg = args.get("examples"); |
| 106 | + if (examplesArg == null) { |
| 107 | + throw new ConfigurationException("ExampleTool missing 'examples' argument"); |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + if (examplesArg instanceof String string) { |
| 112 | + BaseExampleProvider provider = resolveExampleProvider(string); |
| 113 | + return ExampleTool.builder().setExampleProvider(provider).build(); |
| 114 | + } |
| 115 | + if (examplesArg instanceof List) { |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + List<Object> rawList = (List<Object>) examplesArg; |
| 118 | + List<Example> examples = new ArrayList<>(); |
| 119 | + for (Object o : rawList) { |
| 120 | + if (!(o instanceof Map)) { |
| 121 | + throw new ConfigurationException( |
| 122 | + "Invalid example entry. Expected a map with 'input' and 'output'."); |
| 123 | + } |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + Map<String, Object> m = (Map<String, Object>) o; |
| 126 | + Object inputObj = m.get("input"); |
| 127 | + Object outputObj = m.get("output"); |
| 128 | + if (inputObj == null || outputObj == null) { |
| 129 | + throw new ConfigurationException("Each example must include 'input' and 'output'."); |
| 130 | + } |
| 131 | + Content input = MAPPER.convertValue(inputObj, Content.class); |
| 132 | + @SuppressWarnings("unchecked") |
| 133 | + List<Object> outList = (List<Object>) outputObj; |
| 134 | + ImmutableList<Content> outputs = |
| 135 | + outList.stream() |
| 136 | + .map(e -> MAPPER.convertValue(e, Content.class)) |
| 137 | + .collect(toImmutableList()); |
| 138 | + examples.add(Example.builder().input(input).output(outputs).build()); |
| 139 | + } |
| 140 | + Builder b = ExampleTool.builder(); |
| 141 | + for (Example ex : examples) { |
| 142 | + b.addExample(ex); |
| 143 | + } |
| 144 | + return b.build(); |
| 145 | + } |
| 146 | + } catch (RuntimeException e) { |
| 147 | + throw new ConfigurationException("Failed to parse ExampleTool examples", e); |
| 148 | + } |
| 149 | + throw new ConfigurationException( |
| 150 | + "Unsupported 'examples' type. Provide a string provider ref or list of examples."); |
| 151 | + } |
| 152 | + |
| 153 | + /** Overload to match resolver which passes only ToolArgsConfig. */ |
| 154 | + public static ExampleTool fromConfig(ToolArgsConfig args) throws ConfigurationException { |
| 155 | + return fromConfig(args, /* configAbsPath= */ ""); |
| 156 | + } |
| 157 | + |
| 158 | + private static BaseExampleProvider resolveExampleProvider(String ref) |
| 159 | + throws ConfigurationException { |
| 160 | + int lastDot = ref.lastIndexOf('.'); |
| 161 | + if (lastDot <= 0) { |
| 162 | + throw new ConfigurationException( |
| 163 | + "Invalid example provider reference: " + ref + ". Expected ClassName.FIELD"); |
| 164 | + } |
| 165 | + String className = ref.substring(0, lastDot); |
| 166 | + String fieldName = ref.substring(lastDot + 1); |
| 167 | + try { |
| 168 | + Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className); |
| 169 | + Field field = clazz.getField(fieldName); |
| 170 | + if (!Modifier.isStatic(field.getModifiers())) { |
| 171 | + throw new ConfigurationException( |
| 172 | + "Field '" + fieldName + "' in class '" + className + "' is not static"); |
| 173 | + } |
| 174 | + Object instance = field.get(null); |
| 175 | + if (instance instanceof BaseExampleProvider provider) { |
| 176 | + return provider; |
| 177 | + } |
| 178 | + throw new ConfigurationException( |
| 179 | + "Field '" + fieldName + "' in class '" + className + "' is not a BaseExampleProvider"); |
| 180 | + } catch (NoSuchFieldException e) { |
| 181 | + throw new ConfigurationException( |
| 182 | + "Field '" + fieldName + "' not found in class '" + className + "'", e); |
| 183 | + } catch (ClassNotFoundException e) { |
| 184 | + throw new ConfigurationException("Example provider class not found: " + className, e); |
| 185 | + } catch (IllegalAccessException e) { |
| 186 | + throw new ConfigurationException("Cannot access example provider field: " + ref, e); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public static Builder builder() { |
| 191 | + return new Builder(); |
| 192 | + } |
| 193 | + |
| 194 | + public static final class Builder { |
| 195 | + private final List<Example> examples = new ArrayList<>(); |
| 196 | + private String name = "example_tool"; |
| 197 | + private String description = "Adds few-shot examples to the request"; |
| 198 | + private Optional<BaseExampleProvider> provider = Optional.empty(); |
| 199 | + |
| 200 | + @CanIgnoreReturnValue |
| 201 | + public Builder setName(String name) { |
| 202 | + this.name = name; |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + @CanIgnoreReturnValue |
| 207 | + public Builder setDescription(String description) { |
| 208 | + this.description = description; |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + @CanIgnoreReturnValue |
| 213 | + public Builder addExample(Example ex) { |
| 214 | + this.examples.add(ex); |
| 215 | + return this; |
| 216 | + } |
| 217 | + |
| 218 | + @CanIgnoreReturnValue |
| 219 | + public Builder setExampleProvider(BaseExampleProvider provider) { |
| 220 | + this.provider = Optional.ofNullable(provider); |
| 221 | + return this; |
| 222 | + } |
| 223 | + |
| 224 | + public ExampleTool build() { |
| 225 | + return new ExampleTool(this); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments