Re-engaging users
Let’s now think a bit about how we can make your agent a bit more emotionally intelligent to improve the user experience.
For instance, before forgetting the conversation, you might want to try to re-engage your inactive user first.
The logic is simple and should already be familiar to you: if more than a minute passes since the user’s last interaction in an ongoing conversation, ask GPT-3 to generate a message in an attempt to re-engage the user in a creative way.
The new rule looks like this:
...
rule "Re-engage user"
when
Heartbeat(ts: timestamp) from entry-point "signals"
agent: Nola(
memory.isEmpty() == false,
lastInteractionTime before[1m] ts,
pinged == false
)
then
modify(agent) {
addInstruction("Continue conversation with human because they are inactive."),
setPinged(true)
};
agent.askGPT3();
end
With the pinged
flag you control how many times you contact the user.
By setting it to true
, you avoid triggering the rule multiple times and spamming the user with messages.
With addInstruction(...)
you explicitly instruct GPT-3 on what kind of message you want it to generate for re-engaging the user.
We also need to add pinged
and addInstruction
to Nola.java
:
...
@Getter
@Setter
public class Nola extends Agent {
private boolean pinged;
...
public void addInstruction(String text){
memory.add(text + "\n");
trimMemory();
}
...
}
Notice that this way, re-engagement will work only once: whenever the user continues the conversation after Nola’s ping,
the pinged
flag needs to be set back to false
. This way Nola can react again next time the user is idle.
We just add the following line to "Handle message"
:
...
rule "Handle message"
when
Heartbeat(ts: timestamp) from entry-point "signals"
message: TelegramReceivedMessage() from entry-point "signals"
agent: Nola()
then
modify(agent) {
setLastInteractionTime(ts),
addMessageToMemory("Human", message.getText()),
setPinged(false)
};
agent.askGPT3();
delete(message);
end
As always, you can try it with forge reset and forge run.
Well done! You now have a more caring agent that proactively and creatively tries to keep the user in the conversation should they stop responding.