forked from daveshap/NLCA_Question_Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugment_questions.py
More file actions
61 lines (52 loc) · 1.93 KB
/
Copy pathaugment_questions.py
File metadata and controls
61 lines (52 loc) · 1.93 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import openai
import os
import re
with open('openaiapikey.txt', 'r') as infile:
open_ai_api_key = infile.read()
openai.api_key = open_ai_api_key
ctxdir = 'C:/RavenFinetune/contexts/'
outdir = 'C:/RavenFinetune/questions/'
files = os.listdir(outdir)
prompt_name = 'p_questions_important.txt'
def load_prompt(filename, payload):
with open('C:/RavenFinetune/%s' % filename, 'r', encoding='utf-8') as infile:
body = infile.read()
body = body.replace('<<TEXT>>', payload)
return body
def completion(prompt, engine='davinci-instruct-beta', temp=0.9, top_p=0.95, tokens=200, freq_pen=0.5, pres_pen=0.5, stop=['\n\n']):
try:
response = openai.Completion.create(
engine=engine,
prompt=prompt,
temperature=temp,
max_tokens=tokens,
top_p=top_p,
frequency_penalty=freq_pen,
presence_penalty=pres_pen,
stop=stop)
text = response['choices'][0]['text'].strip().splitlines()
questions = ''
for t in text:
if '?' not in t:
continue
questions += t.replace('-','').strip() + '\n'
questions = questions.strip()
return questions
except Exception as oops:
print('ERROR in completion function:', oops)
for f in files:
# check number of existing questions, 4 is the target
with open(outdir + f, 'r', encoding='utf-8') as infile:
priorquestions = infile.read()
if len(priorquestions.splitlines()) >= 4:
continue
# continue with script
with open(ctxdir + f, 'r', encoding='utf-8') as infile:
context = infile.read()
prompt = load_prompt(prompt_name, context)
questions = completion(prompt)
print('\n\n\tCONTEXT:\n', context)
print('\tQUESTIONS:\n', questions)
print('\tFILE:', f)
with open(outdir + f, 'w', encoding='utf-8') as outfile:
outfile.write(priorquestions + '\n' + questions)