Neural narratives in Python #5

I recommend you to check out the previous parts if you don’t know what this “neural narratives” thing is about. In short, I wrote in Python a system to have multi-character conversations with large language models (like Llama 3.1), in which the characters are isolated in terms of memories and bios, so no leakage to other participants like in Mantella. Here’s the GitHub repo.

Today I’ve been busy fully redesigning my web app, now that it includes plenty of interesting features.

Index page:

What it allows you to do is pretty self-explanatory, which is good. The relationship between the places is World > Region > Area > Location, and each of them have specific behavior in-game.

Story hub:

This one requires some explanation: at any point, the user can demand the system to generate story concepts, interesting situations, and interesting dilemmas. The large language model (the AI) will take into account all the info about the character, his or her followers, and their memories, to generate concepts, interesting situations and dilemmas that may fit what’s been going on.

Here are some examples of the interesting dilemmas the system can generate. Each “post-it” can be removed by just clicking on it. And now that I think about it, maybe I should make them yellow too.

Characters hub:

The player character front and center. The character images are generated by courtesy of an OpenAI model, that is provided a special prompt crafted from the character’s information.

Character generation:

Originally I told the AI to generate whatever characters it pleased based on the world, region, area and possibly location, but it ended up creating very similar characters: for example, in a police station, it would create a succession of serious detectives. So I programmed an intermediate step in which the AI creates character generation guidelines focused on coming up with a breadth of possible characters for that place. Of course, it also respects genre.

Character memories:

After each chat (and some other activities), a summary is created of what just took place, and is stored in the memories of all the participants. Those memories are loaded in most activities, and they color the characters’ dialogues and decisions. You can also generate self-reflections: the character, according to their personality, meditates upon their experiences, and writes a sort of journal entry about it, that goes into their memories to color their behavior and dialogue further. And of course, they can self-reflect about their self-reflections.

Location hub:

Lots of stuff to do on this page: generate a description of the place, find a new location among the existing ones matching the genre, visit found locations, and travel to other areas. You can also pick up followers here, or dismiss them.

The page for goal resolution is relatively simple so far, and I developed it just this morning, but it does something quite interesting: given a goal, you ask the large language model to write a narrative of the attempt to fulfill that goal, to decide whether or not the goal was achieved, and then a paragraph or two of the resolution, which gets saved as a memory.

This came about when the owner of a brothel, a succubus, suggested that I should travel to some nearby caves in search of treasure. That sounded interesting, but I would have needed to roleplay the entire thing through dialogues, including the possible obstacles found along the way. So I programmed in a system that figures it out by itself. In the future, I will probably program in a system to resolve explicit confrontations between characters or some opponent, like a bunch of wolves or something.

Now for the meat of this stuff, the chat system:

At any place, the user can choose to chat with any combination of characters present at the location, from those loitering around to the player’s followers. Multi-char convos is the first thing I made sure to program properly.

Chat:

All the NPCs speak in character, according to their personal info, their memories, and the ongoing conversation. There’s no leakage of information from other characters, because personal memories aren’t shared. If it weren’t because the calls to the LLM can be slow according to the whims of those servers, I would call the current system perfect.

So, what’s in the future? I plan on programming a system to generate goals, like it generates concepts, interesting dilemmas and interesting situations. Those goals will be loaded in a page, in case you want to attempt at resolving them. I also want to program in a way of swapping player characters easily. To deepen the simulation, I also want to include the notion of character health, and some way of altering the characters’ equipment and health after the actions have been resolved. At some point, I may rely on some local audio-generation server like XTTS to generate voices for each NPC utterance. That would be cool.

This whole thing has been a lot of fun. If I didn’t gravitate so much toward using it to engage in smut, I would have probably posted plenty of the stuff I’ve produced through the system.

EDIT: I fed this post to the Deep Dive pair, and they produced the following podcast (AI-generated):

One thought on “Neural narratives in Python #5

  1. Pingback: Neural narratives in Python #6 – The Domains of the Emperor Owl

Leave a comment