This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogflow.py
90 lines (77 loc) · 3.13 KB
/
dialogflow.py
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
import os
from uuid import uuid4
from dialogflow_v2beta1 import SessionsClient, KnowledgeBasesClient, DocumentsClient
from dialogflow_v2beta1 import types, enums
from google.api_core.exceptions import InvalidArgument, GoogleAPICallError
from google.api_core.retry import Retry
from util import realpath
from config import project_id
EXTRACTIVE_QA = [enums.Document.KnowledgeType.EXTRACTIVE_QA]
_account = realpath('account.json')
if os.path.isfile(_account):
session = SessionsClient.from_service_account_json(_account)
kb = KnowledgeBasesClient.from_service_account_json(_account)
docs = DocumentsClient.from_service_account_json(_account)
else:
session = SessionsClient()
kb = KnowledgeBasesClient()
docs = DocumentsClient()
class KnowledgeBase:
def __init__(self, id):
if isinstance(id, types.KnowledgeBase):
self._path = id.name
self.caption = id.display_name
else:
self._path = kb.knowledge_base_path(project_id, str(id))
self.caption = kb.get_knowledge_base(self._path).display_name
def __iter__(self):
yield from docs.list_documents(self._path)
def create(self, caption, text=None):
if text is None:
caption, text = caption
doc = types.Document(
display_name=caption, mime_type='text/plain',
knowledge_types=EXTRACTIVE_QA, content=text)
try:
return docs.create_document(self._path, doc).result()
except (InvalidArgument, GoogleAPICallError):
res = [d for d in self if d.display_name == caption]
return res[0] if res else None
class Dialogflow:
def __init__(self, session_id=uuid4(), language_code='en'):
self.session_id = session_id
self._session = session.session_path(project_id, session_id)
self._kb = kb.project_path(project_id)
self.language_code = language_code
self.min_confidence = 0.8
self._retry = {
'retry': Retry(),
'timeout': 10
}
def __call__(self, text=None, event=None):
language_code = self.language_code
if text is not None:
text_input = types.TextInput(text=text, language_code=language_code)
query_input = types.QueryInput(text=text_input)
elif event is not None:
event_input = types.EventInput(name=event, language_code=language_code)
query_input = types.QueryInput(event=event_input)
else:
return None
return session.detect_intent(session=self._session, query_input=query_input, **self._retry)
def get_answers(self, text, kb=True):
res = self(text=text)
if not hasattr(res.query_result, 'knowledge_answers'):
return
if not kb and res.alternative_query_results:
answer = res.alternative_query_results[0]
if answer.action != 'input.unknown' and answer.intent_detection_confidence >= self.min_confidence:
return [answer.fulfillment_text]
return None
return res.query_result.knowledge_answers.answers
def event(self, name, raw=False):
res = self(event=name)
return res if raw else res.query_result.fulfillment_text
def __iter__(self):
for item in kb.list_knowledge_bases(self._kb):
yield KnowledgeBase(item)