diff --git a/owl/camel/toolkits/web_toolkit.py b/owl/camel/toolkits/web_toolkit.py index 8c3036a..de9226a 100644 --- a/owl/camel/toolkits/web_toolkit.py +++ b/owl/camel/toolkits/web_toolkit.py @@ -107,61 +107,119 @@ def _get_bool(d: Any, k: str) -> bool: def _parse_json_output(text: str) -> Dict[str, Any]: - """Extract JSON output from a string.""" + """Extract JSON output from text, supporting non-standard formats and special characters.""" markdown_pattern = r'```(?:json)?\s*(.*?)\s*```' markdown_match = re.search(markdown_pattern, text, re.DOTALL) if markdown_match: text = markdown_match.group(1).strip() - triple_quotes_pattern = r'"""(?:json)?\s*(.*?)\s*"""' - triple_quotes_match = re.search(triple_quotes_pattern, text, re.DOTALL) - if triple_quotes_match: - text = triple_quotes_match.group(1).strip() - - text = text.replace("`", '"') - + text = re.sub(r':\s*`([^`]*)`', r': "\1"', text) + try: return json.loads(text) except json.JSONDecodeError: + fixed_text = re.sub(r'(?