Skip to content

chatomon.knowledge

The knowledge an atom's nucleus draws from.

Classes:

Name Description
Knowledge

Plain-text knowledge added to a prompt in a delimited section.

Knowledge

Knowledge(text: str, *, title: str | None = None)

Plain-text knowledge added to a prompt in a delimited section.

The same object can be given to the atom and, explicitly, to an orbital such as a judge: chatomon never shares it implicitly.

Parameters:

Name Type Description Default
text str

The knowledge text.

required
title str | None

Optional title rendered in the section header.

None

Raises:

Type Description
ChatomonConfigError

If text is empty.

Examples:

from chatomon import Knowledge

faq = Knowledge("Returns are accepted within 30 days.", title="FAQ")
assert faq.render().startswith('<knowledge title="FAQ">')

Methods:

Name Description
from_file

Read knowledge from a text file.

render

Render the knowledge as a delimited prompt section.

Source code in src/chatomon/knowledge.py
35
36
37
38
39
def __init__(self, text: str, *, title: str | None = None) -> None:
    if not text.strip():
        raise ChatomonConfigError("Knowledge text must not be empty")
    self.text = text
    self.title = title

from_file classmethod

from_file(
    path: str | PathLike[str],
    *,
    title: str | None = None,
    encoding: str = "utf-8"
) -> Knowledge

Read knowledge from a text file.

Parameters:

Name Type Description Default
path str | PathLike[str]

Path of the file.

required
title str | None

Optional title rendered in the section header.

None
encoding str

File encoding.

'utf-8'

Returns:

Type Description
Knowledge

The knowledge.

Raises:

Type Description
OSError

If the file cannot be read.

ChatomonConfigError

If the file is empty.

Source code in src/chatomon/knowledge.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@classmethod
def from_file(
    cls,
    path: str | os.PathLike[str],
    *,
    title: str | None = None,
    encoding: str = "utf-8",
) -> Knowledge:
    """Read knowledge from a text file.

    Args:
        path: Path of the file.
        title: Optional title rendered in the section header.
        encoding: File encoding.

    Returns:
        The knowledge.

    Raises:
        OSError: If the file cannot be read.
        ChatomonConfigError: If the file is empty.
    """  # noqa: DOC502 - raised by open() and the constructor
    return cls(Path(path).read_text(encoding=encoding), title=title)

render

render() -> str

Render the knowledge as a delimited prompt section.

Returns:

Type Description
str

The text wrapped in <knowledge> tags.

Source code in src/chatomon/knowledge.py
65
66
67
68
69
70
71
72
def render(self) -> str:
    """Render the knowledge as a delimited prompt section.

    Returns:
        The text wrapped in `<knowledge>` tags.
    """
    header = f'<knowledge title="{self.title}">' if self.title else "<knowledge>"
    return f"{header}\n{self.text}\n</knowledge>"