|
| 1 | +package com.google.adk.flows.llmflows; |
| 2 | + |
| 3 | +import com.google.adk.agents.CallbackContext; |
| 4 | +import com.google.adk.agents.InvocationContext; |
| 5 | +import com.google.adk.agents.LlmAgent; |
| 6 | +import com.google.adk.agents.ReadonlyContext; |
| 7 | +import com.google.adk.events.Event; |
| 8 | +import com.google.adk.models.LlmRequest; |
| 9 | +import com.google.adk.models.LlmResponse; |
| 10 | +import com.google.adk.planners.BasePlanner; |
| 11 | +import com.google.adk.planners.BuiltInPlanner; |
| 12 | +import com.google.common.collect.ImmutableList; |
| 13 | +import com.google.genai.types.Content; |
| 14 | +import com.google.genai.types.Part; |
| 15 | +import io.reactivex.rxjava3.core.Single; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +public class NLPlanning { |
| 22 | + |
| 23 | + static class NlPlanningRequestProcessor implements RequestProcessor { |
| 24 | + |
| 25 | + @Override |
| 26 | + public Single<RequestProcessingResult> processRequest( |
| 27 | + InvocationContext context, LlmRequest llmRequest) { |
| 28 | + |
| 29 | + if (!(context.agent() instanceof LlmAgent)) { |
| 30 | + throw new IllegalArgumentException( |
| 31 | + "Agent in InvocationContext is not an instance of LlmAgent."); |
| 32 | + } |
| 33 | + |
| 34 | + Optional<BasePlanner> plannerOpt = getPlanner(context); |
| 35 | + if (plannerOpt.isEmpty()) { |
| 36 | + return Single.just(RequestProcessor.RequestProcessingResult.create(llmRequest, ImmutableList.of())); |
| 37 | + } |
| 38 | + |
| 39 | + BasePlanner planner = plannerOpt.get(); |
| 40 | + |
| 41 | + // Apply thinking configuration for built-in planners |
| 42 | + if (planner instanceof BuiltInPlanner) { |
| 43 | + llmRequest = ((BuiltInPlanner) planner).applyThinkingConfig(llmRequest); |
| 44 | + } |
| 45 | + |
| 46 | + // Build and append planning instruction |
| 47 | + Optional<String> planningInstruction = |
| 48 | + planner.generatePlanningInstruction(new ReadonlyContext(context), llmRequest); |
| 49 | + |
| 50 | + LlmRequest.Builder b = llmRequest.toBuilder(); |
| 51 | + planningInstruction.ifPresent(s -> b.appendInstructions(ImmutableList.of(s))); |
| 52 | + llmRequest = b.build(); |
| 53 | + |
| 54 | + // Remove thought annotations from request |
| 55 | + llmRequest = removeThoughtFromRequest(llmRequest); |
| 56 | + |
| 57 | + return Single.just(RequestProcessor.RequestProcessingResult.create(llmRequest, ImmutableList.of())); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static class NlPlanningResponseProcessor implements ResponseProcessor { |
| 62 | + |
| 63 | + @Override |
| 64 | + public Single<ResponseProcessingResult> processResponse( |
| 65 | + InvocationContext context, LlmResponse llmResponse) { |
| 66 | + |
| 67 | + if (!(context.agent() instanceof LlmAgent)) { |
| 68 | + throw new IllegalArgumentException( |
| 69 | + "Agent in InvocationContext is not an instance of LlmAgent."); |
| 70 | + } |
| 71 | + |
| 72 | + // Validate response structure |
| 73 | + if (llmResponse == null || llmResponse.content().isEmpty()) { |
| 74 | + return Single.just( |
| 75 | + ResponseProcessor.ResponseProcessingResult.create( |
| 76 | + llmResponse, ImmutableList.of(), Optional.empty())); |
| 77 | + } |
| 78 | + |
| 79 | + Optional<BasePlanner> plannerOpt = getPlanner(context); |
| 80 | + if (plannerOpt.isEmpty()) { |
| 81 | + return Single.just( |
| 82 | + ResponseProcessor.ResponseProcessingResult.create( |
| 83 | + llmResponse, ImmutableList.of(), Optional.empty())); |
| 84 | + } |
| 85 | + |
| 86 | + BasePlanner planner = plannerOpt.get(); |
| 87 | + LlmResponse.Builder responseBuilder = llmResponse.toBuilder(); |
| 88 | + |
| 89 | + // Process the planning response |
| 90 | + CallbackContext callbackContext = new CallbackContext(context, null); |
| 91 | + Optional<List<Part>> processedParts = |
| 92 | + planner.processPlanningResponse( |
| 93 | + callbackContext, llmResponse.content().get().parts().orElse(List.of())); |
| 94 | + |
| 95 | + // Update response with processed parts |
| 96 | + if (processedParts.isPresent()) { |
| 97 | + Content.Builder contentBuilder = llmResponse.content().get().toBuilder(); |
| 98 | + contentBuilder.parts(processedParts.get()); |
| 99 | + responseBuilder.content(contentBuilder.build()); |
| 100 | + } |
| 101 | + |
| 102 | + ImmutableList.Builder<Event> eventsBuilder = ImmutableList.builder(); |
| 103 | + |
| 104 | + // Generate state update event if there are deltas |
| 105 | + if (callbackContext.state().hasDelta()) { |
| 106 | + Event stateUpdateEvent = |
| 107 | + Event.builder() |
| 108 | + .invocationId(context.invocationId()) |
| 109 | + .author(context.agent().name()) |
| 110 | + .branch(context.branch()) |
| 111 | + .actions(callbackContext.eventActions()) |
| 112 | + .build(); |
| 113 | + |
| 114 | + eventsBuilder.add(stateUpdateEvent); |
| 115 | + } |
| 116 | + |
| 117 | + return Single.just( |
| 118 | + ResponseProcessor.ResponseProcessingResult.create( |
| 119 | + responseBuilder.build(), eventsBuilder.build(), Optional.empty())); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Retrieves the planner from the invocation context. |
| 125 | + * |
| 126 | + * @param invocationContext the current invocation context |
| 127 | + * @return optional planner instance, or empty if none available |
| 128 | + */ |
| 129 | + private static Optional<BasePlanner> getPlanner(InvocationContext invocationContext) { |
| 130 | + if (!(invocationContext.agent() instanceof LlmAgent agent)) { |
| 131 | + return Optional.empty(); |
| 132 | + } |
| 133 | + |
| 134 | + return agent.planner(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Removes thought annotations from all parts in the LLM request. |
| 139 | + * |
| 140 | + * <p>This method iterates through all content parts and sets the thought field to false, |
| 141 | + * effectively removing thought markings from the request. |
| 142 | + * |
| 143 | + * @param llmRequest the LLM request to process |
| 144 | + */ |
| 145 | + private static LlmRequest removeThoughtFromRequest(LlmRequest llmRequest) { |
| 146 | + if (llmRequest.contents() == null || llmRequest.contents().isEmpty()) { |
| 147 | + return llmRequest; |
| 148 | + } |
| 149 | + |
| 150 | + // Process each content and update its parts |
| 151 | + List<Content> updatedContents = llmRequest.contents().stream().map(content -> { |
| 152 | + if (content.parts().isEmpty()) { |
| 153 | + return content; |
| 154 | + } |
| 155 | + |
| 156 | + // Update all parts to set thought to false |
| 157 | + List<Part> updatedParts = content.parts().get().stream().map(part -> part.toBuilder().thought(false).build()).collect(Collectors.toList()); |
| 158 | + |
| 159 | + // Return updated content with modified parts |
| 160 | + return content.toBuilder().parts(updatedParts).build(); |
| 161 | + }).collect(Collectors.toList()); |
| 162 | + |
| 163 | + // Return updated LlmRequest with modified contents |
| 164 | + return llmRequest.toBuilder().contents(updatedContents).build(); |
| 165 | + } |
| 166 | +} |
0 commit comments