o
    úUjt
  ã                   @  sf  d Z ddlmZ ddlZddlZddlmZ ej de	ee
ƒ ¡ jd d ƒ¡ ddlmZmZmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ eddd�Zee
ƒ ¡ jd Zej d¡Z ee ƒZ!ee!dd�Z"da#dd„ Z$G dd„ deƒZ%G dd„ deƒZ&e d¡d-dd„ƒZ'e d¡d.d d!„ƒZ(ej)d"e&d#�d/d%d&„ƒZ*e )d'¡ed(ƒfd0d+d,„ƒZ+dS )1a  FastAPI service exposing the travel-insurance agent.

    uvicorn app.server:app --host 0.0.0.0 --port 8000

The LLM is called lazily, so the API + RAG + cache layer boots even if the vLLM server isn't up
yet (useful for testing retrieval/cache in isolation).
é    )ÚannotationsN)ÚPathé   Úsrc)ÚFastAPIÚFileÚHTTPExceptionÚ
UploadFile)ÚFileResponse)Ú	BaseModelÚField)ÚAgent)Úload_configzTravel-Insurance Agentz0.1.0)ÚtitleÚversionZstaticZAGENT_CONFIGT)Zlazy_llmc                  C  s    t d u rddlm}  | tƒa t S )Nr   ©ÚTranscriber)Ú_transcriberZ	agent.sttr   Ú_cfgr   © r   úPC:\Users\cheed\OneDrive\Desktop\Claude work\travel-insurance-agent\app\server.pyÚ_get_transcriber"   s   r   c                   @  s$   e Zd ZU edddd�Zded< dS )ÚChatRequest.r   z$The user's travel-insurance question)Z
min_lengthÚdescriptionÚstrÚqueryN)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__annotations__r   r   r   r   r   +   s   
 r   c                   @  sv   e Zd ZU ded< ded< ded< ded< dZded	< d
Zded< dZded< dZded< dZded< dZ	ded< d
S )ÚChatResponser   Úanswerz	list[str]Z	citationsÚboolÚcachedZgroundedÚ Úguardrail_reasonNzfloat | NoneÚ
similarityr   ÚintÚprompt_tokensÚcompletion_tokensÚtotal_tokensZllmÚroute)
r   r   r   r   r%   r&   r(   r)   r*   r+   r   r   r   r   r    /   s   
 r    ú/Úreturnr
   c                   C  s   t td ƒS )zNServe the voice web UI (must be loaded via http://localhost so the mic works).z
index.htmlN)r
   Ú
STATIC_DIRr   r   r   r   Úindex<   s   r/   z/healthÚdictc                   C  s   ddiS )NÚstatusÚokr   r   r   r   r   ÚhealthB   s   r3   z/chat)Zresponse_modelÚreqc                 C  s    t  | j¡}tdi t  |¡¤ŽS )Nr   )Ú_agentr!   r   r    Zto_dict)r4   Zrespr   r   r   ÚchatG   s   r6   z/transcribe.Úaudior	   c              
   Ã  sr   �zt ƒ }W n ty   tddƒ‚w |  ¡ I dH }z
| |¡}W d|iS  ty8 } ztdd|› �ƒ‚d}~ww )zXTranscribe uploaded mic audio locally (faster-whisper). Audio never leaves this machine.i÷  z7Local STT not installed. Run: pip install -e '.[voice]'Niô  ztranscription failed: Útext)r   ÚImportErrorr   ÚreadÚ
transcribeÚ	Exception)r7   ÚtÚdatar8   Úer   r   r   r;   M   s   €

ÿþ€ÿr;   )r-   r
   )r-   r0   )r4   r   r-   r    )r7   r	   r-   r0   ),Ú__doc__Ú
__future__r   ÚosÚsysÚpathlibr   ÚpathÚinsertr   Ú__file__ÚresolveÚparentsZfastapir   r   r   r	   Zfastapi.responsesr
   Zpydanticr   r   Zagent.pipeliner   Zagent.settingsr   ÚappÚparentr.   ÚenvironÚgetZ_config_pathr   r5   r   r   r   r    r/   r3   Úpostr6   r;   r   r   r   r   Ú<module>   s8    $	