hf_mirrors/personaGPT
Model Introduction
Files & versions
Analysis

A conversational agent with many personalities (PersonaGPT)

PersonaGPT is an open-domain conversational agent designed to do 2 tasks:

  1. decoding personalized responses based on input personality facts (the "persona" profile of the bot).
  2. incorporating turn-level goals into its responses through "action codes" (e.g., "talk about work", "ask about favorite music").

It builds on the DialoGPT-medium pretrained model based on the GPT-2 architecture. This model is trained on the Persona-Chat dataset, with added special tokens to better distinguish between conversational history and personality traits for dyadic conversations. Furthermore, some active learning was used to train the model to do controlled decoding using turn-level goals.

Full Repo

Preprocessing, training and implementation details can be found in the personaGPT repo.

How to Use

  1. Load the model and define some helper functions.
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch
tokenizer = GPT2Tokenizer.from_pretrained("af1tang/personaGPT")
model = GPT2LMHeadModel.from_pretrained("af1tang/personaGPT")
if torch.cuda.is_available():
    model = model.cuda()
## utility functions ##
flatten = lambda l: [item for sublist in l for item in sublist]

def to_data(x):
    if torch.cuda.is_available():
        x = x.cpu()
    return x.data.numpy()

def to_var(x):
    if not torch.is_tensor(x):
        x = torch.Tensor(x)
    if torch.cuda.is_available():
        x = x.cuda()
    return x

def display_dialog_history(dialog_hx):
    for j, line in enumerate(dialog_hx):
        msg = tokenizer.decode(line)
        if j %2 == 0:
            print(">> User: "+ msg)
        else:
            print("Bot: "+msg)
            print()

def generate_next(bot_input_ids, do_sample=True, top_k=10, top_p=.92,
                  max_length=1000, pad_token=tokenizer.eos_token_id):
    full_msg = model.generate(bot_input_ids, do_sample=True,
                                              top_k=top_k, top_p=top_p, 
                                              max_length=max_length, pad_token_id=tokenizer.eos_token_id)
    msg = to_data(full_msg.detach()[0])[bot_input_ids.shape[-1]:]
    return msg
  1. Give your chatbot partner a set of personalities.
# get personality facts for conversation
personas = []
for i in range(3):
    response = input(">> Fact %d: "%(i+1))+ tokenizer.eos_token
    personas.append(response)
personas = tokenizer.encode(''.join(['<|p2|>'] + personas + ['<|sep|>'] + ['<|start|>']))
  1. The first use of PersonaGPT is to do personalized dialog generation. Use the following loop to interact with the model.
# converse for 8 turns
dialog_hx = []
for step in range(8):
    # encode the user input
    user_inp = tokenizer.encode(input(">> User: ") + tokenizer.eos_token)
    # append to the chat history
    dialog_hx.append(user_inp)
        
    # generated a response while limiting the total chat history to 1000 tokens, 
    bot_input_ids = to_var([personas + flatten(dialog_hx)]).long()
    msg = generate_next(bot_input_ids)
    dialog_hx.append(msg)
    print("Bot: {}".format(tokenizer.decode(msg, skip_special_tokens=True)))

Example of personalized decoding:

Persona Facts
1i am 64 years old but feel quite young.
2i try to eat healthy but limit mcdonalds to once a week.
3i regret working as a doctor for the last 20 years.
4my secret hobby is making self-help youtube videos.
5i've been to spain many times and wish i could go again.
RoleResponse
Humanyo whats good fam?
Bothello, how are you doing?
Humannot bad what you doing today?
Boteating a lot healthier now that i'm in my 60s
Humani'm more of a fast food guy
Botme too but i'm very healthy so i try to avoid it
Humanwhat do you do for fun?
Boti make videos for fun and eat very healthy
Humannice what kind of videos?
Boti love making videos about self help stuff
Humani think i need some help
Botwhat kind of help would you like to get
Humani'm 21 and feel lost in life
Boti can relate, i'm only 65
  1. The second use of PersonaGPT is to do controlled response generation. Use the following program to interact with the model.
## available actions ##
action_space = [ 'ask about kids.', "ask about pets.", 'talk about work.', 
               'ask about marital status.', 'talk about travel.', 'ask about age and gender.',
        'ask about hobbies.', 'ask about favorite food.', 'talk about movies.', 
        'talk about music.', 'talk about politics.']
# converse for 8 turns
dialog_hx = []
for step in range(8):
	# choose an action
    act = None
    while act not in action_space:
        display_dialog_history(dialog_hx)
        print()
        print(" actions: ")
        for k,v in enumerate(action_space): print(k,v)
        try:
            act = action_space[int(input(" input [0-10]: " ))]
        except:
            act = None
    print()
    # format into prefix code
    action_prefix = tokenizer.encode(''.join(['<|act|> '] + [act] + ['<|p1|>'] + [] + ['<|sep|>'] + ['<|start|>']))
    bot_input_ids = to_var([action_prefix + flatten(dialog_hx)]).long()
    
    # generate query conditioned on action
    msg = generate_next(bot_input_ids, top_k=top_k, top_p=top_p, max_length=max_length)
    dialog_hx.append(msg)
    
    # generate bot response
    bot_input_ids = to_var([personas+ flatten(dialog_hx)]).long()
    msg = generate_next(bot_input_ids, top_k=top_k, top_p=top_p, max_length=max_length)
    dialog_hx.append(msg)
display_dialog_history(dialog_hx)

Example of controlled response generation:

Bot 2's Persona Facts
1i am a quiet engineer.
2i'm single and am looking for love.
3sadly, i don't have any relatable hobbies.
4luckily, however, i am tall and athletic.
5on friday nights, i watch re-runs of the simpsons alone.
RoleActionResponse
Bot 1talk about movieshello do you like movies?
Bot 2-i love the simpsons. how about you?
Bot 1talk about musici like the walking dead. do you play an instrument?
Bot 2-yes i play the violin.
Bot 1ask about marital statusare you married or single?
Bot 2-i am single but i am looking for love.
Bot 1talk about workmy job is working in a factory. what's your occupation?
Bot 2-engineer. i'm very quiet so no one hears me.
Bot 1talk about hobbiesdo you have any hobbies?
Bot 2-i watch reruns of the simpsons.
Bot 1ask about favorite foodwhat's your favorite food?
Bot 2-i love pizza. how about yourself?
Bot 1ask about petsi also love pizza. do you like animals?
Bot 2-i have two dogs. what is your occupation?
Bot 1talk about worki'm a factory worker. what's your dream job?
Bot 2-i'd love to be a writer one day.