-
Notifications
You must be signed in to change notification settings - Fork 157
Move the planning_re_act_planner python implementation to java version. #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @leweii, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a comprehensive planning framework for LLM agents in Java, effectively porting a previously existing Python implementation. It establishes a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request successfully ports a Python-based Plan-ReAct planner to Java, introducing a BasePlanner
interface and integrating it into the LlmAgent
and processing flows. The implementation includes the PlanReActPlanner
with its specific prompting and response parsing logic, a BuiltInPlanner
for models with native cognitive features, and new flow processors (NLPlanning
) to handle the planning lifecycle. The changes are well-structured, but I've identified a few areas for improvement regarding code clarity, robustness, and testing. Specifically, some redundant checks can be removed, passing null to a constructor can be avoided, and a new test is flawed and requires correction to properly validate the intended behavior.
@Test | ||
public void builtInPlanner_contentListUnchanged() { | ||
|
||
Content content = Content.fromParts(Part.fromText("LLM response")); | ||
TestLlm testLlm = createTestLlm(createLlmResponse(content)); | ||
BuiltInPlanner planner = BuiltInPlanner.buildPlanner(ThinkingConfig.builder().build()); | ||
LlmAgent agent = createTestAgent(testLlm, planner); | ||
var invocationContext = com.google.adk.testing.TestUtils.createInvocationContext(agent); | ||
|
||
List<Content> contents = List.of( | ||
Content.builder() | ||
.role("user") | ||
.parts(Part.fromText("Hello")) | ||
.build(), | ||
Content.builder() | ||
.role("model") | ||
.parts( | ||
Part.builder().thought(true).text("thinking....").build(), | ||
Part.fromText("Here is my response") | ||
) | ||
.build(), | ||
Content.builder() | ||
.role("user") | ||
.parts(Part.fromText("Follow up")) | ||
.build() | ||
); | ||
LlmRequest llmRequest = LlmRequest.builder().contents(contents).build(); | ||
List<Content> originalContents = List.copyOf(llmRequest.contents()); | ||
|
||
// Act | ||
agent.runAsync(invocationContext).blockingSubscribe(); | ||
|
||
// Assert | ||
assertThat(llmRequest.contents()).isEqualTo(originalContents); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is flawed and does not validate the intended behavior. The LlmRequest
object created on line 47 is a local variable that is never passed into the agent's execution flow initiated by agent.runAsync()
. The BaseLlmFlow
creates its own LlmRequest
internally, so the assertion on line 54 will always pass vacuously.
To properly test that the request processing logic does not mutate its input, you should unit test NlPlanningRequestProcessor.processRequest
directly. Here is an example of a more effective test:
@Test
public void nlPlanningRequestProcessor_doesNotMutateOriginalRequest() {
// Arrange
var processor = new NLPlanning.NlPlanningRequestProcessor();
BuiltInPlanner planner = BuiltInPlanner.buildPlanner(ThinkingConfig.builder().build());
LlmAgent agent = createTestAgent(createTestLlm(createLlmResponse(Content.fromText(""))), planner);
var invocationContext = com.google.adk.testing.TestUtils.createInvocationContext(agent);
List<Content> contents = List.of(
Content.builder()
.role("model")
.parts(Part.builder().thought(true).text("thinking....").build())
.build()
);
LlmRequest originalRequest = LlmRequest.builder().contents(contents).build();
List<Content> originalContents = List.copyOf(originalRequest.contents());
// Act
RequestProcessor.RequestProcessingResult result = processor.processRequest(invocationContext, originalRequest).blockingGet();
// Assert
// Check that the original request object is not mutated.
assertThat(originalRequest.contents()).isEqualTo(originalContents);
assertThat(originalRequest.contents().get(0).parts().get().get(0).thought()).isTrue();
// Check that the new request has the thoughts removed.
LlmRequest processedRequest = result.updatedRequest();
assertThat(processedRequest.contents().get(0).parts().get().get(0).thought()).isFalse();
}
if (!(context.agent() instanceof LlmAgent)) { | ||
throw new IllegalArgumentException( | ||
"Agent in InvocationContext is not an instance of LlmAgent."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This instanceof
check for LlmAgent
is redundant. The getPlanner
method called on line 34 already performs this check and returns an empty Optional
if the agent is not an LlmAgent
. The subsequent check if (plannerOpt.isEmpty())
on line 35 is sufficient to handle this case. Removing these lines will simplify the code and avoid duplication, as the same pattern is used in NlPlanningResponseProcessor
.
if (!(context.agent() instanceof LlmAgent)) { | ||
throw new IllegalArgumentException( | ||
"Agent in InvocationContext is not an instance of LlmAgent."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the NlPlanningRequestProcessor
, this instanceof
check is redundant. The getPlanner
method on line 79 handles the case where the agent is not an LlmAgent
by returning an empty Optional
, which is correctly handled by the check on line 80. You can remove these lines to improve code clarity and reduce duplication.
LlmResponse.Builder responseBuilder = llmResponse.toBuilder(); | ||
|
||
// Process the planning response | ||
CallbackContext callbackContext = new CallbackContext(context, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing null
for the eventActions
parameter in the CallbackContext
constructor is not ideal, even though the constructor currently handles it. It's better to be explicit to make the code more robust and readable, especially if the constructor's null-handling logic were to change in the future. Please pass an empty EventActions
object instead.
CallbackContext callbackContext = new CallbackContext(context, null); | |
CallbackContext callbackContext = new CallbackContext(context, EventActions.builder().build()); |
public class BuiltInPlanner implements BasePlanner { | ||
private ThinkingConfig cognitiveConfig; | ||
|
||
private BuiltInPlanner() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
750d6e5
to
1e0bed3
Compare
No description provided.