2023-05-25 12:42:01 +02:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 202,
|
2023-05-25 12:42:01 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import json\n",
|
|
|
|
"import os\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"class DST():\n",
|
|
|
|
" def __init__(self):\n",
|
|
|
|
" self.state = json.load(open('dictionary.json'))\n",
|
|
|
|
"\n",
|
|
|
|
" def update(self, user_act=None):\n",
|
|
|
|
" for intent, domain, slot, value in user_act:\n",
|
|
|
|
" domain = domain.lower()\n",
|
|
|
|
" intent = intent.lower()\n",
|
|
|
|
" slot = slot.lower()\n",
|
|
|
|
" \n",
|
|
|
|
" if domain not in self.state['belief_state']:\n",
|
|
|
|
" continue\n",
|
|
|
|
"\n",
|
|
|
|
" if intent == 'inform':\n",
|
|
|
|
" if slot == 'none' or slot == '':\n",
|
|
|
|
" continue\n",
|
|
|
|
"\n",
|
|
|
|
" domain_dic = self.state['belief_state'][domain]\n",
|
|
|
|
"\n",
|
|
|
|
" if slot in domain_dic:\n",
|
|
|
|
" self.state['belief_state'][domain][slot] = value\n",
|
|
|
|
"\n",
|
|
|
|
" elif intent == 'request':\n",
|
|
|
|
" if domain not in self.state['request_state']:\n",
|
|
|
|
" self.state['request_state'][domain] = {}\n",
|
|
|
|
" if slot not in self.state['request_state'][domain]:\n",
|
|
|
|
" self.state['request_state'][domain][slot] = 0\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" \n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" self.state['user_act'] = user_act\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
" return self.state\n",
|
|
|
|
" def init_session(self):\n",
|
|
|
|
" self.state = json.load(open('dictionary.json'))"
|
2023-05-25 12:42:01 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 203,
|
2023-05-25 12:42:01 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"dst = DST()\n",
|
2023-05-25 15:37:55 +02:00
|
|
|
"user_act = [('inform', 'payment', 'type', 'karta'), ('inform', 'delivery', 'type','paczkomat'), ('inform', 'product', 'type', 'telefon'), ('request', 'product', 'type', '?')]\n",
|
2023-05-25 12:42:01 +02:00
|
|
|
"state = dst.update(user_act)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 204,
|
2023-05-25 12:42:01 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2023-05-25 16:22:48 +02:00
|
|
|
"{'payment': {'type': 'karta', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''}, 'product': {'name': '', 'type': 'telefon', 'brand': '', 'price_range': '', 'price': '', 'quantity': '', 'quality': ''}}\n",
|
2023-05-25 15:37:55 +02:00
|
|
|
"{'product': {'type': 0}}\n"
|
2023-05-25 12:42:01 +02:00
|
|
|
]
|
2023-05-25 13:40:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-05-25 14:18:57 +02:00
|
|
|
"{'user_act': [('inform', 'payment', 'type', 'karta'),\n",
|
|
|
|
" ('inform', 'delivery', 'type', 'paczkomat'),\n",
|
|
|
|
" ('inform', 'product', 'type', 'telefon'),\n",
|
2023-05-25 15:37:55 +02:00
|
|
|
" ('request', 'product', 'type', '?')],\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" 'system_act': [],\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" 'belief_state': {'payment': {'type': 'karta',\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" 'amount': '',\n",
|
|
|
|
" 'loyalty_card': ''},\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''},\n",
|
|
|
|
" 'product': {'name': '',\n",
|
|
|
|
" 'type': 'telefon',\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" 'brand': '',\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
" 'price_range': '',\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" 'price': '',\n",
|
|
|
|
" 'quantity': '',\n",
|
|
|
|
" 'quality': ''}},\n",
|
2023-05-25 15:37:55 +02:00
|
|
|
" 'request_state': {'product': {'type': 0}},\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
" 'terminated': False,\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" 'history': []}"
|
2023-05-25 13:40:17 +02:00
|
|
|
]
|
|
|
|
},
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 204,
|
2023-05-25 13:40:17 +02:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
2023-05-25 12:42:01 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"print(state['belief_state'])\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
"print(state['request_state'])\n",
|
|
|
|
"dst.state"
|
2023-05-25 12:42:01 +02:00
|
|
|
]
|
2023-05-25 13:32:35 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 223,
|
2023-05-25 13:32:35 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from collections import defaultdict\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
"from convlab.policy.policy import Policy\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
"import json\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
"\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
"class SimpleRulePolicy(Policy):\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
" def __init__(self):\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
" Policy.__init__(self)\n",
|
2023-05-25 14:49:44 +02:00
|
|
|
" self.db = json.load(open('product_db.json'))\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
"\n",
|
|
|
|
" def predict(self, state):\n",
|
|
|
|
" self.results = []\n",
|
|
|
|
" system_action = defaultdict(list)\n",
|
|
|
|
" user_action = defaultdict(list)\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" system_acts = []\n",
|
|
|
|
" for intent, domain, slot, value in state['user_act']:\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
" user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))\n",
|
|
|
|
" for user_act in user_action:\n",
|
|
|
|
" self.update_system_action(user_act, user_action, state, system_action)\n",
|
|
|
|
" system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
" state['system_act'] = system_acts\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
" return system_acts\n",
|
|
|
|
"\n",
|
2023-05-25 14:18:57 +02:00
|
|
|
"\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
" def update_system_action(self, user_act, user_action, state, system_action):\n",
|
|
|
|
" domain, intent = user_act\n",
|
2023-05-25 15:06:14 +02:00
|
|
|
" constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']\n",
|
2023-05-25 14:49:44 +02:00
|
|
|
" self.results = self.db['database'][domain]\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
"\n",
|
|
|
|
" # Reguła 1\n",
|
|
|
|
" if intent == 'request':\n",
|
|
|
|
" if len(self.results) == 0:\n",
|
|
|
|
" system_action[(domain, 'NoOffer')] = []\n",
|
|
|
|
" else:\n",
|
2023-05-25 14:49:44 +02:00
|
|
|
" for slot in user_action[user_act]:\n",
|
|
|
|
" if self.results and slot[0] in self.results[0]:\n",
|
2023-05-25 13:32:35 +02:00
|
|
|
" system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])\n",
|
|
|
|
"\n",
|
|
|
|
" # Reguła 2\n",
|
|
|
|
" elif intent == 'inform':\n",
|
|
|
|
" if len(self.results) == 0:\n",
|
|
|
|
" system_action[(domain, 'NoOffer')] = []\n",
|
|
|
|
" else:\n",
|
|
|
|
" system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
" for product in self.results:\n",
|
|
|
|
" if all(product.get(slot, '').lower() == value.lower() for slot, value in constraints):\n",
|
|
|
|
" system_action[(domain, 'Recommend')].append(['Name', product['name']])\n",
|
|
|
|
" break\n",
|
2023-05-25 15:37:38 +02:00
|
|
|
"\n"
|
2023-05-25 13:32:35 +02:00
|
|
|
]
|
2023-05-25 13:40:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 224,
|
2023-05-25 13:40:17 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-05-25 16:22:48 +02:00
|
|
|
"[['Inform', 'product', 'Choice', '11'],\n",
|
|
|
|
" ['Recommend', 'product', 'Name', 'RedBull']]"
|
2023-05-25 13:40:17 +02:00
|
|
|
]
|
|
|
|
},
|
2023-05-25 16:22:48 +02:00
|
|
|
"execution_count": 224,
|
2023-05-25 13:40:17 +02:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2023-05-25 15:37:55 +02:00
|
|
|
"dst = DST()\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
"user_act = [('inform', 'product', 'type', 'energol')]\n",
|
2023-05-25 15:37:55 +02:00
|
|
|
"state = dst.update(user_act)\n",
|
2023-05-25 13:40:17 +02:00
|
|
|
"policy = SimpleRulePolicy()\n",
|
2023-05-25 16:22:48 +02:00
|
|
|
"policy.predict(state)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 225,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stderr",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"WARNING:root:nlu info_dict is not initialized\n",
|
|
|
|
"WARNING:root:dst info_dict is not initialized\n",
|
|
|
|
"WARNING:root:policy info_dict is not initialized\n",
|
|
|
|
"WARNING:root:nlg info_dict is not initialized\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"from convlab.dialog_agent import PipelineAgent\n",
|
|
|
|
"policy = SimpleRulePolicy()\n",
|
|
|
|
"agent = PipelineAgent(nlu=None, dst=dst, policy=policy, nlg=None, name='sys')"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 228,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"[['Inform', 'product', 'Choice', '11'],\n",
|
|
|
|
" ['Recommend', 'product', 'Name', 'pomidor']]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 228,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"agent.response([('inform', 'product', 'type', 'warzywo'), ('inform', 'product', 'price_range', 'tani'), ('inform', 'product', 'quality', 'exquisite')])"
|
2023-05-25 13:40:17 +02:00
|
|
|
]
|
2023-05-25 12:42:01 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "py38",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
"version": "3.8.16"
|
|
|
|
},
|
|
|
|
"orig_nbformat": 4
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|