8000 added qna_maker.py by matthewshim-ms · Pull Request #22 · microsoft/botbuilder-python · GitHub
[go: up one dir, main page]

Skip to content

added qna_maker.py #22

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

Merged
merged 2 commits into from
Feb 21, 2018
Merged
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
8000
Diff view
68 changes: 68 additions & 0 deletions libraries/botbuilder-ai/microsoft/botbuilder/qna_maker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import json
import asyncio
import requests

class QnAMaker:

__qnaMakerServiceEndpoint = 'https://westus.api.cognitive.microsoft.com/qnamaker/v3.0/knowledgebases/'
__json_mime_type = 'application/json'
__api_management_header = 'Ocp-Apim-Subscription-Key'

def __init__(self, options, http_client):

self.__http_client = _http_client or raise TypeError('HTTP Client failed')
self.__options = options or raise TypeError('Options config error')

self.__answerUrl = "%s%s/generateanswer" % (__qnaMakerServiceEndpoint,options.knowledge_base_id)

if self.__options.ScoreThreshold == 0:
self.__options.ScoreThreshold = 0.3 #Note - SHOULD BE 0.3F 'FLOAT'

if self.__options.Top == 0:
self.__options.Top = 1

if self.__options.StrictFilters == None:
self.__options.StrictFilters = MetaData()

if self.__options.MetadataBoost == None:
self.__options.MetadataBoost = MetaData()

async def get_answers(question): # HTTP call
headers = {
__api_management_header : self.__options.subscription_key
"Content-Type" : __json_mime_type
}

payload = json.dumps({
"question" : question
})
# POST request to QnA Service
content = requests.post(self.__answerUrl, headers=headers, data=payload)
return content.json()

class QnAMakerOptions:
def __init__(self, subscription_key, knowledge_base_id, score_threshhold, top, strict_filters, metadata_boost):
self.subscription_key = subscription_key
self.knowledge_base_id = knowledge_base_id
self.score_threshhold = score_threshhold
self.top = top
self.strict_filters = strict_filters
self.metadata_boost = metadata_boost

class MetaData:
def __init__(self, name, value):
self.name = name
self.value = value

class QueryResult:
def __init__(self, questions, answer, score, metadata, source, qna_id):
self.questions = questions
self.answer = answer
self.score = score
self.metadata = metadata
self.source = source
self.qna_id = qna_id

class QueryResults:
def __init__(self, answers):
self.__answers = answers
0