Timothy Jaeryang Baek 4 months ago
parent
commit
40bea00e3d
2 changed files with 42 additions and 25 deletions
  1. 3 1
      backend/open_webui/utils/filter.py
  2. 39 24
      backend/open_webui/utils/plugin.py

+ 3 - 1
backend/open_webui/utils/filter.py

@@ -66,7 +66,9 @@ async def process_filter_functions(
         if not filter:
             continue
 
-        function_module = get_function_module(request, filter_id)
+        function_module = get_function_module(
+            request, filter_id, load_from_db=(filter_type != "stream")
+        )
         # Prepare handler function
         handler = getattr(function_module, filter_type, None)
         if not handler:

+ 39 - 24
backend/open_webui/utils/plugin.py

@@ -166,31 +166,46 @@ def load_function_module_by_id(function_id: str, content: str | None = None):
         os.unlink(temp_file.name)
 
 
-def get_function_module_from_cache(request, function_id):
-    function = Functions.get_function_by_id(function_id)
-    if not function:
-        raise Exception(f"Function not found: {function_id}")
-    content = function.content
-
-    new_content = replace_imports(content)
-    if new_content != content:
-        content = new_content
-        # Update the function content in the database
-        Functions.update_function_by_id(function_id, {"content": content})
+def get_function_module_from_cache(request, function_id, load_from_db=True):
 
-    if (
-        hasattr(request.app.state, "FUNCTION_CONTENTS")
-        and function_id in request.app.state.FUNCTION_CONTENTS
-    ) and (
-        hasattr(request.app.state, "FUNCTIONS")
-        and function_id in request.app.state.FUNCTIONS
-    ):
-        if request.app.state.FUNCTION_CONTENTS[function_id] == content:
-            return request.app.state.FUNCTIONS[function_id], None, None
-
-    function_module, function_type, frontmatter = load_function_module_by_id(
-        function_id, content
-    )
+    if load_from_db:
+        # Always load from the database if requested
+        function = Functions.get_function_by_id(function_id)
+        if not function:
+            raise Exception(f"Function not found: {function_id}")
+        content = function.content
+
+        new_content = replace_imports(content)
+        if new_content != content:
+            content = new_content
+            # Update the function content in the database
+            Functions.update_function_by_id(function_id, {"content": content})
+
+        if (
+            hasattr(request.app.state, "FUNCTION_CONTENTS")
+            and function_id in request.app.state.FUNCTION_CONTENTS
+        ) and (
+            hasattr(request.app.state, "FUNCTIONS")
+            and function_id in request.app.state.FUNCTIONS
+        ):
+            if request.app.state.FUNCTION_CONTENTS[function_id] == content:
+                return request.app.state.FUNCTIONS[function_id], None, None
+
+        function_module, function_type, frontmatter = load_function_module_by_id(
+            function_id, content
+        )
+    else:
+        # Load from cache (e.g. "stream" hook)
+        if (
+            hasattr(request.app.state, "FUNCTIONS")
+            and function_id in request.app.state.FUNCTIONS
+        ):
+            if request.app.state.FUNCTION_CONTENTS[function_id] == content:
+                return request.app.state.FUNCTIONS[function_id], None, None
+
+        function_module, function_type, frontmatter = load_function_module_by_id(
+            function_id
+        )
 
     if not hasattr(request.app.state, "FUNCTIONS"):
         request.app.state.FUNCTIONS = {}