Skip to content
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

feat: more robust parser to get suggested actions #65

Conversation

SuZhou-Joe
Copy link
Member

@SuZhou-Joe SuZhou-Joe commented Dec 12, 2023

Description

The suggested actions tool may output a response like

Here are result { "response": ["{1}", "{2}"], "foo": "bar" }

We need a more robust parser to get the json inside the response and make it as our suggestions.

Issues Resolved

List any issues this PR will resolve, e.g. Closes [...].

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Dec 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (11e5779) 58.67% compared to head (a2dd5c6) 71.26%.
Report is 3 commits behind head on feature/agent-framework.

Additional details and impacted files
@@                     Coverage Diff                      @@
##           feature/agent-framework      #65       +/-   ##
============================================================
+ Coverage                    58.67%   71.26%   +12.58%     
============================================================
  Files                           46       43        -3     
  Lines                         1089     1023       -66     
  Branches                       252      245        -7     
============================================================
+ Hits                           639      729       +90     
+ Misses                         441      292      -149     
+ Partials                         9        2        -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -13,19 +13,79 @@ const sanitize = (content: string) => {
return DOMPurify.sanitize(content, { FORBID_TAGS: ['img'] }).replace(/!+\[/g, '[');
};

const isEveryItemString = (array?: unknown) =>
!!(Array.isArray(array) && array.every((item) => typeof item === 'string'));
Copy link
Member

Choose a reason for hiding this comment

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

seems don't need !! as the result is boolean already

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

const isEveryItemString = (array?: unknown) =>
!!(Array.isArray(array) && array.every((item) => typeof item === 'string'));

export const parseSuggestedActions = (string: string): string[] => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: maybe rename string: string -> value: string? as it sounds a bit odd that the variable name and type are the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, done.

@@ -13,19 +13,79 @@ const sanitize = (content: string) => {
return DOMPurify.sanitize(content, { FORBID_TAGS: ['img'] }).replace(/!+\[/g, '[');
};

const isEveryItemString = (array?: unknown) =>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const isEveryItemString = (array?: unknown) =>
const isEveryItemString = (array?: unknown): array is string[] =>

Maybe we can call it isStringArray?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it stuck me when naming this function and you get the point of this. Change to isStringArray.

const matchedResult = match && match[0];

if (!matchedResult) {
return [];
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a test case to cover this line? When the output doesn't contain any json-like content.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done and thanks for catching.

@SuZhou-Joe SuZhou-Joe merged commit 621b914 into opensearch-project:feature/agent-framework Dec 13, 2023
10 checks passed
@joshuali925
Copy link
Member

is there some prompt difference that causes agent framework to not output a consistent format for suggestions?

joshuali925 pushed a commit to joshuali925/dashboards-assistant that referenced this pull request Dec 13, 2023
)

* feat: more robust parser to get suggested actions

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimize code and optimize performance

Signed-off-by: SuZhou-Joe <[email protected]>

---------

Signed-off-by: SuZhou-Joe <[email protected]>
@xluo-aws
Copy link
Member

Will this be merged to main?

@SuZhou-Joe
Copy link
Member Author

SuZhou-Joe commented Dec 16, 2023

Yeah, it has already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants