-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone-shot-openai.py
69 lines (55 loc) · 2.53 KB
/
one-shot-openai.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
62
63
64
65
66
67
68
69
import openai
import csv
import time
# Define the prompt message
message = """Generate a doctor profile in the United States for UX research purposes. Try to not have the same names as the ones you generated earlier and each persona should be independent of each other.
Ensure that each profile has complete details and a well-described short biography.
Keep the formatting of each response consistent.
Include the following characteristics in exactly this format:
Name:
Age:
Gender:
Ethnicity/Race: [Only choose from BLS-categorized ethnicities]
Income:
Primary motivations: [a short one-paragraph biography describing the doctor's reasons for becoming a doctor]
Short Biography: [a detailed one-paragraph biography describing the doctor's background, experience, and personality]
Only respond to the chat with profile information, no filler text on what you are doing or anything else please. No profile labels like (Profile 1, 2, etc.). Please separate individual profiles by 1 line.
For instance:
Name: [Name]
Age: [Age]
Gender: [Gender]
Ethnicity/Race: [Only choose from BLS-categorized ethnicities]
Income: [Exact salary amount with no dollar sign]
Primary motivations: [a short one-paragraph biography describing the doctor's reasons for becoming a doctor]
Short Biography: [a detailed one-paragraph biography describing the doctor's background, experience, and personality]
Keep exactly this format.
"""
# Define the headers for the CSV file
csv_headers = ["Name", "Age", "Gender", "Ethnicity/Race", "Income", "Primary motivations", "Short Biography"]
openai.api_key = #key
with open("o.csv", mode="a", newline="") as file:
writer = csv.writer(file)
for i in range(10000):
try:
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": message}]
)
reply = response.choices[0].message.content
print(reply)
attributes = reply.split("\n")
data = []
for header in csv_headers:
found = False
for attr in attributes:
if attr.startswith(header + ":"):
# Extract the text after ": "
data.append(attr.split(": ", 1)[1].strip())
found = True
break
if not found:
data.append("")
writer.writerow(data)
except Exception as e:
print("Error generating entry:", e)
time.sleep(1)