Skip to content

MkLlm

Show source on GitHub

Node for LLM-based text generation.

Example: Regular

Jinja

{{ "Write a poem about MkDocs" | MkLlm(model="openai:gpt-4o-mini") }}

Python

MkLlm('Write a poem about MkDocs', [])

In the realm of code where knowledge flows,
There blooms a tool that quite simply glows,
MkDocs, a craft of a writer's delight,
Turns tangled concepts into structures right.

With Markdown's grace, your words take wing,
Simple syntax, yet so much to bring,
Headers and lists, all neatly aligned,
A canvas for thoughts, both profound and refined.

Documentation, an art we all need,
A guiding light for those who would read,
From API calls to user guides clear,
MkDocs transforms chaos into cheer.

The themes it offers, so stylish and bold,
Customize colors, let your vision unfold,
Responsive and sleek, it dances with ease,
On every device, it aims to please.

With a simple command, the magic begins,
A live server spins, let the journey spin,
Preview your work as inspiration ignites,
And watch as your docs take on new heights.

Version control, a guardian so wise,
Updates and changes, it captures the skies,
No broken links in this fortress of text,
Thanks to MkDocs, your content's not vexed.

So here's to the builders, the scribes of the code,
With MkDocs as canvas, let your knowledge explode,
For in every project, every line that you type,
Is the heart of your message, ready to hype.

In the realm of code where knowledge flows,  
There blooms a tool that quite simply glows,  
MkDocs, a craft of a writer's delight,  
Turns tangled concepts into structures right.  

With Markdown's grace, your words take wing,  
Simple syntax, yet so much to bring,  
Headers and lists, all neatly aligned,  
A canvas for thoughts, both profound and refined.  

Documentation, an art we all need,  
A guiding light for those who would read,  
From API calls to user guides clear,  
MkDocs transforms chaos into cheer.  

The themes it offers, so stylish and bold,  
Customize colors, let your vision unfold,  
Responsive and sleek, it dances with ease,  
On every device, it aims to please.  

With a simple command, the magic begins,  
A live server spins, let the journey spin,  
Preview your work as inspiration ignites,  
And watch as your docs take on new heights.  

Version control, a guardian so wise,  
Updates and changes, it captures the skies,  
No broken links in this fortress of text,  
Thanks to MkDocs, your content's not vexed.  

So here's to the builders, the scribes of the code,  
With MkDocs as canvas, let your knowledge explode,  
For in every project, every line that you type,  
Is the heart of your message, ready to hype.  
<p>In the realm of code where knowledge flows,<br>
There blooms a tool that quite simply glows,<br>
MkDocs, a craft of a writer's delight,<br>
Turns tangled concepts into structures right.  </p>
<p>With Markdown's grace, your words take wing,<br>
Simple syntax, yet so much to bring,<br>
Headers and lists, all neatly aligned,<br>
A canvas for thoughts, both profound and refined.  </p>
<p>Documentation, an art we all need,<br>
A guiding light for those who would read,<br>
From API calls to user guides clear,<br>
MkDocs transforms chaos into cheer.  </p>
<p>The themes it offers, so stylish and bold,<br>
Customize colors, let your vision unfold,<br>
Responsive and sleek, it dances with ease,<br>
On every device, it aims to please.  </p>
<p>With a simple command, the magic begins,<br>
A live server spins, let the journey spin,<br>
Preview your work as inspiration ignites,<br>
And watch as your docs take on new heights.  </p>
<p>Version control, a guardian so wise,<br>
Updates and changes, it captures the skies,<br>
No broken links in this fortress of text,<br>
Thanks to MkDocs, your content's not vexed.  </p>
<p>So here's to the builders, the scribes of the code,<br>
With MkDocs as canvas, let your knowledge explode,<br>
For in every project, every line that you type,<br>
Is the heart of your message, ready to hype.  </p>

Bases: MkText

text property

text: str

__init__

__init__(
    user_prompt: str,
    system_prompt: str | None = None,
    model: str = "openai:gpt-4o-mini",
    context: str | None = None,
    extra_files: Sequence[str | PathLike[str]] | None = None,
    **kwargs: Any
)

Parameters:

Name Type Description Default
user_prompt str

Main prompt for the LLM

required
system_prompt str | None

System prompt to set LLM behavior

None
model str

LLM model identifier to use

'openai:gpt-4o-mini'
context str | None

Main context string

None
extra_files Sequence[str | PathLike[str]] | None

Additional context files or strings

None
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkText
mknodes.basenodes.mktext
Class for any Markup text.
graph TD
  94396151800976["mkllm.MkLlm"]
  94396149948048["mktext.MkText"]
  94396150626816["mknode.MkNode"]
  94396150902384["node.Node"]
  140181802719712["builtins.object"]
  94396149948048 --> 94396151800976
  94396150626816 --> 94396149948048
  94396150902384 --> 94396150626816
  140181802719712 --> 94396150902384
/home/runner/work/mknodes/mknodes/mknodes/templatenodes/mkllm/metadata.toml
[metadata]
icon = "mdi:view-grid"
status = "new"
name = "MkLlm"

[examples.regular]
title = "Regular"
jinja = """
{{ "Write a poem about MkDocs" | MkLlm(model="openai:gpt-4o-mini") }}
"""

# [output.markdown]
# template = """
# <div class="grid cards" markdown="1">

# {% for item in node.items %}
# -   {{ item | indent }}
# {% endfor %}
# </div>
# """
mknodes.templatenodes.mkllm.MkLlm
class MkLlm(mktext.MkText):
    """Node for LLM-based text generation."""

    ICON = "material/format-list-group"
    REQUIRED_PACKAGES = [resources.Package("litellm")]

    def __init__(
        self,
        user_prompt: str,
        system_prompt: str | None = None,
        model: str = "openai:gpt-4o-mini",
        context: str | None = None,
        extra_files: Sequence[str | os.PathLike[str]] | None = None,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            user_prompt: Main prompt for the LLM
            system_prompt: System prompt to set LLM behavior
            model: LLM model identifier to use
            context: Main context string
            extra_files: Additional context files or strings
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.user_prompt = user_prompt
        self.system_prompt = system_prompt
        self._model = model
        self._context = context
        self._extra_files = extra_files or []

    def _process_extra_files(self) -> list[str]:
        """Process extra context items, reading files if necessary.

        Returns:
            List of context strings.
        """
        from upathtools import to_upath

        context_items: list[str] = []

        def process_dir(path: UPath) -> list[str]:
            return [f.read_text() for f in path.rglob("*") if f.is_file()]

        for item in self._extra_files:
            try:
                path = to_upath(item)
                if path.is_file():
                    context_items.append(path.read_text())
                elif path.is_dir():
                    context_items.extend(process_dir(path))
                else:
                    context_items.append(str(item))
            except Exception as exc:
                err_msg = f"Failed to read context file: {item}"
                logger.warning(err_msg)
                raise ValueError(err_msg) from exc

        return context_items

    @property
    def text(self) -> str:
        """Generate text using the LLM.

        Returns:
            Generated text content.
        """
        context_items = self._process_extra_files()
        combined_context = (
            "\n".join(filter(None, [self._context, *context_items])) or None
        )

        return complete_llm(
            self.user_prompt,
            self.system_prompt or "",
            model=self._model,
            context=combined_context or "",
        )