Uses OpenAI API to classify whether each main category has deficiencies (booleans). Returns a list of category names predicted as True.
(model_output_text: str)
| 186 | |
| 187 | |
| 188 | def classify_categories(model_output_text: str) -> List[str]: |
| 189 | """ |
| 190 | Uses OpenAI API to classify whether each main category has deficiencies (booleans). |
| 191 | Returns a list of category names predicted as True. |
| 192 | """ |
| 193 | input_text = extract_answer_content(model_output_text) |
| 194 | main_categories = list(DEFICIENCY_CATEGORIES.keys()) |
| 195 | |
| 196 | # Load prompt from file |
| 197 | prompt_template = load_classify_categories_prompt() |
| 198 | prompt = prompt_template.format( |
| 199 | categories=json.dumps(main_categories, indent=2), |
| 200 | input_text=input_text |
| 201 | ) |
| 202 | |
| 203 | try: |
| 204 | client = openai.OpenAI( |
| 205 | api_key=os.getenv("OPENAI_API_KEY"), |
| 206 | base_url=os.getenv("OPENAI_API_BASE_URL") |
| 207 | ) |
| 208 | |
| 209 | model_type = os.getenv("MODEL_TYPE", "gpt-4o-2024-08-06") |
| 210 | |
| 211 | completion = client.chat.completions.create( |
| 212 | model=model_type, |
| 213 | messages=[ |
| 214 | {"role": "system", "content": "You are an expert in slide design analysis. Your output must be a valid JSON object with booleans per category."}, |
| 215 | {"role": "user", "content": prompt} |
| 216 | ], |
| 217 | response_format={"type": "json_object"}, |
| 218 | temperature=0.0 |
| 219 | ) |
| 220 | |
| 221 | response_text = completion.choices[0].message.content |
| 222 | if not response_text: |
| 223 | return [] |
| 224 | |
| 225 | parsed = CategoryBooleanResponse.model_validate_json(response_text) |
| 226 | bools = { |
| 227 | "Composition & Layout": getattr(parsed, "composition_layout", False), |
| 228 | "Typography": getattr(parsed, "typography", False), |
| 229 | "Imagery & Visualizations": getattr(parsed, "imagery_visualizations", False), |
| 230 | } |
| 231 | return [name for name, flag in bools.items() if flag] |
| 232 | |
| 233 | except Exception as e: |
| 234 | print(f"Error calling OpenAI API or parsing response: {e}") |
| 235 | return [] |
| 236 | |
| 237 | |
| 238 | class MultiTaskTester: |
no test coverage detected