-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpt4-data.py
More file actions
58 lines (48 loc) · 1.57 KB
/
Copy pathgpt4-data.py
File metadata and controls
58 lines (48 loc) · 1.57 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
import openai
import json
from openai import OpenAI
import os
# Load your OpenAI API key
os.environ["OPENAI_API_KEY"] = 'key'
client = OpenAI()
def generate_qa_pairs(num_pair):
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{
"role": "system",
"content": "You are an expert on providing Q&A pairs on Harry Potter topic. Each Q and A would be in one line. Your respond will be in the format: \nQuestion Answer\nQuestion Answer"
},
{
"role": "user",
"content": f"Give me {num_pair} pairs about harry potter"
}
],
temperature=0.8,
max_tokens=512,
top_p=1,
frequency_penalty=0.4,
presence_penalty=0.4
)
return response
# Generate the QA pairs
trails = 4
qa_dict = []
for n in range(trails):
qa_text = generate_qa_pairs(10).choices[0].message.content
print(qa_text)
# parse and convert it into JSON
for qa_pair in qa_text.split('\n'):
qa_dict.append({"question": qa_pair.split('?')[0].strip()+'?',
"answer": qa_pair.split('?')[1].strip()})
# Save the results to a JSON file
try:
with open('qa_pairs.json', 'r') as f:
data = json.load(f)
except FileNotFoundError:
data = []
print(data)
data.extend(qa_dict)
with open('qa_pairs.json', 'w') as f:
json.dump(data, f, indent=2)
print("QA pairs have been saved to 'qa_pairs.json'.")