File size: 6,307 Bytes
bdfb457 | 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | import ast
import json
import random
import time
import names
from google import genai
# ----------------------------
# Gemini Client
# ----------------------------
client = genai.Client(
api_key="API"
)
# ----------------------------
# Models (fallback order)
# ----------------------------
MODELS = [
"gemini-3.6-flash",
"gemini-3.5-flash",
"gemini-3.1-flash-lite",
"gemini-3.5-flash-lite",
"gemini-2.5-flash",
"gemini-2.5-flash-lite",
"gemini-3.1-flash-lite-preview",
"gemini-2.5-pro",
"gemini-3.1-pro-preview",
"gemini-3-pro-preview",
"gemini-pro-latest",
"gemini-flash-latest"
]
current_model = 0
# ----------------------------
# Generation Settings
# ----------------------------
generation_config = {
"max_output_tokens": 65536,
"thinking_level": "medium",
"temperature": 1.2,
}
# ----------------------------
# Load system instruction
# ----------------------------
with open("EmailPrompt.txt", "r", encoding="utf-8") as f:
inst = f.read().strip()
# ----------------------------
# Load email topics
# ----------------------------
with open("Topics.txt", "r", encoding="utf-8") as f:
topics = ast.literal_eval(f.read())
# ----------------------------
# Name Generator
# ----------------------------
def generate_person_name(role):
"""
Generates names with occasional titles.
Recipients are more likely to have titles because
professional emails often address people formally.
"""
name = names.get_full_name()
if role == "recipient":
title_chance = 0.35
else:
title_chance = 0.10
if random.random() < title_chance:
title = random.choice([
"Mr.",
"Mrs.",
"Ms.",
"Dr.",
"Prof."
])
return f"{title} {name}"
return name
# ----------------------------
# Prompt Generator
# ----------------------------
def create_email_prompt(topic):
sender = generate_person_name("sender")
recipient = generate_person_name("recipient")
templates = [
f"Write an email about {topic}.",
f"Create an email from {sender} to {recipient} about {topic}.",
f"Write an email from {sender} explaining {topic} to {recipient}.",
f"Write an email to {recipient} regarding {topic}.",
f"Write an email where {sender} needs to discuss {topic} with {recipient}.",
f"Generate a professional email from {sender} concerning {topic}.",
f"Write an email from {sender} requesting something related to {topic}.",
f"Create an email that {sender} would send to {recipient} about {topic}.",
f"Write an email addressed to {recipient} explaining the situation involving {topic}.",
f"Generate a workplace-style email from {sender} about {topic}.",
f"Write an email where an employee contacts {recipient} about {topic}.",
f"Write an email where I need to communicate {topic} to someone important.",
f"Create a formal email discussing {topic}.",
f"Write a message that could be sent to a supervisor about {topic}.",
f"Write an email from a person named {sender} about {topic}."
]
return random.choice(templates)
# ----------------------------
# Generate Dataset
# ----------------------------
with open("terrible_emails_dataset.jsonl", "w", encoding="utf-8") as outfile:
for topic in topics:
prompt = create_email_prompt(topic)
print(
f"[{MODELS[current_model]}] Generating: {topic}"
)
while True:
try:
interaction = client.interactions.create(
model=MODELS[current_model],
input=prompt,
system_instruction=inst,
generation_config=generation_config,
)
assistant_response = interaction.output_text
break
except Exception as e:
error = str(e)
error_lower = error.lower()
print(error)
# ----------------------------
# Quota handling
# ----------------------------
if any(x in error_lower for x in (
"resource_exhausted",
"quota",
"daily limit",
"per day",
"429",
"rate limit",
)):
if current_model < len(MODELS) - 1:
current_model += 1
print(
f"Switching to {MODELS[current_model]}"
)
continue
raise RuntimeError(
"All models exhausted quota."
)
# ----------------------------
# Temporary errors
# ----------------------------
if any(x in error_lower for x in (
"500",
"502",
"503",
"504",
"internal",
"timeout",
"connection",
"unavailable",
)):
print(
"Temporary error. Retrying in 15 seconds..."
)
time.sleep(15)
continue
raise
dataset_entry = {
"messages": [
{
"role": "user",
"content": prompt,
},
{
"role": "assistant",
"content": assistant_response,
},
]
}
outfile.write(
json.dumps(
dataset_entry,
ensure_ascii=False
)
)
outfile.write("\n")
outfile.flush()
print("Done!") |