utils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import os
  2. import re
  3. import subprocess
  4. import sys
  5. from importlib import util
  6. import types
  7. import tempfile
  8. from open_webui.apps.webui.models.functions import Functions
  9. from open_webui.apps.webui.models.tools import Tools
  10. def extract_frontmatter(content):
  11. """
  12. Extract frontmatter as a dictionary from the provided content string.
  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. lines = content.splitlines()
  20. if len(lines) < 1 or lines[0].strip() != '"""':
  21. # The content doesn't start with triple quotes
  22. return {}
  23. frontmatter_started = True
  24. for line in lines[1:]:
  25. if '"""' in line:
  26. if frontmatter_started:
  27. frontmatter_ended = True
  28. break
  29. if frontmatter_started and not frontmatter_ended:
  30. match = frontmatter_pattern.match(line)
  31. if match:
  32. key, value = match.groups()
  33. frontmatter[key.strip()] = value.strip()
  34. except Exception as e:
  35. print(f"An error occurred: {e}")
  36. return {}
  37. return frontmatter
  38. def replace_imports(content):
  39. """
  40. Replace the import paths in the content.
  41. """
  42. replacements = {
  43. "from utils": "from open_webui.utils",
  44. "from apps": "from open_webui.apps",
  45. "from main": "from open_webui.main",
  46. "from config": "from open_webui.config",
  47. }
  48. for old, new in replacements.items():
  49. content = content.replace(old, new)
  50. return content
  51. def load_tools_module_by_id(toolkit_id, content=None):
  52. if content is None:
  53. tool = Tools.get_tool_by_id(toolkit_id)
  54. if not tool:
  55. raise Exception(f"Toolkit not found: {toolkit_id}")
  56. content = tool.content
  57. content = replace_imports(content)
  58. Tools.update_tool_by_id(toolkit_id, {"content": content})
  59. else:
  60. frontmatter = extract_frontmatter(content)
  61. # Install required packages found within the frontmatter
  62. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  63. module_name = f"tool_{toolkit_id}"
  64. module = types.ModuleType(module_name)
  65. sys.modules[module_name] = module
  66. # Create a temporary file and use it to define `__file__` so
  67. # that it works as expected from the module's perspective.
  68. temp_file = tempfile.NamedTemporaryFile(delete=False)
  69. temp_file.close()
  70. try:
  71. with open(temp_file.name, "w", encoding="utf-8") as f:
  72. f.write(content)
  73. module.__dict__["__file__"] = temp_file.name
  74. # Executing the modified content in the created module's namespace
  75. exec(content, module.__dict__)
  76. frontmatter = extract_frontmatter(content)
  77. print(f"Loaded module: {module.__name__}")
  78. # Create and return the object if the class 'Tools' is found in the module
  79. if hasattr(module, "Tools"):
  80. return module.Tools(), frontmatter
  81. else:
  82. raise Exception("No Tools class found in the module")
  83. except Exception as e:
  84. print(f"Error loading module: {toolkit_id}: {e}")
  85. del sys.modules[module_name] # Clean up
  86. raise e
  87. finally:
  88. os.unlink(temp_file.name)
  89. def load_function_module_by_id(function_id, content=None):
  90. if content is None:
  91. function = Functions.get_function_by_id(function_id)
  92. if not function:
  93. raise Exception(f"Function not found: {function_id}")
  94. content = function.content
  95. content = replace_imports(content)
  96. Functions.update_function_by_id(function_id, {"content": content})
  97. else:
  98. frontmatter = extract_frontmatter(content)
  99. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  100. module_name = f"function_{function_id}"
  101. module = types.ModuleType(module_name)
  102. sys.modules[module_name] = module
  103. # Create a temporary file and use it to define `__file__` so
  104. # that it works as expected from the module's perspective.
  105. temp_file = tempfile.NamedTemporaryFile(delete=False)
  106. temp_file.close()
  107. try:
  108. with open(temp_file.name, "w", encoding="utf-8") as f:
  109. f.write(content)
  110. module.__dict__["__file__"] = temp_file.name
  111. # Execute the modified content in the created module's namespace
  112. exec(content, module.__dict__)
  113. frontmatter = extract_frontmatter(content)
  114. print(f"Loaded module: {module.__name__}")
  115. # Create appropriate object based on available class type in the module
  116. if hasattr(module, "Pipe"):
  117. return module.Pipe(), "pipe", frontmatter
  118. elif hasattr(module, "Filter"):
  119. return module.Filter(), "filter", frontmatter
  120. elif hasattr(module, "Action"):
  121. return module.Action(), "action", frontmatter
  122. else:
  123. raise Exception("No Function class found in the module")
  124. except Exception as e:
  125. print(f"Error loading module: {function_id}: {e}")
  126. del sys.modules[module_name] # Cleanup by removing the module in case of error
  127. Functions.update_function_by_id(function_id, {"is_active": False})
  128. raise e
  129. finally:
  130. os.unlink(temp_file.name)
  131. def install_frontmatter_requirements(requirements):
  132. if requirements:
  133. req_list = [req.strip() for req in requirements.split(",")]
  134. for req in req_list:
  135. print(f"Installing requirement: {req}")
  136. subprocess.check_call([sys.executable, "-m", "pip", "install", req])
  137. else:
  138. print("No requirements found in frontmatter.")