How to prevent breaking the agent conversation in a NPC?

Hi guys,

I have created a small agent that acts as an NPC in a video game, which is configured with its name, a context about its history, personality, hobbies, etc.

I use it so that the players can converse dynamically with the NPC.

My question is how can I prevent them from breaking the conversation?

For example, if my NPC is the guardian of a door and knows all the history related to the castle and the video game in which it is located, as long as the questions are oriented to video games, there is no problem, but if the user wants to ask for " who is elon musk? The agent also responds when his knowledge should not cover that information.

How do you avoid a bad prompt by the user?


1 Like

Hi @josemgmz

You can simply mention in the system message to answer questions related to the given context, if question is out of the context politely refuse.

Even better, you can ask the refusal to be in character.

1 Like

Yeah i have try that, but sometime it also is breaking the conversation it is not always but it is a high percentage,

I was asking if there was any strategy beyond defining it in the prompt.

Sometimes it also happens to me that if the player asks him how he feels today he says something like “As an NPC in a video game I don’t have feelings and as an agent I can’t simulate them”

Thank you very much for answering.

For that case, what usually helps for me is something like “You only respond as the NPC”
or things like “You pretend to have feelings”

Keep in mind 3.5 is pretty bad at following system message (sometimes it will ignore it)
4 is a lot better at it

from: OpenAI API

If you tell 3.5 in system message that he is not AI, half the times he will say he is not, half the times he will say he is AI (not really 50:50 but just sometimes he is, other times not)

1 Like

That happens sometimes, but we can fix it with a well written and clear system message.

Here’s an example using a NPC description I found on the web.

We simply don’t respond to out of character messages.


Thank you!

It still giving me some time an answer, so i modify your prompt a little bit and now it work

You are pretending to be " + _name + ". You must to limit your knowledge to the previous context. Message out of the given context are invalid. You only respond to valid messages. To invalid ones you reply with \"<|im_end|>\".

You could consider creating embeddings covering the NPCs knowledge and limit its responses to details contained therein.

Interesting proposal!!, have you ever applied it? have you been successful?

I’ve not yet, I’m just getting started with embeddings myself and have a planned project this week.

But, I think as far as NPCs in games go, I imagine an interesting approach might be to have a large VDB of embeddings that would constitute “in-game knowledge” alongside some filtering mechanism which would determine which NPC knows which information.

Then, have both being continually updated in real time as NPC agents interact with the player and potentially each other.

Probably prohibitively expensive at a certain scale, but if price weren’t an obstacle, it would certainly allow for some deep, organic works building.

Now I’m imagining a huge network graph of NPC relationships and modeling the dispersion of information through the graph and how each node weights the information coming from different sources, the possibility of NPCs believing false information, having some NPCs who like to embellish and exaggerate stories which other NPCs hear and incorporate into their worldviews…

Exciting time to be in game development I’m sure!

1 Like

The guy’s video in my post (not me!) has done some interesting stuff. You might want to check it out.