8000 QnA Parity: Adding Active Learning Feature by Zerryth · Pull Request #264 · microsoft/botbuilder-python · GitHub
[go: up one dir, main page]

Skip to content

QnA Parity: Adding Active Learning Feature #264

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 29 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c4dea4f
Initial active learning commit w/FeedbackRecord model
Zerryth Jul 18, 2019
ecf797f
port FeedbackRecords & added param types to FeedbackRecord
Zerryth Jul 18, 2019
77ec785
added all qna models
Zerryth Jul 18, 2019
8b74b2b
begin porting HttpRequestUtils
Zerryth Jul 19, 2019
f0b8839
ported GenerateAnswerUtils
Zerryth Jul 22, 2019
149a101
Merge branch 'master' of https://github.com/microsoft/botbuilder-pyth…
Zerryth Jul 22, 2019
fa28650
used black to auto format qna library
Zerryth Jul 22, 2019
c0a08d2
ported TrainUtils
Zerryth Jul 23, 2019
8978af5
TrainUtil now calls Train API in call_train()
Zerryth Jul 23, 2019
fbdc409
resolved conflicts in qna imports
Zerryth Jul 23, 2019
449fa83
black to format all libraries
Zerryth Jul 23, 2019
36fbc34
ported ActiveLearningUtils class
Zerryth Jul 24, 2019
912feaa
Merge branch 'master' into Zerryth/ActiveLearning
Zerryth Jul 24, 2019
2544638
Ported all QnAMaker methods; reorganized folder structure according t…
Zerryth Jul 24, 2019
30b1fd8
resolved conflicts
Zerryth Jul 24, 2019
9c3a148
resolved conflicts AND SAVED CHANGES this time
Zerryth Jul 24, 2019
3670a3c
merge conflicts 3
Zerryth Jul 24, 2019
8346dc0
conflicts 4
Zerryth Jul 24, 2019
a70f672
fixed linting errors
Zerryth Jul 24, 2019
91e71f7
black to format
Zerryth Jul 24, 2019
d10f532
made QnA classes serializable using msrest
Zerryth Jul 25, 2019
0b78e1f
changed class name to GenerateAnswerRequestBody to be consistent with…
Zerryth Jul 25, 2019
5146812
updated GenerateAnswerRequestBody's to document params correctly
Zerryth Jul 26, 2019
2b2939a
Merge branch 'master' of https://github.com/microsoft/botbuilder-pyth…
Zerryth Jul 31, 2019
2603f14
added call train api test case
Zerryth Jul 31, 2019
d71abb6
completed tests for low score variation
Zerryth Jul 31, 2019
109d317
removed context; changed matched kb ans to check id; removed legacy s…
Zerryth Aug 1, 2019
5f6d87b
removed legacy tests
Zerryth Aug 1, 2019
ba1fac3
removed TODO comment from qna __init__ file
Zerryth Aug 1, 2019
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
25 changes: 21 additions & 4 deletions libraries/botbuilder-ai/botbuilder/ai/qna/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .metadata import Metadata
from .query_result import QueryResult
from .qnamaker import QnAMaker
from .qnamaker_endpoint import QnAMakerEndpoint
from .qnamaker_options import QnAMakerOptions
from .qnamaker_telemetry_client import QnAMakerTelemetryClient
from .qna_telemetry_constants import QnATelemetryConstants
from .qnamaker_trace_info import QnAMakerTraceInfo
from .utils import (
ActiveLearningUtils,
GenerateAnswerUtils,
HttpRequestUtils,
QnATelemetryConstants,
)

from .models import (
FeedbackRecord,
FeedbackRecords,
Metadata,
QnAMakerTraceInfo,
QueryResult,
QueryResults,
)

__all__ = [
"ActiveLearningUtils",
"FeedbackRecord",
"FeedbackRecords",
"GenerateAnswerUtils",
"HttpRequestUtils",
"Metadata",
"QueryResult",
"QueryResults",
"QnAMaker",
"QnAMakerEndpoint",
"QnAMakerOptions",
Expand Down
8 changes: 0 additions & 8 deletions libraries/botbuilder-ai/botbuilder/ai/qna/metadata.py

This file was deleted.

26 changes: 26 additions & 0 deletions libraries/botbuilder-ai/botbuilder/ai/qna/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

from .feedback_record import FeedbackRecord
from .feedback_records import FeedbackRecords
from .generate_answer_request_body import GenerateAnswerRequestBody
from .metadata import Metadata
from .qnamaker_trace_info import QnAMakerTraceInfo
from .query_result import QueryResult
from .query_results import QueryResults
from .train_request_body import TrainRequestBody

__all__ = [
"FeedbackRecord",
"FeedbackRecords",
"GenerateAnswerRequestBody",
"Metadata",
"QnAMakerTraceInfo",
"QueryResult",
"QueryResults",
"TrainRequestBody",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from msrest.serialization import Model


class FeedbackRecord(Model):
""" Active learning feedback record. """

_attribute_map = {
"user_id": {"key": "userId", "type": "str"},
"user_question": {"key": "userQuestion", "type": "str"},
"qna_id": {"key": "qnaId", "type": "int"},
}

def __init__(self, user_id: str, user_question: str, qna_id: int, **kwargs):
"""
Parameters:
-----------

user_id: ID of the user.

user_question: User question.

qna_id: QnA ID.
"""

super().__init__(**kwargs)

self.user_id = user_id
self.user_question = user_question
self.qna_id = qna_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List

from msrest.serialization import Model

from .feedback_record import FeedbackRecord


class FeedbackRecords(Model):
""" Active learning feedback records. """

_attribute_map = {"records": {"key": "records", "type": "[FeedbackRecord]"}}

def __init__(self, records: List[FeedbackRecord], **kwargs):
"""
Parameter(s):
-------------

records: List of feedback records.
"""

super().__init__(**kwargs)

self.records = records
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List

from msrest.serialization import Model

from .metadata import Metadata


class GenerateAnswerRequestBody(Model):
""" Question used as the payload body for QnA Maker's Generate Answer API. """

_attribute_map = {
"question": {"key": "question", "type": "str"},
"top": {"key": "top", "type": "int"},
"score_threshold": {"key": "scoreThreshold", "type": "float"},
"strict_filters": {"key": "strictFilters", "type": "[Metadata]"},
}

def __init__(
self,
question: str,
top: int,
score_threshold: float,
strict_filters: List[Metadata],
**kwargs
):
"""
Parameters:
-----------

question: The user question to query against the knowledge base.

top: Max number of answers to be returned for the question.

score_threshold: Threshold for answers returned based on score.

strict_filters: Find only answers that contain these metadata.
"""

super().__init__(**kwargs)

self.question = question
self.top = top
self.score_threshold = score_threshold
self.strict_filters = strict_filters
28 changes: 28 additions & 0 deletions libraries/botbuilder-ai/botbuilder/ai/qna/models/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from msrest.serialization import Model


class Metadata(Model):
""" Metadata associated with the answer. """

_attribute_map = {
"name": {"key": "name", "type": "str"},
"value": {"key": "value", "type": "str"},
}

def __init__(self, name: str, value: str, **kwargs):
"""
Parameters:
-----------

name: Metadata name. Max length: 100.

value: Metadata value. Max length: 100.
"""

super().__init__(**kwargs)

self.name = name
self.value = value
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List

from botbuilder.schema import Activity
from .metadata import Metadata
from .query_result import QueryResult


class QnAMakerTraceInfo:
""" Represents all the trice info that we collect from the QnAMaker Middleware. """

def __init__(
self,
message: Activity,
query_results: List[QueryResult],
knowledge_base_id: str,
score_threshold: float,
top: int,
strict_filters: List[Metadata],
):
"""
Parameters:
-----------

message: Message which instigated the query to QnA Maker.

query_results: Results that QnA Maker returned.

knowledge_base_id: ID of the knowledge base that is being queried.

score_threshold: The minimum score threshold, used to filter returned results.

top: Number of ranked results that are asked to be returned.

strict_filters: Filters used on query.
"""
self.message = message
self.query_results = query_results
self.knowledge_base_id = knowledge_base_id
self.score_threshold = score_threshold
self.top = top
self.strict_filters = strict_filters
42 changes: 42 additions & 0 deletions libraries/botbuilder-ai/botbuilder/ai/qna/models/query_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List

from .metadata import Metadata


class QueryResult:
""" Represents an individual result from a knowledge base query. """

def __init__(
self,
questions: List[str],
answer: str,
score: float,
metadata: object = None,
source: str = None,
id: int = None, # pylint: disable=invalid-name
):
"""
Parameters:
-----------

questions: The list of questions indexed in the QnA Service for the given answer (if any).

answer: Answer from the knowledge base.

score: Confidence on a scale from 0.0 to 1.0 that the answer matches the user's intent.

metadata: Metadata associated with the answer (if any).

source: The source from which the QnA was extracted (if any).

id: The index of the answer in the knowledge base. V3 uses 'qnaId', V4 uses 'id' (if any).
"""
self.questions = questions
self.answer = answer
self.score = score
self.metadata = list(map(lambda meta: Metadata(**meta), metadata))
self.source = source
self.id = id # pylint: disable=invalid-name
19 changes: 19 additions & 0 deletions libraries/botbuilder-ai/botbuilder/ai/qna/models/query_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List

from .query_result import QueryResult


class QueryResults:
""" Contains answers for a user query. """

def __init__(self, answers: List[QueryResult]):
"""
Parameters:
-----------

answers: The answers for a user query.
"""
self.answers = answers
< 440E /tbody>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import List
from msrest.serialization import Model

from .feedback_record import FeedbackRecord


class TrainRequestBody(Model):
""" Class the models the request body that is sent as feedback to the Train API. """

_attribute_map = {
"feedback_records": {"key": "feedbackRecords", "type": "[FeedbackRecord]"}
}

def __init__(self, feedback_records: List[FeedbackRecord], **kwargs):
"""
Parameters:
-----------

feedback_records: List of feedback records.
"""

super().__init__(**kwargs)

self.feedback_records = feedback_records
Loading
0