dialog_policy
This commit is contained in:
parent
72e17d2106
commit
ee1d7e45d4
24
src/service/convlab/policy/policy.py
Normal file
24
src/service/convlab/policy/policy.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
"""Policy Interface"""
|
||||||
|
from convlab.util.module import Module
|
||||||
|
|
||||||
|
|
||||||
|
class Policy(Module):
|
||||||
|
"""Policy module interface."""
|
||||||
|
|
||||||
|
def predict(self, state):
|
||||||
|
"""Predict the next agent action given dialog state.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
state (dict or list of list):
|
||||||
|
when the policy takes dialogue state as input, the type is dict.
|
||||||
|
else when the policy takes dialogue act as input, the type is list of list.
|
||||||
|
Returns:
|
||||||
|
action (list of list or str):
|
||||||
|
when the policy outputs dialogue act, the type is list of list.
|
||||||
|
else when the policy outputs utterance directly, the type is str.
|
||||||
|
"""
|
||||||
|
return []
|
||||||
|
|
||||||
|
def update_memory(self, utterance_list, state_list, action_list, reward_list):
|
||||||
|
pass
|
||||||
|
|
25
src/service/convlab/util/module.py
Normal file
25
src/service/convlab/util/module.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
"""module interface."""
|
||||||
|
from abc import ABC
|
||||||
|
|
||||||
|
|
||||||
|
class Module(ABC):
|
||||||
|
|
||||||
|
def train(self, *args, **kwargs):
|
||||||
|
"""Model training entry point"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test(self, *args, **kwargs):
|
||||||
|
"""Model testing entry point"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def from_cache(self, *args, **kwargs):
|
||||||
|
"""restore internal state for multi-turn dialog"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
def to_cache(self, *args, **kwargs):
|
||||||
|
"""save internal state for multi-turn dialog"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
def init_session(self):
|
||||||
|
"""Init the class variables for a new session."""
|
||||||
|
pass
|
38
src/service/convlab/util/restaurant/dbquery.py
Normal file
38
src/service/convlab/util/restaurant/dbquery.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
"""
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
from fuzzywuzzy import fuzz
|
||||||
|
from itertools import chain
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
class Database(object):
|
||||||
|
def __init__(self):
|
||||||
|
super(Database, self).__init__()
|
||||||
|
# loading databases
|
||||||
|
domains = ['menu', 'pizza', 'drink', 'size']
|
||||||
|
self.dbs = {}
|
||||||
|
for domain in domains:
|
||||||
|
with open(os.path.join(os.path.dirname(
|
||||||
|
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))),
|
||||||
|
'data/restaurant/db/{}_db.json'.format(domain))) as f:
|
||||||
|
self.dbs[domain] = json.load(f)
|
||||||
|
|
||||||
|
def query(self, domain):
|
||||||
|
"""Returns the list of entities for a given domain
|
||||||
|
based on the annotation of the belief state"""
|
||||||
|
# query the db
|
||||||
|
if domain == 'pizza':
|
||||||
|
return [{'Name': random.choice(self.dbs[domain]['name'])}]
|
||||||
|
if domain == 'menu':
|
||||||
|
return deepcopy(self.dbs[domain])
|
||||||
|
if domain == 'drink':
|
||||||
|
return [{'Name': random.choice(self.dbs[domain]['name'])}]
|
||||||
|
if domain == 'size':
|
||||||
|
return [{'Size': random.choice(self.dbs[domain]['size'])}]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
db = Database()
|
4
src/service/data/restaurant/db/confirm_db.json
Normal file
4
src/service/data/restaurant/db/confirm_db.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[
|
||||||
|
"true",
|
||||||
|
"false"
|
||||||
|
]
|
5
src/service/data/restaurant/db/dough_db.json
Normal file
5
src/service/data/restaurant/db/dough_db.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[
|
||||||
|
"pepsi",
|
||||||
|
"cola",
|
||||||
|
"water"
|
||||||
|
]
|
11
src/service/data/restaurant/db/drink_db.json
Normal file
11
src/service/data/restaurant/db/drink_db.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"name":"pepsi"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name":"cola"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name":"water"
|
||||||
|
}
|
||||||
|
]
|
3
src/service/data/restaurant/db/food_db.json
Normal file
3
src/service/data/restaurant/db/food_db.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[
|
||||||
|
"pizza"
|
||||||
|
]
|
5
src/service/data/restaurant/db/meat_db.json
Normal file
5
src/service/data/restaurant/db/meat_db.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[
|
||||||
|
"chicken",
|
||||||
|
"ham",
|
||||||
|
"tuna"
|
||||||
|
]
|
7
src/service/data/restaurant/db/menu_db.json
Normal file
7
src/service/data/restaurant/db/menu_db.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[
|
||||||
|
"capri",
|
||||||
|
"margarita",
|
||||||
|
"hawajska",
|
||||||
|
"barcelona",
|
||||||
|
"tuna"
|
||||||
|
]
|
51
src/service/data/restaurant/db/pizza_db.json
Normal file
51
src/service/data/restaurant/db/pizza_db.json
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "capri",
|
||||||
|
"ingredient": [
|
||||||
|
"tomato",
|
||||||
|
"ham",
|
||||||
|
"mushrooms",
|
||||||
|
"cheese"
|
||||||
|
],
|
||||||
|
"price": 25
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "margarita",
|
||||||
|
"ingredient": [
|
||||||
|
"tomato",
|
||||||
|
"cheese"
|
||||||
|
],
|
||||||
|
"price": 20
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "hawajska",
|
||||||
|
"ingredient": [
|
||||||
|
"tomato",
|
||||||
|
"pineapple",
|
||||||
|
"chicken",
|
||||||
|
"cheese"
|
||||||
|
],
|
||||||
|
"price": 30
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "barcelona",
|
||||||
|
"ingredient": [
|
||||||
|
"tomato",
|
||||||
|
"onion",
|
||||||
|
"ham",
|
||||||
|
"pepper",
|
||||||
|
"cheese"
|
||||||
|
],
|
||||||
|
"price": 40
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tuna",
|
||||||
|
"ingredient": [
|
||||||
|
"tomato",
|
||||||
|
"tuna",
|
||||||
|
"onion",
|
||||||
|
"cheese"
|
||||||
|
],
|
||||||
|
"price": 40
|
||||||
|
}
|
||||||
|
]
|
4
src/service/data/restaurant/db/sauce_db.json
Normal file
4
src/service/data/restaurant/db/sauce_db.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[
|
||||||
|
"garlic",
|
||||||
|
"1000w"
|
||||||
|
]
|
14
src/service/data/restaurant/db/size_db.json
Normal file
14
src/service/data/restaurant/db/size_db.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"size": "m",
|
||||||
|
"price_multiplier": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"size": "l",
|
||||||
|
"price_multiplier": 1.2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"size": "xl",
|
||||||
|
"price_multiplier": 1.4
|
||||||
|
}
|
||||||
|
]
|
@ -1,8 +1,59 @@
|
|||||||
from model.frame import Frame
|
from collections import defaultdict
|
||||||
|
import copy
|
||||||
|
import json
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
class DialogPolicy:
|
from convlab.policy.policy import Policy
|
||||||
def next_dialogue_act(self, frames: list[Frame]) -> Frame:
|
from convlab.util.restaurant.dbquery import Database
|
||||||
if frames[-1].act == "welcomemsg":
|
|
||||||
return Frame("system", "welcomemsg", [])
|
class SimpleRulePolicy(Policy):
|
||||||
else:
|
def __init__(self):
|
||||||
return Frame("system", "canthelp", [])
|
Policy.__init__(self)
|
||||||
|
self.db = Database()
|
||||||
|
|
||||||
|
def predict(self, state):
|
||||||
|
self.results = []
|
||||||
|
system_action = defaultdict(list)
|
||||||
|
user_action = defaultdict(list)
|
||||||
|
|
||||||
|
for intent, domain, slot, value in state['user_action']:
|
||||||
|
user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))
|
||||||
|
|
||||||
|
for user_act in user_action:
|
||||||
|
self.update_system_action(user_act, user_action, state, system_action)
|
||||||
|
|
||||||
|
# Reguła 3
|
||||||
|
if any(True for slots in user_action.values() for (slot, _) in slots if slot in ['pizza', 'size', 'drink']):
|
||||||
|
if self.results:
|
||||||
|
system_action = {('Ordering', 'Order'): [["Ref", self.results[0].get('Ref', 'N/A')]]}
|
||||||
|
|
||||||
|
system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]
|
||||||
|
state['system_action'] = system_acts
|
||||||
|
return system_acts
|
||||||
|
|
||||||
|
def update_system_action(self, user_act, user_action, state, system_action):
|
||||||
|
domain, intent = user_act
|
||||||
|
constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']
|
||||||
|
self.results = deepcopy(self.db.query(domain.lower(), constraints))
|
||||||
|
|
||||||
|
# Reguła 1
|
||||||
|
if intent == 'request':
|
||||||
|
if len(self.results) == 0:
|
||||||
|
system_action[(domain, 'NoOffer')] = []
|
||||||
|
else:
|
||||||
|
for slot in user_action[user_act]:
|
||||||
|
if slot[0] in self.results[0]:
|
||||||
|
system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])
|
||||||
|
|
||||||
|
# Reguła 2
|
||||||
|
elif intent == 'inform':
|
||||||
|
if len(self.results) == 0:
|
||||||
|
system_action[(domain, 'NoOffer')] = []
|
||||||
|
else:
|
||||||
|
system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])
|
||||||
|
choice = self.results[0]
|
||||||
|
|
||||||
|
if domain in ["pizza", "drink"]:
|
||||||
|
system_action[(domain, 'Recommend')].append(['Name', choice['name']])
|
||||||
|
if domain in ["size"]:
|
||||||
|
system_action[(domain, 'Recommend')].append(['Size', choice['size']])
|
Loading…
Reference in New Issue
Block a user