1
+ import json
2
+ import asyncio
3
+ import requests
4
+
5
+ class QnAMaker :
6
+
7
+ __qnaMakerServiceEndpoint = 'https://westus.api.cognitive.microsoft.com/qnamaker/v3.0/knowledgebases/'
8
+ __json_mime_type = 'application/json'
9
+ __api_management_header = 'Ocp-Apim-Subscription-Key'
10
+
11
+ def __init__ (self , options , http_client ):
12
+
13
+ self .__http_client = _http_client or raise TypeError ('HTTP Client failed' )
14
+ self .__options = options or raise TypeError ('Options config error' )
15
+
16
+ self .__answerUrl = "%s%s/generateanswer" % (__qnaMakerServiceEndpoint ,options .knowledge_base_id )
17
+
18
+ if self .__options .ScoreThreshold == 0 :
19
+ self .__options .ScoreThreshold = 0.3 #Note - SHOULD BE 0.3F 'FLOAT'
20
+
21
+ if self .__options .Top == 0 :
22
+ self .__options .Top = 1
23
+
24
+ if self .__options .StrictFilters == None :
25
+ self .__options .StrictFilters = MetaData ()
26
+
27
+ if self .__options .MetadataBoost == None :
28
+ self .__options .MetadataBoost = MetaData ()
29
+
30
+ async def get_answers (question ): # HTTP call
31
+ headers = {
32
+ "Ocp-Apim-Subscription-Key" : __api_management_header ,
33
+ "Content-Type" : __json_mime_type
34
+ }
35
+
36
+ payload = json .dumps ({
37
+ "question" : question
38
+ })
39
+ # POST request to QnA Service
40
+ content = requests .post (self .__answerUrl , headers = headers , data = payload )
41
+ return content .json ()
42
+
43
+ class QnAMakerOptions :
44
+ def __init__ (self , subscription_key , knowledge_base_id , score_threshhold , top , strict_filters , metadata_boost ):
45
+ self .subscription_key = subscription_key
46
+ self .knowledge_base_id = knowledge_base_id
47
+ self .score_threshhold = score_threshhold
48
+ self .top = top
49
+ self .strict_filters = strict_filters
50
+ self .metadata_boost = metadata_boost
51
+
52
+ class MetaData :
53
+ def __init__ (self , name , value ):
54
+ self .name = name
55
+ self .value = value
56
+
57
+ class QueryResult :
58
+ def __init__ (self , questions , answer , score , metadata , source , qna_id ):
59
+ self .questions = questions
60
+ self .answer = answer
61
+ self .score = score
62
+ self .metadata = metadata
63
+ self .source = source
64
+ self .qna_id = qna_id
65
+
66
+ class QueryResults :
67
+ def __init__ (self , answers ):
68
+ self .__answers = answers
0 commit comments