Compare commits

...

6 Commits

Author SHA1 Message Date
09157ce36f Change requirements 2024-06-09 15:58:01 +02:00
1cd62db966 dialog_policy 2024-06-09 15:57:31 +02:00
d789fcc10b Dodanie normalizacji do DSM 2024-06-07 15:51:02 +02:00
adfea71871 Dodanie normalizacji do DSM 2024-06-07 15:49:41 +02:00
d02bd4b5ae Poprawa oznaczeń dialogów 2024-06-07 12:53:38 +02:00
b64eb49ba0 Implementacja DSM wraz z testami 2024-06-07 12:49:41 +02:00
21 changed files with 336 additions and 27 deletions

View File

@ -1,9 +1,6 @@
{
"addr": null,
"confirm": [
"true",
"false"
],
"address": null,
"order-complete": false,
"dough": ["thick"],
"drink": ["pepsi", "cola", "water"],
"food": ["pizza"],

View File

@ -2,7 +2,7 @@ kto treść akt
user Dzień dobry, chciałbym zamówić pizzę. request(menu)
system Dzień dobry,\nOferujemy pizze:\nmargharita\ncapri\nhawajska\nbarcelona\ntuna\nWystępują w rozmiarach M (31cm), L (41cm) , XL (52cm) inform(menu)
user jaka to jest pizza barcelona? request(barcelona)
system Pizza barcelona posiada: pomidory, cebulę i szynkę. inform(pizza.info=barcelona)
system Pizza barcelona posiada: pomidory, cebulę i szynkę. inform(pizza=barcelona)
user to poproszę inform(pizza=barcelona)
system Rozumiem.\nCałość zamówienia to:\npizza barcelona L (szynka parmeńska) - 45ł\nPotwierdza Pan zamówienie? inform(pizza=barcelona,size=l,price=45)
user tak, odbiorę na miejscu inform(collection-method=pickup)

1 kto treść akt
2 user Dzień dobry, chciałbym zamówić pizzę. request(menu)
3 system Dzień dobry,\nOferujemy pizze:\nmargharita\ncapri\nhawajska\nbarcelona\ntuna\nWystępują w rozmiarach M (31cm), L (41cm) , XL (52cm) inform(menu)
4 user jaka to jest pizza barcelona? request(barcelona)
5 system Pizza barcelona posiada: pomidory, cebulę i szynkę. inform(pizza.info=barcelona) inform(pizza=barcelona)
6 user to poproszę inform(pizza=barcelona)
7 system Rozumiem.\nCałość zamówienia to:\npizza barcelona L (szynka parmeńska) - 45ł\nPotwierdza Pan zamówienie? inform(pizza=barcelona,size=l,price=45)
8 user tak, odbiorę na miejscu inform(collection-method=pickup)

View File

@ -4,7 +4,7 @@ system Dzień dobry. Jestem wirtualnym asystentem restauracji 'pizzeria'.\nOferu
user Z czym jest pizza barcelona? request(barcelona)
system Pizza barcelona posiada: pomidory, cebulę i szynkę. inform(pizza=barcelona,ingredient=tomato,ingredient=onion,ingredient=ham)
user Jakie dodatki można dodać do tej pizzy? request(additional_ingredient)
system Do każdej pizzy istnieje ten sam zestaw dodatków są to: szynka, kawałki kurczaka, papryczka jalapeno, czosnek, karczoch, pomidor, papryka czerwona, ser, cebula, mięta inform(additional_ingredient.info)
system Do każdej pizzy istnieje ten sam zestaw dodatków są to: szynka, kawałki kurczaka, papryczka jalapeno, czosnek, karczoch, pomidor, papryka czerwona, ser, cebula, mięta inform(additional_ingredient)
user To ja chętnie zamówię pizzę barcelona z dodatkiem świeżej mięty request(pizza=barcelona,pizza.ingredient=mint)
system Życzy Pan sobie do tego jakiś napój? offer(drink)
user A jakie so? request(drink)

1 kto treść akt
4 user Z czym jest pizza barcelona? request(barcelona)
5 system Pizza barcelona posiada: pomidory, cebulę i szynkę. inform(pizza=barcelona,ingredient=tomato,ingredient=onion,ingredient=ham)
6 user Jakie dodatki można dodać do tej pizzy? request(additional_ingredient)
7 system Do każdej pizzy istnieje ten sam zestaw dodatków są to: szynka, kawałki kurczaka, papryczka jalapeno, czosnek, karczoch, pomidor, papryka czerwona, ser, cebula, mięta inform(additional_ingredient.info) inform(additional_ingredient)
8 user To ja chętnie zamówię pizzę barcelona z dodatkiem świeżej mięty request(pizza=barcelona,pizza.ingredient=mint)
9 system Życzy Pan sobie do tego jakiś napój? offer(drink)
10 user A jakie so? request(drink)

View File

@ -1,5 +1,7 @@
flair==0.13.1
conllu==4.5.3
pandas==1.5.3
numpy==1.26.4
torch==2.3.0
convlab==3.0.2a0
numpy==1.24.4
fuzzywuzzy==0.18.0

View File

@ -18,10 +18,10 @@ while True:
# print(frame)
# DSM
# monitor.append(frame)
monitor.update(frame)
# DP
# print(dialog_policy.next_dialogue_act(monitor.get_all()).act)
# print(dialog_policy.next_dialogue_act(monitor.read()).act)
# NLG
act, slots = parse_frame(frame)

View 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

View 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

View 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()

View File

@ -0,0 +1,4 @@
[
"true",
"false"
]

View File

@ -0,0 +1,5 @@
[
"pepsi",
"cola",
"water"
]

View File

@ -0,0 +1,11 @@
[
{
"name":"pepsi"
},
{
"name":"cola"
},
{
"name":"water"
}
]

View File

@ -0,0 +1,3 @@
[
"pizza"
]

View File

@ -0,0 +1,5 @@
[
"chicken",
"ham",
"tuna"
]

View File

@ -0,0 +1,7 @@
[
"capri",
"margarita",
"hawajska",
"barcelona",
"tuna"
]

View 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
}
]

View File

@ -0,0 +1,4 @@
[
"garlic",
"1000w"
]

View File

@ -0,0 +1,14 @@
[
{
"size": "m",
"price_multiplier": 1
},
{
"size": "l",
"price_multiplier": 1.2
},
{
"size": "xl",
"price_multiplier": 1.4
}
]

View File

@ -1,8 +1,61 @@
from model.frame import Frame
from collections import defaultdict
import copy
import json
from copy import deepcopy
class DialogPolicy:
def next_dialogue_act(self, frames: list[Frame]) -> Frame:
if frames[-1].act == "welcomemsg":
return Frame("system", "welcomemsg", [])
else:
return Frame("system", "canthelp", [])
from convlab.policy.policy import Policy
from convlab.util.restaurant.dbquery import Database
class SimpleRulePolicy(Policy):
def __init__(self):
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']])
dialogPolicy = SimpleRulePolicy()

View File

@ -1,14 +1,60 @@
from model.frame import Frame
from src.model.frame import Frame
from convlab.dst.dst import DST
import json
import copy
class DialogStateMonitor:
dialog = []
def normalize(value):
value = value.lower()
return ' '.join(value.split())
def append(self, frame: Frame):
self.dialog.append(frame)
def get_all(self) -> [Frame]:
return self.dialog
class DialogStateMonitor(DST):
def __init__(self, initial_state_file: str = '../attributes.json'):
DST.__init__(self)
with open(initial_state_file) as file:
self.__initial_state = json.load(file)
self.__memory = copy.deepcopy(self.__initial_state)
def get_last(self) -> Frame:
return self.dialog[len(self.dialog) - 1]
# def __access_memory__(self, path: str) -> str | int | float | None:
# result = self.state['memory']
# for segment in path.split('.'):
# if segment not in result:
# return None
# result = result[segment]
# return result
def update(self, frame: Frame):
if frame.source != 'user':
return
if frame.act == 'inform/order':
new_order = dict()
for slot in frame.slots:
new_order[slot.name] = normalize(slot.value)
self.__memory['order'].append(new_order)
elif frame.act == 'inform/address':
for slot in frame.slots:
self.__memory['address'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/phone':
for slot in frame.slots:
self.__memory['phone'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/order-complete':
self.__memory['order-complete'] = True
elif frame.act == 'inform/delivery':
for slot in frame.slots:
self.__memory['delivery'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/payment':
for slot in frame.slots:
self.__memory['payment'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/time':
for slot in frame.slots:
self.__memory['time'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/name':
for slot in frame.slots:
self.__memory['name'][slot.name] = normalize(slot.value)
def read(self) -> dict:
return self.__memory
def reset(self):
self.__memory = copy.deepcopy(self.__initial_state)

0
src/test/__init__.py Normal file
View File

View File

@ -0,0 +1,20 @@
from src.service.dialog_state_monitor import DialogStateMonitor
from src.model.frame import Frame
from src.model.slot import Slot
dst = DialogStateMonitor()
assert dst.read()['pizza']['capri']['price'] == 25
dst.update(Frame('user', 'inform/order', [Slot('B-pizza', 'margaritta'), Slot('B-sauce', 'ketchup')]))
dst.update(Frame('user', 'inform/order', [Slot('B-pizza', 'carbonara')]))
dst.update(Frame('user', 'inform/order-complete', []))
assert dst.read()['order'][0]['B-pizza'] == 'margaritta'
assert dst.read()['order'][0]['B-sauce'] == 'ketchup'
assert dst.read()['order-complete'] == True
dst.reset()
assert dst.read()['order'] == []
assert dst.read()['order-complete'] == False