You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
2.4 KiB

import json
import logging
from openai import OpenAI
from foodie_config import OPENAI_API_KEY, AUTHORS, LIGHT_TASK_MODEL
from datetime import datetime, timezone
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
client = OpenAI(api_key=OPENAI_API_KEY)
def generate_background(author):
# Include the DOB in the prompt to contextualize the timeline
dob = author.get('dob', '1980-01-01') # Fallback DOB if not provided
current_year = datetime.now().year
birth_year = int(dob.split('-')[0])
age = current_year - birth_year # Calculate approximate age
prompt = (
f"Generate a fictional background for a food writer named {author['username']} with the persona '{author['persona']}'. "
f"They were born on {dob} and are currently {age} years old. Use this to create a realistic timeline for their life events. "
"Include: hometown, cultural influences, an early food memory (from their childhood, appropriate for their birth year), "
"and how they became a food writer (considering their age and career timeline). "
f"Match the tone of their bio: '{author['bio']}'. Keep it concise (100-150 words). "
"Return as JSON: {'username': '...', 'hometown': '...', 'cultural_influences': '...', 'early_memory': '...', 'career_path': '...'}"
)
try:
response = client.chat.completions.create(
model=LIGHT_TASK_MODEL,
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": f"Generate background for {author['username']}."}
],
max_tokens=200
)
raw_result = response.choices[0].message.content.strip()
cleaned_result = raw_result.replace('```json', '').replace('```', '').strip()
return json.loads(cleaned_result)
except Exception as e:
logging.error(f"Failed to generate background for {author['username']}: {e}")
return None
def main():
backgrounds = []
for author in AUTHORS:
background = generate_background(author)
if background:
backgrounds.append(background)
logging.info(f"Generated background for {author['username']}")
with open('/home/shane/foodie_automator/author_backgrounds.json', 'w') as f:
json.dump(backgrounds, f, indent=2)
logging.info("Saved backgrounds to author_backgrounds.json")
if __name__ == "__main__":
main()