Discuss Scratch

--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Hello everyone. Welcome to my forum post on how I developed an intelligent chatbot from scratch (No pun intended). In this post, I will share with you the process of creating my chatbot, Sigma, my chatbot. I'll explain how it works, why I made certain decisions, and how it became so popular. I'll also give a little background information about how I was able to make it.

I hope you will find this post informative and interesting. Feel free to leave your comments and questions below. Let’s get started!
Table of Contents:
  1. How did I learn to make chatbots, and why?
  2. How was Sigma made?
  3. How does Sigma work?
  4. How did Sigma become so popular?
  5. FAQ

How did I learn to make chatbots, and why?

Right now, Sigma is the most complex project I have ever built, technically and artistically. It has nearly 4,000 blocks, and many complex scripts. However, Sigma wasn't my first chatbot.

At the time of writing, I've been using Scratch for over 5 years. Within those 5 years, I've built 3 chatbots (I like chatbots, lol). The first was… pretty bad. It was slow, ugly, and didn't really understand anything besides basic greetings and very limited small talk. However, building the chatbot gave me something I didn't have before… experience. That experience would send me on the right path to be creating much bigger projects.

Fast forward a few years, and I built my next chatbot: the first version of Sigma. It wasn't great, but it was still miles ahead of my first chatbot. It had a proper UI, better optimization, and much better speech recognition, although, admittedly, it was still not great. All of the responses were contained inside a giant “if-else” loop that was becoming impossible to manage. That's kind of a problem, especially with something as complicated as a chatbot.

However, again, when I built the first version of Sigma, it again gave me something I didn't have before: experience. This time, I learned how to write an input detector, a text renderer, and a much better speech recognition system based off of lists. All of this knowledge was invaluable. Then, something amazing happened…

Nothing.

No, seriously. At this point, I started to become less active on Scratch. I was learning multiple programming languages, including C++ and C#, and I was learning how to use professional game development software, like Unreal Engine. After a while, I also started contributing source code to Godot Engine. Doing all of this taught me a great deal about how real-life professional code is written.

After about a year, I decided to start doing Scratch again. This time, I had real programming experience, and I wanted to make something big. But… what?

While playing my old projects again for fun, I realized just how badly Sigma was at holding conversations. Almost anything I typed would trigger an “I don't know what you're saying” response. This was really bad, since the whole point of a chatbot is to, um… chat. It needed to be fixed. So, with experience and a goal, I set out to create a new version of Sigma; one that can actually hold conversations.

How was Sigma made?

Sigma is an incredibly large project, with many differnet parts making it work. You might assume that the first thing I started working on was the chatbot itself, and rightly so. After all, I was developing a chatbot, right?

Actually, the chatbot was one of the last parts of Sigma that I worked on. The first thing I did was design the UI in Inkscape. I got inspiration for the layout from Jrehd3's AI Chatbot, and the aesthetic from OS projects on Scratch, such as BlueOS.

I think the end result turned out nicely and helped me stay motivated. Next, I built the input detector. This took only about an hour, since it was something I'd done before. After that, I moved on to building the renderer. This was where the challenge started to begin. I'd built a text renderer in the past, but it was quite basic. This time, I had a much bigger list of features I wanted the renderer to have, such as word wrapping, adjustable text size, and individual character spacing.

The most tedious part of making the renderer was definitely the font. I wanted to use a custom font, but Scratch doesn't support them. So, I had to manually import a vector image of a font, split each character into its own sprite, and adjust them so that they were in the same position and had the same size. This process took several days.

After that, I started coding. As I said, I already had experience with Scratch text renderers, so it wasn't that difficult for me. This renderer also includes word wrapping, which was a feature I figured out how to implement on my own, and I was super happy with how well it worked. After I finished work on the renderer, I started on the chatbot itself. At this point, I was checking out other chatbots on Scratch for inspiration, and one in particular stood out to me: Cirquit by MasterofTheBrick.

Cirquit was far more advanced than most chatbots I'd seen on Scratch. It was able to reply to almost everything I said. Upon looking inside, I discovered that it used a similar list-based input system as the old version of Sigma. However, the code was far more organized and better designed. I was really impressed. A lot of how the new version of Sigma's reply tree was designed was inspired by Cirquit's, earning it a permanent spot in Sigma's credits.

After I'd done my research, I began building. I kept adding more replies, more phrases, and more small features. After a few days, I decided that I wanted Sigma to be able to handle do math. Oh boy. I wanted it to be able to understand complex arithmetic equations and to be able to solve them. At this point, I had no experience with how calculators work or how math was solved on computers.

After a day of research, I found something called the shunting-yard algorithm on Wikipedia. Apparently, this allows computers to solve math equations with proper order of operations. (Multiplication before addition, etc.) So, using the Wikipedia article as a reference, I built a calculator. I was shocked. This complex and rather confusing algorithm only took a day to learn to make… and it worked like a charm! Sigma now had math solving abilities! Of course, it currently lacks the ability to solve advanced math operations, such as powers or functions, but I could add those in the future.

I was getting excitied. Sigma was almost done. At this point, I had some friends and family try out the bot. This was crucial to making sure it could hold conversations. I observed how they communicated with the bot and how it was tripping up. Because of this, I was able to find new things that Sigma should respond to really quickly and easily. I think that this form of testing is one of the most important things any project could have.

Finally, I wrote out a super nice description for the project. The instructions were simple and to-the-point, and the notes and credits documented all the amazing projects that inspired Sigma.

Then.. Sigma was released to the world.

Overall, Sigma took about 2-3 weeks of development, not counting break-days or off-days. It was a moment of great pride and accomplishment when I shared it.

How does Sigma work?

Sigma | ᴄʜᴀᴛʙᴏᴛ is incredibly complex, with many different independent parts. I won't be talking about the renderer or the input engine. Instead, I'll explain how the chatbot itself breaks down user input and how it responds to it.
Note: The code in this explanation is not identical to Sigma's actual code. Rather, it gives an example of what it does.

At its simplest form, the chatbot works in 2 steps: reading input and responding to it. To read input, the chatbot uses a very simple form of natural language processing. The input is split into different words, and punctuation is removed. To respond to the input, the chatbot uses a decision-tree. Basically, it checks for certain words or combinations of words, and it then gives the appropriate response. For example, suppose we wanted the chatbot to respond to the sentence “I like coding.” We'd add in a new branch on the tree that checks for the combination of words “I,” “like,” and “coding.” If the player input contains those words, we can respond with something like: “It's great that you like to code!” This system certainly has its pros and cons, but on Scratch, it's a great way to build a chatbot. Let's take a look at some code now.

set [acceptedInputChars v] to [abcdefghijklmnopqrstuvwxyz1234567890]

define Tokenize text (text)
delete (all v) of [playerInput v]
add [] to [playerInput v]
set [iterator v] to [0]
repeat (length of (text))
change [iterator v] by (1)
if <(letter (iterator) of (text)) = [ ]> then
add [] to [playerInput v]
else
if <(acceptedInputChars) contains (letter (iterator) of (text)) ?::operators> then
replace item (length of [playerInput v] :: list) of [playerInput v] with (join (item (length of [playerInput v] :: list) of [playerInput v] :: list) (letter (iterator) of (text)))
end
end
end

The Tokenize Text custom block is a crucial component of Sigma, allowing the chatbot to process user input in a way that makes it very easy to understand what the user was trying to say. Here's how it works:
  • We loop through every character in the given text, checking each one to see if it is a letter or number.
  • If the current character is a letter or number, we add it to the current word in the list. This helps us build a list of individual words from the input text.
  • Special characters like punctuation are filtered out to make it easier to identify specific words later on. This ensures that we only have to worry about the words that the input contains.
  • If the current letter is a space, a new list entry is added. This ensures that we can easily check for individual words later on.

If we were to use the block like this:

Tokenize text [Hello, how are you today?]::custom

Then it would give us a list that looks like this:

Hello
how
are
you
today

Now, let's talk about how we can read the tokenized text and respond to it.

define Output (text) if <condition>
if <<not <condition>> or <(hasChatbotReplied) = [true]>> then
stop [this script v]
end
set [hasChatbotReplied v] to [true]
say (text) for (5) secs

The “Output” custom block is really basic. It works by checking whether a certain condition has been met, and then sending the appropriate response if it has. How you want your chatbot's reply to be shown to the player is up to you. I just used a “Say” block as an example. In the example below, we use the “Output” block to create a chatbot response that is triggered when the user input contains the words “hi” and “there,” in that order:

Output [Hi, how can I help?] if <<[playerInput v] contains [hi] ?> and <(item ((item # of (hi v) in [playerInput v] :: list) + (1)) of [playerInput v] :: list) = [there]>>::custom
<(item ((item # of (hi v) in [playerInput v] :: list) + (1)) of [playerInput v] :: list) = [there]> // Adding on separate line so you can see it

By splitting up the player's input into individual words, we can easily check for combinations of words like this. This allows us to create more sophisticated responses that take into account not just individual words, but also the context in which they appear.

As you may have noticed, we use a variable called “hasChatbotReplied” when we output text. This variable helps to keep track of whether the chatbot has already sent a response to the user, so that it doesn't send duplicate messages. For example, if we have a block that checks for “Hi there” and another block that checks for “Hi,” and the user inputs “Hi there,” both responses could technically be used. However, we don't want the chatbot to send both messages. By using the “hasChatbotReplied” variable, we can prevent this from happening and ensure that the chatbot only sends one response to the user.

By building up a large amount of these responses, we can have the chatbot make replies to a lot of different things that the players will say. Filling up the response tree takes time. Sigma currently has over 250 responses, and that number is still growing. The best way to figure out what to have your chatbot respond to is by talking to it. Find out how you talk with it, and what needs to say. If possible, have others try it out. Getting feedback from others is helpful, as it allows you to gain a broader perspective and identify gaps in the chatbot's capabilities. After a bit of time, you'll have a really intelligent chatbot.

How did Sigma become so popular?

I think this might interest a few people, since I was never a popular Scratcher, and have never had a popular project before now. Before I talk about this, I think it's important to stress that Scratch is not about popularity. Popularity is just an extra bonus that adds on to the fun of creating things. Even though I understood how people get popular for a long time, I've never tried to, because it's never been my priority. Sigma's popularity shocked me, as it was like catching lightning in a bottle. But I think I have a few ideas about how it became so popular, so I'll try to explain…
By the way, if you'd like a comprehensive guide on how to get your projects popular, I've written a dedicated topic on that.

  1. Before the release

    The most important ingredient in popularity is making a really good project. I've always been a believer in quality over quantity, meaning, making 1 or 2 really good projects that take a long time to make is much better than creating 50 or 100 projects that get put together in just 2 hours. I was also passionate about what I was building, so that allowed me to put in a lot of effort and time into creating Sigma. It's not about trying to gain popularity, but about creating something that I was passionate about and that others find value in.

    TL;DR: I put a lot of effort into the project.

  2. Sharing the project

    Just before I shared the project, I made a thumbnail for it. Believe it or not, the thumbnail of your project is one of the biggest things that determines its popularity. With the thousands of projects getting shared every day, you need to make yours stand out. That's what I did with my thumbnail. I made a professional-looking image of the chatbot, with frosted glass, some colors, and some text to demonstrate what it does. Because of this, people are drawn to the project's thumbnail, since it stands out, and they can easily understand what it does within a split second, even before clicking on it.

    Another important thing is to write out a nice description for your project. This includes the Instructions and the Notes and Credits. Remember, if you need to write an entire page about how to use your project, it's probably too unintuitive. The instructions should be concise and to the point, while also being helpful. That's what I did with Sigma.

    The Notes and Credits is my favorite part of a project's description. Here, you can add your inspirations, credits, and tags. Remember, things in your Notes and Credits will help your project be more searchable, so make sure everything in it is relevant to the project.

    TL;DR: I presented myself well.

  3. After the release

    After I released my project, it initially gained little popularity. However, I did something very important: I listened to user feedback. When people would find issues or suggest new features, I was quick to respond. That helped me gain the favor of others, and helped the chatbot get much better really quickly.

    After a few days, some people were asking if my chatbot could be added to the SDS, which was about Living Objects at the time. After it was added, the project's popularity started exploding. As I said, the thumbnail stood out among the other projects in the studio and caught the attention of many. Also, to the curator who added my project, and to those who asked it to be added, thank you.

    This is where things really started to kick off. Propelled by the popularity in the SDS, my project got onto the Trending page, eventually reaching the 18th spot at its peak! This was huge! I've never been on the Trending page before. I had tons of people playing Sigma and giving me feedback. It was a lot of fun to watch it get better and to watch others use it. However, after it reached about 4000 views, things started to calm down.

    For a few days, it seemed like that Sigma's viral time was over, and that was okay. I was super happy that so many people were able to use it and help improve it. However, a few days after, I woke up, and had hundreds of messages.

    I was shocked. Did a famous Scratcher love my project? Did it get back onto the Trending page? Nope. I was on the Front Page! Sigma was among the projects that the community was loving! I still have no idea how this happened. The project was already older than most of the other projects on that page, and it had less loves. But it somehow did, and it's what's caused Sigma to become as popular as it is.


    TL;DR: I engaged with the community, I took feedback, and a lot of very fortunate, coincidental things happened just the right way to help Sigma get popular.

FAQ

  • Why did you name the chatbot “Sigma?”
    I just liked the sound of it. At the time, my friend and I were using the Greek alphabet to name a lot of different things, and Sigma sounded cool to me.

  • Why isn't your chatbot actually AI?
    With the release of AI language models like ChatGPT and Bing, people now finally have an understanding of what AI is truly capable of, and it changed their expectations of what they expect out of future similar software. There are a few different reasons why I decided not to do actual AI for this chatbot, but the main one is that I don't know how. Machine learning is a very niche, very complex category of programming that requires a lot of experience to be proficient in. Besides, as a project to entertain kids and teens on Scratch, the decision-tree model I used works very well.

  • Why not use a public list so that users can help make the chatbot better?
    That's a good question. If I'm not going to use AI, why not just use a cloud list to allow the players to help add responses to the chatbot? This is actually a very bad idea. First of all, using cloud lists would effectively take away the ability to understand multiple words independently, as the chatbot currently does. Second, that would be a moderation nightmare. I've already seen people comment about the ridiculous things they've said to Sigma. Imagine if they could just have the bot respond to things like that. In the end, it's just not a viable strategy… at least, not without much better coding knowledge than what I have.

  • Why is your chatbot's code similar to Cirquit's?
    It's true that I was inspired by some of Cirquit's code design choices when creating Sigma. However, I did write all of the code in this project myself from scratch. As far as I'm aware, Sigma and Criquit don't share any identical code.

  • Why does Sigma rickroll people?
    Because it never wants to let you down.

I hope this proved to be an informative post that helped you understand the ins and outs of my chatbot. Maybe you'll be able to build a chatbot with this knowledge. I invite you to try out Sigma for yourself and let me know what you think. Thanks for reading!

- –HyperZ–

Last edited by --HyperZ-- (June 5, 2023 03:51:00)

Crispydogs101
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Dude! this chat bot is slick! It can actually be like you are talking to a real robot! Love this amazing tool you made! I'm super surprised this topic has no replies at all since this is a famous project here. I think you deserve the fame right now.

How long did it take for you to make this project by the way?
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

@Crispydogs101 Wow, thank you! ❤️

Crispydogs101 wrote:

How long did it take for you to make this project by the way?
About 3 weeks.
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

--HyperZ-- wrote:

About 3 weeks.
only?!
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Bump
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Bump
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

bump
Adzboy
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

wikibro300000 wrote:

bump
Please don't bump so soon after another bump as it can be considered spam
-TwentyFour-
Scratcher
500+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Your chatbox is very cool and fun but sometimes annoying .
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

I love your chat bot, tho when I was testing its maths, sometimes it would just say okay, which is kinda weird
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

wikibro300000 wrote:

I love your chat bot, tho when I was testing its maths, sometimes it would just say okay, which is kinda weird

Hi! Thanks for the feedback! The bot doesn't understand words like “plus” and “minus,” only + and -. If I were to have it recognize those words, then they'd become operators, and that could confuse the bot when they're not being used with math.
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

--HyperZ-- wrote:

only + and -. If I were to have it recognize those words, then they'd become operators, and that could confuse the bot when they're not being used with math.
oh, i did use + and - but does it solve multiplication?
Linaviolet
Scratcher
25 posts

Zero to Hero: How I Developed an Intelligent Chatbot

Hello! This is great I tried out the chatbot but I saw in the comments here that the chatbot also can do maths

The bot doesn't understand words like “plus” and “minus,” only + and -.

But when I tried it out it wouldn't let me type in - or + .I just wanted to tell you in case it was a bug, or maybe my computer just wasn't loading or something.
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Linaviolet wrote:

But when I tried it out it wouldn't let me type in - or + .I just wanted to tell you in case it was a bug, or maybe my computer just wasn't loading or something.

It does that sometimes. I think it's a Scratch issue. Refreshing the page usually works for me.
CoolGuy247365
Scratcher
19 posts

Zero to Hero: How I Developed an Intelligent Chatbot

It is so cool your chatbot! You are cool!
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot


oh, i did use + and - but does it solve multiplication?
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

wikibro300000 wrote:

oh, i did use + and - but does it solve multiplication?
Yes, using the asterisk (*).
--HyperZ--
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

Bump
wikibro300000
Scratcher
1000+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

--HyperZ-- wrote:

wikibro300000 wrote:

oh, i did use + and - but does it solve multiplication?
Yes, using the asterisk (*).
Oh ok, I just used x
codeisBOOM
Scratcher
100+ posts

Zero to Hero: How I Developed an Intelligent Chatbot

sigma from tiktok
☠️☠️☠️

Powered by DjangoBB