Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new Dialogflow scenario for explainability #16

Merged
merged 9 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
9 changes: 7 additions & 2 deletions Webhook/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@
qanaryComponents = None
qanaryComponentNames = None


def getComponents():
global qanaryComponents
global qanaryComponentNames
response = urllib.request.urlopen('http://demos.swe.htwk-leipzig.de:40111/components').read().decode()
response = urllib.request.urlopen(
'http://pie.qanary.net:8000/components').read().decode()
Copy link
Member

Choose a reason for hiding this comment

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

Please replace the hard-coded URL by a constant initialized at the beginning of the Python file.

There should be something like:

from decouple import config # required: pip install python-decouple 
QANARY_COMPONENTS_ENDPOINT = config('QANARY_COMPONENTS_ENDPOINT')

Then create a file .env for your local environment containing:

QANARY_COMPONENTS_ENDPOINT="http://demos.swe.htwk-leipzig.de:40111/components"

see https://simpleisbetterthancomplex.com/2015/11/26/package-of-the-week-python-decouple.html

Copy link
Collaborator Author

@muskan-k muskan-k Aug 18, 2023

Choose a reason for hiding this comment

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

@anbo-de what you are reviewing is an outdated file. If you could please notice the "outdated" label on the file you've left your reviews on. These are old changes. As part of the fix for issue - #17, a .env file was created and all the URLs were added there and the changes were made in all the other files where the constant were imported from .env files. If you could please see the Files tab in this PR, you will be able to find the latest updated file and you will not find direct URLs that you have just reviewed and commented to. Please let me know if you can find it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

body = ast.literal_eval(response)
qanaryComponents = fuzzyset.FuzzySet()
qanaryComponents = fuzzyset.FuzzySet()
qanaryComponentNames = set()
for i in range(len(body)):
qanaryComponents.add(body[i]['name'])
# TODO
qanaryComponentNames.add(body[i]['name'])


def getQanaryComponentNames():
global qanaryComponentNames
getComponents()
return qanaryComponentNames


def getQanaryComponents():
global qanaryComponents
getComponents()
Expand Down
1 change: 1 addition & 0 deletions Webhook/fulfillment/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class FulfillmentMessages(BaseModel):
intentMap['qanary-CreateProfile'] = intents.createProfileIntent
intentMap['qanary-DeactivateComponent'] = intents.deactivateComponentIntent
intentMap['qanary-EmptyComponents'] = intents.emptyComponentsIntent
intentMap['qanary-GetExplanationOfPrevAnswer'] = intents.getExplanationOfPrevAnswerIntent
intentMap['qanary-RemoveComponentFromProfile'] = intents.removeComponentFromProfileIntent
intentMap['qanary-ResetComponents'] = intents.resetComponentsIntent
intentMap['qanary-ShowActiveComponents'] = intents.showActiveComponentsIntent
Expand Down
File renamed without changes.
59 changes: 52 additions & 7 deletions Webhook/helpers/intents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import ast
from SPARQLWrapper import SPARQLWrapper, JSON, POST
import requests
import config
# from data import defaultComponents, profileComponents, vizURL
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

defaultComponents = ["NED-DBpediaSpotlight", "SparqlExecuter",
"OpenTapiocaNED", "BirthDataQueryBuilder", "WikidataQueryExecuter"]
profileComponents = []
vizURL = "https://dbpedia-rdf-viz.herokuapp.com/visualize/"
Copy link
Member

Choose a reason for hiding this comment

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

Please externalize value as described for components.py.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sessionIdManagement = {}
lastKbquestion = {}
lastGraphId = {}
lastKbAnswer = {}
profiles = {}


Expand All @@ -26,7 +31,7 @@ def activateComponentIntent(agent):
sessionId = agent['session'].split('/')[4]
if sessionId not in sessionIdManagement:
sessionIdManagement[sessionId] = {
'components': config.defaultComponents.copy()}
'components': defaultComponents.copy()}
getComponent = sessionIdManagement.get(sessionId)
localcomponentList = getComponent['components']
index = localcomponentList.index(
Expand Down Expand Up @@ -56,11 +61,12 @@ def getAnswerFromDbpedia(query):
sparql.setReturnFormat(JSON)
sparql.setMethod(POST)
result = sparql.queryAndConvert()
print(result)
return result['results']['bindings'][0]['answer']['value']


def getAnswerFromQanary(graphId):
endpointUrl = 'http://demos.swe.htwk-leipzig.de:40111/sparql'
endpointUrl = 'http://pie.qanary.net:8000/sparql'
Copy link
Member

Choose a reason for hiding this comment

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

Please externalize value as described for components.py.

output = "No answer available."
query = """
Copy link
Member

Choose a reason for hiding this comment

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

Remark: better to use format() for inserting the graphId into the query.

PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
Expand All @@ -78,6 +84,7 @@ def getAnswerFromQanary(graphId):
sparql.setReturnFormat(JSON)
sparql.setMethod(POST)
result = sparql.queryAndConvert()
print(result)
dbpediaQuery = result['results']['bindings'][0]['resultAsSparqlQuery']['value']
result = getAnswerFromDbpedia(dbpediaQuery)
if result is not None:
Expand All @@ -88,21 +95,58 @@ def getAnswerFromQanary(graphId):
def askQanaryIntent(agent):
sessionId = agent['session'].split('/')[4]
lastKbquestion[sessionId] = agent['queryResult']['queryText']
print(sessionIdManagement)
getComponent = sessionIdManagement[sessionId]

show = getComponent['components']
print(show)
params = {
"question": lastKbquestion[sessionId],
"componentlist[]": show
}
response = requests.post(
'http://demos.swe.htwk-leipzig.de:40111/startquestionansweringwithtextquestion', params)
'http://pie.qanary.net:8000/startquestionansweringwithtextquestion', params)
Copy link
Member

Choose a reason for hiding this comment

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

Please externalize value as described for components.py.

print(response)
responseDict = ast.literal_eval(response.text)
print(responseDict)
currentGraphId = responseDict['inGraph']
lastGraphId[sessionId] = currentGraphId
output = getAnswerFromQanary(currentGraphId)
lastKbAnswer[sessionId] = output
return output


def getExplanationOfPrevAnswerIntent(agent):
# sessionId = agent['session'].split('/')[4]
# try:
# lastGraphIdOfSession = lastGraphId[sessionId]
# except:
# lastGraphIdOfSession = None
explanation = "Sorry, there was no previously asked question in this session."
# if lastGraphIdOfSession is not None:
endpointUrl = 'http://pie.qanary.net:8000/sparql'
Copy link
Member

Choose a reason for hiding this comment

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

Please externalize value as described for components.py.

output = "No explanation available."
graphId = "24a1132b-6c7d-4770-8f30-e87cd43f8cf8"
queryAnnotationsOfPrevQuestion = """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
SELECT *
FROM <"""+graphId+""">
WHERE {
?annotationId rdf:type ?type.
?annotationId oa:hasBody ?body.
?annotationId oa:hasTarget ?target.
?annotationId oa:annotatedBy $X .
}"""
sparql = SPARQLWrapper(endpointUrl)
sparql.setQuery(queryAnnotationsOfPrevQuestion)
sparql.setReturnFormat(JSON)
sparql.setMethod(POST)
result = sparql.queryAndConvert()

return result if not None else explanation


def activateProfileIntent(agent):
profileName = agent['queryResult']['parameters']['profilename']
defaultcomponent = fuzzyset.FuzzySet()
Expand Down Expand Up @@ -131,6 +175,7 @@ def activateProfileIntent(agent):
def addComponentToProfileIntent(agent):
profileName = agent['queryResult']['parameters']['profilename']
sessionId = sessionId = agent['session'].split('/')[4]
print(sessionId)
if sessionId+profileName in profiles:
componentName = agent['queryResult']['parameters']['componentname']
qanaryComponentList = components.getQanaryComponents()
Expand Down Expand Up @@ -187,7 +232,7 @@ def createProfileIntent(agent):
output = 'Profile \'' + profileName + '\' already exists.'
else:
profiles[sessionId +
profileName] = {'components': config.profileComponents.copy()}
profileName] = {'components': profileComponents.copy()}
output = ' Profile \'' + profileName + '\' added successfully. Now to use this profile you can say \'start ' + \
profileName + '\' to activate the profile.'
return output
Expand Down Expand Up @@ -257,7 +302,7 @@ def removeComponentFromProfileIntent(agent):
def resetComponentsIntent(agent):
sessionId = agent['session'].split('/')[4]
sessionIdManagement[sessionId] = {
'components': config.defaultComponents.copy()}
'components': defaultComponents.copy()}
output = 'Reset successfully, the components list is now set to default component list.'
return output

Expand All @@ -278,5 +323,5 @@ def showActiveComponentsIntent(agent):
def showRdfVisualizationIntent(agent):
sessionId = agent['session'].split('/')[4]
currentGraphId = lastGraphId[sessionId]
output = f"Go to this link to see the RDF visualization: {config.vizURL}{currentGraphId}"
output = f"Go to this link to see the RDF visualization: {vizURL}{currentGraphId}"
return output
8 changes: 4 additions & 4 deletions Webhook/requests/activateComponent.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"responseId": "6ea20321-43c4-4daf-a96e-7b209bc9de47-97a30490",
"queryResult": {
"queryText": "add ldshuyo",
"queryText": "activate component",
"parameters": {
"activatecomponent": [
"ldshuyo"
"NED-DBpediaSpotlight"
]
},
"allRequiredParamsPresent": true,
Expand All @@ -24,10 +24,10 @@
"no-input": 0,
"no-match": 0,
"activatecomponent": [
"ldshuyo"
"NED-DBpediaSpotlight"
],
"activatecomponent.original": [
"ldshuyo"
"NED-DBpediaSpotlight"
]
}
}
Expand Down
8 changes: 4 additions & 4 deletions Webhook/requests/addComponentToProfile.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"responseId": "f735da0b-93ee-4f48-b815-f584a400669e-97a30490",
"queryResult": {
"queryText": "to profile Sam add ldshuyo",
"queryText": "to profile Sam add NED-DBpediaSpotlight",
"parameters": {
"componentname": "ldshuyo",
"componentname": "NED-DBpediaSpotlight",
"profilename": "Sam"
},
"allRequiredParamsPresent": true,
Expand All @@ -22,8 +22,8 @@
"parameters": {
"no-input": 0,
"no-match": 0,
"componentname": "ldshuyo",
"componentname.original": "ldshuyo",
"componentname": "NED-DBpediaSpotlight",
"componentname.original": "NED-DBpediaSpotlight",
"profilename": "Sam",
"profilename.original": "Sam"
}
Expand Down
38 changes: 38 additions & 0 deletions Webhook/requests/askqanaryIntent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"responseId": "349e65c0-4680-4d5e-af5e-04f51c5812b5-1b0ea404",
"queryResult": {
"queryText": "real name of superhero captain America",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentText": "No answer available.",
"fulfillmentMessages": [
{
"text": {
"text": [
"No answer available."
]
}
}
],
"outputContexts": [
{
"name": "projects/fabled-alchemy-379117/agent/sessions/c9edd005-b579-95b3-1762-39677eccf5c2/contexts/__system_counters__",
"parameters": {
"no-input": 0,
"no-match": 0
}
}
],
"intent": {
"name": "projects/fabled-alchemy-379117/agent/intents/031bc0e9-26dd-4585-b933-14fa56a01a5a",
"displayName": "qanary-AskQanary"
},
"intentDetectionConfidence": 0.7662884,
"languageCode": "en"
},
"originalDetectIntentRequest": {
"source": "DIALOGFLOW_CONSOLE",
"payload": {}
},
"session": "projects/dbpedia-chatbot-v1-kaab/agent/sessions/18191cd9-d123-f842-fdef-48df376ca011"
}
48 changes: 48 additions & 0 deletions Webhook/requests/getExplanationLastQuestion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"responseId": "13261220-eae4-4728-8107-be1eb42b0e08-1b0ea404",
"queryResult": {
"queryText": "give explanation of previous",
"parameters": {
"componententity": ""
},
"allRequiredParamsPresent": true,
"fulfillmentText": "Providing an explanation shortly",
"fulfillmentMessages": [
{
"text": {
"text": [
"Providing an explanation shortly"
]
}
}
],
"outputContexts": [
{
"name": "projects/fabled-alchemy-379117/agent/sessions/ce995465-31ab-1f41-626c-3e0482373fb7/contexts/__system_counters__",
"parameters": {
"no-input": 0,
"no-match": 0,
"componententity": "",
"componententity.original": ""
}
}
],
"intent": {
"name": "projects/fabled-alchemy-379117/agent/intents/0260af35-cc47-4d60-9711-a99ae712f801",
"displayName": "qanary-GetExplanationOfPrevAnswer"
},
"intentDetectionConfidence": 0.74499756,
"languageCode": "en",
"sentimentAnalysisResult": {
"queryTextSentiment": {
"score": -0.4,
"magnitude": 0.4
}
}
},
"originalDetectIntentRequest": {
"source": "DIALOGFLOW_CONSOLE",
"payload": {}
},
"session": "projects/dbpedia-chatbot-v1-kaab/agent/sessions/18191cd9-d123-f842-fdef-48df376ca011"
}
3 changes: 1 addition & 2 deletions Webhook/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ starlette==0.20.4
uvicorn==0.18.3
gunicorn==20.1.0
requests==2.25.1
rapidfuzz==2.15.1
config==0.5.1
rapidfuzz==2.15.1