File tree 1 file changed +10
-0
lines changed
agents-api/agents_api/activities
1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,15 @@ def chunk_doc(string: str) -> list[str]:
103
103
return [" " .join ([sent .text for sent in chunk ]) for chunk in doc ._ .chunks ]
104
104
105
105
106
+ def safe_extract_json (string : str ) -> dict :
107
+ if len (string ) > MAX_STRING_LENGTH :
108
+ msg = f"String exceeds maximum length of { MAX_STRING_LENGTH } "
109
+ raise ValueError (msg )
110
+ extarct_String = string [string .find ("```json" )+ 7 :string .find ("```" , string .find ("```json" )+ 7 )]
111
+ return json .loads (extarct_String )
112
+
113
+
114
+
106
115
# Restricted set of allowed functions
107
116
ALLOWED_FUNCTIONS = {
108
117
# Basic Python builtins
@@ -131,6 +140,7 @@ def chunk_doc(string: str) -> list[str]:
131
140
"load_yaml" : safe_yaml_load ,
132
141
"dump_json" : json .dumps ,
133
142
"dump_yaml" : yaml .dump ,
143
+ "extract_json" : safe_extract_json ,
134
144
# Regex and NLP functions (using re2 which is safe against ReDoS)
135
145
"search_regex" : lambda pattern , string : re2 .search (pattern , string ),
136
146
"match_regex" : lambda pattern , string : bool (re2 .fullmatch (pattern , string )),
You can’t perform that action at this time.
0 commit comments