-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathopenfunctions_utils.py
36 lines (33 loc) · 1.41 KB
/
openfunctions_utils.py
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
from utils.python_parser import parse_python_function_call
from utils.java_parser import parse_java_function_call
from utils.js_parser import parse_javascript_function_call
FN_CALL_DELIMITER = "<<function>>"
def strip_function_calls(content: str) -> list[str]:
"""
Split the content by the function call delimiter and remove empty strings
"""
return [element.strip() for element in content.split(FN_CALL_DELIMITER)[2:] if element.strip()]
def parse_function_call(call: str) -> dict[str, any]:
"""
This is temporary. The long term solution is to union all the
types of the parameters from the user's input function definition,
and check which language is a proper super set of the union type.
"""
try:
return parse_python_function_call(call)
except Exception as e:
# If Python parsing fails, try Java parsing
try:
java_result = parse_java_function_call(call)
if not java_result:
raise Exception("Java parsing failed")
return java_result
except Exception as e:
# If Java parsing also fails, try JavaScript parsing
try:
javascript_result = parse_javascript_function_call(call)
if not javascript_result:
raise Exception("JavaScript parsing failed")
return javascript_result
except:
return None