[507a54]: / production / action-server / actions / processors.py

Download this file

91 lines (76 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from abc import ABC, abstractmethod
from decouple import config
import requests
import json
from .utils import get_processor_conf
class Processor(ABC):
'''
Meta class for processors.
'''
@abstractmethod
def __init__(self, host=None):
pass
@abstractmethod
def process(self, tracker, context):
pass
class NoProcessor(Processor):
'''
A processor that doesn't change the input context.
'''
def __init__(self):
pass
def process(self, tracker, context):
return context
class QAProcessor(Processor):
'''
Finds the answer of question in given context.
'''
def __init__(self, host):
self.host = host
self.api_key = config('QA_API_KEY')
self.headers = {'Content-Type': 'application/json'}
def process(self, tracker, context):
question = tracker.latest_message['text']
payload = json.dumps({
"question": question,
"context": context,
"api_key": self.api_key,
})
response = requests.request("POST",
self.host,
headers=self.headers,
data=payload
)
response = response.json()['answer']
return response
class SummarizerProcessor(Processor):
'''
Summarizes given context.
'''
def __init__(self, host):
self.host = host
self.api_key = config('SUMMARIZER_API_KEY')
self.headers = {'Content-Type': 'application/json'}
def process(self, tracker, context):
payload = json.dumps({
"context": context,
"api_key": self.api_key,
})
response = requests.request("POST",
self.host,
headers=self.headers,
data=payload
)
response = response.json()['summary']
return response
def create_processor():
'''
Returns a processor object based on config file.
'''
conf = get_processor_conf()
if conf['type']=='no_process':
return NoProcessor()
elif conf['type']=='QA':
return QAProcessor(conf['host_url'])
elif conf['type']=='summarizer':
return SummarizerProcessor(conf['host_url'])