o
    È¤TjV
  ã                   @  sr   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 g d¢Z
e d	¡ZeG d
d„ dƒƒZG dd„ dƒZdS )aH  Output guardrails.

The fine-tune teaches the model to behave; guardrails are the deterministic safety net that runs
*after* generation:
  - require an inline citation when we actually retrieved context,
  - block personalized financial/"which plan should I buy" advice,
  - escalate to a human when we have no grounded answer.
é    )ÚannotationsN)Ú	dataclassé   )ÚRetrievedChunk)ÚConfig)z7\byou should (?:buy|choose|purchase|get|pick|go with)\bz \bi recommend (?:that you|you)\bz+\bthe best (?:plan|policy|option) for you\bz\bi'd (?:suggest|advise) you\bz\[\d+\]c                   @  s*   e Zd ZU ded< ded< dZded< dS )ÚGuardrailResultÚstrÚanswerÚboolÚokÚ ÚreasonN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   © r   r   úZC:\Users\cheed\OneDrive\Desktop\Claude work\travel-insurance-agent\src\agent\guardrails.pyr      s   
 r   c                   @  s    e Zd Zddd„Zddd„ZdS )Ú
GuardrailsÚcfgr   c                 C  s8   t | dd¡ƒ| _| dd¡ ¡ | _dd„ tD ƒ| _d S )Nzguardrails.require_citationTzguardrails.escalation_messagezCI can't answer that confidently. Please contact a licensed advisor.c                 S  s   g | ]	}t  |t j¡‘qS r   )ÚreÚcompileÚ
IGNORECASE)Ú.0Úpr   r   r   Ú
<listcomp>+   s    z'Guardrails.__init__.<locals>.<listcomp>)r
   ÚgetÚrequire_citationÚstripÚ
escalationÚ_ADVICE_PATTERNSÚ_advice_res)Úselfr   r   r   r   Ú__init__%   s   þýzGuardrails.__init__r	   r   Úchunksúlist[RetrievedChunk]Úreturnr   c                 C  sŽ   |pd  ¡ }|st| jddd�S |st| jddd�S | jD ]}| |¡r0t| jd ddd�  S q| jrAt |¡sAt| jddd�S t|d	d
�S )Nr   FZ
no_context)r   r   Zempty_answerzY (I can explain the differences between plans, but I can't tell you which one to choose.)Zpersonalized_adviceZmissing_citationT)r   )r   r   r   r!   Úsearchr   Ú_CITATION_RE)r"   r	   r$   ÚtextZrxr   r   r   Úcheck-   s$   

ÿ
ûÿ
zGuardrails.checkN)r   r   )r	   r   r$   r%   r&   r   )r   r   r   r#   r*   r   r   r   r   r   $   s    
r   )Ú__doc__Ú
__future__r   r   Údataclassesr   Úpromptr   Úsettingsr   r    r   r(   r   r   r   r   r   r   Ú<module>   s    
