
    7|he+                         d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZmZ  ej6                  e      Z G d de      Z G d de      Z G d de      Z y)z.Tools for interacting with a Power BI dataset.    N)perf_counter)AnyDictOptionalTuple)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTool)
ConfigDictFieldmodel_validator)_import_tiktoken)BAD_REQUEST_RESPONSEDEFAULT_FEWSHOT_EXAMPLESRETRY_RESPONSE)PowerBIDataset
json_to_mdc            	          e Zd ZU dZdZeed<   dZeed<   dZe	ed<    e
d	      Zeed
<   eZee   ed<    e
ed      Zeee	f   ed<   dZeed<   dZeed<   dZee   ed<    ed      Z ed      ededefd              Zdedee   fdZ	 d#dedee   de	defdZ	 d#dedee   de	defdZ deee	f   de!ee   ee	   f   fd Z"d!ede!e#ef   fd"Z$y)$QueryPowerBIToolz%Tool for querying a Power BI Dataset.query_powerbinamea  
    Input to this tool is a detailed question about the dataset, output is a result from the dataset. It will try to answer the question using the dataset, and if it cannot, it will ask for clarification.

    Example Input: "How many rows are in table1?"
    descriptionN	llm_chainTexcludepowerbiexamples)default_factoryr   session_cache   max_iterationsi  output_token_limittiktoken_model_namearbitrary_types_allowedbefore)modevaluesreturnc                     |d   }|j                   j                  D ]&  }|dvst        d|j                   j                         |S )z8Make sure the LLM chain has the correct input variables.r   )
tool_inputtablesschemasr   zrLLM chain for QueryPowerBITool must have input variables ['tool_input', 'tables', 'schemas', 'examples'], found %s)promptinput_variables
ValueError)clsr(   r   vars       e/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/tools/powerbi/tool.py"validate_llm_chain_input_variablesz3QueryPowerBITool.validate_llm_chain_input_variables.   s[     ;'	##33 	CII  I$$44 	     r+   c                 >    || j                   vry| j                   |   S )zCheck if the input is present in the cache.

        If the value is a bad request, overwrite with the escalated version,
        if not present return None.N)r   )selfr+   s     r3   _check_cachezQueryPowerBITool._check_cache=   s&    
 T///!!*--r5   run_managerkwargsc                    | j                  |      x}rt        j                  d||       |S 	 t        j                  d|       | j                  j                  || j                  j                         | j                  j                         | j                  |r|j                         nd      }|dk(  r|| j                  |<   | j                  |   S t        j                  d|       t               }| j                  j                  |      }t               }	t        j                  d	|       t        j                  d
|	|z
  d       | j                  |      \  }
}|"d|v rd| j                  |<   | j                  |   S |j!                  dd      }|r<|| j"                  k  r-| j%                  t'        j(                  |||      ||dz         S |
r|
nt+        j(                  |      | j                  |<   | j                  |   S # t        $ r+}d| | j                  |<   | j                  |   cY d}~S d}~ww xY w):Execute the query, return the results or an error message.Found cached result for %s: %s%Running PBI Query Tool with input: %sNr+   r,   r-   r   	callbacksError on call to LLM: I cannot answer thiszPBI Query:
%scommandPBI Result: %sPBI Query duration: 0.6fTokenExpiredzCAuthentication token expired or invalid, please try reauthenticate.
iterationsr   r+   queryerror   r+   r9   rI   rL   )r8   loggerdebuginfor   predictr   get_table_namesget_schemasr   	get_child	Exceptionr   r   run_parse_outputgetr!   _runr   formatr   r7   r+   r9   r:   cacherK   exc
start_time
pbi_resultend_timeresultrL   rI   s                r3   r[   zQueryPowerBITool._runF   s3    %%j1151LL9:uML	2KK?LNN**%||3350025@+//1d + E **-2Dz*%%j11$e,!^
\\%%e%4
>%z2+Hz,A$+GHI**:65!8U z* %%j11ZZa0
Z$"5"5599)00)e (%>    F"6"="=E"J 	:& !!*--A  	2/EcU-KDz*%%j11	2s   BH	 		H= H82H=8H=c                   K   | j                  |      x}rt        j                  d||       | dS 	 t        j                  d|       | j                  j                  || j                  j                         | j                  j                         | j                  |r|j                         nd       d{   }|dk(  r|| j                  |<   | j                  |   S t        j                  d|       t               }| j                  j                  |	       d{   }t               }	t        j                  d
|       t        j                  d|	|z
  d       | j                  |      \  }
}|&d|v sd|v rd| j                  |<   | j                  |   S |j!                  dd      }|rD|| j"                  k  r5| j%                  t'        j(                  |||      ||dz          d{   S |
r|
nt+        j(                  |      | j                  |<   | j                  |   S 7 r# t        $ r+}d| | j                  |<   | j                  |   cY d}~S d}~ww xY w7 E7 ww)r<   r=   z3, from cache, you have already asked this question.r>   Nr?   rA   rB   zPBI Query: %srC   rE   rF   rG   rH   
TokenErrorzkAuthentication token expired or invalid, please try to reauthenticate or check the scope of the credential.rI   r   rJ   rM   rN   rO   )r8   rP   rQ   rR   r   apredictr   rT   rU   r   rV   rW   r   r   arunrY   rZ   r!   _arunr   r\   r   r]   s                r3   rh   zQueryPowerBITool._arun|   sc     %%j1151LL9:uMWOPP	2KK?L..11%||3350025@+//1d 2  E **-2Dz*%%j11OU+!^
<<,,U,;;
>%z2+Hz,A$+GHI**:6.E"9\U=R} z* %%j11ZZa0
Z$"5"55)00)e (%> $    F"6"="=E"J 	:& !!*--Q  	2/EcU-KDz*%%j11	2 <sg   0I)BH- 8H*9H- =A"I)I$ CI)/I'0:I)*H- -	I!6 II!I)I!!I)'I)ra   c                 N   d|v ro|d   d   d   d   d   }t        |      dk(  rt        j                  d       yt        |      }| j	                  |      \  }}|rd| d| j
                   d	d
fS |d
fS d|v r+d|d   v rd|d   d   v rd
|d   d   d   d   d   fS d
|d   fS d
|fS )z2Parse the output of the query to a markdown table.resultsr   r,   rowsz%0 records in result, query was valid.)Nz^0 rows returned, this might be correct, but please validate if all filter values were correct?z[Result too large, please try to be more specific or use the `TOPN` function. The result is z tokens long, the limit is z tokens.NrL   z	pbi.errordetailsdetail)lenrP   rR   r   _result_too_larger"   )r7   ra   rk   rc   too_longlengths         r3   rY   zQueryPowerBITool._parse_output   s#    
"i(+H5a8@D4yA~CD  %F#55f=Hfqrxqy  zU  VZ  Vm  Vm  Un  nv  w  4<j z'22G!4[!AAZ0=iHKHUUUG,,,Zr5   rc   c                     | j                   rft               }|j                  | j                         }t        |j	                  |            }t
        j                  d|       || j                  kD  |fS y)z!Tokenize the output of the query.zResult length: %s)Fr   )r#   r   encoding_for_modelrn   encoderP   rR   r"   )r7   rc   	tiktoken_encodingrq   s        r3   ro   z"QueryPowerBITool._result_too_large   sd    ##(*I 33D4L4LMH01FKK+V4D333V;;r5   N)%__name__
__module____qualname____doc__r   str__annotations__r   r   r   r   r   r   r   r   r   dictr   r   r!   intr"   r#   r   model_configr   classmethodr4   r8   r	   r[   r   rh   r   rY   boolro    r5   r3   r   r      s   /D#K 
 Is#D1G^16Hhsm6$)$$MM4S>MNC"")-#- $L (#	  $.s .x} . <@4.4. 784. 	4.
 
4.r AE4.4. <=4. 	4.
 
4.l sCx. 	x}hsm+	, < dCi0@ r5   r   c                       e Zd ZU dZdZeed<   dZeed<    ed      Z	e
ed<    ed	      Z	 ddedee   defdZ	 ddedee   defdZy
)InfoPowerBIToolz2Tool for getting metadata about a PowerBI Dataset.schema_powerbir   z
    Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
    Be sure that the tables actually exist by calling list_tables_powerbi first!

    Example Input: "table1, table2, table3"
    r   Tr   r   r$   Nr+   r9   r)   c                 V    | j                   j                  |j                  d            S )z4Get the schema for tables in a comma-separated list., )r   get_table_infosplitr7   r+   r9   s      r3   r[   zInfoPowerBITool._run   s$     ||**:+;+;D+ABBr5   c                 r   K   | j                   j                  |j                  d             d {   S 7 w)Nr   )r   aget_table_infor   r   s      r3   rh   zInfoPowerBITool._arun   s.     
 \\11*2B2B42HIIIIs   .757rw   rx   ry   rz   r{   r   r|   r}   r   r   r   r   r   r   r   r	   r[   r   rh   r   r5   r3   r   r      s    < D# K  $D1G^1 $L <@CC 78C 
	C AEJJ <=J 
	Jr5   r   c                       e Zd ZU dZdZeed<   dZeed<    ed      Z	e
ed<    ed	      Z	 	 ddee   dee   defdZ	 	 ddee   dee   defdZy
)ListPowerBIToolzTool for getting tables names.list_tables_powerbir   zUInput is an empty string, output is a comma separated list of tables in the database.r   Tr   r   r$   Nr+   r9   r)   c                 T    dj                  | j                  j                               S zGet the names of the tables.r   joinr   rT   r   s      r3   r[   zListPowerBITool._run  s      yy55788r5   c                 \   K   dj                  | j                  j                               S wr   r   r   s      r3   rh   zListPowerBITool._arun  s$      yy55788s   *,)NNr   r   r5   r3   r   r      s    (%D#%nKn#D1G^1 $L %);?9SM9 789 
	9 %)@D9SM9 <=9 
	9r5   r   )!r{   loggingtimer   typingr   r   r   r   langchain_core.callbacksr   r	   langchain_core.toolsr
   pydanticr   r   r   &langchain_community.chat_models.openair   (langchain_community.tools.powerbi.promptr   r   r   %langchain_community.utilities.powerbir   r   	getLoggerrx   rP   r   r   r   r   r5   r3   <module>r      ss    4   - - * 7 7 C 
 M			8	$x DJh J@9h 9r5   