openfunctions_utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from utils.python_parser import parse_python_function_call
  2. from utils.java_parser import parse_java_function_call
  3. from utils.js_parser import parse_javascript_function_call
  4. FN_CALL_DELIMITER = "<<function>>"
  5. def strip_function_calls(content: str) -> list[str]:
  6. """
  7. Split the content by the function call delimiter and remove empty strings
  8. """
  9. return [element.strip() for element in content.split(FN_CALL_DELIMITER)[2:] if element.strip()]
  10. def parse_function_call(call: str) -> dict[str, any]:
  11. """
  12. This is temporary. The long term solution is to union all the
  13. types of the parameters from the user's input function definition,
  14. and check which language is a proper super set of the union type.
  15. """
  16. try:
  17. return parse_python_function_call(call)
  18. except Exception as e:
  19. # If Python parsing fails, try Java parsing
  20. try:
  21. java_result = parse_java_function_call(call)
  22. if not java_result:
  23. raise Exception("Java parsing failed")
  24. return java_result
  25. except Exception as e:
  26. # If Java parsing also fails, try JavaScript parsing
  27. try:
  28. javascript_result = parse_javascript_function_call(call)
  29. if not javascript_result:
  30. raise Exception("JavaScript parsing failed")
  31. return javascript_result
  32. except:
  33. return None