Using Semantic Search for Dungeons and Dragons

The task ahead of us

As a long time Dungeon Master, one thing I often face when introducing new players to Dungeons and Dragons is the overwhelming amount of rules involving magic. If you don’t know, every spell has quite a few specifics to it and you cannot just declare that you throw fire at your enemies. There are dice involved, measurements, sometimes material, verbal or somatic components… it is a lot… but as a new player, you often really just want to throw fire at goblins or make plants grow real fast… So I came up with an idea: What if you could just type what you want to do and my program looked up the relevant rules for you?

To do this, the question really becomes how to associate the meaning of the user input text with the meaning of what the rules say. And to answer that, we have to talk about semantic search.

Putting it all together

So, how do we use all of this to find the relevant DnD spells?

For this purpose, we are going to use something called a Sentence Encoder, which is just a model that takes the text, segments it into tokens (each with an embedding) and then returns a single embedding corresponding to the overall meaning of the whole sequence. We are then going to find the vector that represents each of the spell descriptions in the rulebook and place all of them in a multi-dimensional space, just like the ones we discussed above. Then, once the user describes what they want to do, we will also find the vector representation for the input and place it in the same space. All we have left to do is to find the relation between the input vector and each of the spell description vectors and output the most closely related spells.

If you want to see the full program in action and play around with it, you can find it in the free tools section. Just beware, the server I am hosting it on is a bit slow and it may take a moment to load.

If you want to read through the source code, you can find all of it in the GitHub Repository.

If you would like to learn more about how sentence encoders (and other types of Large Language Models) update the meanings of individual tokens based on their context, check out my guide on Understanding Multi-Headed Self-Attention.