|
| 1 | +/* |
| 2 | + * Copyright 2026 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.flows.llmflows; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.adk.agents.InvocationContext; |
| 22 | +import com.google.adk.agents.LlmAgent; |
| 23 | +import com.google.adk.agents.RunConfig; |
| 24 | +import com.google.adk.examples.BaseExampleProvider; |
| 25 | +import com.google.adk.examples.Example; |
| 26 | +import com.google.adk.models.LlmRequest; |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import com.google.genai.types.Content; |
| 29 | +import com.google.genai.types.Part; |
| 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 ExamplesTest { |
| 37 | + |
| 38 | + private static class TestExampleProvider implements BaseExampleProvider { |
| 39 | + @Override |
| 40 | + public List<Example> getExamples(String query) { |
| 41 | + return ImmutableList.of( |
| 42 | + Example.builder() |
| 43 | + .input(Content.fromParts(Part.fromText("input1"))) |
| 44 | + .output( |
| 45 | + ImmutableList.of( |
| 46 | + Content.builder().parts(Part.fromText("output1")).role("model").build())) |
| 47 | + .build()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void processRequest_withExampleProvider_addsExamplesToInstructions() { |
| 53 | + LlmAgent agent = |
| 54 | + LlmAgent.builder().name("test-agent").exampleProvider(new TestExampleProvider()).build(); |
| 55 | + InvocationContext context = |
| 56 | + InvocationContext.builder() |
| 57 | + .invocationId("invocation1") |
| 58 | + .agent(agent) |
| 59 | + .userContent(Content.fromParts(Part.fromText("what is up?"))) |
| 60 | + .runConfig(RunConfig.builder().build()) |
| 61 | + .build(); |
| 62 | + LlmRequest request = LlmRequest.builder().build(); |
| 63 | + Examples examplesProcessor = new Examples(); |
| 64 | + |
| 65 | + RequestProcessor.RequestProcessingResult result = |
| 66 | + examplesProcessor.processRequest(context, request).blockingGet(); |
| 67 | + |
| 68 | + assertThat(result.updatedRequest().getSystemInstructions()).isNotEmpty(); |
| 69 | + assertThat(result.updatedRequest().getSystemInstructions().get(0)) |
| 70 | + .contains("[user]\ninput1\n\n[model]\noutput1\n"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void processRequest_withoutExampleProvider_doesNotAddExamplesToInstructions() { |
| 75 | + LlmAgent agent = LlmAgent.builder().name("test-agent").build(); |
| 76 | + InvocationContext context = |
| 77 | + InvocationContext.builder() |
| 78 | + .invocationId("invocation1") |
| 79 | + .agent(agent) |
| 80 | + .userContent(Content.fromParts(Part.fromText("what is up?"))) |
| 81 | + .runConfig(RunConfig.builder().build()) |
| 82 | + .build(); |
| 83 | + LlmRequest request = LlmRequest.builder().build(); |
| 84 | + Examples examplesProcessor = new Examples(); |
| 85 | + |
| 86 | + RequestProcessor.RequestProcessingResult result = |
| 87 | + examplesProcessor.processRequest(context, request).blockingGet(); |
| 88 | + |
| 89 | + assertThat(result.updatedRequest().getSystemInstructions()).isEmpty(); |
| 90 | + } |
| 91 | +} |
0 commit comments