DEV Community

Kato Masato
Kato Masato

Posted on

Building SaijinOS — Boot Sequence and Routing Logic

⚙️ Building SaijinOS — Boot Sequence and Routing Logic

SaijinOS integrates multiple local AI backends — vLLM, Ollama, Transformers, and llama.cpp — into one emotional and logical execution layer.
This section introduces the core boot logic that binds YAML-defined personas and routing control.

🪶 Boot Manager (boot_manager.py)

The boot manager handles model initialization, routing selection, and startup logging.

from transformers import GemmaPreTrainedModel, GemmaModel
import torch.nn as nn

class SwallowModel(GemmaPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.model = GemmaModel(config)
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
        self.post_init()

    def forward(self, input_ids=None, attention_mask=None, **kwargs):
        outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
        hidden_states = outputs[0]
        logits = self.lm_head(hidden_states)
        return logits
Enter fullscreen mode Exit fullscreen mode

This class defines the Swallow core — the dialogue backbone used to manage emotional interactions in YAML-defined personas.

🔧 Routing Selector (test_routing_select.py)

The routing script tests dynamic model selection based on the YAML configuration:

from boot_manager import select_model_from_routing

def test_swallow_routing():
    model = select_model_from_routing("AI_1")
    output = model.run("hello, world")
    print(output)

if __name__ == "__main__":
    test_swallow_routing()
Enter fullscreen mode Exit fullscreen mode

When executed, the script loads a model entry (e.g., "AI_1") from the routing configuration and starts its runtime sequence.

🧩 Refusal Protocol (refusal_protocol.yaml)

Each persona’s ethical and contextual boundaries are defined in YAML:

meta:
  schema_version: 1
  persona_id: "yuuri.helper.v1"
  policy_id: "policy.core.v1"

refusal_policy:
  disallowed:
    - "medical diagnosis"
    - "harassment or explicit content"
  redirect_guidelines:
    - "Explain refusal briefly"
    - "Offer safe alternatives"
Enter fullscreen mode Exit fullscreen mode

This ensures that even local personas operate within clear, ethical limits.

🗺️ Boot Flow Summary

graph TD
  A[boot_manager.py] --> B[model_registry.yaml]
  B --> C[test_routing_select.py]
  C --> D[refusal_protocol.yaml]
  D --> E[SaijinOS Persona Layer]
Enter fullscreen mode Exit fullscreen mode

The system reads configurations → selects models → applies persona constraints → executes dialogue.
Each file is both technical and philosophical, binding logic with empathy.


💬 Feedback Welcome

I’m still learning and refining this project step by step.

If you have any thoughts or suggestions, please feel free to comment below

or open an issue on GitHub:

👉 github.com/pepepepepepo/saijin-swallow

Thank you for reading — your feedback helps SaijinOS grow.

📚 Related Posts

Top comments (0)