This project implements a Monte Carlo Tree Search (MCTS) algorithm designed to enhance the creative capabilities of large language models (LLMs). The system employs a tree-based exploration strategy to iteratively refine LLM responses, optimizing quality through multiple feedback-driven iterations.
Inspired by decision-making algorithms used in games and planning, this approach adapts MCTS for creative contexts, such as short story generation, AI-user conversations and content fine-tuning. By combining structured feedback loops and quality metrics, it ensures systematic improvement of outputs.
This framework is particularly useful for tasks that require high-quality text, such as blog post creation, social media marketing, story creation and more.
Each response is treated as a node in a tree. New variations are generated as child nodes.
Nodes are evaluated for quality using a combination of LLM-based scoring, metric-driven evaluation, and historical feedback analysis.
Each metric can be enabled or disabled individually, and weights can be assigned to prioritize one metric over the others. The combined score (ranging from 0 to 100) is then used to assist in selecting the best node to expand.
The system includes a real-time visualization tool that displays:
Basic usage:
query = "Write a short story about..." mcts_sr_algorithm( query, topic="creative", iterations=5, qvalue_threshold=80, importance_sampling=True )
Example: Write a short story about a cat finding an owner.
Whiskers had been on her own for as long as she could remember. She scavenged for food and shelter, always keeping to herself. But as the days went by, she began to feel the loneliness belonging to her. She longed for a warm lap to snuggle up in, a gentle hand to pet her soft fur, and a loving voice to whisper sweet nothings in her ear. One crisp autumn morning, Whiskers decided to explore the small town she lived in. She had heard rumors of a cozy little bakery on Main Street, where the aroma of freshly baked bread wafted out into the streets. As she padded along the sidewalk, her ears perked up at the sweet scent, and her tail twitched with excitement. As she pushed open the door, a bell above it rang out, announcing her arrival. The baker, Mrs. Jenkins, looked up from her work and smiled. Whiskers was immediately smitten with the kind, round-faced woman. She padded over to the counter, rubbing against Mrs. Jenkins' legs in search of attention. Mrs. Jenkins laughed and reached out to pet Whiskers, who began to purr contentedly. "Ah, a stray cat," she said to herself. "Just what I need." She scooped Whiskers up gently and cradled her in her arms, showering her with affection. From that moment on, Whiskers was hooked. She followed Mrs. Jenkins everywhere, purring loudly as the baker petted her, fed her treats, and even built her a cozy little bed in the bakery. The townspeople soon grew accustomed to seeing the pair, and children would giggle with delight as Whiskers chased after scraps of bread on the floor. As winter rolled in, Whiskers would snuggle up beside Mrs. Jenkins on cold days, her fur a warm blanket against the chill. On spring mornings, she'd watch with wonder as the snow melted away, revealing a world full of new sights and smells. Whiskers knew she had found her forever home. And as she drifted off to sleep each night, surrounded by the comforting sights and smells of the bakery, she knew she would never have to face the loneliness again. She was exactly where she belonged, with the warm and loving Mrs. Jenkins by her side.
Whiskers had been roaming the streets for as long as she could remember. She scavenged for food and shelter, always keeping to herself. But as the days went by, she began to feel the ache of loneliness. She longed for a warm place to call her own, a gentle touch to soothe her fur, and a loving voice to calm her worries. The winds howled through the autumn nights, and Whiskers huddled in her makeshift bed, her ears peeled for the sound of footsteps. One crisp morning, Whiskers decided to explore the small town she lived in. The sun cast long shadows across the pavement as she padded along Main Street, her ears perked up at the aroma of freshly baked bread wafting from the cozy little bakery. The scent was intoxicating, a mix of warm spices and sweet vanilla that made her tail twitch with excitement. She pushed open the door, and the bell above it rang out in greeting. Mrs. Jenkins, the baker, looked up from her work and smiled. Whiskers was taken aback by the kind, round-faced woman. She padded over to the counter, rubbing against Mrs. Jenkins' legs in search of attention. Mrs. Jenkins laughed and reached out to pet Whiskers, her gentle touch sending shivers down Whiskers' spine. "Ah, a stray cat," she said to herself. "Just what I need." She scooped Whiskers up gently and cradled her in her arms, showering her with affection. From that moment on, Whiskers was hooked. She followed Mrs. Jenkins everywhere, purring loudly as the baker petted her, fed her treats, and even built her a cozy little bed in the bakery. But despite her newfound sense of security, Whiskers' fear of animal control still lingered. She watched the streets anxiously, her ears tuned to the sound of sirens. One fateful day, animal control showed up at the bakery. Whiskers' heart skipped a beat as she cowered behind Mrs. Jenkins. But the baker refused to let them take Whiskers away, and a fierce argument ensued. Mrs. Jenkins stood firm, her voice calm but resolute. "You're a part of this family now, little one," she whispered to Whiskers. "We're not going to let you go." The authorities left empty-handed, and Whiskers purred with relief as Mrs. Jenkins cradled her in her arms once more. As the seasons changed, Whiskers and Mrs. Jenkins grew closer still. Whiskers learned to recognize Mrs. Jenkins' gentle touch and soothing voice, and her independence began to fade. Mrs. Jenkins, for her part, found comfort in Whiskers' companionship, and their bond became a source of strength and solace. They sat together on the bakery's windowsill, watching the sun set over the rooftops, Whiskers' purrs a gentle accompaniment to the sweet scent of freshly baked bread.
This project bridges the gap between decision-making algorithms and language model optimization. By adapting MCTS to creative contexts, it enables:
This project demonstrates the potential of Monte Carlo Tree Search to elevate the quality and creativity of large language models. By combining structured feedback, rigorous evaluation, and iterative refinement, it offers a powerful framework for optimizing LLM outputs across diverse applications.
There are no datasets linked
There are no datasets linked