-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (66 loc) · 2.32 KB
/
utils.py
File metadata and controls
86 lines (66 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Utility functions for Multi-Agent Content Generation System.
Provides common utilities used across agents and logic blocks.
"""
import re
import json
import logging
from typing import Any, Optional
logger = logging.getLogger(__name__)
def clean_json_response(response: str) -> str:
"""
Clean JSON response from LLM by removing markdown code blocks.
This function handles common LLM response patterns where JSON is wrapped
in markdown code fences like ```json ... ``` or ``` ... ```.
Args:
response: Raw LLM response that may contain markdown formatting
Returns:
Cleaned JSON string ready for parsing
Example:
>>> clean_json_response('```json\\n{"key": "value"}\\n```')
'{"key": "value"}'
"""
if not response:
return ""
cleaned = response.strip()
# Remove markdown code blocks
if "```" in cleaned:
# Try to extract content between code fences
pattern = r'```(?:json)?\s*([\s\S]*?)\s*```'
match = re.search(pattern, cleaned)
if match:
cleaned = match.group(1).strip()
else:
# Fallback: remove all occurrences of ```json and ```
cleaned = cleaned.replace("```json", "").replace("```", "").strip()
return cleaned
def parse_json_safely(response: str, default: Any = None) -> Optional[Any]:
"""
Safely parse JSON from LLM response, handling common formatting issues.
Args:
response: Raw LLM response
default: Default value to return on parse failure
Returns:
Parsed JSON object or default value
"""
try:
cleaned = clean_json_response(response)
return json.loads(cleaned)
except json.JSONDecodeError as e:
logger.warning(f"Failed to parse JSON: {e}")
return default
except Exception as e:
logger.error(f"Unexpected error parsing JSON: {e}")
return default
def truncate_string(s: str, max_length: int = 100) -> str:
"""
Truncate a string to a maximum length with ellipsis.
Args:
s: String to truncate
max_length: Maximum length including ellipsis
Returns:
Truncated string with '...' if necessary
"""
if len(s) <= max_length:
return s
return s[:max_length - 3] + "..."