-
Notifications
You must be signed in to change notification settings - Fork 0
/
qna.py
43 lines (32 loc) · 1020 Bytes
/
qna.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
36
37
38
39
40
41
42
43
# imports
import requests
import dotenv
import os
# .env init
dotenv.load_dotenv()
# ask queries
def ask(que):
url = os.getenv('QNA_URL')
payload = '{"question":"' + que + '"}'
headers = {
'Authorization': 'EndpointKey ' + os.getenv('QNA_AUTH_KEY'),
'Content-Type': 'application/json',
'Cookie': os.getenv('QNA_COOKIE')
}
response = requests.request("POST", url, headers=headers, data = payload)
return response.json()['answers'][0]['answer']
# search results
def search(que):
url = os.getenv('QNA_URL')
payload = '{"question":"' + que + '","top":3}'
headers = {
'Authorization': 'EndpointKey ' + os.getenv('QNA_AUTH_KEY'),
'Content-Type': 'application/json',
'Cookie': os.getenv('QNA_COOKIE')
}
response = requests.request("POST", url, headers=headers, data = payload)
answers = [{
'answer': ans['answer'],
'qid': ans['id']
} for ans in response.json()['answers']]
return answers