Skip to content

[example][java] Add single agent example - #225

Merged
xintongsong merged 2 commits into
apache:mainfrom
letaoj:single-agent-example
Oct 1, 2025
Merged

xintongsong merged 2 commits into
apache:mainfrom
letaoj:single-agent-example

Conversation

@letaoj

@letaoj letaoj commented Sep 26, 2025 •

Copy link
Copy Markdown
Contributor

Linked issue: #xxx

Purpose of change

Adding an example usage of SingleAgent in java

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.WorkflowSingleAgentExample; cd ..

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.esotericsoftware.kryo.util.UnsafeUtil (file:/Users/letaoj/.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
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[The user is very satisfied with the comfort and performance of the product.]}
ProductReviewAnalysisRes{id='B000YFSR4W', score=5, reasons=[No dissatisfaction mentioned in the review.]}
Transportation issue for product [B000YFSR4W], the customer feedback: The magazine was already damaged when I received it — the whole book was wet!
ProductReviewAnalysisRes{id='B000YFSR4W', score=1, reasons=[The product arrived damaged, with the entire book wet.]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[The product fits well and meets expectations.]}
ProductReviewAnalysisRes{id='B001IKJOLW', score=4, reasons=[The heel slipping during turns is a minor inconvenience.]}
ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[The product is described as comfortable, lightweight, and well-made with good support.]}
ProductReviewAnalysisRes{id='B014IBJKNO', score=5, reasons=[The user expressed satisfaction with the product, mentioning it is lightweight and comfortable.]}
ProductReviewAnalysisRes{id='B001IKJOLW', score=5, reasons=[No issues mentioned with the product]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[Positive sentiment towards the product]}
ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[No dissatisfaction mentioned in the review.]}
ProductReviewAnalysisRes{id='B0092UF54A', score=4, reasons=[The product met expectations in terms of appearance and comfort.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[Excellent fit for cross-training and HITT videos.]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[The user expressed satisfaction with the comfort and appearance of the product.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=2, reasons=[The product is described as expensive for its quality, indicating dissatisfaction with the value for money.]}
ProductReviewAnalysisRes{id='B014IBJKNO', score=5, reasons=[The product met expectations.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[Comfortable for people with bunion issues or previous surgeries, Sole wears great]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=2, reasons=[Insufficient cushioning in the footbed caused discomfort and a blister during a long walk.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[Positive sentiment about the product]}
Transportation issue for product [B001IKJOLW], the customer feedback: The magazine was already damaged when I received it — the whole book was wet!
ProductReviewAnalysisRes{id='B001IKJOLW', score=1, reasons=[The product arrived damaged, with the entire book wet.]}
ProductReviewAnalysisRes{id='B000YFSR5G', score=2, reasons=[The product did not fit well., The product was not comfortable.]}
ProductReviewAnalysisRes{id='B0014F7B98', score=5, reasons=[The product is described as cute and comfortable, indicating a positive experience.]}
ProductReviewAnalysisRes{id='B000YFSR5G', score=5, reasons=[No specific dissatisfaction mentioned; review is neutral and positive.]}
ProductReviewAnalysisRes{id='B014IBJKNO', score=4, reasons=[The user mentioned the shoe is light weight, which is a positive aspect.]}
ProductReviewAnalysisRes{id='B0092UF54A', score=5, reasons=[The user expressed extreme satisfaction with the product.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[The user expressed strong satisfaction with the product comfort and weight.]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=5, reasons=[Positive feedback on product quality]}
ProductReviewAnalysisRes{id='B0092UF54A', score=5, reasons=[The user expressed satisfaction with the product's comfort and fit.]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[No specific dissatisfaction mentioned; review is neutral or positive.]}
ProductReviewAnalysisRes{id='B001IKJOLW', score=5, reasons=[The user is very satisfied with the comfort and performance of the shoes.]}
ProductReviewAnalysisRes{id='B005AGO4LU', score=5, reasons=[The user expressed satisfaction with the comfort of the product.]}
ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[Positive experience with product comfort and usability.]}
ProductReviewAnalysisRes{id='B009MA34NY', score=5, reasons=[Positive feedback on fit]}
ProductReviewAnalysisRes{id='B0014F7B98', score=5, reasons=[The product arrived on time.]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=1, reasons=[The product had a defect with the green inserts on the sole falling off after minimal use.]}
ProductReviewAnalysisRes{id='B010RRWKT4', score=5, reasons=[Positive feedback on product quality and comfort]}
ProductReviewAnalysisRes{id='B0058YEJ5K', score=2, reasons=[The right shoe was too loose on the heel and caused discomfort when tightened.]}

API

Documentation

public String toString() {
if (success) {
return result.toString();
return getResultAsString();

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.

bug: when result is null...we got NPE

@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 contribution.

The reason why the agent result is not correct is the ChatMessage produced by processInput action doesn't have correspond keyword argument to fill the Prompt in ChatModel. I described the detail below.

And you can refer to the branch https://github.com/wenjin272/flink-agents/tree/single-agent-example-fix which has the patch

String.format(
"{\n" + "\"id\": %s,\n" + "\"review\": \"%s\"\n" + "}",
inputObj.getId(), inputObj.getReview());
ChatMessage msg = new ChatMessage(MessageRole.USER, content);

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.

Because the Prompt we used should be filled with keyword arguments {"input": input}

public static final Prompt REVIEW_ANALYSIS_PROMPT =
            new Prompt(
                    Arrays.asList(
                            new ChatMessage(MessageRole.SYSTEM, REVIEW_ANALYSIS_SYSTEM_PROMPT_STR),
                            new ChatMessage(MessageRole.USER, "\"input\":\n" + "{input}")));

Here, we should provide the keyword arguments in msg, or the Prompt won't be filled. And this is exactly the reason why your can't get correct result.
ChatMessage msg = new ChatMessage(MessageRole.USER, "", Map.of("input", content));

This behavior is indeed not clear for user, we have create a issue #220 , and will fix it in next version.

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 the detailed explanation. I was not able to figure it out and thought it might be a model issue :|

@wenjin272

Copy link
Copy Markdown
Contributor

The ci failure is caused by java compile error.

You may need rebase master and adjust some code for the addChatModelConnection interface has been replaced by addResource

@letaoj
letaoj force-pushed the single-agent-example branch from 7e21c71 to 4112213 Compare September 29, 2025 06:34
@letaoj

letaoj commented Sep 29, 2025

Copy link
Copy Markdown
Contributor Author

Thanks again @wenjin272! I'm able to run the workflow now with correct output!

@letaoj
letaoj force-pushed the single-agent-example branch 2 times, most recently from 6489ae4 to 7362e21 Compare September 29, 2025 06:41
@letaoj
letaoj requested a review from wenjin272 September 29, 2025 06:42
@letaoj
letaoj force-pushed the single-agent-example branch from 7362e21 to 7933dd1 Compare September 29, 2025 22:27
@wenjin272

Copy link
Copy Markdown
Contributor

LGTM. Please take a look at your convenience @xintongsong @Sxnan

@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.

Thank both @letaoj and @wenjin272. Just one last comment from my side.

@letaoj
letaoj requested a review from xintongsong September 30, 2025 23:24
@xintongsong
xintongsong force-pushed the single-agent-example branch from 8cdb972 to 7086a88 Compare October 1, 2025 00:48

@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. I just moved the NOTICE file to resources/META-INF/.

@xintongsong
xintongsong merged commit ed9cad1 into apache:main Oct 1, 2025
@letaoj
letaoj deleted the single-agent-example branch October 1, 2025 01:29
@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