We're Hiring!
Whitepaper
Docs
Sign In
@benasrau
·
8 months ago
·
8 months ago
function
Message Date And Time
Get
Last Updated
8 months ago
Created
8 months ago
Function
filter
v0.1.1
Name
Message Date And Time
Downloads
791+
Saves
0+
Description
Gives model current date and time context for each message. Don't forget to adjust the timezone in the settings.
Function Code
Show
""" title: Message Date And Time author: benasraudys author_url: https://github.com/benasraudys funding_url: https://github.com/benasraudys description: Gives model current date and time context for each message. Don't forget to adjust the timezone in the settings. version: 0.1.1 required_open_webui_version: 0.6.4 """ import datetime import os from pydantic import BaseModel, Field from typing import Callable, Awaitable, Any, Optional class Filter: class Valves(BaseModel): timezone_hours: int = Field( default=0, description="Timezone offset hours (e.g., 5 for UTC+5:30, -4 for UTC-4:00)", ) timezone_minutes: int = Field( default=0, description="Timezone offset minutes (e.g., 30 for UTC+5:30, 45 for UTC-4:45)", ) southern_hemisphere: bool = Field( default=False, description="Enable if you're in the Southern Hemisphere (Australia, South America, etc.)", ) def __init__(self): self.valves = self.Valves( **{ "timezone_hours": int(os.getenv("DATETIME_TIMEZONE_HOURS", "0")), "timezone_minutes": int(os.getenv("DATETIME_TIMEZONE_MINUTES", "0")), "southern_hemisphere": os.getenv( "DATETIME_SOUTHERN_HEMISPHERE", "false" ).lower() == "true", } ) def get_season(self, month, southern_hemisphere=False): if not southern_hemisphere: if 3 <= month <= 5: return "Spring" elif 6 <= month <= 8: return "Summer" elif 9 <= month <= 11: return "Autumn" else: return "Winter" else: if 3 <= month <= 5: return "Autumn" elif 6 <= month <= 8: return "Winter" elif 9 <= month <= 11: return "Spring" else: return "Summer" def get_time_of_day(self, hour): if 5 <= hour < 12: return "Morning" elif 12 <= hour < 17: return "Afternoon" elif 17 <= hour < 21: return "Evening" else: return "Night" async def inlet( self, body: dict, __event_emitter__: Callable[[Any], Awaitable[None]], __request__: Any, __user__: Optional[dict] = None, __model__: Optional[dict] = None, ) -> dict: now_utc = datetime.datetime.utcnow() timezone_hours = self.valves.timezone_hours timezone_minutes = self.valves.timezone_minutes total_offset_minutes = (timezone_hours * 60) + timezone_minutes now = now_utc + datetime.timedelta(minutes=total_offset_minutes) month = now.month hour = now.hour formatted_date = now.strftime("%B %d, %Y") formatted_time = now.strftime("%H:%M:%S") day_of_week = now.strftime("%A") hours_offset = abs(total_offset_minutes) // 60 minutes_offset = abs(total_offset_minutes) % 60 if minutes_offset == 0: if total_offset_minutes >= 0: timezone_str = f"UTC+{hours_offset}" else: timezone_str = f"UTC-{hours_offset}" else: if total_offset_minutes >= 0: timezone_str = f"UTC+{hours_offset}:{minutes_offset:02d}" else: timezone_str = f"UTC-{hours_offset}:{minutes_offset:02d}" season = self.get_season(month, self.valves.southern_hemisphere) time_of_day = self.get_time_of_day(hour) context = f"Current date is {day_of_week}, {formatted_date}, {season}, {time_of_day}, the user time is {formatted_time} {timezone_str}" datetime_message = { "role": "system", "content": f"Time context: {context}. ", } if "messages" in body and isinstance(body["messages"], list): body["messages"].insert(0, datetime_message) else: body["messages"] = [datetime_message] return body
Sponsored by Open WebUI Inc.
We are hiring!
Shape the way humanity engages with
intelligence
.
0