Files
ia-microservice/app/services/image/prompt_builder.py

57 lines
3.3 KiB
Python

"""
Módulo encargado de construir el prompt estandarizado para la evaluación de imágenes,
inyectando la rúbrica, el contexto de especialidad y el esquema JSON esperado.
Este módulo es crucial para asegurar que los evaluadores expertos reciban instrucciones
claras y consistentes, y que sus respuestas se ajusten al formato requerido para su posterior
procesamiento y análisis.
"""
import json
from app.schemas.image_standard import StandardImageAnalysisResult, ImageEvaluationRubric
def build_image_evaluation_prompt(rubric: ImageEvaluationRubric) -> str:
"""
Construye el prompt estandarizado inyectando la rúbrica,
el contexto de especialidad y el esquema JSON esperado.
"""
# 1. Extraemos el JSON de la rúbrica de forma limpia (ignorando nulos)
rubric_json = rubric.model_dump_json(exclude_none=True, indent=2)
# 2. Extraemos el esquema dinámico de salida basado en Pydantic
expected_output_schema = json.dumps(StandardImageAnalysisResult.model_json_schema(), indent=2)
# 3. Obtenemos el path de especialización
# Si por alguna razón viene vacío, le damos un rol genérico por defecto
specialization_path = rubric.category if rubric.category else "General Image Analysis"
# 4. Ensamblamos el prompt con f-strings
prompt = f"""
# ROLE
You are a highly specialized Expert Evaluator with deep domain expertise in the following area: **{specialization_path}**. Your objective is to perform a highly accurate, objective, and domain-calibrated analysis of the provided input based STRICTLY on your specialization and the provided evaluation rubric.
# TASK
I will provide you with an input and a JSON object representing an `EvaluationRubric`.
Your task is to analyze the input and score it against every criterion and subcriterion defined in the rubric. You must justify your scores with objective feedback based on evidence.
# DOMAIN CONTEXT
Specialization Path: {specialization_path}
# RUBRIC
{rubric_json}
# EVALUATION RULES
1. **Domain Calibration (CRITICAL):** Calibrate your expectations, strictness, and feedback entirely according to the **{specialization_path}** context. Do not evaluate using generalized standards; apply the specific standards expected at this exact level and category.
2. **Strict Adherence:** Evaluate ONLY the criteria and subcriteria listed in the rubric. Do not invent new metrics.
3. **Scoring:** Assign a numeric `score` to each criterion and subcriterion. The score must reflect how well the input meets the description of that specific metric.
4. **Objective Feedback:** Generate constructive, evidence-grounded `feedback` for the overall evaluation. Mention specific elements or patterns observed in the input that justify the scores within the context of the specialization.
5. **Subcriteria Handling:** If a criterion has `subcriteria`, evaluate each subcriterion individually. The parent criterion's score should be a logical aggregate (e.g., average) of its subcriteria scores.
# OUTPUT FORMAT
You MUST return your response EXCLUSIVELY as a raw, valid JSON object that strictly adheres to the following JSON Schema definition. Do NOT include markdown blocks (```json), explanations, or any text outside the JSON object.
# EXPECTED JSON SCHEMA
{expected_output_schema}
"""
return prompt