qna_maker 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -3
- data/bin/setup +1 -2
- data/lib/qna_maker.rb +20 -1
- data/lib/qna_maker/alteration.rb +15 -1
- data/lib/qna_maker/answer.rb +10 -0
- data/lib/qna_maker/endpoints/create_kb.rb +28 -2
- data/lib/qna_maker/endpoints/delete_kb.rb +14 -0
- data/lib/qna_maker/endpoints/download_alterations.rb +6 -0
- data/lib/qna_maker/endpoints/download_kb.rb +5 -0
- data/lib/qna_maker/endpoints/generate_answer.rb +11 -0
- data/lib/qna_maker/endpoints/publish_kb.rb +5 -0
- data/lib/qna_maker/endpoints/train_kb.rb +11 -0
- data/lib/qna_maker/endpoints/update_alterations.rb +10 -1
- data/lib/qna_maker/endpoints/update_kb.rb +10 -1
- data/lib/qna_maker/errors.rb +12 -9
- data/lib/qna_maker/string.rb +8 -0
- data/lib/qna_maker/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29492e8b4bfac09cf1e52269c608dba416a723e2d6738e5b0b41377a21798935
|
4
|
+
data.tar.gz: 7789f79de95d5996c3809a2b34ebb134ac9097451e3e19166d4d7bb63e425686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d1f018e0be2926afc882e26a099b04edcce677d85588bbbea8d722365bf1ab0d25d56c4230c03c48ba72e6cce802ceb39d9c6f69046ac26d3676504c51678fc
|
7
|
+
data.tar.gz: 808612670bdbcdf03972db62f1f082d1e78f575889a4e85c55985f70e71069c0604b86f1ff90323bdd9ce4cf89fb4a939d2ff16e86cf764bb14500c4d0a00a66
|
data/README.md
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
# QnAMaker
|
2
2
|
|
3
|
-
](https://rubygems.org/gems/qna_maker)
|
4
|
+
[](https://rubygems.org/gems/qna_maker)
|
4
5
|
[](https://travis-ci.org/Birdie0/qna_maker)
|
5
|
-
[](https://inch-ci.org/github/Birdie0/qna_maker)
|
7
|
+
[](https://codeclimate.com/github/Birdie0/qna_maker/maintainability)
|
6
8
|
|
7
9
|
API wrapper for [QnA Maker](https://qnamaker.ai)
|
8
10
|
|
11
|
+
## [QnA API docs](https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe)
|
12
|
+
|
9
13
|
## Installation
|
10
14
|
|
11
15
|
Add this line to your application's Gemfile:
|
@@ -53,7 +57,7 @@ Gem is under active development! It isn't finished yet!
|
|
53
57
|
* [x] **PATCH** - Train Knowledge Base
|
54
58
|
* [x] **PATCH** - Update Alterations
|
55
59
|
* [x] **PATCH** - Update Knowledge Base
|
56
|
-
* [
|
60
|
+
* [x] Documentation
|
57
61
|
* [ ] Tests
|
58
62
|
|
59
63
|
## Development
|
data/bin/setup
CHANGED
data/lib/qna_maker.rb
CHANGED
@@ -18,11 +18,30 @@ require 'qna_maker/endpoints/update_alterations'
|
|
18
18
|
require 'http'
|
19
19
|
|
20
20
|
module QnAMaker
|
21
|
+
#
|
22
|
+
# Client instance
|
23
|
+
#
|
21
24
|
class Client
|
22
25
|
BASE_URL = 'https://westus.api.cognitive.microsoft.com/QnAMaker/v2.0/knowledgebases'.freeze
|
23
26
|
|
24
|
-
|
27
|
+
# @!attribute [r] knowledgebase_id
|
28
|
+
# @return [String]
|
29
|
+
attr_reader :knowledgebase_id
|
30
|
+
# @!attribute [r] subscription_key
|
31
|
+
# @return [String]
|
32
|
+
attr_reader :subscription_key
|
33
|
+
# @!attribute [r] data_extraction_results
|
34
|
+
# @return [Array<String>]
|
35
|
+
attr_reader :data_extraction_results
|
25
36
|
|
37
|
+
#
|
38
|
+
# <Description>
|
39
|
+
#
|
40
|
+
# @param [String] knowledgebase_id this should be get from QnAMaker portal
|
41
|
+
# @param [String] subscription_key QnAMaker::Client provides access to
|
42
|
+
# this API. Found in your QnAMaker Service accounts (https://qnamaker.ai
|
43
|
+
# @param [Array<Hash{String => String, String => String, String => String}>] data_extraction_results ata extraction results.
|
44
|
+
#
|
26
45
|
def initialize(knowledgebase_id, subscription_key, data_extraction_results = [])
|
27
46
|
@knowledgebase_id = knowledgebase_id
|
28
47
|
@subscription_key = subscription_key
|
data/lib/qna_maker/alteration.rb
CHANGED
@@ -1,7 +1,21 @@
|
|
1
1
|
module QnAMaker
|
2
|
+
#
|
3
|
+
# Alteration class
|
4
|
+
#
|
2
5
|
class Alteration
|
3
|
-
|
6
|
+
# @!attribute [r] word
|
7
|
+
# @return [String] word of alteration
|
8
|
+
attr_reader :word
|
9
|
+
# @!attribute [r] alterations
|
10
|
+
# @return [Array<Alteration>] list of alterations for word
|
11
|
+
attr_reader :alterations
|
4
12
|
|
13
|
+
#
|
14
|
+
# Creates an object for alteration.
|
15
|
+
#
|
16
|
+
# @param [String] word
|
17
|
+
# @param [Array<Alteration>] alterations list of alterations
|
18
|
+
#
|
5
19
|
def initialize(word, alterations)
|
6
20
|
@word = word
|
7
21
|
@alterations = alterations
|
data/lib/qna_maker/answer.rb
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
module QnAMaker
|
2
|
+
#
|
3
|
+
# Answer class
|
4
|
+
#
|
2
5
|
class Answer
|
3
6
|
attr_reader :answer, :questions, :score
|
4
7
|
|
8
|
+
#
|
9
|
+
# Creates an object for storing data about answer.
|
10
|
+
#
|
11
|
+
# @param [String] answer text
|
12
|
+
# @param [Array<String>] questions list of questions for asked question
|
13
|
+
# @param [Float] score probability that answer suits asked question
|
14
|
+
#
|
5
15
|
def initialize(answer, questions, score)
|
6
16
|
@answer = answer
|
7
17
|
@questions = questions
|
@@ -1,5 +1,16 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Creates a new knowledge base.
|
5
|
+
#
|
6
|
+
# @param [String] name friendly name for the knowledge base (Required)
|
7
|
+
# @param [Array<Array(String, String)>] qna_pairs list of question and answer pairs to be added to the knowledge base.
|
8
|
+
# Max 1000 Q-A pairs per request.
|
9
|
+
# @param [Array<String>] urls list of URLs to be processed and indexed in the knowledge base.
|
10
|
+
# In case of existing URL, it will be fetched again and KB will be updated with new data. Max 5 urls per request.
|
11
|
+
#
|
12
|
+
# @return [Client] client object
|
13
|
+
#
|
3
14
|
def create_kb(name, qna_pairs = [], urls = [])
|
4
15
|
response = @http.post(
|
5
16
|
"#{BASE_URL}/create",
|
@@ -12,7 +23,7 @@ module QnAMaker
|
|
12
23
|
|
13
24
|
case response.code
|
14
25
|
when 201
|
15
|
-
|
26
|
+
QnAMaker::Client.new(
|
16
27
|
response.parse['kbId'],
|
17
28
|
@subscription_key,
|
18
29
|
response.parse['dataExtractionResults']
|
@@ -26,6 +37,21 @@ module QnAMaker
|
|
26
37
|
end
|
27
38
|
end
|
28
39
|
|
40
|
+
#
|
41
|
+
# Creates a new knowledge base.
|
42
|
+
#
|
43
|
+
# @param [String] name friendly name for the knowledge base (Required)
|
44
|
+
# @param [String] subscription_key Subscription key which provides access to
|
45
|
+
# this API. Found in your QnAMaker Service accounts (https://qnamaker.ai)
|
46
|
+
# @param [Array<Array(String, String)>] qna_pairs list of question and
|
47
|
+
# answer pairs to be added to the knowledge base. Max 1000 Q-A pairs per
|
48
|
+
# request.
|
49
|
+
# @param [Array<String>] urls list of URLs to be processed and indexed in
|
50
|
+
# the knowledge base. In case of existing URL, it will be fetched again
|
51
|
+
# and KB will be updated with new data. Max 5 urls per request.
|
52
|
+
#
|
53
|
+
# @return [Client] client object
|
54
|
+
#
|
29
55
|
def self.create_kb(name, subscription_key, qna_pairs = [], urls = [])
|
30
56
|
response = HTTP.headers('Ocp-Apim-Subscription-Key' => subscription_key).post(
|
31
57
|
"#{BASE_URL}/create",
|
@@ -38,7 +64,7 @@ module QnAMaker
|
|
38
64
|
|
39
65
|
case response.code
|
40
66
|
when 201
|
41
|
-
|
67
|
+
QnAMaker::Client.new(
|
42
68
|
response.parse['kbId'],
|
43
69
|
subscription_key,
|
44
70
|
response.parse['dataExtractionResults']
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Deletes the current knowledge base and all data associated with it.
|
5
|
+
#
|
6
|
+
# @return [nil] on success
|
7
|
+
#
|
3
8
|
def delete_kb
|
4
9
|
response = @http.delete(
|
5
10
|
"#{BASE_URL}/#{knowledgebase_id}"
|
@@ -23,6 +28,15 @@ module QnAMaker
|
|
23
28
|
end
|
24
29
|
end
|
25
30
|
|
31
|
+
#
|
32
|
+
# Deletes the specified knowledge base and all data associated with it.
|
33
|
+
#
|
34
|
+
# @param [String] knowledgebase_id knowledge base identity
|
35
|
+
# @param [String] subscription_key Subscription key which provides access to
|
36
|
+
# this API. Found in your QnAMaker Service accounts (https://qnamaker.ai)
|
37
|
+
#
|
38
|
+
# @return [nil] on success
|
39
|
+
#
|
26
40
|
def self.delete_kb(knowledgebase_id, subscription_key)
|
27
41
|
response = HTTP.headers('Ocp-Apim-Subscription-Key' => subscription_key).delete(
|
28
42
|
"#{BASE_URL}/#{knowledgebase_id}"
|
@@ -1,5 +1,11 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Downloads all word alterations (synonyms) that have been automatically
|
5
|
+
# mined or added by the user.
|
6
|
+
#
|
7
|
+
# @return [Array<Alteration>] list of alterations
|
8
|
+
#
|
3
9
|
def download_alterations
|
4
10
|
response = @http.get(
|
5
11
|
"#{BASE_URL}/#{@knowledgebase_id}/downloadAlterations"
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Downloads all the data associated with the specified knowledge base
|
5
|
+
#
|
6
|
+
# @return [String] SAS url (valid for 30 mins) to tsv file in blob storage
|
7
|
+
#
|
3
8
|
def download_kb
|
4
9
|
response = @http.get(
|
5
10
|
"#{BASE_URL}/#{@knowledgebase_id}"
|
@@ -1,5 +1,16 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Returns the list of answers for the given question sorted in descending
|
5
|
+
# order of ranking score.
|
6
|
+
#
|
7
|
+
# @param [String] question user question to be queried against your
|
8
|
+
# knowledge base.
|
9
|
+
# @param [Integer] top number of ranked results you want in the output.
|
10
|
+
#
|
11
|
+
# @return [Array<Answer>] list of answers for the user query sorted in
|
12
|
+
# decreasing order of ranking score.
|
13
|
+
#
|
3
14
|
def generate_answer(question, top = 1)
|
4
15
|
response = @http.post(
|
5
16
|
"#{BASE_URL}/#{@knowledgebase_id}/generateAnswer",
|
@@ -1,5 +1,16 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# The developer of the knowledge base service can use this API to submit
|
5
|
+
# user feedback for tuning question-answer matching. QnA Maker uses active
|
6
|
+
# learning to learn from the user utterances that come on a published
|
7
|
+
# Knowledge base service.
|
8
|
+
#
|
9
|
+
# @param [Array<Array(String, String, String, String)>] feedback_records
|
10
|
+
# \[user_id, user_question, kb_question, kb_answer]
|
11
|
+
#
|
12
|
+
# @return [nil] on success
|
13
|
+
#
|
3
14
|
def train_kb(feedback_records = [])
|
4
15
|
feedback_records = feedback_records.map do |record|
|
5
16
|
{ userId: record[0],
|
@@ -1,9 +1,18 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
+
#
|
4
|
+
# Replaces word alterations (synonyms) for the KB with the give records.
|
5
|
+
#
|
6
|
+
# @param [Array(String, Array<String>)] add word alterations to be added
|
7
|
+
# @param [Array(String, Array<String>)] delete word alterations to be removed
|
8
|
+
#
|
9
|
+
# @return [nil] on success
|
10
|
+
#
|
3
11
|
def update_alterations(add = [], delete = [])
|
4
12
|
response = @http.patch(
|
5
13
|
"#{BASE_URL}/#{@knowledgebase_id}/updateAlterations",
|
6
|
-
json: { add: add,
|
14
|
+
json: { add: add.map {|i| {word: i[0], alterations: i[1]} },
|
15
|
+
delete: delete.map {|i| {word: i[0], alterations: i[1]} } }
|
7
16
|
)
|
8
17
|
|
9
18
|
case response.code
|
@@ -1,6 +1,15 @@
|
|
1
1
|
module QnAMaker
|
2
2
|
class Client
|
3
|
-
|
3
|
+
#
|
4
|
+
# Add or delete QnA Pairs and / or URLs to an existing knowledge base.
|
5
|
+
#
|
6
|
+
# @param [Array<Array(String, String)>] add \[question, answer\] data to be added to the knowledge base.
|
7
|
+
# @param [Array<Array(String, String)>] delete \[question, answer\] data to be removed to the knowledge base.
|
8
|
+
# @param [Array<String>] add_urls list of URLs to be processed and indexed in the knowledge base.
|
9
|
+
#
|
10
|
+
# @return [nil] on success
|
11
|
+
#
|
12
|
+
def update_kb(add: [], delete: [], add_urls: [])
|
4
13
|
response = @http.patch(
|
5
14
|
"#{BASE_URL}/#{@knowledgebase_id}",
|
6
15
|
json: {
|
data/lib/qna_maker/errors.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
1
|
module QnAMaker
|
2
|
-
class
|
2
|
+
class Error < StandardError
|
3
3
|
end
|
4
4
|
|
5
|
-
class
|
5
|
+
class BadArgumentError < Error
|
6
6
|
end
|
7
7
|
|
8
|
-
class
|
8
|
+
class NotFoundError < Error
|
9
9
|
end
|
10
10
|
|
11
|
-
class
|
11
|
+
class UnauthorizedError < Error
|
12
12
|
end
|
13
13
|
|
14
|
-
class
|
14
|
+
class QuotaExceededError < Error
|
15
15
|
end
|
16
16
|
|
17
|
-
class
|
17
|
+
class OperationTimeOutError < Error
|
18
18
|
end
|
19
19
|
|
20
|
-
class
|
20
|
+
class RateLimitExceededError < Error
|
21
21
|
end
|
22
22
|
|
23
|
-
class
|
23
|
+
class ConflictError < Error
|
24
24
|
end
|
25
25
|
|
26
|
-
class
|
26
|
+
class ForbiddenError < Error
|
27
|
+
end
|
28
|
+
|
29
|
+
class UnknownError < Error
|
27
30
|
end
|
28
31
|
end
|
data/lib/qna_maker/string.rb
CHANGED
@@ -2,7 +2,15 @@ require 'htmlentities'
|
|
2
2
|
|
3
3
|
CODER = HTMLEntities.new
|
4
4
|
|
5
|
+
#
|
6
|
+
# Monkey-patched String method for fixing responses.
|
7
|
+
#
|
5
8
|
class String
|
9
|
+
#
|
10
|
+
# Fixes <, & and other &'s characters, replaces crlf to lf newlines.
|
11
|
+
#
|
12
|
+
# @return [String] same text with correctly displayed &xxxx; characters and lf newlines.
|
13
|
+
#
|
6
14
|
def normalize
|
7
15
|
CODER.decode(self).gsub(/\r\n?/, "\n")
|
8
16
|
end
|
data/lib/qna_maker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qna_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Birdie0
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: htmlentities
|