Skip to content

molecules

Molecules represent self-contained conceptual units that combine atoms to create more complex but focused components. They focus on "what something is" rather than "what it does."

See Design Principles - Molecules for more details.

Bases: Memory

A memory that can have a parent and children Links, forming a hierarchical structure of interconnected memories.

The content can be either a [Notion][silverlingua.core.molecules.notion.Notion] or a [Memory][silverlingua.core.atoms.memory.Memory]. (You can still use the content as a string via str(link.content).)

content instance-attribute

content: Union[Notion, Memory]

parent class-attribute instance-attribute

parent: Optional[Link] = None

children class-attribute instance-attribute

children: List[Link] = Field(default_factory=list)

path property

path: List[Link]

Returns the path from the root to this Link.

root property

root: Link

Returns the root Link of this Link.

depth property

depth: int

Returns 1 based depth of this Link.

is_root property

is_root: bool

Returns whether this Link is a root Link.

is_leaf property

is_leaf: bool

Returns whether this Link is a leaf Link.

is_branch property

is_branch: bool

Returns whether this Link is a branch Link.

path_string property

path_string: str

Returns the path from the root to this Link as a string.

Example: "root>child>grandchild"

add_child

add_child(child: Link) -> None
Source code in src/SilverLingua/core/molecules/link.py
def add_child(self, child: "Link") -> None:
    self.children.append(child)
    child.parent = self

remove_child

remove_child(child: Link) -> None
Source code in src/SilverLingua/core/molecules/link.py
def remove_child(self, child: "Link") -> None:
    self.children.remove(child)
    child.parent = None

Notion

Notion(content: str, role: Union[ChatRole, ReactRole, str], persistent: bool = False)

Bases: Memory

A memory that stores the role associated with its content. The role is usually a [ChatRole][silverlingua.core.atoms.role.chat.ChatRole] or a [ReactRole][silverlingua.core.atoms.role.react.ReactRole]. (See [atoms/roles][silverlingua.core.atoms.role])

Attributes:

Name Type Description
role str

The role of the notion.

content str

The content of the memory.

persistent bool

Whether the notion should be stored in long-term memory.

Source code in src/SilverLingua/core/molecules/notion.py
def __init__(
    self,
    content: str,
    role: Union[ChatRole, ReactRole, str],
    persistent: bool = False,
):
    super().__init__(content=content, role=role, persistent=persistent)

content instance-attribute

content: str

model_config class-attribute instance-attribute

model_config = ConfigDict(from_attributes=True)

role instance-attribute

role: str

persistent class-attribute instance-attribute

persistent: bool = False

chat_role property

chat_role: ChatRole

Gets the chat based role Enum (e.g. Role.SYSTEM, Role.HUMAN, etc.)

(See config)

react_role property

react_role: ReactRole

Gets the react based role Enum (e.g. Role.THOUGHT, Role.OBSERVATION, etc.)

(See config)

validate_role classmethod

validate_role(v: Union[ChatRole, ReactRole, str])
Source code in src/SilverLingua/core/molecules/notion.py
@field_validator("role", mode="before")
@classmethod
def validate_role(cls, v: Union[ChatRole, ReactRole, str]):
    if isinstance(v, (ChatRole, ReactRole)):
        return v.value.value
    elif isinstance(v, str):
        return v
    raise ValueError(f"Expected a ChatRole, ReactRole, or a string, got {type(v)}")