-
Notifications
You must be signed in to change notification settings - Fork 0
/
QnAMaker.py
35 lines (30 loc) · 1.27 KB
/
QnAMaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
import json
import random
# url = "https://caboqna.azurewebsites.net/qnamaker/knowledgebases/e6f059e2-e1c1-44c8-a494-6e3a385a1979/generateAnswer"
url = "https://caboqna.azurewebsites.net/qnamaker/knowledgebases/d7955609-c5a4-43d3-b59b-500e40a09636/generateAnswer"
def QnAMaker(message_text):
# 發送request到QnAMaker Endpoint要答案
response = requests.post(
url,
json.dumps({'question': message_text,
'scoreThreshold': 90, #信賴分數,準確度
}),
headers={
'Content-Type': 'application/json',
'Authorization': '8fb473c8-97c9-426e-943f-bc8b403fd157'
}
)
data = response.json()
print('respone =', data)
try:
#我們使用免費service可能會超過限制(一秒可以發的request數)
if "error" in data:
return data["error"]["message"]
# 隨機選一個答案
answer = random.choice(data['answers'][0]['answer'].split(','))
if answer == 'No good match found in KB.':
return
return answer
except Exception:
return "Error occurs when finding answer"