Views
No views yet
1commonsense_questions = {
2 "cause": 'What could have caused the last thing said to happen?',
3 "prerequisities": 'What prerequisites are required for the last thing said to occur?',
4 "motivation": 'What is an emotion or human drive that motivates Speaker based on what they just said?',
5 "subsequent": 'What might happen after what Speaker just said?',
6 "desire": 'What does Speaker want to do next?',
7 "desire_o": 'What will Listener want to do next based on what Speaker just said?',
8 "react": 'How is Speaker feeling after what they just said?',
9 "react_o": 'How does Listener feel because of what Speaker just said?',
10 "attribute": 'What is a likely characteristic of Speaker based on what they just said?',
11 "constituents": 'What is a breakdown of the last thing said into a series of required subevents?'
12}1generation_config = {
2 "repetition_penalty": 1.0,
3 "num_beams": 10,
4 "num_beam_groups": 10,
5 "diversity_penalty": 0.5
6}1import torch
2from transformers import AutoTokenizer, T5ForConditionalGeneration
3
4device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5tokenizer = AutoTokenizer.from_pretrained("sefinch/ConvoSenseGenerator")
6model = T5ForConditionalGeneration.from_pretrained("sefinch/ConvoSenseGenerator").to(device)
7
8# ConvoSenseGenerator covers these commonsense types, using the provided questions
9commonsense_questions = {
10 "cause": 'What could have caused the last thing said to happen?',
11 "prerequisities": 'What prerequisites are required for the last thing said to occur?',
12 "motivation": 'What is an emotion or human drive that motivates Speaker based on what they just said?',
13 "subsequent": 'What might happen after what Speaker just said?',
14 "desire": 'What does Speaker want to do next?',
15 "desire_o": 'What will Listener want to do next based on what Speaker just said?',
16 "react": 'How is Speaker feeling after what they just said?',
17 "react_o": 'How does Listener feel because of what Speaker just said?',
18 "attribute": 'What is a likely characteristic of Speaker based on what they just said?',
19 "constituents": 'What is a breakdown of the last thing said into a series of required subevents?'
20}
21
22def format_input(conversation_history, commonsense_type):
23
24 # prefix last turn with Speaker, and alternately prefix each previous turn with either Listener or Speaker
25 prefixed_turns = list(
26 reversed(
27 [
28 f"{'Speaker' if i % 2 == 0 else 'Listener'}: {u}"
29 for i, u in enumerate(reversed(conversation_history))
30 ]
31 )
32 )
33
34 # model expects a maximum of 7 total conversation turns to be given
35 truncated_turns = prefixed_turns[-7:]
36
37 # conversation representation separates the turns with newlines
38 conversation_string = '\n'.join(truncated_turns)
39
40 # format the full input including the commonsense question
41 input_text = f"provide a reasonable answer to the question based on the dialogue:\n{conversation_string}\n\n[Question] {commonsense_questions[commonsense_type]}\n[Answer]"
42
43 return input_text
44
45def generate(conversation_history, commonsense_type):
46 # convert the input into the expected format to run the model
47 input_text = format_input(conversation_history, commonsense_type)
48
49 # tokenize the input_text
50 inputs = tokenizer([input_text], return_tensors="pt").to(device)
51
52 # get multiple model generations using the best-performing generation configuration (based on experiments detailed in paper)
53 outputs = model.generate(
54 inputs["input_ids"],
55 repetition_penalty=1.0,
56 num_beams=10,
57 num_beam_groups=10,
58 diversity_penalty=0.5,
59 num_return_sequences=5,
60 max_new_tokens=400
61 )
62
63 # decode the generated inferences
64 inferences = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False)
65
66 return inferences
67
68conversation = [
69 "Hey, I'm trying to convince my parents to get a dog, but they say it's too much work.",
70 "Well, you could offer to do everything for taking care of it. Have you tried that?",
71 "But I don't want to have to take the dog out for walks when it is the winter!"
72]
73
74inferences = generate(conversation, "cause")
75print('\n'.join(inferences))
76
77# Outputs:
78# the speaker's fear of the cold and the inconvenience of having to take the dog out in the winter.
79# the speaker's preference for indoor activities during winter, such as watching movies or playing video games.
80# the speaker's fear of getting sick from taking the dog out in the cold.
81# a previous negative experience with taking dogs for walks in the winter.
82# the listener's suggestion to offer to help with taking care of the dog, which the speaker may have considered but was not willing to do.@article{convosense_finch:24,
author = {Finch, Sarah E. and Choi, Jinho D.},
title = "{ConvoSense: Overcoming Monotonous Commonsense Inferences for Conversational AI}",
journal = {Transactions of the Association for Computational Linguistics},
volume = {12},
pages = {467-483},
year = {2024},
month = {05},
issn = {2307-387X},
doi = {10.1162/tacl_a_00659},
url = {https://doi.org/10.1162/tacl\_a\_00659},
eprint = {https://direct.mit.edu/tacl/article-pdf/doi/10.1162/tacl\_a\_00659/2369521/tacl\_a\_00659.pdf},
}