Get multiple 'last user messages', tracker

Hey guys,

Is there a way to access, say, the last 2 user messages in a custom action with the Tracker module? I use the tracker.latest_message.get to access the last message, but what if I want the last 2, or any other number? Apologies if this is already available somewhere - I couldn’t find anything :confused:

Thanks in advance!

Hi,

I think there is no premade solution to this. You can however access older messages through the list of events stored in the tracker. For example

for event in tracker.events:
    if event.get("event") == "user":
        dispatcher.utter_message("{}".format(event.get("text")))

would make the bot echo all previous user messages.

What are you trying to achieve with this though?

Ah, I think this would do the trick, yes - thanks!

Basically, I’m trying to make a search engine-ish bot, where the user messages are the query, and the user does not always provide all details in the last message alone. I could go with slots/forms, but I was looking for the most flexible thing to do, as the user does not always have a clear idea of what he is looking for.

To remedy this, I’m using a cosine similarity function to match the vectorized user query with the best-matching vectorized search results, and I would sometimes want to vecotrize the last few messages to get as much (unclear) context vectorization as possible. Works alright so far with placeholder search results, but I haven’t upscaled to proper documents yet.

Anyway, thanks again!

That sounds interesting, when you’re done please consider sharing it here on the forum :slight_smile:

I sure will!

Just to share a few more details - the bot takes in a message and runs cosine similarity; if no response has a similarity greater than a threshold (to be managed as a hyperparameter), the bot takes the previous message as well, and concatenates those last two user messages before the vectorization. If this concatenated message does not yield a higher similarity response as well, the bot asks for more information from the user. (I initially wanted to concatenate even more messages, but this slows down the bot too much)

If the match does meet the threshold, the bot returns the highest-likelihood match (that does meet the threshold), and then asks if this was a satisfactory response. If the user says ‘no’, the bot gives the next-highest-likelihood match (again, if it does meet the threshold), and cycles through all responses that meet the threshold criterion.

The mechanics of it are working alright so far, but I’ve yet to scale up to proper documents, which would probably happen in a few weeks/months

This is very interesting!

Have you been able to achieve this? If so, do you mind sharing it please?

Thanks a lot, bro its working fine now