Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,18 @@ dmypy.json

# Pyre type checker
.pyre/

# VSCode
.vscode/
.history/
*.DS_Store
*.wpr
*.wpu

# Rasa stuff
models/
*.db*
*.dot
terms/
results/
.rasa/
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rasa/rasa-sdk:3.3.0
ENV TZ=Etc/UTC

COPY actions /app/actions

USER root
RUN /opt/venv/bin/python -m pip install --upgrade pip
RUN pip install --no-cache-dir -r /app/actions/requirements.txt

USER 1001
CMD ["start", "--actions", "actions"]
26 changes: 22 additions & 4 deletions actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,45 @@

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import requests
import aiohttp
import json
import logging

logger = logging.getLogger(__name__)

class ActionHaystack(Action):

def name(self) -> Text:
return "call_haystack"

def run(self, dispatcher: CollectingDispatcher,
async def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

url = "http://localhost:8000/query"
payload = {"query": str(tracker.latest_message["text"])}
payload = {
"query": str(tracker.latest_message["text"]),
"params":{"filters": {}, "Retriever": {"top_k": 3}, "Reader": {"top_k": 3}, "Query": {"debug": False}}
}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, json=payload).json()

logger.debug(f"Calling {url}, ask: {str(tracker.latest_message['text'])}")
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as response:
response = await response.json()
except aiohttp.ClientConnectorError as e:
logger.error(f"Haystack service not responding: {str(e)}")
response = {"answers":[{"answer":f"Haystack service not responding: {str(e)}"}]}

if response["answers"]:
logger.debug(f"Answers returned by haystack:\n{response}")
answer = response["answers"][0]["answer"]
if not answer:
answer = response["answers"][1]["answer"]
logger.debug(f"selected answer: {answer}")
else:
answer = "No Answer Found!"
Comment on lines 45 to 52
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the answer = response["answers"][1]["answer"] might cause an index error if there are no answers at all or less than 2 answers

Suggested change
if response["answers"]:
logger.debug(f"Answers returned by haystack:\n{response}")
answer = response["answers"][0]["answer"]
if not answer:
answer = response["answers"][1]["answer"]
logger.debug(f"selected answer: {answer}")
else:
answer = "No Answer Found!"
answer = "No Answer Found!"
for answer_object in response.get("answers") or []:
current_answer = answer_object["answer"]
if current_answer:
answer = current_answer
logger.debug(f"selected answer: {answer}")
break


Expand Down
1 change: 1 addition & 0 deletions actions/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aiohttp==3.8.1
8 changes: 8 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
# Build action server image
VERS=3.3.0
REGISTRY=<Docker organization>
IMAGE=haystack-action

time docker buildx build --platform linux/amd64,linux/arm64 --output=type=registry --tag ${REGISTRY}/${IMAGE}:${VERS} --tag ${REGISTRY}/${IMAGE}:latest .
docker push ${REGISTRY}/${IMAGE} --all-tags
3 changes: 1 addition & 2 deletions data/nlu.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2.0"
version: "3.1"

nlu:
- intent: knowledge_question
Expand Down Expand Up @@ -30,7 +30,6 @@ nlu:

- intent: goodbye
examples: |
- good afternoon
- cu
- good by
- cee you later
Expand Down
2 changes: 1 addition & 1 deletion data/rules.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2.0"
version: "3.1"

rules:

Expand Down
2 changes: 1 addition & 1 deletion data/stories.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2.0"
version: "3.1"

stories:

Expand Down
2 changes: 1 addition & 1 deletion domain.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.0'
version: "3.1"
config:
store_entities_as_slots: true
session_config:
Expand Down