Skip to content

config

Module

Module(name: str, description: str, version: str, tools: List[Tool], chat_roles: List[type[ChatRole]], react_roles: List[type[ReactRole]], **kwargs)

Bases: BaseModel

A module that can be loaded into SilverLingua.

Attributes:

Name Type Description
name str

The name of the module.

description str

A description of the module.

version str

The version of the module.

tools List[Tool]

The tools in the module.

chat_roles List[type[ChatRole]]

The chat roles in the module.

react_roles List[type[ReactRole]]

The react roles in the module.

Source code in src/SilverLingua/config.py
def __init__(
    self,
    name: str,
    description: str,
    version: str,
    tools: List[Tool],
    chat_roles: List[type[ChatRole]],
    react_roles: List[type[ReactRole]],
    **kwargs,
):
    super().__init__(
        name=name,
        description=description,
        version=version,
        tools=tools,
        chat_roles=chat_roles,
        react_roles=react_roles,
        **kwargs,
    )

    Config.register_module(self)

name instance-attribute

name: str

description instance-attribute

description: str

version instance-attribute

version: str

tools instance-attribute

tools: List[Tool]

chat_roles instance-attribute

chat_roles: List[type[ChatRole]]

react_roles instance-attribute

react_roles: List[type[ReactRole]]

check_chat_roles

check_chat_roles(v)
Source code in src/SilverLingua/config.py
@field_validator("chat_roles", mode="plain")
def check_chat_roles(cls, v):
    for role in v:
        if not issubclass(role, Enum):
            raise TypeError("Expected an enum")
        else:
            for member in role:
                if (
                    not ChatRole[member.name]
                    or member.value != ChatRole[member.name].value
                ):
                    raise TypeError("members must match ChatRole members.")
    return v

check_react_roles

check_react_roles(v)
Source code in src/SilverLingua/config.py
@field_validator("react_roles", mode="plain")
def check_react_roles(cls, v):
    for role in v:
        if not issubclass(role, Enum):
            raise TypeError("Expected an enum")
        else:
            for member in role:
                if (
                    not ReactRole[member.name]
                    or member.value != ReactRole[member.name].value
                ):
                    raise TypeError("members must match ReactRole members.")
    return v

Config

modules class-attribute instance-attribute

modules: List[Module] = []

chat_roles class-attribute instance-attribute

chat_roles: List[type[ChatRole]] = [ChatRole]

react_roles class-attribute instance-attribute

react_roles: List[type[ReactRole]] = [ReactRole]

tools class-attribute instance-attribute

tools: List[Tool] = []

get_chat_role classmethod

get_chat_role(role: str) -> Optional[ChatRole]

Attempts to get the standardized ChatRole enum from 'role'. If not, returns None.

This is usually used internally for maintaining consistency in Notions across different LLM backends.

Source code in src/SilverLingua/config.py
@classmethod
def get_chat_role(self, role: str) -> Optional[ChatRole]:
    """
    Attempts to get the standardized ChatRole enum from 'role'.
    If not, returns None.

    This is usually used internally for maintaining
    consistency in Notions across different LLM backends.
    """
    for enum_class in self.chat_roles:
        for enum_member in enum_class:
            if str(enum_member.value).lower() == str(role).lower():
                return ChatRole[enum_member.name]
    return None

get_react_role classmethod

get_react_role(role: str) -> Optional[ReactRole]

Attempts to get the standardized ReactRole enum from 'role'. If not, returns None.

This is usually used internally for maintaining consistency in Notions across different LLM backends.

Source code in src/SilverLingua/config.py
@classmethod
def get_react_role(self, role: str) -> Optional[ReactRole]:
    """
    Attempts to get the standardized ReactRole enum from 'role'.
    If not, returns None.

    This is usually used internally for maintaining
    consistency in Notions across different LLM backends.
    """
    for enum_class in self.react_roles:
        for enum_member in enum_class:
            if enum_member.value == role:
                return ReactRole[enum_member.name]
    return None

get_tool classmethod

get_tool(name: str) -> Optional[Tool]

Attempts to get the tool with the given name. If not, returns None.

Source code in src/SilverLingua/config.py
@classmethod
def get_tool(self, name: str) -> Optional[Tool]:
    """
    Attempts to get the tool with the given name.
    If not, returns None.
    """
    for tool in self.tools:
        if tool.name == name:
            return tool
    return None

add_tool classmethod

add_tool(tool: Tool) -> None

Adds a tool to the config.

Source code in src/SilverLingua/config.py
@classmethod
def add_tool(self, tool: Tool) -> None:
    """
    Adds a tool to the config.
    """
    self.tools.append(tool)

add_chat_role classmethod

add_chat_role(role: Type[Enum]) -> None

Adds a chat role to the config.

Source code in src/SilverLingua/config.py
@classmethod
def add_chat_role(self, role: Type[Enum]) -> None:
    """
    Adds a chat role to the config.
    """
    if not issubclass(role, Enum):
        raise TypeError("Expected an enum")
    self.chat_roles.append(role)

add_react_role classmethod

add_react_role(role: Type[Enum]) -> None

Adds a react role to the config.

Source code in src/SilverLingua/config.py
@classmethod
def add_react_role(self, role: Type[Enum]) -> None:
    """
    Adds a react role to the config.
    """
    if not issubclass(role, Enum):
        raise TypeError("Expected an enum")
    self.react_roles.append(role)

register_module classmethod

register_module(module: Module) -> None

Registers a module.

Source code in src/SilverLingua/config.py
@classmethod
def register_module(self, module: Module) -> None:
    """
    Registers a module.
    """
    self.modules.append(module)
    for tool in module.tools:
        self.add_tool(tool)
    for chat_role in module.chat_roles:
        self.add_chat_role(chat_role)
    for react_role in module.react_roles:
        self.add_react_role(react_role)

    logger.debug(
        f'Registered module {module.name}@{module.version}: "{module.description}"'
    )