
Craft Your Own Myth: A Python Hero's Journey Story Generator Using Ollama
Ever dreamed of crafting your own epic tale? From bedtime stories to coding adventures, myths resonate deeply. This Python-based Hero's Journey story generator combines your personal details with timeless narrative structures, creating legends that feel both personal and grand. Whether you're an educator seeking to spark student imaginations or a programmer looking for a creative hobby, this tool offers:
- A familiar story framework (Campbell’s Hero’s Journey) for universal appeal.
- Rich symbolic depth (Thompson Motif Index) for authentic folklore.
- Local AI inference via Ollama for complete data privacy.
- Offline narration for hands-free listening.
- Markdown export for seamless integration.
This guide walks you through each component, offering practical, tested code you can adapt today.
Why Build a Hero's Journey Story Generator? Key Objectives
The goal is to create an easily maintainable solution demonstrating clear separation of concerns and solid architecture, even for a quirky script. Specific objectives include:
- Personalizing each myth. Tailoring the tale with the user’s name, birthdate, and life stage.
- Enriching narratives. Infusing stories with authentic folklore motifs for added depth.
- Modular code. Structuring the code into clear, testable components for easy updates.
- Secure data. Running everything offline to safeguard your personal information.
- Accessible formats. Delivering both text and audio outputs for flexible consumption.
Understanding the Hero's Journey: The Core Structure
Joseph Campbell’s Hero’s Journey forms the heart of every epic. This 12-stage narrative template provides a framework for stories across cultures. Here are key stages:
- Ordinary World: The hero's everyday environment is established.
- Call to Adventure: A challenge or opportunity arises.
- Refusal of the Call: Initial hesitation or doubt surfaces.
- Meeting the Mentor: Wise guidance and support appear.
- Crossing the Threshold: Entering the unknown world.
- Road of Trials: Facing tests, challenges, and ordeals.
- Transformation: Experiencing a profound metamorphosis.
- Atonement & Reward: Claiming the treasure or victory.
- Return with Elixir: Bringing knowledge or wisdom back home.
By aligning our prompts with these stages, each generated story follows a familiar yet uniquely personalized arc.
Ollama: Powering Local AI Inference
Ollama empowers you to run large language models directly on your machine. Here's why it's ideal for our Hero's Journey story generator:
- Privacy: Your data remains on your device, ensuring complete confidentiality.
- Performance: Local inference eliminates network latency for rapid response times.
- Flexibility: Easily swap models by modifying a single string in your code.
Thompson Motif Index: Adding Folklore Depth
The Thompson Motif Index (TMI) catalogues over 6,000 recurring narrative elements from global folklore. Examples include:
- B210: Animal helper aids hero.
- U752: Magical mirror reveals truth.
By selecting motifs from a local JSON file, we enhance AI-generated output with symbolic significance, grounding each tale in mythic tradition. A well-prepared JSON file is required and can be obtained from the fbkarsdorp/tmi
GitHub repository (data/tmi.json). Save the file in your project directory as tmi.json
.
Generator Architecture: A Modular Approach
The generator's pipeline embodies a clear, linear flow for seamless component interaction.
- Setup: The
check_model()
,get_user_input()
, andload_motifs()
methods ensure proper configuration. - Generation: The
generate_hero_journey()
method is the core engine for crafting the story. - Output:
save_to_markdown()
andnarrate_story()
deliver the story in various formats.
Function Breakdown: Diving into the Code
Let's explore the core functions, detailing their implementation and usage.
Initialization (__init__)
This sets up the necessary components for generating the story:
Faker
: Generates or sanitizes hero names.- TTS engine: Preconfigured for offline narration.
- Ollama client & model: Establish the LLM interface.
Model Verification (check_model)
check_model()
verifies that the specified Ollama model is available locally, pulling it if needed to facilitate an offline workflow. It even provides a progress bar to show the downloading progress.
User Input (get_user_input)
get_user_input
gathers user information like name and birthdate for personalization. Faker is used to generate a random name when the name input is left blank. Birthdate is validated to use the YYYY-MM-DD
format.
Motif Sampling (load_motifs)
load_motifs
loads the motif dataset ("tmi.json
") and randomly picks three motifs. This sets the narrative's thematic tone, adding folklore depth.
Generating Story Segments (generate_story_part)
generate_story_part
sets up a system prompt to instruct the AI to behave as a storyteller and uses the user prompt of the specific part to generate the content. It also sets other parameters like temperature and number of threads.
Orchestrating the Journey (generate_hero_journey)
generate_hero_journey
contextualizes each Hero’s Journey stage with the user's data and motifs. Then, it generates the six narrative blocks, weaving personal details with symbolic elements.
Markdown Export (save_to_markdown)
This function produces a structured Markdown document, ideal for sharing on GitHub, blogs, or other platforms.
Text-to-Speech (narrate_story)
The narrate_story
function synthesizes speech for each story segment, creating an audio version of the generated myth.
By following this comprehensive guide, you can build your own Hero's Journey Story Generator, crafting personalized myths with ease and creativity. You'll gain a solid understanding of AI integration, narrative structure, and Python programming.