mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-16 19:34:44 +08:00
feat: lite deep researcher implementation
This commit is contained in:
3
src/utils/__init__.py
Normal file
3
src/utils/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
工具函数包
|
||||
"""
|
||||
36
src/utils/json_utils.py
Normal file
36
src/utils/json_utils.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import logging
|
||||
import json
|
||||
import json_repair
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def repair_json_output(content: str) -> str:
|
||||
"""
|
||||
Repair and normalize JSON output.
|
||||
|
||||
Args:
|
||||
content (str): String content that may contain JSON
|
||||
|
||||
Returns:
|
||||
str: Repaired JSON string, or original content if not JSON
|
||||
"""
|
||||
content = content.strip()
|
||||
if content.startswith(("{", "[")) or "```json" in content or "```ts" in content:
|
||||
try:
|
||||
# If content is wrapped in ```json code block, extract the JSON part
|
||||
if content.startswith("```json"):
|
||||
content = content.removeprefix("```json")
|
||||
|
||||
if content.startswith("```ts"):
|
||||
content = content.removeprefix("```ts")
|
||||
|
||||
if content.endswith("```"):
|
||||
content = content.removesuffix("```")
|
||||
|
||||
# Try to repair and parse JSON
|
||||
repaired_content = json_repair.loads(content)
|
||||
return json.dumps(repaired_content, ensure_ascii=False)
|
||||
except Exception as e:
|
||||
logger.warning(f"JSON repair failed: {e}")
|
||||
return content
|
||||
Reference in New Issue
Block a user