Conversation
Remove comments in output json str, after json value content, maybe start with #, maybe start with //, particularly, it is not inside the string value Addtionly, if you do not want JSONDecodeError to occur, you can add 'Delete comments in json' after FORMAT_CONSTRAINT in action_node.py
…Delete comments in json' after FORMAT_CONSTRAINT in action_node.py
metagpt/actions/action_node.py
Outdated
| LANGUAGE_CONSTRAINT = "Language: Please use the same language as Human INPUT." | ||
| FORMAT_CONSTRAINT = f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else." | ||
| FORMAT_CONSTRAINT = (f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else. " | ||
| f"Delete comments in json") |
There was a problem hiding this comment.
due to ActionNode can generate json or markdown format example, we suggest not to add explicit json keyword in template str. Can the new added code in repair_llm_xx solved the problem, If so, maybe it's no need to add here.
There was a problem hiding this comment.
Well, I agree with you.
metagpt/actions/action_node.py
Outdated
| FORMAT_CONSTRAINT = (f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else. " | ||
| f"Delete comments in json") | ||
| # Delete comments in json | ||
| # If you don't want JSONDecodeError to occur, you can add Delete comments in json after FORMAT_CONSTRAINT |
There was a problem hiding this comment.
seems no need to add this explanation in extra lines.
| elif line.endswith("},") and output.endswith("},"): | ||
| new_line = line[:-1] | ||
| # remove comments in output json str, after json value content, maybe start with #, maybe start with // | ||
| elif rline[col_no] == "#" or rline[col_no] == "/": |
There was a problem hiding this comment.
since you have removed comments in the repair pipeline in repair_json_format , there is no need to do it again here.
| return output | ||
|
|
||
|
|
||
| def remove_comments_from_line(line): |
There was a problem hiding this comment.
add unittest case for // like https://github.com/geekan/MetaGPT/blob/main/tests/metagpt/utils/test_repair_llm_raw_output.py#L131-L141
There was a problem hiding this comment.
Modify code based on feedback of action_node.py and repair_llm_raw_output.py, add code in test_repair_llm_raw_output.py. Please have a look, thanks.
…tput.py, add code in test_repair_llm_raw_output.py
|
LGTM |
Solve JSONDecodeError, about issue #749
To avoid JSONDecodeError:
Remove comments in output json str, after json value content, maybe start with #, maybe start with //, particularly, it is not inside the string value
Addtionly, if you do not want JSONDecodeError to occur, you can add 'Delete comments in json' after FORMAT_CONSTRAINT in action_node.py
Features
The json content returned by a LLM may contain comments, maybe start with #, maybe start with //, it's random. This can lead to subsequent json parsing errors that affect the overall code execution.
The Error has happened whether I use gpt-4-1106-preview or GLM-4.
These code changes are intended to fix this problem.
Feature Docs
Influence
These code changes are intended to fix JSONDecodeError.
Result
See the issue #749 in detail
The json comments maybe start with #

The json comments maybe start with //

So we need to fix the code repair_llm_raw_output.py.
Add the second way to fix bug about raised JSONDecodeError:
Add prompt "Delete comments in json" of FORMAT_CONSTRAINT in action_node.py, as follows:
raised JSONDecodeError as follows:

add "Delete comments in json", solve the problem:
