utils.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import re
  3. import subprocess
  4. import sys
  5. from importlib import util
  6. import types
  7. from open_webui.apps.webui.models.functions import Functions
  8. from open_webui.apps.webui.models.tools import Tools
  9. from open_webui.config import FUNCTIONS_DIR, TOOLS_DIR
  10. def extract_frontmatter(file_path):
  11. """
  12. Extract frontmatter as a dictionary from the specified file path.
  13. """
  14. frontmatter = {}
  15. frontmatter_started = False
  16. frontmatter_ended = False
  17. frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE)
  18. try:
  19. with open(file_path, "r", encoding="utf-8") as file:
  20. first_line = file.readline()
  21. if first_line.strip() != '"""':
  22. # The file doesn't start with triple quotes
  23. return {}
  24. frontmatter_started = True
  25. for line in file:
  26. if '"""' in line:
  27. if frontmatter_started:
  28. frontmatter_ended = True
  29. break
  30. if frontmatter_started and not frontmatter_ended:
  31. match = frontmatter_pattern.match(line)
  32. if match:
  33. key, value = match.groups()
  34. frontmatter[key.strip()] = value.strip()
  35. except FileNotFoundError:
  36. print(f"Error: The file {file_path} does not exist.")
  37. return {}
  38. except Exception as e:
  39. print(f"An error occurred: {e}")
  40. return {}
  41. return frontmatter
  42. def replace_imports(content):
  43. """
  44. Replace the import paths in the content.
  45. """
  46. replacements = {
  47. "from utils": "from open_webui.utils",
  48. "from apps": "from open_webui.apps",
  49. "from main": "from open_webui.main",
  50. "from config": "from open_webui.config",
  51. }
  52. for old, new in replacements.items():
  53. content = content.replace(old, new)
  54. return content
  55. def load_toolkit_module_by_id(toolkit_id, content=None):
  56. if content is None:
  57. tool = Tools.get_tool_by_id(toolkit_id)
  58. if not tool:
  59. raise Exception(f"Toolkit not found: {toolkit_id}")
  60. content = tool.content
  61. content = replace_imports(content)
  62. Tools.update_tool_by_id(toolkit_id, {"content": content})
  63. module_name = f"{toolkit_id}"
  64. module = types.ModuleType(module_name)
  65. sys.modules[module_name] = module
  66. try:
  67. # Executing the modified content in the created module's namespace
  68. exec(content, module.__dict__)
  69. # Extract frontmatter, assuming content can be treated directly as a string
  70. frontmatter = extract_frontmatter(
  71. content
  72. ) # Ensure this method is adaptable to handle content strings
  73. # Install required packages found within the frontmatter
  74. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  75. print(f"Loaded module: {module.__name__}")
  76. # Create and return the object if the class 'Tools' is found in the module
  77. if hasattr(module, "Tools"):
  78. return module.Tools(), frontmatter
  79. else:
  80. raise Exception("No Tools class found in the module")
  81. except Exception as e:
  82. print(f"Error loading module: {toolkit_id}")
  83. del sys.modules[module_name] # Clean up
  84. raise e
  85. def load_function_module_by_id(function_id, content=None):
  86. if content is None:
  87. function = Functions.get_function_by_id(function_id)
  88. if not function:
  89. raise Exception(f"Function not found: {function_id}")
  90. content = function.content
  91. content = replace_imports(content)
  92. Functions.update_function_by_id(function_id, {"content": content})
  93. module_name = f"{function_id}"
  94. module = types.ModuleType(module_name)
  95. sys.modules[module_name] = module
  96. try:
  97. # Execute the modified content in the created module's namespace
  98. exec(content, module.__dict__)
  99. # Extract the frontmatter from the content, simulate file-like behaviour
  100. frontmatter = extract_frontmatter(
  101. content
  102. ) # This function needs to handle string inputs
  103. # Install necessary requirements specified in frontmatter
  104. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  105. print(f"Loaded module: {module.__name__}")
  106. # Create appropriate object based on available class type in the module
  107. if hasattr(module, "Pipe"):
  108. return module.Pipe(), "pipe", frontmatter
  109. elif hasattr(module, "Filter"):
  110. return module.Filter(), "filter", frontmatter
  111. elif hasattr(module, "Action"):
  112. return module.Action(), "action", frontmatter
  113. else:
  114. raise Exception("No Function class found in the module")
  115. except Exception as e:
  116. print(f"Error loading module: {function_id}")
  117. del sys.modules[module_name] # Cleanup by removing the module in case of error
  118. Functions.update_function_by_id(function_id, {"is_active": False})
  119. raise e
  120. def install_frontmatter_requirements(requirements):
  121. if requirements:
  122. req_list = [req.strip() for req in requirements.split(",")]
  123. for req in req_list:
  124. print(f"Installing requirement: {req}")
  125. subprocess.check_call([sys.executable, "-m", "pip", "install", req])
  126. else:
  127. print("No requirements found in frontmatter.")