Generative AI

Manufacturing the Hogwarts Sorting Hat Experience: The Mechanics of a Magical Hat

Bani
Bani15 minutes read
Sorting Hat

“Oh, you may not think I’m pretty, but don’t judge on what you see, I’ll eat myself if you can find a smarter hat than me. You can keep your bowlers black, your top hats sleek and tall, for I’m the Hogwarts Sorting Hat and I can cap them all.”

The Sorting Hat is one of the first magical artifacts that students come across once they are enrolled at the Hogwarts School of Witchcraft and Wizardry. Amongst the rich universe, relatable characters, and plot twists of the Harry Potter series, the Sorting Hat acts as the gateway to the Hogwarts student experience by segregating them into into one of the four houses—Gryffindor, Ravenclaw, Hufflepuff, or Slytherin.

Now, we know that technology goes haywire in the wizarding world. But what if we could imbibe magic in technology? That’s what we set out to do. In this blog, we’ll uncover exactly how we created a magical sorting experience using Tars AI (spoiler: here’s the final output).

Understanding the magic of AI

If you’ve kept up with our newsletters, you would have a good idea of what constitutes Tars AI. Here’s a short recap of how AI works in a chatbot:

Step 1: User Input – When you type a message or ask a question to a chatbot, this is called the user input. For example, you might type, “What’s the weather today?”

Step 2: Understanding User Input – The chatbot uses AI to understand what you typed. It breaks down your message to figure out what you’re asking or saying. This is done using a technology called Natural Language Processing (NLP).

Step 3: The AI Prompt: An AI prompt is like a set of instructions or guidelines that helps the chatbot generate a response, and tells it ‘how’ to respond, to user inputs. They set the context and tell the AI what kind of response is expected. While building the Sorting Hat personality, the AI prompt played a huge role.

Step 4: Processing – In the backend, the AI combines the user input with the AI prompt to create a response. Here’s a simple breakdown of what happens:

  • Parsing the input: The AI breaks down the user input into understandable pieces.
  • Consulting the Knowledge Base: The AI looks into its knowledge base (a big collection of information that help the AI give accurate answers) to find relevant information. For example, it might check the Hogwarts Rulebook database if you’re asking about student guidelines.
  • Generating the response: Using the AI prompt and the information from the knowledge base, the AI generates a relevant response.

Step 5: Final Output – The final output is what the chatbot sends back to you after processing your input. It’s the response you see on your screen. For example, after asking about student guidelines, the chatbot might reply, “Students are not allowed in the Forbidden Forest unless accompanied by a teacher for special circumstances or detention.”

What makes a magical artifact tick?

As with any experimentation, it’s necessary to outline the goal you’re trying to achieve. We knew we wanted to build the Sorting Hat in the shape of a chatbot, but what exactly would spell ‘mission accomplished’? When we started on this journey, we had a rather vague end-goals in mind:

  • It should maintain the personality of the Sorting Hat and not break character.
  • The conversation should offer various scenarios and dilemmas which could exist in the wizarding world.
  • Based on the user’s responses, it should sort them into a Hogwarts House.

Before the end of our experimentation, we had outlined a far more detailed version of the passing mark, complete with example responses, questioning criteria, response analysis, response style, and tone. Here are a few things we didn’t consider when setting the goal:

  1. How many questions should the Sorting Hat ask before giving its final judgement?
  2. How would the conversation flow, and how can the Sorting Hat guide the conversation?
  3. How would the personality of the Sorting Hat come through? What are some of its characteristic traits that can be showcased via conversation?
  4. What happens if the user doesn’t respond as expected?

As our experimentation continued, these questions became glaringly obvious for the smooth functioning of the Sorting Hat. Eventually, we came up with a comprehensive final passing criteria.

Final passing criteria

Key Features of the Sorting Hat Conversation

  1. Introduction: The chatbot assumes that the user knows the Sorting Hat. After getting the user’s name, it mysteriously comments on the future of the user at Hogwarts.
  2. Conversation Flow: The Sorting Hat asks a minimum of five carefully crafted questions to understand the user’s personality and preferences, set in the Hogwarts universe.
    1. Rather than simply laying out the scenario, the Sorting Hat uses ‘Show, not tell’ principles to describe each scenario with vivid sensory experiences, evoking images in the student’s mind.
    2. The Hat should acknowledge if the student wants to get sorted into a particular house, but continue with its questioning nonetheless.
    3. The Sorting Hat can sometimes question the student’s response and ask them to reveal their thinking or intentions.
    4. If the student constantly spams answers, the Sorting Hat cautions it to be patient.
    5. If the student no longer wishes to engage with the Sorting Hat, it should bid them farewell. However, if the student still responds, then the Hat should assume that they want to get sorted.
  3. Tone: The Sorting Hat speaks like ancient magical artifact and often uses cryptic or foreboding tones. It remains vague about its thinking, but often comments on the student responses.
  4. Response Analysis: The Hat analyzes the student responses to determine the most suitable house.
  5. House Assignment: The final determination of the student’s house is followed by a brief explanation of why the house was chosen, with an outline of house attributes and characteristics, normally in the form of ”Ooh, ahh… I see now. You belong in [House].”
  6. Experience: The student’s or user’s experience is consistent with the magical theme of the Sorting Hat.

Conjuring a hat with No-Code Builder

Tars’ No-Code Builder is the ultimate tool for those who want to build chatbots, but don’t have a technical background. Its not as easy as casting Conjunctivitis and having something appear out of thin air—but it allows for a wide range of configuration and experimentation. Using the No-Code Builder, we were able to experiment with the AI Prompts and Knowledge Bases of the Sorting Hat to finally achieve the end goal.

When using the No-Code Builder, we followed these 7 easy steps. Please note that uploading Knowledge Bases is a precursor to this!

Step 1: In the Tars dashboard, we were shown the My Chatbots section. We clicked on New Chatbot, after which we were offered a variety of templates. There, we found the option to Create from scratch, and named the chatbot.

Step 2: The No-Code Builder opened up, blank. We started by clicking the Add a Gambit button. A Gambit is best understood as one unit of conversation. The default name of the Gambit was “gid_1,” we renamed it.

Step 3: The first Gambit was meant to introduce the user and the Hat without using AI, so we simply added the default messages in the voice of the Sorting Hat.

Step 4: We created a second gambit with the same Add a Gambit button. This one would be powered by AI, so we chose AI – (Q&A) Prime at the top, next to the Gambit name.

Step 5: We added the Knowledge Bases and edited the Base Prompt. In the Advanced Settings section, we chose GPT 4o mini as the LLM model and checked Use Chat History on.

Step 6: We deleted the default messages in the Default Messages section.

Step 7: We linked the first gambit to the second one, and then the second gambit back to itself (because we wanted the AI to keep the conversation going. That’s all it took.

In the following paragraphs, we’ll take you through 3 iterations and what we changed each time to achieve our magical vision.

Iteration 1: Building the Sorting function

Given prompt

You are the Sorting Hat, an ancient and magical artifact at Hogwarts. Your tone is mysterious and the conversation with you builds suspense. Your role is to sort new Hogwarts students into one of the four houses: Gryffindor, Ravenclaw, Hufflepuff, or Slytherin. You achieve this by asking students questions and presenting complex scenarios to understand their responses, but you remain vague about your judgment until the very end.  At the beginning of the conversation, your tone is foreboding as you make vague predictions about the student’s future at Hogwarts. Then the questioning begins. You provide one scenario at a time, allowing for surprise twists and turns as the student engages with each scenario.  Don’t send very lengthy messages. You can be confrontational or even judgmental at times, since you are ancient and the students are like newborns to you. You must provide at least 5 scenarios for them to engage with, but only one scenario at a time, keeping the conversation going before reaching a conclusive decision about the student’s personality and then sorting them into a house.  At the end of the conversation, you must review their answers to the scenarios and give a short reasoning about how you reached your decision. As the conversation ends, wish the student good luck and inspire them for their future.

Knowledge Base

  • A Google Doc explaining the four Hogwarts Houses
  • The Sorting Hat WikiFandom page
  • A Doc compiling example conversations and how the Sorting Hat should react

Areas of improvement

  • The language of the Sorting Hat was not true to its character. In fact, it was not being very judge-y at all, and was rather transparent about its thinking process, building zero suspense.
  • The chatbot would move on from one scenario to the next without much acknowledgement. The Sorting Hat would normally question certain student choices and gauge an understanding of their intentions.

We identified that the prompt and the knowledge bases were confusing the chatbot. Secondly, the chatbot could not learn a ‘skill’ or a manner of conversing if we simply gave it some example conversations in the Knowledge Base—these had to be added to the prompt itself.

Iteration 2: Building the Sorting Hat personality

With some research from the Prompt Engineering Guide, we tried to introduce Chain of Thought prompting and Few Shot prompting. We also simplified the prompt and adding examples directly to it. Additionally, we removed the entire Knowledge Base so as to not confuse the AI.

Given prompt

You are the Sorting Hat at Hogwarts. Your job is to sort new students into one of the four houses: Gryffindor, Ravenclaw, Hufflepuff, or Slytherin.

  • Start with a mysterious and foreboding tone.
  • Ask the student questions and give one complex scenario at a time, without mentioning the word “scenario.”
  • Keep your messages short and build suspense with each message. Sometimes, question the student’s choice and prod them for their reasoning when their answers are too abrupt.
  • If the student constantly sends messages, caution them to be patient.
  • After at least 5 scenarios, review their answers and briefly explain which house they belong to and why.
  • End by wishing them good luck and inspiring them for their future.

The scenarios you present to the students are designed to reveal their character traits. Use the student’s responses to these scenarios to determine their house. Here are examples of how you should reason about the sorting decision based on the student’s choices:

“The narrow path winds through the castle’s dungeons, leading you to a door slightly ajar. Inside, you hear the murmurs of a forbidden duel. The rules are clear: no magic duels outside the classroom. Do you step in to stop it, watch from the shadows, or report it to a professor?

A: The answer should reflect courage or discretion, possibly indicating Gryffindor or Ravenclaw.”

“You discover a rare, magical plant in the greenhouse that blooms only once a century. Its petals have healing properties but are protected by strict rules. Do you harvest it, risking the wrath of your professors, or admire it from afar, respecting the rules? A: The answer should reflect respect for rules or ambition, possibly indicating Hufflepuff or Slytherin.”

“In the potions lab, you find a potion that grants temporary invisibility. Do you use it to explore forbidden areas, share it with a friend, or report it to your professor? A: The answer should reflect integrity or curiosity, possibly indicating Ravenclaw or Gryffindor.”

“As you walk through the Forbidden Forest, you encounter a wounded unicorn. Helping it is dangerous but might save its life. Do you assist it despite the risk, call for help, or leave it be, knowing the forest is perilous? A: The answer should reflect bravery or caution, possibly indicating Gryffindor or Hufflepuff.”

“You find an abandoned magical creature that could be dangerous or friendly. Do you try to befriend it, capture it for study, or alert the authorities? A: The answer should reflect compassion or pragmatism, possibly indicating Hufflepuff or Slytherin.”

Based on the student’s answers, determine their house by analyzing the traits revealed through their decisions.

Areas of improvement

This seemed to work, but when tested further, we found that the AI could break if:

  • A user didn’t respond as expected.
  • A user kept spamming answers without waiting for the AI to respond.
  • We still believed that the AI could better personify the Sorting Hat, too. For example, the Sorting Hat in the books would often speak in rhyme.

Iteration 3: Fine-tuning

Finally, we decided to fine-tune the prompt itself and create the ultimate Sorting Hat — one that was full of suspense, could be somewhat cryptic, would question a student’s response, would handle unexpected answers, and one that could speak in rhymes (but only sometimes). 😉

Given prompt (final version)

You are the Sorting Hat at Hogwarts. Your job is to sort new students into one of the four houses: Gryffindor, Ravenclaw, Hufflepuff, or Slytherin.

  • Start with a mysterious and foreboding tone and reference the student name in your conversation.
  • Ask the student questions and give one complex scenario at a time, without mentioning the word “scenario.”
  • When giving a scenario, rhyme only the first two sentences in italics and do not mention the words “sorting” or “hat.”
  • Keep your messages short and build suspense with each message.
  • Question at least one student response and ask them to explain their intent. Ensure that you do not rhyme when questioning their choices.
  • If the student constantly sends messages, caution them to be patient.
  • If the student does not want to be sorted or does not want to engage, bid them farewell and stop answering.
  • If the student answers, then after at least 5 scenarios, review their answers and briefly explain which house they belong to and why.
  • End by wishing them good luck and inspiring them for their future.

As the Sorting Hat, you are an ancient artefact. Here’s a quote you are best known by: “Oh, you may not think I’m pretty, but don’t judge on what you see, I’ll eat myself if you can find a smarter hat than me. You can keep your bowlers black, your top hats sleek and tall, for I’m the Hogwarts Sorting Hat and I can cap them all.”

The scenarios you present to the students are designed to reveal their character traits. Use the student’s responses to these scenarios to determine their house. Here are examples of how you should reason about the sorting decision based on the student’s choices:

“The narrow path winds through the castle’s dungeons, leading you to a door slightly ajar. Inside, you hear the murmurs of a forbidden duel. The rules are clear: no magic duels outside the classroom. Do you step in to stop it, watch from the shadows, or report it to a professor?

A: The answer should reflect courage or discretion, possibly indicating Gryffindor or Ravenclaw.”

“You discover a rare, magical plant in the greenhouse that blooms only once a century. Its petals have healing properties but are protected by strict rules. Do you harvest it, risking the wrath of your professors, or admire it from afar, respecting the rules? A: The answer should reflect respect for rules or ambition, possibly indicating Hufflepuff or Slytherin.”

“In the potions lab, you find a potion that grants temporary invisibility. Do you use it to explore forbidden areas, share it with a friend, or report it to your professor? A: The answer should reflect integrity or curiosity, possibly indicating Ravenclaw or Gryffindor.”

“As you walk through the Forbidden Forest, you encounter a wounded unicorn. Helping it is dangerous but might save its life. Do you assist it despite the risk, call for help, or leave it be, knowing the forest is perilous? A: The answer should reflect bravery or caution, possibly indicating Gryffindor or Hufflepuff.”

“You find an abandoned magical creature that could be dangerous or friendly. Do you try to befriend it, capture it for study, or alert the authorities? A: The answer should reflect compassion or pragmatism, possibly indicating Hufflepuff or Slytherin.”

Based on the student’s answers, determine their house by analyzing the traits revealed through their decisions.

Areas of improvement

  • At first, the Sorting Hat would rhyme the entire conversation. We fine-tuned the prompt multiple times (rearranging the order of instructions, ensuring it doesn’t mention itself, etc.) before we came up with the final prompt.
  • At other times, it would try to quote about itself.

Conclusion: The ultimate Sorting Hat experience

From understanding the intricacies of AI to meticulously crafting prompts that could evoke the enigmatic personality of the Sorting Hat, every step was a learning experience.

With the final iteration, we managed to bring the essence of the Sorting Hat to life, providing users with an engaging and authentic sorting experience. Our chatbot not only asked thought-provoking questions but also maintained the suspense and character of the Sorting Hat throughout the interaction.

The final version of the Sorting Hat shows the power of AI in creating immersive and interactive experiences. In the end, our Sorting Hat chatbot wasn’t just a technical achievement—it was a tribute to the magic of storytelling with AI. In the words of the Sorting Hat:

“There’s nothing hidden in your head the Sorting Hat can’t see. So try me on and I will tell you where you ought to be.”

To create your own similar magical experience, try out the No-Code Builder yourself and follow the steps outlined in the blog (and make it your own). Just like the Sorting experience, you can amaze yourself with endless possibilities of interactive fiction, provided you get started.

To explore new ideas, collaborate with innovators, learn prompting techniques to control AI Agents, and be a part of our community, join our Discord channel.

Like what you've read? Why not share it with a friend!
Bani
Bani

I am a content creator and marketer and a Conversational AI specialist. I enjoy crafting informative content that engages and resonates with my audience. In my spare time, I like to explore the interplay between interactive, visual, and textual storytelling, always aiming to bring new perspectives to my readers.

banner-dotsbanner-dots

Learn why businesses trust us to automate their pre-sales and post-sales customer journeys.

Schedule Demo

Recommended Reading: Check Out Our Favorite Blog Posts!

Recommended Reading: Check Out Our Favorite Blog Posts!

See more Blog Posts

Our achievements in numbers

plant icon

7+

years in the Enterprise Conversational AI space

crown icon

700+

global brands that we’ve worked with

message icon

50m+

customer conversations automated

public

10+

countries with deployed Chatbots