Ë
    »¦iR  ã                   ó(  — d dl mZmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlm Z   G d„ de«      Z! G d„ de«      Z" G d„ de«      Z# G d„ de«      Z$ G d„ d«      Z%y)é    )Ú	BaseModelÚField)Ú	TypedDict)Ú	AnnotatedÚListÚDictÚLiteralÚOptionalÚTupleÚUnion)Úsettings)ÚTool)Ú
ChatOpenAI)ÚToolNode)Ú
StateGraphÚSTARTÚENDÚadd_messages)ÚCompiledStateGraph)ÚAIMessageChunk)Úparse_partial_json)ÚSystemMessagec                   ó"   — e Zd ZU eeef   ed<   y)ÚStateÚmessagesN)Ú__name__Ú
__module__Ú__qualname__r   Úlistr   Ú__annotations__© ó    ú2/var/www/html/hotal-chatbot-be/app/rag/__init__.pyr   r      s   … Ø˜˜lÐ*Ñ+Ô+r"   r   c            	       ó@   — e Zd ZdZddeeef   dee   deeef   fd„Z	y)ÚRetrieverToolz;Custom Tool Just For Override _to_args_and_kwargs function.NÚ
tool_inputÚtool_call_idÚreturnc                 ó4   — t        |t        «      r|fi fS d|fS )Nr!   )Ú
isinstanceÚstr)Úselfr&   r'   s      r#   Ú_to_args_and_kwargsz!RetrieverTool._to_args_and_kwargs   s#   € Ü�j¤#Ô&Ø�= "Ð$Ð$à�z�>Ð!r"   ©N)
r   r   r   Ú__doc__r   r+   r   r
   r   r-   r!   r"   r#   r%   r%      s:   „ ÙEñ"¨e°C¸°IÑ.>ð "ÈhÐWZÉmð "ÐglÐmrÐtxÐmxÑgyô "r"   r%   c            
       ó¢   — e Zd ZU  ed¬«      Zeed<    ed¬«      Zeed<    ed¬«      Ze	ed<    ed¬«      Z
e	ed	<   dedede	d	e	d
df
d„Zdd„Zy)ÚSearchRoomsSchemazThe start date of the stay©ÚdescriptionÚ	from_datezThe end date of the stayÚto_datezThe number of adultsÚadultszThe number of childrenÚchildrenr(   Nc                 ó<   — || _         || _        || _        || _        y r.   )r4   r5   r6   r7   )r,   r4   r5   r6   r7   s        r#   Ú__init__zSearchRoomsSchema.__init__$   s   € Ø"ˆŒØˆŒØˆŒØ ˆ�r"   c                 ó"   — || j                   |<   y r.   )Ú__dict__)r,   ÚnameÚvalues      r#   Ú__setattr__zSearchRoomsSchema.__setattr__*   s   € Ø#ˆ�‰�dÒr"   )r(   N)r   r   r   r   r4   r+   r    r5   r6   Úintr7   r9   r>   r!   r"   r#   r1   r1      sp   … ÙÐ'CÔD€IˆsÓDÙÐ%?Ô@€GˆSÓ@ÙÐ$:Ô;€FˆCÓ;ÙÐ&>Ô?€HˆcÓ?ð! #ð !°ð !¸Sð !ÈCð !ÐTXó !ô$r"   r1   c                   óL   — e Zd ZU  ed¬«      Zeed<    ed¬«      Zee   ed<   y)ÚClassifyResponsez6Final answer to the user's question in markdown formatr2   ÚanswerzIGive 2-3 related questions from user's perspective to the user's questionÚrelated_questionsN)	r   r   r   r   rB   r+   r    rC   r   r!   r"   r#   rA   rA   -   s0   … ÙÐ$\Ô]€FˆCÓ]Ù#(ð  6Aô  $BÐ�t˜C‘yô  Br"   rA   c                   óx   — e Zd Zd„ Zddefd„Zdee   fd„Zde	de
d   fd	„Zdd
„Zde	fd„Zi fdefd„Zdefd„Zy)Ú
HotelAgentc                 óD   — t        dt        j                  ¬«      | _        y )Nzgpt-5.2)ÚmodelÚapi_key)r   r   ÚOPENAI_API_KEYÚllm©r,   s    r#   r9   zHotelAgent.__init__3   s   € Ü I´x×7NÑ7NÔOˆ�r"   NÚqueryc              ‹   ó   K  — y­w)NzRoom search completedr!   )r,   rL   Ú	callbacksÚkwargss       r#   Údata_retrievalzHotelAgent.data_retrieval6   s	   è ø€ Ø&ùs   ‚r(   c              ƒ   ó\   K  — t        dd| j                  | j                  t        ¬«      gS ­w)NrP   al  Retrieves detailed and up-to-date information about Plaza Beach Resorts, including room availability for specified dates, occupancy limits (adults and children), room types, amenities, and property-specific details. Use this tool only when the requested information is not already available in the system prompt. Do not use it for greetings or unrelated questions.)r<   r3   ÚfuncÚ	coroutineÚargs_schema)r%   rP   r1   rK   s    r#   Ú	get_toolszHotelAgent.get_tools9   s8   è ø€ äØ%ð`ð ×(Ñ(Ø×-Ñ-Ü-ôð
ð 	
ùs   ‚*,Ústate©ÚcontinueÚendc              ƒ   óf   K  — |d   }|d   }|j                   }|sy|d   j                  d«      }y­w)Nr   éÿÿÿÿrY   r   r<   rX   )Ú
tool_callsÚget)r,   rV   r   Úlast_messager\   Ú	tool_names         r#   Úshould_continuezHotelAgent.should_continueJ   sA   è ø€ à˜Ñ$ˆØ ‘|ˆØ!×,Ñ,ˆ
áØà˜q‘M×%Ñ% fÓ-ˆ	àùs   ‚/1c              ƒ   ó²  K  — | j                  «       ƒ d{  –—† }t        t        ¬«      }|j                  d| j                  «       |j                  dt        |¬«      «       |j                  t        d«       |j                  d| j                  dt        dœ«       |j                  dd«       |j                  dt        «       |j                  «       S 7 Œ¾­w)z³
        Builds and compiles the state graph defining agent behavior and tool usage.

        Returns:
            CompiledStateGraph: Compiled graph ready for execution.
        N)Ústate_schemaÚagentÚtools©rd   rW   )rU   r   r   Úadd_noderc   r   Úadd_edger   Úadd_conditional_edgesr`   r   Úcompile)r,   rd   Úbuilders      r#   Úbuild_graphzHotelAgent.build_graphW   s³   è ø€ ð —n‘nÓ&×&ˆÜ¬%Ô0ˆà×Ñ˜ §¡Ô,Ø×Ñ˜¤(°Ô"7Ô8à×Ñœ Ô(Ø×%Ñ%ØØ× Ñ à"Üñô	
ð 	×Ñ˜ 'Ô*Ø×Ñ˜¤#Ô&à�‰Ó Ð ð' 'ús   ‚C–C—B?Cc              ƒ   óF  K  — | j                  «       ƒ d {  –—† }d| j                  _        | j                  j                  |¬«      }| j	                  «       ƒ d {  –—† }|j                  |d   «       |j                  |ddit        ¬«      ƒ d {  –—† }d|giS 7 Œ„7 ŒA7 Œ­w)NTre   r   Úrun_nameÚAgent)ÚconfigÚresponse_format)rU   rJ   Úuse_responses_apiÚ
bind_toolsÚget_agent_promptÚextendÚainvokerA   )r,   rV   rd   Úllm_with_toolsr   Úresponses         r#   rc   zHotelAgent.agentt   s¢   è ø€ à—n‘nÓ&×&ˆà%)ˆ�‰Ô"ØŸ™×,Ñ,°5Ð,Ó9ˆà×.Ñ.Ó0×0ˆØ�‰˜˜jÑ)Ô*à'×/Ñ/ØØ˜wÐ'Ü.ð 0ó ÷ ˆð
 ˜X˜JÐ'Ð'ð 'øð
 1øðús4   ‚B!–B—AB!ÁBÁ5B!ÂBÂ
B!ÂB!ÂB!c              ƒ   ón   K  — d}t        |¬«      g}|j                  |j                  dg «      «       |S ­w)NaÇ  
        You are the official AI assistant for Plaza Beach Resorts (St. Pete Beach, Florida), featuring three hotels: Plaza Beach Hotel (beachfront), Bayview Plaza Waterfront Resort & Marina (waterfront, pet-friendly, couples-friendly), and Bay Palms Waterfront Resort & Marina (family-friendly suites with kitchens).

        Your role:
        - Provide helpful, friendly, and professional information only about these three hotels.
        - You are only here for guide users about the hotels, you can't perform any actions like booking, cancellation, payment, availability check or reservation changes etc.
        - Politely refuse unrelated questions.
        - Don't tell user about the tools you have or what you can't do(until user ask about it), you are only here to answer questions about the hotels.
        - Use the data retrieval tool only if needed and never mention it.
        - Never guess. If information is unavailable, say so politely.
        - Don't give big examples in your response, just answer the question. We have related questions for user to ask you.
        - Keep responses clear, concise, and welcoming.
        
        Important: Your response should be in markdown formatting.
        )ÚcontentÚchat_history)r   rt   r]   )r,   ÚinputsÚsystem_templater   s       r#   rs   zHotelAgent.get_agent_prompt…   s<   è ø€ ðˆô"  /Ô2ð
ˆð 	�‰˜Ÿ
™
 >°2Ó6Ô7àˆùs   ‚35r{   c                óø  K  — | j                  «       ƒ d {  –—† }d}d}d }|j                  |ddid¬«      2 3 d {  –—† }|\  }}|sŒt        |t        «      sŒ"t	        |d«      sŒ/|j
                  sŒ<t        |j
                  «      sŒR|j
                  d   j                  d«      }	|	sŒs||	z  }t        |«      }|�(d	|v r$|d	   t        |«      d  }
||
z  }|
dk(  rŒ¥d
|
dœ­–— || _	        Œµ7 ŒÕ7 Œ²6 d|dœ­–— y ­w)NÚ rm   Úgraphr   )ro   Ústream_modery   r   ÚtextrB   Ústream)ÚeventÚdataÚfinal)
rk   Úastreamr*   r   Úhasattrry   Úlenr]   r   rB   )r,   r{   r   Úfinal_answerrB   Úescaped_textrƒ   ÚchunkÚmetaÚtokenÚ	new_tokens              r#   r†   zHotelAgent.astreamž   s  è ø€ Ø×&Ñ&Ó(×(ˆàˆØˆØˆà Ÿ=™=¨¸ÀWÐ8MÐ[e˜=Ôf÷ 	%�%Ø‰JˆE�$âœ E¬>Õ:¼wÀuÈiÕ?XÐ]b×]jÓ]jÔorÐsx÷  tAñ  tAõ  pBØŸ™ aÑ(×,Ñ,¨VÓ4�ÙØØ Ñ%�Ü1°,Ó?�àÐ+°¸LÑ0HØ ,¨XÑ 6´s¸6³{°|Ð D�IØ˜iÑ'�FØ  B’Ø Ø$,°iÑ@Ó@Ø$�•ð- )øð	%øÐfð$  ¨Ñ6Ô6ùsN   ‚C:–C,— C:·C0»C.¼C0¿C:ÁC:ÁC:Á&C:Á3C:Â	A$C:Ã.C0Ã0
C:r.   )r(   r   )r   r   r   r9   r+   rP   r   r%   rU   r   r	   r`   rk   rc   r   rs   Údictr†   r!   r"   r#   rE   rE   1   sl   „ òPñ'¨ó 'ð
  mÑ!4ó 
ð"¨5ð °WÐ=NÑ5Oó ó!ð:( ó (ð" -/ñ °4ó ð27 Dô 7r"   rE   N)&Úpydanticr   r   Útyping_extensionsr   Útypingr   r   r   r	   r
   r   r   Ú
app.configr   Úlangchain_core.toolsr   Ú!langchain_openai.chat_models.baser   Úlanggraph.prebuiltr   Úlanggraph.graphr   r   r   r   Úlanggraph.graph.stater   Úlangchain_core.messagesr   Ú"langchain_core.output_parsers.jsonr   r   r   r%   r1   rA   rE   r!   r"   r#   Ú<module>r›      sp   ðß %Ý 'ß I× IÑ Iå å %Ý 8å 'ß @Ó @Ý 4Ý 2Ý AÝ 1ô,ˆIô ,ô"�Dô "ô$˜	ô $ôB�yô B÷F7ò F7r"   