Message

Bases: BaseModel

Source code in llm_utils/aiweb_common/generate/ChatSchemas.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Message(BaseModel):
    role: Role = Field(example=Role.human)
    content: str  # May be plain text or a JSON string encoding image update triggers
    time: datetime = Field(
        default_factory=lambda: datetime.now(pytz.timezone("US/Central")),
        description="The time the message was created",
        example=datetime.now(pytz.timezone("US/Central")).isoformat(),  # Example in Central Time
    )

    def __init__(self, **data):
        super().__init__(**data)
        # Convert time to Central Time if it's not already
        if self.time.tzinfo is None:
            self.time = pytz.timezone("US/Central").localize(self.time)
        else:
            self.time = self.time.astimezone(pytz.timezone("US/Central"))

    def get_image_update(self):
        """
        If content encodes an image update trigger (as JSON), return the image update dict.
        Otherwise, return None.

        Returns
        -------
        dict or None
            The image update trigger dict if present, else None.

        Example
        -------
        >>> msg = Message(content='{"text": "Here is your image", "image_update": {"url": "img.png"}}')
        >>> msg.get_image_update()
        {'url': 'img.png'}
        """
        import json
        try:
            obj = json.loads(self.content)
            if isinstance(obj, dict) and "image_update" in obj:
                return obj["image_update"]
        except Exception:
            pass
        return None

    class Config:
        json_encoders = {datetime: lambda v: v.astimezone(pytz.timezone("US/Central")).isoformat()}

get_image_update()

If content encodes an image update trigger (as JSON), return the image update dict. Otherwise, return None.

Returns

dict or None The image update trigger dict if present, else None.

Example

msg = Message(content='{"text": "Here is your image", "image_update": {"url": "img.png"}}') msg.get_image_update() {'url': 'img.png'}

Source code in llm_utils/aiweb_common/generate/ChatSchemas.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def get_image_update(self):
    """
    If content encodes an image update trigger (as JSON), return the image update dict.
    Otherwise, return None.

    Returns
    -------
    dict or None
        The image update trigger dict if present, else None.

    Example
    -------
    >>> msg = Message(content='{"text": "Here is your image", "image_update": {"url": "img.png"}}')
    >>> msg.get_image_update()
    {'url': 'img.png'}
    """
    import json
    try:
        obj = json.loads(self.content)
        if isinstance(obj, dict) and "image_update" in obj:
            return obj["image_update"]
    except Exception:
        pass
    return None