""" GPT-based researcher implementation. Uses the latest Responses API with text_format for structured outputs. """ from openai import OpenAI from backend.pydantic_schema import ImageAdEssentialsOutput from backend.prompt import get_system_prompt, get_user_prompt from dotenv import load_dotenv import os load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") def researcher_gpt(target_audience: str, product_category: str, product_description: str): """ GPT-based researcher function using the Responses API. Args: target_audience: Target audience from the predefined list product_category: Product category (e.g., "ring", "bangles") product_description: Description of the product Returns: list[ImageAdEssentials]: List of psychology triggers, angles, and concepts """ # Initialize GPT client gpt_client = OpenAI(api_key=OPENAI_API_KEY) # Get prompts system_prompt = get_system_prompt() user_prompt = get_user_prompt(target_audience, product_category, product_description) # Use the Responses API with text_format for structured output response = gpt_client.responses.parse( model="gpt-4o", instructions=system_prompt, input=[ { "role": "user", "content": user_prompt } ], text_format=ImageAdEssentialsOutput, ) # output_parsed returns the Pydantic model directly if response.output_parsed: return response.output_parsed.output else: raise ValueError("GPT returned an unparseable response.")