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/add json process tool #5555

Merged
merged 18 commits into from
Jun 28, 2024

Conversation

scotlandowl
Copy link
Contributor

Description

The existing template-transform node is quite complex when it comes to processing JSON data. For example, to extract just the URL from {"result": "{"url": "https://github.com/langgenius/dify"}\n"}, the complex code {{ result | replace('\n','') | replace('{"url": "','') | replace('"}','') }} needs to be used. To address this, a tool that uses jsonpath-ng for JSON processing has been introduced. This tool can parse, insert, delete and replace JSON content using simple instructions, greatly improving the ease of handling JSON. For the above example, simply inputting 'url' in json_filter is enough to obtain "https://github.com/langgenius/dify". This tool has significantly enhanced the convenience of JSON processing.

20240624_214703.mp4

parse

Given a JSON content and a json_filter, return the parsed result.
When the parsed result is a JSON structure, return it in JSON format. In other cases such as strings, numbers, etc., return a string.

insert

Given a JSON content, a query path, and a new_value:

  • If the query path exists:
    1. If the matched item is a dictionary, update an existing key-value pair or add a new one.
    2. If the matched item is a list, insert the new_value at the specified index, or append it to the end.
    3. For other types, directly replace the original value with the new_value.
  • If the query path does not exist and create_path is true, create the new path and insert the new_value.
  • Otherwise, do not make any changes.

delete

Given a JSON content and query. Based on the type of the query match, perform different deletion operations:

  • For dictionaries, delete the specified key.
  • For lists, remove the specified element.
  • For other cases, try to delete the corresponding key in the parent element.

replace

Given the JSON content, query, replace_value, and replace_model:

  • If replace_model is "pattern", for each result matching the query, replace the replace_pattern in the input string with replace_value.
  • If replace_model is "key", replace the key of each match with replace_key.
  • If replace_model is "value", replace the value of each match with replace_value.

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • TODO

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. 💪 enhancement New feature or request 🔨 feat:tools Tools for agent, function call related stuff. labels Jun 24, 2024
@crazywoola
Copy link
Member

Please fix the errors in CI, and make sure the commit messages are in English.

@scotlandowl
Copy link
Contributor Author

Please fix the errors in CI, and make sure the commit messages are in English.

The python and yaml styles have been fixed.

@scotlandowl
Copy link
Contributor Author

@Yeuoly I have submitted the requested changes. Please review when you have a moment. Thank you.

@scotlandowl scotlandowl requested a review from Yeuoly June 25, 2024 10:11
@Yeuoly
Copy link
Collaborator

Yeuoly commented Jun 27, 2024

hello, pls rebase to main to fix conflicts.

@scotlandowl
Copy link
Contributor Author

hello, pls rebase to main to fix conflicts.

The branch is now up-to-date with the main branch and includes the latest dependency updates.

@scotlandowl
Copy link
Contributor Author

@Yeuoly hello, the python style error in /tools/insert.py has been fixed.

@scotlandowl scotlandowl requested a review from Yeuoly June 27, 2024 09:47
Yeuoly
Yeuoly previously approved these changes Jun 27, 2024
Copy link
Collaborator

@Yeuoly Yeuoly left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks for your amazing tools!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 27, 2024
@scotlandowl scotlandowl requested a review from Yeuoly June 27, 2024 10:20
@scotlandowl
Copy link
Contributor Author

LGTM, Thanks for your amazing tools!

Thank you. And I want to ask if I need to resolve conflicts all the time?

Copy link
Collaborator

@Yeuoly Yeuoly left a comment

Choose a reason for hiding this comment

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

LGTM

@Yeuoly Yeuoly merged commit 73ce945 into langgenius:main Jun 28, 2024
5 checks passed
ZhouhaoJiang added a commit that referenced this pull request Jun 28, 2024
* refs/heads/main:
  Feat/add json process tool (#5555)
  fix: do not remove (#5682)
  Feature/add qwen llm (#5659)
  Fix docker command (#5681)
  feature: Add presence_penalty and frequency_penalty parameters to the … (#5637)
  Feat/fix ops trace (#5672)
  feat: xxo enhancement. (#5671)
  chore: rearrange python dependencies in groups (#5603)
  chore: delete unused resource (#5667)
@takatost takatost mentioned this pull request Jun 28, 2024
Scorpion1221 added a commit to yybht155/dify that referenced this pull request Jun 29, 2024
* commit '9513155fa46bc9bd1ed06381c163a831aea57d42': (32 commits)
  chore: support both $$ and $ latex format (langgenius#5723)
  Rename README to README.md (langgenius#5727)
  Chore/set entrypoint scripts permissions (langgenius#5726)
  add README for new docker/ directory (langgenius#5724)
  bump to 0.6.12 (langgenius#5712)
  Ignore new middleware.env docker file (langgenius#5715)
  fix: _convert_prompt_message_to_dict parameters err (langgenius#5716)
  fix: fix-app-site-missing command (langgenius#5714)
  feat: add fix-app-site-missing command (langgenius#5711)
  add docker-legacy and docker/nginx/conf.d/default.conf to .gitignore (langgenius#5707)
  fix: do not remove (langgenius#5706)
  build(deps): bump braces from 3.0.2 to 3.0.3 in /web (langgenius#5705)
  fix: yarn lock file missing (langgenius#5703)
  feat: add support Spark4.0 (langgenius#5688)
  Correction of Typo in French (langgenius#5699)
  Chore/improve deployment flow (langgenius#4299)
  feat: change TRACE_QUEUE_MANAGER_INTERVAL default value  (langgenius#5698)
  Feat/add json process tool (langgenius#5555)
  fix: do not remove (langgenius#5682)
  Feature/add qwen llm (langgenius#5659)
  ...

# Conflicts:
#	api/poetry.lock
#	api/pyproject.toml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request 🔨 feat:tools Tools for agent, function call related stuff. lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants