feat: add decode option to json process tools (#7138)

This commit is contained in:
Jeff Li 2024-08-09 19:05:27 +08:00 committed by GitHub
parent 7ebad74372
commit d338f69837
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 12 deletions

View File

@ -36,21 +36,26 @@ class JSONParseTool(BuiltinTool):
# get create path
create_path = tool_parameters.get('create_path', False)
# get value decode.
# if true, it will be decoded to an dict
value_decode = tool_parameters.get('value_decode', False)
ensure_ascii = tool_parameters.get('ensure_ascii', True)
try:
result = self._insert(content, query, new_value, ensure_ascii, index, create_path)
result = self._insert(content, query, new_value, ensure_ascii, value_decode, index, create_path)
return self.create_text_message(str(result))
except Exception:
return self.create_text_message('Failed to insert JSON content')
def _insert(self, origin_json, query, new_value, ensure_ascii: bool, index=None, create_path=False):
def _insert(self, origin_json, query, new_value, ensure_ascii: bool, value_decode: bool, index=None, create_path=False):
try:
input_data = json.loads(origin_json)
expr = parse(query)
try:
new_value = json.loads(new_value)
except json.JSONDecodeError:
new_value = new_value
if value_decode is True:
try:
new_value = json.loads(new_value)
except json.JSONDecodeError:
return "Cannot decode new value to json object"
matches = expr.find(input_data)

View File

@ -47,10 +47,22 @@ parameters:
pt_BR: New Value
human_description:
en_US: New Value
zh_Hans: 新值
zh_Hans: 插入的新值
pt_BR: New Value
llm_description: New Value to insert
form: llm
- name: value_decode
type: boolean
default: false
label:
en_US: Decode Value
zh_Hans: 解码值
pt_BR: Decode Value
human_description:
en_US: Whether to decode the value to a JSON object
zh_Hans: 是否将值解码为 JSON 对象
pt_BR: Whether to decode the value to a JSON object
form: form
- name: create_path
type: select
required: true

View File

@ -35,6 +35,10 @@ class JSONReplaceTool(BuiltinTool):
if not replace_model:
return self.create_text_message('Invalid parameter replace_model')
# get value decode.
# if true, it will be decoded to an dict
value_decode = tool_parameters.get('value_decode', False)
ensure_ascii = tool_parameters.get('ensure_ascii', True)
try:
if replace_model == 'pattern':
@ -42,17 +46,17 @@ class JSONReplaceTool(BuiltinTool):
replace_pattern = tool_parameters.get('replace_pattern', '')
if not replace_pattern:
return self.create_text_message('Invalid parameter replace_pattern')
result = self._replace_pattern(content, query, replace_pattern, replace_value, ensure_ascii)
result = self._replace_pattern(content, query, replace_pattern, replace_value, ensure_ascii, value_decode)
elif replace_model == 'key':
result = self._replace_key(content, query, replace_value, ensure_ascii)
elif replace_model == 'value':
result = self._replace_value(content, query, replace_value, ensure_ascii)
result = self._replace_value(content, query, replace_value, ensure_ascii, value_decode)
return self.create_text_message(str(result))
except Exception:
return self.create_text_message('Failed to replace JSON content')
# Replace pattern
def _replace_pattern(self, content: str, query: str, replace_pattern: str, replace_value: str, ensure_ascii: bool) -> str:
def _replace_pattern(self, content: str, query: str, replace_pattern: str, replace_value: str, ensure_ascii: bool, value_decode: bool) -> str:
try:
input_data = json.loads(content)
expr = parse(query)
@ -61,6 +65,12 @@ class JSONReplaceTool(BuiltinTool):
for match in matches:
new_value = match.value.replace(replace_pattern, replace_value)
if value_decode is True:
try:
new_value = json.loads(new_value)
except json.JSONDecodeError:
return "Cannot decode replace value to json object"
match.full_path.update(input_data, new_value)
return json.dumps(input_data, ensure_ascii=ensure_ascii)
@ -92,10 +102,15 @@ class JSONReplaceTool(BuiltinTool):
return str(e)
# Replace value
def _replace_value(self, content: str, query: str, replace_value: str, ensure_ascii: bool) -> str:
def _replace_value(self, content: str, query: str, replace_value: str, ensure_ascii: bool, value_decode: bool) -> str:
try:
input_data = json.loads(content)
expr = parse(query)
if value_decode is True:
try:
replace_value = json.loads(replace_value)
except json.JSONDecodeError:
return "Cannot decode replace value to json object"
matches = expr.find(input_data)

View File

@ -60,10 +60,22 @@ parameters:
pt_BR: Replace Value
human_description:
en_US: New Value
zh_Hans: New Value
zh_Hans: 新值
pt_BR: New Value
llm_description: New Value to replace
form: llm
- name: value_decode
type: boolean
default: false
label:
en_US: Decode Value
zh_Hans: 解码值
pt_BR: Decode Value
human_description:
en_US: Whether to decode the value to a JSON object (Does not apply to replace key)
zh_Hans: 是否将值解码为 JSON 对象 (不适用于键替换)
pt_BR: Whether to decode the value to a JSON object (Does not apply to replace key)
form: form
- name: replace_model
type: select
required: true