Skip to content

role

Role-related functionality for SilverLingua.

This module provides enums and utilities for handling different types of roles in conversations: - Chat roles (system, human, AI, etc.) - React roles (thought, observation, action, etc.)

ChatRole

Bases: Enum

Standardized roles for chat messages.

Warning

Do not instantiate this enum directly. Use [create_chat_role()][silverlingua.core.atoms.role.chat.create_chat_role] instead.

SYSTEM class-attribute instance-attribute

SYSTEM = RoleMember('SYSTEM', 'SYSTEM')

HUMAN class-attribute instance-attribute

HUMAN = RoleMember('HUMAN', 'HUMAN')

AI class-attribute instance-attribute

AI = RoleMember('AI', 'AI')

TOOL_CALL class-attribute instance-attribute

TOOL_CALL = RoleMember('TOOL_CALL', 'TOOL_CALL')

TOOL_RESPONSE class-attribute instance-attribute

TOOL_RESPONSE = RoleMember('TOOL_RESPONSE', 'TOOL_RESPONSE')

RoleMember

RoleMember(name, value, parent=None)

Used in ChatRole and ReactRole.

Warning

Do not instantiate this class directly. See [ChatRole][silverlingua.core.atoms.role.chat.ChatRole] and [ReactRole][silverlingua.core.atoms.role.react.ReactRole] for more information.

Source code in src/SilverLingua/core/atoms/role/member.py
def __init__(self, name, value, parent=None):
    self.__dict__["_name"] = name
    self.__dict__["_value"] = value
    self.__dict__["_parent"] = parent

name property

name: str

value property

value: str

ReactRole

Bases: Enum

Standardized roles for ReAct framework to help with chain-of-thought prompting.

See: https://www.promptingguide.ai/techniques/react

Warning

Do not instantiate this enum directly. Use [create_react_role()][silverlingua.core.atoms.role.react.create_react_role] instead.

QUESTION class-attribute instance-attribute

QUESTION = RoleMember('QUESTION', 'QUESTION')

THOUGHT class-attribute instance-attribute

THOUGHT = RoleMember('THOUGHT', 'THOUGHT')

ACTION class-attribute instance-attribute

ACTION = RoleMember('ACTION', 'ACTION')

OBSERVATION class-attribute instance-attribute

OBSERVATION = RoleMember('OBSERVATION', 'OBSERVATION')

ANSWER class-attribute instance-attribute

ANSWER = RoleMember('ANSWER', 'ANSWER')

create_chat_role

create_chat_role(name: str, SYSTEM: str, HUMAN: str, AI: str, TOOL_CALL: str, TOOL_RESPONSE: str) -> Type[ChatRole]

Create a new ChatRole enum with only the values of the RoleMembers changed.

This will ensure that the parent of each member is ChatRole, which means that the members will be equal to the members of ChatRole.

Example
OpenAIChatRole = create_chat_role(
    "OpenAIChatRole",
    SYSTEM="system",
    HUMAN="user",
    AI="assistant",
    TOOL_CALL="function_call",
    TOOL_RESPONSE="function",
)

assert OpenAIChatRole.SYSTEM == ChatRole.SYSTEM # True
Source code in src/SilverLingua/core/atoms/role/chat.py
def create_chat_role(
    name: str, SYSTEM: str, HUMAN: str, AI: str, TOOL_CALL: str, TOOL_RESPONSE: str
) -> Type[ChatRole]:
    """
    Create a new ChatRole enum with only the values of the RoleMembers changed.

    This will ensure that the parent of each member is ChatRole, which means
    that the members will be equal to the members of ChatRole.

    Example:
        ```python
        OpenAIChatRole = create_chat_role(
            "OpenAIChatRole",
            SYSTEM="system",
            HUMAN="user",
            AI="assistant",
            TOOL_CALL="function_call",
            TOOL_RESPONSE="function",
        )

        assert OpenAIChatRole.SYSTEM == ChatRole.SYSTEM # True
        ```
    """
    return Enum(
        name,
        {
            "SYSTEM": RoleMember("SYSTEM", SYSTEM, ChatRole),
            "HUMAN": RoleMember("HUMAN", HUMAN, ChatRole),
            "AI": RoleMember("AI", AI, ChatRole),
            "TOOL_CALL": RoleMember("TOOL_CALL", TOOL_CALL, ChatRole),
            "TOOL_RESPONSE": RoleMember("TOOL_RESPONSE", TOOL_RESPONSE, ChatRole),
        },
    )

create_react_role

create_react_role(name: str, QUESTION: str, THOUGHT: str, ACTION: str, OBSERVATION: str, ANSWER: str) -> Type[ReactRole]
Source code in src/SilverLingua/core/atoms/role/react.py
def create_react_role(
    name: str,
    QUESTION: str,
    THOUGHT: str,
    ACTION: str,
    OBSERVATION: str,
    ANSWER: str,
) -> Type[ReactRole]:
    return Enum(
        name,
        {
            "QUESTION": RoleMember("QUESTION", QUESTION, ReactRole),
            "THOUGHT": RoleMember("THOUGHT", THOUGHT, ReactRole),
            "ACTION": RoleMember("ACTION", ACTION, ReactRole),
            "OBSERVATION": RoleMember("OBSERVATION", OBSERVATION, ReactRole),
            "ANSWER": RoleMember("ANSWER", ANSWER, ReactRole),
        },
    )