-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_q_generator.py
61 lines (51 loc) · 1.7 KB
/
llm_q_generator.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Author: Harsh Kohli
# Date Created: 23-12-2023
import os
from utils.ioutils import read_from_json, write_to_json
from openai import OpenAI
from constants import prompt1, prompt2, conditions_file, data_file
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
temp = 0.0
conditions_data = read_from_json(conditions_file)
for sample in conditions_data:
templates = sample['condition_templates']
paraphrased_conditions = []
user_conditions = ''
for template in templates:
one_print = ''
if len(template) > 1:
one_print = 'Either '
for index, y in enumerate(template):
one_print = one_print + y
if index != (len(template) - 1):
one_print = one_print + ', or '
completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt1 + one_print,
}
],
temperature=temp,
# model="gpt-4",
model="gpt-4-1106-preview",
)
paraphrased_condition = completion.choices[0].message.content
paraphrased_conditions.append(paraphrased_condition)
user_conditions = user_conditions + paraphrased_condition + ' '
completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt2 + user_conditions,
}
],
temperature=temp,
# model="gpt-4",
model="gpt-4-1106-preview",
)
final_requirement = completion.choices[0].message.content
sample['final_question'] = final_requirement
write_to_json(conditions_data, data_file)