Skip to content

Commit

Permalink
types + linting
Browse files Browse the repository at this point in the history
  • Loading branch information
erohmensing committed Dec 31, 2019
1 parent b76a804 commit 4bbb632
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions demo/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
UserUtteranceReverted,
ConversationPaused,
EventType,
UserUttered,
)
from demo.api import MailChimpAPI
from demo.algolia import AlgoliaAPI
Expand Down Expand Up @@ -644,8 +643,7 @@ def run(self, dispatcher, tracker, domain) -> List[EventType]:
return []



def get_last_event_for(tracker, event_type: Text, skip: int = 0) -> Optional[Any]:
def get_last_event_for(tracker, event_type: Text, skip: int = 0) -> Optional[EventType]:
skipped = 0
for e in reversed(tracker.events):
if e.get("event") == event_type:
Expand All @@ -654,6 +652,7 @@ def get_last_event_for(tracker, event_type: Text, skip: int = 0) -> Optional[Any
return e
return None


class ActionDocsSearch(Action):
def name(self):
return "action_docs_search"
Expand All @@ -663,7 +662,8 @@ def run(self, dispatcher, tracker, domain):
# If we're in a TwoStageFallback we need to look back one more user utterance to get the actual text
if search_text == "/technical_question{}":
last_user_event = get_last_event_for(tracker, "user", skip=2)
search_text = last_user_event.get('text')
if last_user_event:
search_text = last_user_event.get("text")

# Search of docs pages
algolia = AlgoliaAPI(
Expand Down Expand Up @@ -695,7 +695,7 @@ def run(self, dispatcher, tracker, domain):
# If we're in a TwoStageFallback we need to look back two more user utterance to get the actual text
if search_text == "/technical_question{}" or search_text == "/deny":
last_user_event = get_last_event_for(tracker, "user", skip=3)
search_text = last_user_event.get('text')
search_text = last_user_event.get("text")

# Search forum
discourse = DiscourseAPI("https://forum.rasa.com/search")
Expand Down
6 changes: 3 additions & 3 deletions demo/algolia.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
from algoliasearch.search_client import SearchClient
from typing import Text, List


class AlgoliaAPI(object):
"""Class to connect to the Algolia API"""

def __init__(self, app_id: Text, search_key: Text, index: Text):
self.client = SearchClient.create(app_id, search_key)
self.index = self.client.init_index(index)


def get_algolia_link(self, hits: List, index: int):
doc_link = f"- [{hits[index].get('hierarchy').get('lvl0')}"
if hits[index]['hierarchy'].get('lvl1'):
if hits[index]["hierarchy"].get("lvl1"):
doc_link += f"/{hits[index].get('hierarchy').get('lvl1').strip()}"
if hits[index]['hierarchy'].get('lvl2'):
if hits[index]["hierarchy"].get("lvl2"):
doc_link += f"/{hits[index].get('hierarchy').get('lvl2').strip()}"
doc_link += f"]({hits[index].get('url')})"
return doc_link
Expand Down
12 changes: 7 additions & 5 deletions demo/discourse.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import requests
from typing import Text
from typing import Any, Dict, List, Text


class DiscourseAPI(object):
"""Class to connect to the Algolia API"""

def __init__(self, url: Text):
self.url = url

def get_discourse_links(self, topics: [Text], index: int):
@staticmethod
def get_discourse_links(topics: List[Dict[Text, Any]], index: int):
doc_url = f"https://forum.rasa.com/t/{topics[index]['slug']}/{str(topics[index]['id'])}"
forum = f"- [{topics[index]['title']}]({doc_url})"
return forum

def query(self, search_string: Text, include_blurbs=False):
params = {'term': search_string, 'include_blurbs': include_blurbs}
params = {"term": search_string, "include_blurbs": include_blurbs}
res = requests.get(url=f"{self.url}/query.json", params=params)
return res

def search(self, search_string: Text, include_blurbs=False):
params = {'q': search_string}
headers={"Content-Type":"application/json; charset=utf-8"}
params = {"q": search_string}
headers = {"Content-Type": "application/json; charset=utf-8"}
res = requests.get(url=self.url, params=params, headers=headers)
return res
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 88
ignore = W503, E121, E126, E211, E225, E501, E203, E402, F401, F811, E231
ignore = W503, E121, E126, E501, E402, F811

0 comments on commit 4bbb632

Please sign in to comment.