-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
e5a7db2
88dfe24
c481a71
cb778bc
e8e9801
5458990
5870281
08c4bbb
b0411af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.autopep8" | ||
}, | ||
"python.formatting.provider": "none" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please externalize value as described for components.py. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
sessionIdManagement = {} | ||
lastKbquestion = {} | ||
lastGraphId = {} | ||
lastKbAnswer = {} | ||
profiles = {} | ||
|
||
|
||
|
@@ -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( | ||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark: better to use |
||
PREFIX oa: <http://www.w3.org/ns/openannotation/core/> | ||
|
@@ -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: | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 |
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" | ||
} |
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" | ||
} |
There was a problem hiding this comment.
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:
Then create a file
.env
for your local environment containing:see https://simpleisbetterthancomplex.com/2015/11/26/package-of-the-week-python-decouple.html
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/dbpedia/dbpedia-chatbot-backend/blob/08c4bbbeb58f26ef361b636eb4a1c4481ebc4b6c/Webhook/components.py#L17C9-L17C52