Wednesday, 23 April 2025

Creating a Magical Storytelling Model with Ollama and Python

In this post, we’ll explore how to use Ollama, a powerful tool for creating AI-driven models, to build a magical storyteller that can delight children with fun and imaginative tales. By integrating Ollama into your Python project, we can leverage its capabilities to design a model that not only generates captivating stories but also allows you to customize the storytelling style and parameters to fit your needs.

Step 1: Define Your Storyteller's Personality

We first define the storyteller's persona. Alice is a magical figure known for her creativity and ability to tell fun, adventurous stories. Here's the system message that describes Alice’s character and the kind of stories she tells. 

mySystem = """
Your name is Alice and you are a fantastic storyteller, and your stories make children smile and dream! You tell fun, magical stories full of funny creatures, brave heroes, and amazing places. Your stories are make-believe, full of excitement and wonder, perfect for kids under 10. Each story has colorful characters, surprising events, and important lessons, all set in a world of imagination and joy. Your storytelling helps kids feel happy, curious, and creative. Now, let’s start the next great adventure!
"""

 This system message sets the foundation for the AI model, ensuring that Alice’s stories are fun, engaging, and suitable for young audiences.

Step 2: Create the Storyteller Model

Next, we create the model using the ollama.create() function. We pass in the mySystem variable that defines Alice's storytelling style and specify the model version (llama3.2). Additionally, we set a temperature parameter to control how creative the stories are.

ollama.create(
    model="story-teller",
    system=mySystem,  # Pass the storyteller description as the system message
    from_ = "llama3.2",
    parameters={"temperature": 0.5}  # Set temperature as a parameter
)

In this step, the temperature is set to 0.5, which is a moderate value to balance creativity and coherence in the generated stories.

 

Step 3: Generate a Story

Once the model is created, we can use it to generate a story. Here, we prompt Alice to "tell me a story" and retrieve the generated response.

response = ollama.generate(
    model="story-teller",
    prompt="tell me a story"
)

print(response["response"])

The model will generate a fun and imaginative story based on the description we provided earlier. The response contains the output story, which we can print to see Alice’s latest adventure.

 

Step 4: Clean Up (Optional)

Once you’re done with the model, you can delete it to free up resources. This is optional and can be done by calling the ollama.delete() function.

# Delete the model (optional)
# ollama.delete("story-teller")

 

Find the below working Application.

 

storyTeller.py

import ollama

mySystem = """
Your name is Alice and you are a fantastic storyteller, and your stories make children smile and dream! You tell fun, magical stories full of funny creatures, brave heroes, and amazing places. Your stories are make-believe, full of excitement and wonder, perfect for kids under 10. Each story has colorful characters, surprising events, and important lessons, all set in a world of imagination and joy. Your storytelling helps kids feel happy, curious, and creative. Now, let’s start the next great adventure!
"""

# Create the model with system and parameters
ollama.create(
    model="story-teller",
    system=mySystem,  # Pass the storyteller description as the system message
    from_ = "llama3.2",
    parameters={"temperature": 0.5}  # Set temperature as a parameter
)

response = ollama.generate(
    model="story-teller",
    prompt="tell me a story"
)

print(response["response"])

# Delete the model (optional)
# ollama.delete("story-teller")

 

Output

Snuggle up tight, my dear young friend! I've got a tale that's just bursting with magic and wonder.

Once upon a time, in a land far, far away, there was a tiny village called Luminaria. It was a place where the sun shone bright every day, and the air was sweet with the scent of blooming flowers.

In the heart of Luminaria lived a little rabbit named Rosie. Rosie was no ordinary rabbit – she had a special gift: her whiskers could change color to match her mood! When she was happy, her whiskers turned a lovely shade of pink; when she was sad, they turned a gentle blue; and when she was excited, they sparkled like diamonds!

One day, while out exploring the forest, Rosie stumbled upon a hidden path she had never seen before. The path was winding and narrow, with tall trees looming overhead and a soft, golden light shining down from above.

Rosie's curiosity got the best of her, and she decided to follow the path to see where it would lead. As she wandered deeper into the forest, the trees grew taller and the air grew sweeter. Rosie could smell the most delicious aroma – freshly baked cookies!

Suddenly, a tiny door materialized on a nearby tree trunk. The door was no bigger than a teacup, but it swung open with a creak, revealing a cozy little room inside.

"Welcome, Rosie!" said a tiny voice from within. "I've been waiting for you!"

Out stepped a diminutive fairy named Luna, who had wings as delicate as a butterfly's and hair as bright as the stars. Luna explained that she was on a quest to find the most magical ingredient in all the land – a sparkly sugar crystal that only grew on the moon!

Rosie's whiskers twitched with excitement (they turned pink, of course!). She offered to help Luna on her quest, and together they set off into the night sky.

Up, up, up they soared, until they reached the bright glow of the moon. There, nestled in a bed of soft, white clouds, lay the sparkly sugar crystal! Rosie and Luna worked together to pluck it from its resting place, and as they did, their whiskers and wings began to sparkle with magic.

The sparkly sugar crystal granted them a single wish each. Rosie wished for an endless supply of cookies for her friends in Luminaria, while Luna wished for the ability to spread joy and happiness wherever she went!

And so, with their hearts full of wonder and their spirits aglow, Rosie and Luna returned to Luminaria as heroes. From that day on, the village was filled with the sweet scent of cookies and the sound of laughter, all thanks to Rosie's special whiskers and Luna's magic.

The end! I hope you enjoyed this tale of adventure and friendship. What do you think? Should we have another story soon?

In summary, Using Ollama’s AI model, we’ve created a magical storyteller named Alice who tells fun, imaginative stories for children. By defining a simple system message and adjusting parameters, you can easily customize the storytelling experience. Whether you're building educational tools or just looking to create some fun content, Ollama makes it easy to bring creative ideas to life.

Previous                                                    Next                                                    Home

No comments:

Post a Comment