Timothy Jaeryang Baek 1 month ago
parent
commit
66f00ce4bf
1 changed files with 34 additions and 31 deletions
  1. 34 31
      backend/open_webui/utils/task.py

+ 34 - 31
backend/open_webui/utils/task.py

@@ -39,40 +39,43 @@ def prompt_variables_template(template: str, variables: dict[str, str]) -> str:
 
 
 def prompt_template(template: str, user: Optional[Any] = None) -> str:
-    if hasattr(user, "model_dump"):
-        user = user.model_dump()
 
     USER_VARIABLES = {}
 
-    if isinstance(user, dict):
-        birth_date = user.get("date_of_birth")
-        age = None
-
-        if birth_date:
-            try:
-                # If birth_date is str, convert to datetime
-                if isinstance(birth_date, str):
-                    birth_date = datetime.strptime(birth_date, "%Y-%m-%d")
-
-                today = datetime.now()
-                age = (
-                    today.year
-                    - birth_date.year
-                    - ((today.month, today.day) < (birth_date.month, birth_date.day))
-                )
-            except Exception as e:
-                pass
-
-        USER_VARIABLES = {
-            "name": str(user.get("name")),
-            "location": str(user.get("info", {}).get("location")),
-            "bio": str(user.get("bio")),
-            "gender": str(user.get("gender")),
-            "birth_date": str(birth_date),
-            "age": str(age),
-        }
-
-    print(USER_VARIABLES)
+    if user:
+        if hasattr(user, "model_dump"):
+            user = user.model_dump()
+
+        if isinstance(user, dict):
+            birth_date = user.get("date_of_birth")
+            age = None
+
+            if birth_date:
+                try:
+                    # If birth_date is str, convert to datetime
+                    if isinstance(birth_date, str):
+                        birth_date = datetime.strptime(birth_date, "%Y-%m-%d")
+
+                    today = datetime.now()
+                    age = (
+                        today.year
+                        - birth_date.year
+                        - (
+                            (today.month, today.day)
+                            < (birth_date.month, birth_date.day)
+                        )
+                    )
+                except Exception as e:
+                    pass
+
+            USER_VARIABLES = {
+                "name": str(user.get("name")),
+                "location": str(user.get("info", {}).get("location")),
+                "bio": str(user.get("bio")),
+                "gender": str(user.get("gender")),
+                "birth_date": str(birth_date),
+                "age": str(age),
+            }
 
     # Get the current date
     current_date = datetime.now()