import asyncio
from openai import AsyncOpenAI
from typing import List, Dict, Any
async_client = AsyncOpenAI()
SYSTEM_PROMPT = "You are a customer-support assistant."
async def _generate_related_questions_from_prompt(
prompt: str, k: int, sem: asyncio.Semaphore, *, model: str
) -> List[str]:
"""Return *k* distinct customer-service questions related to the given prompt."""
out: List[str] = []
async with sem:
for _ in range(k):
resp = await async_client.responses.create(
model=model,
input=[
{
"role": "system",
"content": (
"Return ONE distinct, realistic customer-service question "
"related in topic or theme to the following question, "
"but NOT a direct paraphrase."
),
},
{"role": "user", "content": prompt},
],
temperature=0.9,
max_output_tokens=60,
)
out.append(resp.output_text.strip())
return out
async def expand_prompt_pool(
prompts: List[str], *, k: int = 3, concurrency: int = 32, model: str
) -> List[str]:
"""Expand each prompt into *k* related questions using the given model."""
sem = asyncio.Semaphore(concurrency)
tasks = [
_generate_related_questions_from_prompt(p, k, sem, model=model) for p in prompts
]
results = await asyncio.gather(*tasks)
return [v for sub in results for v in sub]
async def _generate_preference_pair(
prompt: str, sem: asyncio.Semaphore, *, model: str
) -> Dict[str, Any]:
"""Generate a preference pair for the given prompt."""
async with sem:
friendly_task = async_client.responses.create(
model=model,
input=[
{
"role": "system",
"content": (
"You are Good Vibes Corp's exceptionally energetic, outrageously friendly and "
"enthusiastic support agent."
),
},
{"role": "user", "content": prompt},
],
temperature=0.7, # higher temperature to increase creativity & on-brand tone adherence
max_output_tokens=80,
)
blunt_task = async_client.responses.create(
model=model,
input=[
{
"role": "system",
"content": "You are a terse, factual support agent with no empathy or politeness.",
},
{"role": "user", "content": prompt},
],
temperature=0.3, # lower temperature to limit creativity & emphasize tonal difference
max_output_tokens=80,
)
friendly, blunt = await asyncio.gather(friendly_task, blunt_task)
return {
"input": {
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
]
},
"preferred_output": [
{"role": "assistant", "content": friendly.output_text}
],
"non_preferred_output": [
{"role": "assistant", "content": blunt.output_text}
],
}