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

Multi-agent chat first pass #264

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ yarn-debug.log*
yarn-error.log*

*/src/config.js
**/_generated/**
71 changes: 71 additions & 0 deletions crowdsourcing/dialogues/multi_party_chat/frontend/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import React from "react";
import ReactDOM from "react-dom";
import "bootstrap-chat/styles.css";

import { ChatApp, ChatMessage, DefaultTaskDescription } from "bootstrap-chat";

function RenderChatMessage({ message, mephistoContext, appContext, idx }) {
const { agentId } = mephistoContext;
const { currentAgentNames } = appContext.taskContext;

return (
<div>
<ChatMessage
isSelf={message.id === agentId || message.id in currentAgentNames}
agentName={
message.id in currentAgentNames
? currentAgentNames[message.id]
: message.id
}
message={message.text}
taskData={message.task_data}
messageId={message.message_id}
/>
</div>
);
}

function MainApp() {
return (
<ChatApp
renderMessage={({ message, idx, mephistoContext, appContext }) => (
<RenderChatMessage
message={message}
mephistoContext={mephistoContext}
appContext={appContext}
idx={idx}
key={message.message_id + "-" + idx}
/>
)}
defaultAppSettings={{volume: 0}}
renderSidePane={({ appContext, mephistoContext: { taskConfig } }) => (
<DefaultTaskDescription
chatTitle={taskConfig.chat_title}
taskDescriptionHtml={taskConfig.task_description}
>
<b>You've been assigned: </b>
{appContext.taskContext?.persona?.name}
<p>
<b>Your Persona: </b>
{appContext.taskContext?.persona?.persona}
</p>
<b>You are in: </b>
{appContext.taskContext?.location?.name}
<p>
{appContext.taskContext?.location?.description}
</p>
</DefaultTaskDescription>
)}
/>
);
}

ReactDOM.render(<MainApp />, document.getElementById("app"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#@package _global_
mephisto:
blueprint:
world_file: ${task_dir}/worlds.py
task_description_file: ${task_dir}/task_description.html
onboarding_qualification: multi-party-chat-qualification
custom_source_dir: ${task_dir}/frontend
num_conversations: 2
task:
task_name: light-multi-party-local
task_title: "Multi-Party Fantasy Chat"
task_description: >
In this task you will have a conversation with two other people
in a fantasy setting. You will all be given characters, and should
have a conversation about your assigned characters and the setting,
taking natural turns.
task_reward: 1.80
task_tags: "dynamic,chat,testing,fantasy,role-playing"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#@package _global_
mephisto:
blueprint:
world_file: ${task_dir}/worlds.py
task_description_file: ${task_dir}/task_description.html
onboarding_qualification: light-multi-party-chat-qualification
blocking_qualification: light-multi-party-chat-block
custom_source_dir: ${task_dir}/frontend
num_conversations: 10
task:
task_name: light-multi-party-chat-pilot
task_title: "Multi-Party Fantasy Chat"
task_description: >
In this task you will have a conversation with two other people
in a fantasy setting. You will all be given characters, and should
have a conversation about your assigned characters and the setting,
taking natural turns.
task_reward: 1.80
max_num_concurrent_units: 15
task_tags: "dynamic,chat,testing,fantasy,role-playing"
assignment_duration_in_seconds: 2400
database:
_database_type: singleton
qualify_new_workers: true
56 changes: 56 additions & 0 deletions crowdsourcing/dialogues/multi_party_chat/review_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from mephisto.abstractions.databases.local_database import LocalMephistoDB
from mephisto.tools.examine_utils import run_examine_or_review, print_results
from mephisto.data_model.worker import Worker
from light.colors import Colors as C

db = LocalMephistoDB()


def format_data_for_printing(data):
messages = data['data']['messages']
main_actor = data['data']['agent_name']
context = messages[0]['task_data']
first_message_time = messages[0]['timestamp']
location = context['location']
loc_text = f"{C.BOLD_BLUE}{location['name']}{C.RESET}: {location['description']}"
character = context['persona']
char_text = f"{C.BOLD_GREEN}{character['name']}{C.RESET}: {character['persona']}"
messages = messages[1:]

def format_message(message):
color = C.BOLD_GREEN if message['id'].lower() == character['name'].lower() else C.RESET
return f"{color}{message['id']}{C.RESET}: {message['text']}"


formatted_messages = [format_message(m) for m in messages]
message_text = "\n".join(formatted_messages)

player_messages = [m for m in messages if m['id'].lower() == character['name'].lower()]
message_count = len(player_messages)
avg_message_len = sum([len(m['text'].split()) for m in player_messages])/message_count
timestamps = [0] + [m['timestamp'] - first_message_time for m in player_messages]
durations = [timestamps[i+1]-timestamps[i] for i in range(message_count)]
avg_duration = sum(durations)/message_count
min_duration = min(durations)

stats = (
f"Messages: {message_count} - AVG len: {avg_message_len:.2f}\n"
f"AVG duration: {avg_duration:.2f} - min duration: {min_duration:.2f}"
)

return f"{stats}\n{loc_text}\n{char_text}\n{message_text}"


def main():
db = LocalMephistoDB()
run_examine_or_review(db, format_data_for_printing)


if __name__ == '__main__':
main()
113 changes: 113 additions & 0 deletions crowdsourcing/dialogues/multi_party_chat/run_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


import os
from mephisto.operations.operator import Operator
from mephisto.tools.scripts import load_db_and_process_config
from mephisto.abstractions.blueprints.parlai_chat.parlai_chat_blueprint import (
BLUEPRINT_TYPE,
SharedParlAITaskState,
)

from light.graph.builders.one_room_builder import OneRoomChatBuilder
from light.data_model.light_database import LIGHTDatabase
import hydra
from omegaconf import DictConfig
from dataclasses import dataclass, field
from typing import List, Any

TASK_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
LIGHT_DB_PATH = "~/ParlAI/data/LIGHT/merged.db"

hydra_defaults = [
{"mephisto/blueprint": BLUEPRINT_TYPE},
{"mephisto/architect": "local"},
{"mephisto/provider": "mock"},
{"conf": "multi_chat"},
]

from mephisto.operations.hydra_config import RunScriptConfig, register_script_config


@dataclass
class ScriptConfig(RunScriptConfig):
defaults: List[Any] = field(default_factory=lambda: hydra_defaults)
task_dir: str = TASK_DIRECTORY
num_turns: int = field(
default=8,
metadata={
"help": "Number of min turns per worker before a conversation is complete"
},
)
turn_timeout: int = field(
default=300,
metadata={
"help": "Maximum response time before kicking "
"a worker out, default 300 seconds"
},
)
qualify_new_workers: bool = False


register_script_config(name="scriptconfig", module=ScriptConfig)


@hydra.main(config_path="hydra_configs", config_name="scriptconfig")
def main(cfg: DictConfig) -> None:
db, cfg = load_db_and_process_config(cfg)
ldb = LIGHTDatabase(LIGHT_DB_PATH)

world_opt = {
"num_turns": cfg.num_turns,
"turn_timeout": cfg.turn_timeout,
"builder": OneRoomChatBuilder(
ldb=ldb,
opt={
"db_path": LIGHT_DB_PATH,
"model_path": "/checkpoint/light/models",
"suggestion_type": "hybrid",
"hybridity_prob": 0.2,
},
),
}

custom_bundle_path = cfg.mephisto.blueprint.get("custom_source_bundle", None)
if custom_bundle_path is not None:
assert os.path.exists(custom_bundle_path), (
"Must build the custom bundle with `npm install; npm run dev` from within "
f"the {TASK_DIRECTORY}/webapp directory in order to demo a custom bundle "
)
world_opt["send_task_data"] = True

shared_state = SharedParlAITaskState(
world_opt=world_opt, onboarding_world_opt=world_opt
)

if cfg.qualify_new_workers:
shared_state.mturk_specific_qualifications = [
{
"QualificationTypeId": "00000000000000000040",
"Comparator": "GreaterThanOrEqualTo",
"IntegerValues": [1500],
"ActionsGuarded": "DiscoverPreviewAndAccept",
},
{
"QualificationTypeId": "000000000000000000L0",
"Comparator": "GreaterThanOrEqualTo",
"IntegerValues": [95],
"ActionsGuarded": "DiscoverPreviewAndAccept",
},
]

operator = Operator(db)

operator.validate_and_run_config(cfg.mephisto, shared_state)
operator.wait_for_runs_then_shutdown(skip_input=True, log_rate=30)


if __name__ == "__main__":
main()
37 changes: 37 additions & 0 deletions crowdsourcing/dialogues/multi_party_chat/task_description.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h3>What is this task?</h3>
<p>
In this task you will have a conversation with two other people
in a fantasy setting. You will all be given characters, and should
have a conversation about your assigned characters and the setting,
taking natural turns.
</p>
<br />
<h3>What do I talk about?</h3>
<p>
Anything, so long as you remain in character in the medieval fantasy world.
You should try to learn about your partners, or talk about yourself, or
the location you have all been assigned to.
</p>
<br />
<h3>When does the task end?</h3>
<p>
The conversation ends when all characters have exceeded the minimum
number of turns (8). Please don't repeatedly spam messages to hit this
minimum, as this will exclude you from doing future chats.
</p>

<br />
<h3>What NOT to do:</h3>
<ul>
<li>Do not talk about the task itself, or about MTurk</li>
<li>Avoid racism, sexism, hate speech, and other forms of inappropriate conversation. </li>
<li>Avoid <b>real world</b> topics and locations, and instead remain in the medieval fantasy setting.</li>
<li>
Don't direct into conversations where you pretend to take actions
(like "here you go! *gives item*"), stick to strictly chat.
</li>
<li>
Don't idle for too long or you will be disconnected from the chat
and unable to submit.
</li>
</ul>
Loading