
    NgO                     x   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dl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 d d
lmZ 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&  ej'        e(          Z)dZ*dZ+dZ, G d de-e          Z.dee#e&f         fdZ/de-fdZ0 G d de          Z1dS )    N)Enum)
HTTPStatus)AnyDictListOptionalTuple)ClientTimeout)Document)get_runtime_environment)get_from_dict_or_env)VectorStoreRetriever)	BaseModel)Responserequest)RequestException)AppAuthContextContext	FrameworkPromptQaRuntimez0.1.1zhttp://localhost:8000zhttps://api.daxa.aic                       e Zd ZdZdZdZdZdS )Routesz2Routes available for the Pebblo API as enumerator.z/v1/app/discoverz
/v1/promptz/v1/prompt/governanceN)__name__
__module____qualname____doc__retrieval_app_discoverpromptprompt_governance     q/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chains/pebblo_retrieval/utilities.pyr   r   %   s'        <</F/r$   r   returnc                  4   t                      } t          d|                     dd                    }t          j                    }t          |j        t          j        d         |                     dd          |j	        |j
        t                      |                     dd          |                     d	d          
          }d|j        v rd|_        d|_        t                              d|            t                              d|            ||fS )zFetch the current Framework and Runtime details.

    Returns:
        Tuple[Framework, Runtime]: Framework and Runtime for the current app instance.
    	langchainlibrary_versionN)nameversionPWDplatformunknownruntimeruntime_version)hostpathr-   os
os_versioniplanguagelanguage_versionDarwindesktopzMac OSXz
framework zruntime )r   r   getr-   unamer   noder3   environsystemr+   get_iptyper/   loggerdebug)runtime_env	frameworkr;   r/   s       r%   get_runtimerE   -   s    *++K+//2CT"J"J  I NEZZY77<=88I66$):IFF	 	 	G 7: #
LL)i))***
LL%G%%&&&gr$   c                      ddl } |                                 }	 |                     |          }n%# t          $ r |                     d          }Y nw xY w|S )zJFetch local runtime ip address.

    Returns:
        str: IP address
    r   N	localhost)socketgethostnamegethostbyname	Exception)rH   r1   	public_ips      r%   r?   r?   L   sp     MMMD6((..		 6 6 6((55			6s   0 AAc                       e Zd ZU dZee         ed<   	 dZeed<   	 ee         ed<   	 ee         ed<   	 def fdZ	d	e
d
dfdZ	 d(dededededee         dee         deeef         deded
dfdZ	 d(dededededee         dee         deeef         deded
dfdZded
eeeeef         f         fdZded
eeeeef         f         fdZd(ded
efdZe	 	 d)deded ed!ee         d"ed
ee         fd#            Zed!ed$ee         d
dfd%            Ze	 	 d)deded ed!ee         d"ed
efd&            Z	 d(dededededee         dee         deeef         deded
efd'Z xZ S )*PebbloRetrievalAPIWrapperz!Wrapper for Pebblo Retrieval API.api_keylocalclassifier_locationclassifier_url	cloud_urlkwargsc                     t          |ddd          |d<   t          |ddt                    |d<   t          |ddt                    |d<    t                      j        d	i | dS )
z%Validate that api key in environment.rO   PEBBLO_API_KEY rR   PEBBLO_CLASSIFIER_URLrS   PEBBLO_CLOUD_URLNr#   )r   _DEFAULT_CLASSIFIER_URL_DEFAULT_PEBBLO_CLOUD_URLsuper__init__)selfrT   	__class__s     r%   r]   z"PebbloRetrievalAPIWrapper.__init__h   s    0I/
 
y $8$&=?V$
 $
  3K!35N
 
{ 	""6"""""r$   appr&   Nc                 <   d}|                     d          }| j        dk    rF|                                 }| j         t          j        j         }|                     d|||          }| j        r|                     d          }|rCt          j
        |j                                      d          }|                    d|i           |                    dt          i           | j         t          j        j         }|                     d|||          }dS dS )	z
        Send app discovery request to Pebblo server & cloud.

        Args:
            app (App): App instance to be discovered.
        NTexclude_unsetrP   POSTcloud_requestpebblo_server_versionpebblo_client_version)dictrQ   _make_headersrR   r   r    valuemake_requestrO   jsonloadstextr:   updatePLUGIN_VERSIONrS   )	r^   r`   pebblo_resppayloadheadersapp_discover_urlrg   pebblo_cloud_url_s	            r%   send_app_discoverz+PebbloRetrievalAPIWrapper.send_app_discoveru   s9    (((..#w..((**G&M(E(KMM  ++F4DgwWWK< 	N((t(<<G Q(,
;3C(D(D(H(H+) )%  79NOPPPNN3^DEEE"&.W&2O2UWW!!&*:GWMMAAA	N 	Nr$   Fapp_name	retrieverquestionanswerauth_contextdocsprompt_entitiesprompt_timeprompt_gov_enabledc
                 b   d}
|                      |||||||||		  	        }| j        dk    rF|                                 }| j         t          j        j         }|                     d|||          }
| j        r| j        dk    r.|
r|
	                                nd}
| 
                    ||
           |                     d          }| j         t          j        j         }|                     d|||          }dS | j        dk    r)t                              d           t          d          dS a  
        Send prompt to Pebblo server for classification.
        Then send prompt to Daxa cloud(If api_key is present).

        Args:
            app_name (str): Name of the app.
            retriever (VectorStoreRetriever): Retriever instance.
            question (str): Question asked in the prompt.
            answer (str): Answer generated by the model.
            auth_context (Optional[AuthContext]): Authentication context.
            docs (List[Document]): List of documents retrieved.
            prompt_entities (Dict[str, Any]): Entities present in the prompt.
            prompt_time (str): Time when the prompt was generated.
            prompt_gov_enabled (bool): Whether prompt governance is enabled.
        NrP   rd   Tre   zpebblo-cloudz6API key is missing for sending prompt to Pebblo cloud.)build_prompt_qa_payloadrQ   rj   rR   r   r!   rk   rl   rO   rm   update_cloud_payloadrS   rA   warning	NameErrorr^   ry   rz   r{   r|   r}   r~   r   r   r   rr   rs   rt   
prompt_urlpebblo_cloud_prompt_urlrw   s                   r%   send_promptz%PebbloRetrievalAPIWrapper.send_prompt   s\   6 ..

 

 #w..((**G /F1DFFJ++FJQQK< 	V'722 5@Ik..000T))';???((t(<<G)-&N9L&N&N#!!&*A7GTTAAA%77NNSTTTTUUU 87r$   c
                 N  K   d}
|                      |||||||||		  	        }| j        dk    rL|                                 }| j         t          j        j         }|                     d|||           d{V }
| j        rq| j        dk    r| 	                    ||
           |                     d          }| j
         t          j        j         }|                     d|||           d{V }dS | j        dk    r)t                              d           t          d          dS r   )r   rQ   rj   rR   r   r!   rk   amake_requestrO   r   rS   rA   r   r   r   s                   r%   asend_promptz&PebbloRetrievalAPIWrapper.asend_prompt   s|     6 ..

 

 #w..((**G /F1DFFJ $ 2 26:wPW X XXXXXXXK< 	V'722 ))';???((t(<<G)-&N9L&N&N#((/'       AAA %77NNSTTTTUUU 87r$   c                 r   d|i}i dd}d}| j         dk    r|                                 }| j         t          j        j         }|                     d|||          }|rV|                                                    di           |d<   |                                                    dd          |d<   ||fS )	  
        Check the validity of the given prompt using a remote classification service.

        This method sends a prompt to a remote classifier service and return entities
        present in prompt or not.

        Args:
            question (str): The prompt question to be validated.

        Returns:
            bool: True if the prompt is valid (does not contain deny list entities),
            False otherwise.
            dict: The entities present in the prompt
        r!   r   entitiesentityCountTrP   rd   r   r   )	rQ   rj   rR   r   r"   rk   rl   rm   r:   r^   r{   prompt_payloadr   is_valid_promptrt   prompt_gov_api_urlrr   s           r%   check_prompt_validityz/PebbloRetrievalAPIWrapper.check_prompt_validity  s     #H--/ B B $#w..((**G&H(@(FHH  ++*G^ K  .9.>.>.@.@.D.DZQS.T.T
+1<1A1A1C1C1G1G!12 2. //r$   c                 :  K   d|i}i dd}d}| j         dk    r|                                 }| j         t          j        j         }|                     d|||           d{V }|r2|                    di           |d<   |                    d	d          |d	<   ||fS )
r   r!   r   r   TrP   rd   Nr   r   )rQ   rj   rR   r   r"   rk   r   r:   r   s           r%   acheck_prompt_validityz0PebbloRetrievalAPIWrapper.acheck_prompt_validity0  s      " #H--/ B B $#w..((**G&H(@(FHH  !% 2 2*G^! !      K  S.9ooj".M.M
+1<PQ1R1R.//r$   rf   c                     ddd}|r>| j         r|                    d| j         i           nt                              d           |S )z
        Generate headers for the request.

        args:
            cloud_request (bool): flag indicating whether the request is for Pebblo
            cloud.
        returns:
            dict: Headers for the request.

        zapplication/json)AcceptzContent-Typez	x-api-keyz,API key is missing for Pebblo cloud request.)rO   rp   rA   r   )r^   rf   rt   s      r%   rj   z'PebbloRetrievalAPIWrapper._make_headersQ  s`     ).
 
  	O| OT\:;;;;MNNNr$      methodurlrt   rs   timeoutc           
      
   	 t          | ||||          }t                              d| |j         j        t	          t          |j         j        r|j         j        ng                     t	          |j                             |j        t          j	        k    r#t          
                    d|j                    no|j        t          j        k    r#t          
                    d|j                    n7|j        t          j        k    r"t          
                    d|j                    |S # t          $ r t          
                    d|           Y n1t          $ r%}t          
                    d|           Y d}~nd}~ww xY wdS )	a  
        Make a request to the Pebblo server/cloud API.

        Args:
            method (str): HTTP method (GET, POST, PUT, DELETE, etc.).
            url (str): URL for the request.
            headers (dict): Headers for the request.
            payload (Optional[dict]): Payload for the request (for POST, PUT, etc.).
            timeout (int): Timeout for the request in seconds.

        Returns:
            Optional[Response]: Response object if the request is successful.
        )r   r   rt   rm   r   z5Request: method %s, url %s, len %s response status %sPebblo Server: Error $Pebblo received an invalid payload: -Pebblo returned an unexpected response code: Unable to reach server %sz'An Exception caught in make_request: %sN)r   rA   rB   r   strlenbodystatus_coder   INTERNAL_SERVER_ERRORr   BAD_REQUESTro   OKr   rK   )r   r   rt   rs   r   responsees          r%   rl   z&PebbloRetrievalAPIWrapper.make_requesth  s   *	I3gw  H LLG $C1A1FN(--BOOPPH())   #z'GGGMx7KMMNNNN%)???UhmUUVVVV%66.+. .  
 O 	= 	= 	=NN6<<<<< 	I 	I 	INNDaHHHHHHHH	Its   D(D+ +%F 	F E;;F rr   c                 4   |r|                      di           }|                    |                     di                                di                      |                    dd           |                      di           }|                    |                     di                                di                      |                    dd           |                      dg           }|D ]}|                    dd           dS i | d<   i | d<   g | d<   dS )z
        Update the payload with response, prompt and context from Pebblo response.

        Args:
            payload (dict): Payload to be updated.
            pebblo_resp (Optional[dict]): Response from Pebblo server.
        r   retrieval_datadataNr!   contextdoc)r:   rp   pop)rs   rr   r   r!   r   context_datas         r%   r   z.PebbloRetrievalAPIWrapper.update_cloud_payload  s(     	${{:r22HOOKOO,<bAAEEjRTUUVVVLL&&&[[2..FMM+//*:B??CCHbQQRRRJJvt$$$kk)R00G ' . .  ----. . #%GJ "GH!#GIr$   c                 f  K   	 t          |          }t          j                    4 d{V }|                    | ||||          4 d{V 	 }|j        t
          j        k    r#t                              d|j                    no|j        t
          j	        k    r#t                              d|j
                    n7|j        t
          j        k    r"t                              d|j                    |                                 d{V }ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  n# 1 d{V swxY w Y   |S # t          $ r t                              d|           Y n1t          $ r%}	t                              d|	           Y d}	~	nd}	~	ww xY wdS )	a  
        Make a async request to the Pebblo server/cloud API.

        Args:
            method (str): HTTP method (GET, POST, PUT, DELETE, etc.).
            url (str): URL for the request.
            headers (dict): Headers for the request.
            payload (Optional[dict]): Payload for the request (for POST, PUT, etc.).
            timeout (int): Timeout for the request in seconds.

        Returns:
            Any: Response json if the request is successful.
        )totalN)r   r   rm   rt   r   r   r   r   r   z(An Exception caught in amake_request: %s)r
   aiohttpClientSessionr   statusr   r   rA   r   r   ro   r   rm   r   rK   )
r   r   rt   rs   r   client_timeoutasessionr   response_jsonr   s
             r%   r   z'PebbloRetrievalAPIWrapper.amake_request  s     *	J*999N,.. : : : : : : :(#++! #* ,   : : : : : : : : **JJJ'Px'P'PQQQQ!J,BBBVx}VV    "JM991'1 1   +3--//$9$9$9$9$9$9M%: : : : : : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : :( !  	= 	= 	=NN6<<<<< 	J 	J 	JNNEqIIIIIIII	Jtse   )E !ECD#E#
D-	-E0D-	1E4E 
EE EE %F. 	F.	F))F.c
                 R   t          |fd|D             t          ||                    di           |                    dd          |	          t          |          ||r|j        nd|rt	          |d          r|j        ng | j        	          }
|
                    d
          S )a  
        Build the QA payload for the prompt.

         Args:
            app_name (str): Name of the app.
            retriever (VectorStoreRetriever): Retriever instance.
            question (str): Question asked in the prompt.
            answer (str): Answer generated by the model.
            auth_context (Optional[AuthContext]): Authentication context.
            docs (List[Document]): List of documents retrieved.
            prompt_entities (Dict[str, Any]): Entities present in the prompt.
            prompt_time (str): Time when the prompt was generated.
            prompt_gov_enabled (bool): Whether prompt governance is enabled.

        Returns:
            dict: The QA payload for the prompt.
        c           
         g | ]}t          |t                    t          |j                            d |j                            d                    |j        j        j        j        |j                            d                    S )	full_pathsourcepb_checksum)retrieved_fromr   	vector_dbr   )	
isinstancer   r   metadatar:   page_contentvectorstorer_   r   ).0r   rz   s     r%   
<listcomp>zEPebbloRetrievalAPIWrapper.build_prompt_qa_payload.<locals>.<listcomp>  s        c8,,#&<#3#3#S\%5%5h%?%?$ $ ('3=F # 0 0 ? ?    r$   r   r   r   )r   r   r   r   )r   r.   	user_auth)r*   r   r!   r   r   useruser_identitiesrQ   Trb   )r   r   r:   user_idhasattrr   rQ   ri   )r^   ry   rz   r{   r|   r}   r~   r   r   r   qas     `        r%   r   z1PebbloRetrievalAPIWrapper.build_prompt_qa_payload  s    :         (,,Z<<+//qAA#5	   (((#)5D%%9 'k B BL22 $ 85
 
 
8 wwTw***r$   )F)Nr   )!r   r   r   r   r   r   __annotations__rQ   r   r]   r   rx   r   r   r   r   r   boolr   r   r	   r   r   ri   rj   staticmethodintr   rl   r   r   r   __classcell__)r_   s   @r%   rN   rN   \   s:        ++c]"&&&&ISM!!!&}!# # # # # # #NS NT N N N NR $);V ;V;V (;V 	;V
 ;V {+;V 8n;V c3h;V ;V !;V 
;V ;V ;V ;VN $)<V <V<V (<V 	<V
 <V {+<V 8n<V c3h<V <V !<V 
<V <V <V <V|0c 0eD$sCx.<P6Q 0 0 0 0B00	tT#s(^#	$0 0 0 0B 4 D    . 
 #'/ /// / $	/
 / 
(	/ / / \/b $d $$ $D $ $ $ \$0 
 #'/ /// / $	/
 / 
/ / / \/v $)9+ 9+9+ (9+ 	9+
 9+ {+9+ 8n9+ c3h9+ 9+ !9+ 
9+ 9+ 9+ 9+ 9+ 9+ 9+ 9+r$   rN   )2rm   loggingr3   r-   enumr   httpr   typingr   r   r   r   r	   r   r
   langchain_core.documentsr   langchain_core.envr   langchain_core.utilsr   langchain_core.vectorstoresr   pydanticr   requestsr   r   requests.exceptionsr   2langchain_community.chains.pebblo_retrieval.modelsr   r   r   r   r   r   r   	getLoggerr   rA   rq   rZ   r[   r   r   rE   r?   rN   r#   r$   r%   <module>r      s?     				              3 3 3 3 3 3 3 3 3 3 3 3 3 3  ! ! ! ! ! ! - - - - - - 6 6 6 6 6 6 5 5 5 5 5 5 < < < < < <       & & & & & & & & 0 0 0 0 0 0                  
	8	$	$1 1 0 0 0 0 0S$ 0 0 0U9g-.    >     B+ B+ B+ B+ B+	 B+ B+ B+ B+ B+r$   