Skip to content

[example][java] Add ReAct agent example - #242

Merged
xintongsong merged 1 commit into
apache:mainfrom
letaoj:react-agent-example
Oct 7, 2025
Merged

xintongsong merged 1 commit into
apache:mainfrom
letaoj:react-agent-example

Conversation

@letaoj

@letaoj letaoj commented Oct 1, 2025 •

Copy link
Copy Markdown
Contributor

Linked issue: #229

Purpose of change

This change added a example for the ReAct agent quickstart guide in Java similar to what python did. In this change, we are made a couple improvement to the existing single and multiple agent example to remove unused connection setup.

Tests

❯ cd examples; java --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.desktop/java.awt.font=ALL-UNNAMED -cp "target/classes:$(mvn dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout)" org.apache.flink.agents.examples.ReActAgentExample; cd ..
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.esotericsoftware.kryo.util.UnsafeUtil (file:/Users/ljiang/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar) to constructor java.nio.DirectByteBuffer(long,int,java.lang.Object)
WARNING: Please consider reporting this to the maintainers of com.esotericsoftware.kryo.util.UnsafeUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
8> ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[good support, comfortable, lightweight, well-made, long lasting]}
4> ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[]}
1> ProductReviewAnalysisRes{id='B000YFSR5G', score=2, reasons=[did not fit well, not comfortable]}
5> ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[]}
7> ProductReviewAnalysisRes{id='B001IKJOLW', score=5, reasons=[]}
9> ProductReviewAnalysisRes{id='B000YFSR4W', score=5, reasons=[]}
6> ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[]}
11> ProductReviewAnalysisRes{id='B000YFSR5G', score=5, reasons=[]}
10> ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[comfortable]}
12> ProductReviewAnalysisRes{id='B014IBJKNO', score=5, reasons=[]}
3> ProductReviewAnalysisRes{id='B0092UF54A', score=5, reasons=[excellent product quality]}
8> ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[]}
2> ProductReviewAnalysisRes{id='B000YFSR4W', score=1, reasons=[damaged product upon delivery]}
4> ProductReviewAnalysisRes{id='B000KPIHQ4', score=5, reasons=[]}
1> ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[comfortable]}
5> ProductReviewAnalysisRes{id='B005AGO4LU', score=5, reasons=[comfortable, great for weight training]}
7> ProductReviewAnalysisRes{id='B0058YEJ5K', score=2, reasons=[poor quality, overpriced]}

API

Documentation

@letaoj
letaoj force-pushed the react-agent-example branch from 10ffbff to 43b5e39 Compare October 1, 2025 22:57
@letaoj

letaoj commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

One thing that I'm not sure is whether the notifyShipping tools was called or not. At least, i'm not able to see it from the short period that I ran it.

@letaoj

letaoj commented Oct 1, 2025 •

Copy link
Copy Markdown
Contributor Author

@wenjin272 @xintongsong @Sxnan please take a look when you gets time.

@letaoj
letaoj force-pushed the react-agent-example branch from 43b5e39 to 288f9f0 Compare October 2, 2025 21:16

@wenjin272 wenjin272 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @letaoj, thanks for your work.

The tool is exactly called, but it fell into recursion.

"Notify the shipping manager when product received a negative review due to shipping damage.")
public static void notifyShippingManager(
@ToolParam(name = "id") String id, @ToolParam(name = "review") String review) {
notifyShippingManager(id, review);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use org.apache.flink.agents.examples.agents.CustomTypesAndResources.notifyShippingManager( id, review); here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. This is not an obvious bug. Is that possible to write this code in a different way so that I do not need to have this sort of redundent call in the ReAct agent?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add @tool and @ToolParam directly on the notifyShippingManager method in CustomTypesAndResources, and register it in agents execution envrionment.

@letaoj
letaoj force-pushed the react-agent-example branch from 288f9f0 to 8766f98 Compare October 6, 2025 06:35
@wenjin272

Copy link
Copy Markdown
Contributor

LGTM

@xintongsong xintongsong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

DataStream<Row> productReviewStream =
env.fromSource(
FileSource.forRecordStreamFormat(
new TextLineFormat(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextLineFormat has been deprecated and removed in Flink 2.x. I think we should use TextLineInputFormat instead.

@xintongsong
xintongsong merged commit f7481aa into apache:main Oct 7, 2025
@Sxnan Sxnan added priority/major Default priority of the PR or issue. fixVersion/0.1.0 The feature or bug should be implemented/fixed in the 0.1.0 version. doc-not-needed Your PR changes do not impact docs labels Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs fixVersion/0.1.0 The feature or bug should be implemented/fixed in the 0.1.0 version. priority/major Default priority of the PR or issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants