Skip to content

Serializers

base_serializer(messages, **kwargs)

Serialize only messages role and content.

Source code in light_agents/utils/serializers.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def base_serializer(
    messages: MutableSequence[MessageBase], **kwargs: Any
) -> List[dict[str, str]]:
    """Serialize only messages role and content."""
    if not isinstance(messages, list):
        raise ValueError("The messages must be a list.")

    serialized_messages = []
    for message in messages:
        serialized_messages.append(
            {"content": message.content, "role": message.role.value}
        )
    return serialized_messages

role_mapping_serializer(messages, **kwargs)

Serialize only messages role and content.

It uses a mapping to change the roles to supported values for other LLMs. If a role is not in the mapping, it will be ignored.

Source code in light_agents/utils/serializers.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def role_mapping_serializer(
    messages: MutableSequence[MessageBase], **kwargs: Any
) -> List[dict[str, str]]:
    """Serialize only messages role and content.

    It uses a mapping to change the roles to supported values for other LLMs.
    If a role is not in the mapping, it will be ignored.
    """
    if "roles_mapping" not in kwargs:
        raise ValueError("'roles_mapping' dict must be provided.")

    if not isinstance(messages, list):
        raise ValueError("The messages must be a list.")
    serialized_messages = []
    for message in messages:
        converted_role = kwargs["roles_mapping"].get(message.role, None)
        if converted_role:
            serialized_messages.append(
                {"content": message.content, "role": converted_role}
            )
    return serialized_messages