-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
enhancement (json): enhance JSON Process Tool extraction to return st… #10575
base: main
Are you sure you want to change the base?
Conversation
@crazywoola If you have any questions about the code, please let me know in time. |
@crazywoola |
Hi there, With these updates, the toolset would address a wider range of needs while remaining easy to use. Let me know your thoughts on this proposal! |
Hi Pedro, Yes, Absolutely correct. Just like you said, for backward compatibility. I also agree, making a new JSON Parse & Extractor tool might be a better choice than modifying the current one. I will modify this part of the code, adding a JSON Extractor in the JSON Process Tool, while retaining the existing four functions: Parse, Insert, Delete, Replace. @crazywoola have u anything comments and suggestion about this? |
…ructured messages and improve error handling
643fb5c
to
e3474b6
Compare
@crazywoola Thx PedroGomes02 |
Hi, my original idea is to adapt the parse tool to:
This way, we can use the parse tool either to filter/extract specific information or to return the fully parsed JSON, or both. parse.py def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
"""
# get tool parameters
content = tool_parameters.get("content", "")
json_filter = tool_parameters.get("json_filter", "")
ensure_ascii = tool_parameters.get("ensure_ascii", True)
output_full_parsed_json = tool_parameters.get("output_full_parsed_json", True)
if not content:
return self.create_text_message("Invalid parameter content")
try:
final_result = []
if output_full_parsed_json:
# parse full json
json_content = json.loads(content)
# append json_messages to final_result
if isinstance(json_content, list):
for item in json_content:
final_result.append(self.create_json_message(item))
else:
final_result.append(self.create_json_message(json_content))
if json_filter:
filtered_result = self._extract(content, json_filter, ensure_ascii)
final_result.append(self.create_text_message(str(filtered_result)))
return final_result
except Exception:
return self.create_text_message("Failed to extract JSON content") parse.yamlidentity:
name: parse
author: Mingwei Zhang
label:
en_US: JSON Parse
zh_Hans: JSON 解析
pt_BR: JSON Parse
description:
human:
en_US: A tool for extracting JSON objects
zh_Hans: 一个解析JSON对象的工具
pt_BR: A tool for extracting JSON objects
llm: A tool for extracting JSON objects
parameters:
- name: content
type: string
required: true
label:
en_US: JSON data
zh_Hans: JSON数据
pt_BR: JSON data
human_description:
en_US: JSON data
zh_Hans: JSON数据
pt_BR: JSON数据
llm_description: JSON data to be processed
form: llm
- name: json_filter
type: string
required: false
label:
en_US: JSON filter
zh_Hans: JSON解析对象
pt_BR: JSON filter
human_description:
en_US: JSON fields to be parsed
zh_Hans: 需要解析的 JSON 字段
pt_BR: JSON fields to be parsed
llm_description: JSON fields to be parsed
form: llm
- name: ensure_ascii
type: boolean
default: true
label:
en_US: Ensure ASCII
zh_Hans: 确保 ASCII
pt_BR: Ensure ASCII
human_description:
en_US: Ensure the JSON output is ASCII encoded
zh_Hans: 确保输出的 JSON 是 ASCII 编码
pt_BR: Ensure the JSON output is ASCII encoded
form: form
- name: output_full_parsed_json
type: boolean
default: true
label:
en_US: Output Full Parsed JSON
zh_Hans: 输出完整解析的 JSON
pt_BR: Output Full Parsed JSON
human_description:
en_US: The full parsed JSON is also outputted
zh_Hans: 完整解析的 JSON 也已输出
pt_BR: The full parsed JSON is also outputted
form: form |
This code implementation is really better, re-commit. |
This is to replace parse.py tool, not create a next extractor tool |
sorry, follow you code. |
…ructured messages and improve error handling
Checklist:
Important
Please review the checklist below before submitting your pull request.
dev/reformat
(backend) andcd web && npx lint-staged
(frontend) to appease the lint godsDescription
Link issue #10559
Demo data:
JSONPath filters:
$.Tables[*]
Expected
Actual
Did not parse the json array objects, fixed return objects by filters.
Type of Change
Testing Instructions
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