
    Eg$                         d dl mZmZmZ d dlmZmZ d dlmZmZ d dl	m
Z
mZmZ d dl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  G d de      Z G d d      Zy)    )UnionDictTuple)datetimetimezone)create_agent_promptget_agent_prompt)
gen_reportfact_reportmulti_fact_report)
factSchemareportSchemamultiFactSchema)Tool)AgentExecutor)MessagesPlaceholder)
ChatOpenAI)create_openai_functions_agentc                   4    e Zd ZdZdeeef   deeef   fdZy)
CustomToolz;Custom Tool Just For Override _to_args_and_kwargs function.
tool_inputreturnc                 4    t        |t              r|fi fS d|fS )N )
isinstancestr)selfr   s     0/var/www/html/chat-with-data/app/rag/__init__.py_to_args_and_kwargszCustomTool._to_args_and_kwargs   s#    j#&="$$z>!    N)	__name__
__module____qualname____doc__r   r   r   r   r   r   r    r   r   r      s+    E"eCI.> "5PTCU "r    r   c                   V    e Zd Zdededededededdfd	Zd
edefdZg dfdede	fdZ
y)RAGllmquerydb_userdb_passworddb_namedb_hostr   Nc                 X    || _         || _        || _        || _        || _        || _        y )N)r'   r(   r)   r*   r+   r,   )r   r'   r(   r)   r*   r+   r,   s          r   __init__zRAG.__init__   s2     
&r    	tool_namec           
      p  K   dt         t        ddt        t        ddt        t
        dd}t        ||j                  |      j                  d      |j                  |      j                  d      |j                  |      j                  d      d|j                  |      j                  d	      
      S w)NzAlways use this tool to fetch data from sql database. Always formate date fields like HH:MM:SS - DD/MM/YYYY. Always place important fields first or if user ask changes in any field then place that first.)descriptionfuncschemazLUse this tool to fetch the past data from the fact file for particular date.zFUse this tool to fetch the past data from the fact file in date range.)genReportToolfactReportToolmultiFactReportToolr1   r2   Tr3   )namer1   r2   	coroutinereturn_directargs_schema)r
   r   r   r   r   r   r   get)r   r/   tool_detailss      r   create_toolzRAG.create_tool)   s      l"& l!" f''#
$ %)))488G""9-11&9#''	266v>$((377A
 	
s   B4B6chat_historyc           	      >  K   || _         t        t               t        d      g      }| j	                  d       d {   | j	                  d       d {   | j	                  d       d {   g}t        t        | j                  ||      |d	      }d
}d
}t        dd      5 }|j                         }d d d        t        dd      5 }	|	j                         }d d d        |j                  | j                  |||t        j                         j                  d      d|dd       d {   }
d|
j                  d      t        j                   t"        j$                        j'                         dS 7 <7 '7 # 1 sw Y   xY w# 1 sw Y   xY w7 jw)Nr>   )variable_name)system_messageextra_prompt_messagesr4   r5   r6   )r'   toolspromptT)agentrC   return_intermediate_steps z0/var/www/html/chat-with-data/test/fullSchema.txtrz6/var/www/html/chat-with-data/test/hotel_2023-06-08.txtz%Y-%m-%d)inputr>   contextfactdate
SuperAgent)	callbacksrun_name)configbotoutput)tz)roleanswer	timestamp)rN   r   r	   r   r=   r   r   r'   openreadainvoker(   r   todaystrftimer;   nowr   utcrV   )r   r>   rN   rD   rC   rE   r3   rK   filefactFileresponses              r   rY   zRAG.ainvokeF   s    "$4D4F^q  AO  _P  ^Q  R''88''(899''(=>>@ /DHHERXY&*

 DcJdYY[F KJCPT\==?D Q \flvz  EM  ES  ES  EU  E^  E^  _i  Ej  (l  BK  Xd  ue  f  f(,,x*@x||_g_k_kOlOvOvOxyy# 99> KJPP fsq   <FE:FE=F.F /9F(F9FFAF0F1A
F=F FFFFF)r!   r"   r#   r   r   r.   r   r=   listdictrY   r   r    r   r&   r&      s{      	
    "
# 
* 
: 13 z z zr    r&   N)typingr   r   r   r   r   app.utils.promptsr   r	   app.rag.toolsr
   r   r   app.schemas.chat_schemar   r   r   langchain.toolsr   langchain.agentsr   langchain.prompts.chatr   !langchain_openai.chat_models.baser   ,langchain.agents.openai_functions_agent.baser   r   r&   r   r    r   <module>rl      sF    % % ' C D D M M   * 6 8 V" "Ez Ezr    