Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

[BlenderBot2] clean up reply during interactive mode #4379

Merged
merged 7 commits into from
Mar 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions projects/blenderbot2/agents/blenderbot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
The Memory Decoder examines the context and generates memories to write to
the long-term memory module.
"""
import re
import copy
import torch
import torch.nn
Expand Down Expand Up @@ -253,6 +254,12 @@ def add_cmdline_args(
help='filter input to the global knowledge retriever such that any utterance containing '
'the phrase will not be given as input.',
)
bb2_group.add_argument(
'--clean-reply',
type=bool,
default=False,
help='filter reply during self_observe',
)
q_gen_group = parser.add_argument_group('BlenderBot2 Query Generator Args')
q_gen_group.add_argument(
'--query-generator-ignore-phrase',
Expand Down Expand Up @@ -898,6 +905,39 @@ def compute_loss(
else:
return loss

def _clean_text(self, txt):
cleaned_txt = re.sub(r'_[\S]*unsafe_*', '', txt, flags=re.IGNORECASE)
return cleaned_txt.strip()

def self_observe(self, self_message: Message) -> None:
"""
Observe one's own utterance.
Override TorchAgent.self_observe with the optional cleaned text

:param self_message:
The message corresponding to the output from batch_act.
"""

if (
self.opt('clean_reply', False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it returns here and doesn't go to cleaning the text if clean_reply is True?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to ALWAYS clean the reply and I think it's the right thing to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to ALWAYS clean the reply and I think it's the right thing to do?

or self.observation['episode_done'] # last example in the episode
or use_reply == 'none' # not including our own responses anyway
jxmsML marked this conversation as resolved.
Show resolved Hide resolved
or (
use_reply == 'label'
and any([x in self.observation for l in ['labels', 'eval_labels']])
) # has true label
):
return super().self_observe(self_message)

# otherwise, we use the CLEANED last output the model generated
if self_message is not None:
last_reply = self_message['text']
clean_reply = self._clean_text(last_reply)
self.history.add_reply(clean_reply)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should just override the History class and do this logic there?

return

raise RuntimeError("Unexpected case in self_observe.")


class BlenderBot2FidAgent(FidAgent, BlenderBot2RagAgent):
model: BlenderBot2FidModel
Expand Down