
    hO                     h   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jN                  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      Z1y)    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y)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     s/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/chains/pebblo_retrieval/utilities.pyr   r   %   s    </F/r$   r   returnc                     t               } t        d| j                  dd            }t        j                         }t        |j                  t        j                  d   | j                  dd      |j                  |j                  t               | j                  dd      | j                  d	d      
      }d|j                  v rd|_        d|_        t        j                  d|        t        j                  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I NNEZZZZY7<<==8I6$):IF	G 7:: #
LL:i[)*
LL8G9%&gr$   c                      ddl } | j                         }	 | j                  |      }|S # t        $ r | j                  d      }Y |S w xY w)zJFetch local runtime ip address.

    Returns:
        str: IP address
    r   N	localhost)socketgethostnamegethostbyname	Exception)rH   r1   	public_ips      r%   r?   r?   L   sY     D6((.	   6((5	6s   ) AAc                   N    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        |   d	i | y)
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   sh    0I/
y $8$&=?V$
  3K!35N
{ 	"6"r$   appr&   Nc                 L   d}|j                  d      }| j                  dk(  rL| j                         }| j                   t        j
                  j                   }| j                  d|||      }| j                  r| j                  d      }|rAt        j                  |j                        j                  d      }|j                  d|i       |j                  dt        i       | j                   t        j
                  j                   }| j                  d|||      }yy)	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   s    (((.##w.((*G&&'(E(E(K(K'LM  ++F4DgwWK<<((t(<G(,

;3C3C(D(H(H+)%  79NOPNN3^DE"&..!1&2O2O2U2U1VW!!&*:GWMA r$   app_name	retrieverquestionanswerauth_contextdocsprompt_entitiesprompt_timeprompt_gov_enabledc
                 n   d}
| j                  |||||||||		      }| j                  dk(  rL| j                         }| j                   t        j
                  j                   }| j                  d|||      }
| j                  r| j                  dk(  r&|
r|
j                         nd}
| j                  ||
       | j                  d      }| j                   t        j
                  j                   }| j                  d|||      }y| j                  dk(  r t        j                  d       t        d      y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   s7   6 ..

 ##w.((*G //01D1D0EFJ++FJQK<<''72 5@k..0T))';?((t(<G)-(89L9L8M&N#!!&*A7GTA%%7NNSTTUU 8r$   c
                 v  K   d}
| j                  |||||||||		      }| j                  dk(  rT| j                         }| j                   t        j
                  j                   }| j                  d|||       d{   }
| j                  rx| j                  dk(  r| j                  ||
       | j                  d      }| j                   t        j
                  j                   }| j                  d|||       d{   }y| j                  dk(  r t        j                  d       t        d      y7 7 7wr   )r   rQ   rj   rR   r   r!   rk   amake_requestrO   r   rS   rA   r   r   r   s                   r%   asend_promptz&PebbloRetrievalAPIWrapper.asend_prompt   sB    6 ..

 ##w.((*G //01D1D0EFJ $ 2 26:wPW XXK<<''72 ))';?((t(<G)-(89L9L8M&N#((/' A %%7NNSTTUU 8 Ys%   A:D9<D5=BD9?D7 6D97D9c                 f   d|i}i dd}d}| j                   dk(  r| j                         }| j                   t        j                  j
                   }| j                  d|||      }|rF|j                         j                  di       |d<   |j                         j                  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 $##w.((*G&&'(@(@(F(F'GH  ++*G^K .9.>.>.@.D.DZQS.T
+1<1A1A1C1G1G!12. //r$   c                 J  K   d|i}i dd}d}| j                   dk(  r| j                         }| j                   t        j                  j
                   }| j                  d|||       d{   }|r*|j                  di       |d<   |j                  d	d      |d	<   ||fS 7 4w)
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 $##w.((*G&&'(@(@(F(F'GH  !% 2 2*G^! K .9ooj".M
+1<PQ1R.//s   A*B#,B!-5B#rf   c                     ddd}|r@| j                   r|j                  d| j                   i       |S t        j                  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  sM     ).
 ||T\\:;  MNr$   methodurlrt   rs   timeoutc           
      >   	 t        | ||||      }t        j                  d| |j                   j                  t	        t        |j                   j                  r|j                   j                  ng             t	        |j                               |j                  t        j                  k\  r$t        j                  d|j                          |S |j                  t        j                  k\  r$t        j                  d|j                          |S |j                  t        j                  k7  r"t        j                  d|j                          |S # t        $ r t        j                  d|       Y yt        $ r }t        j                  d|       Y d}~yd}~ww xY w)	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  sd   *	I3gwH LLG  $$C1A1A1F1F((--BOPH(() ##z'G'GG!6x7K7K6LMN O %%)?)??!Ehmm_UV O %%6C++,.
 O 	=NN6<   	INNDaHH	Is,   CE A E A E F4F<FFrr   c                    |r| j                  di       }|j                  |j                  di       j                  di              |j                  dd       | j                  di       }|j                  |j                  di       j                  di              |j                  dd       | j                  dg       }|D ]  }|j                  dd        yi | d<   i | d<   g | d<   y)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2HOOKOO,<bAEEjRTUVLL&[[2.FMM+//*:B?CCHbQRJJvt$kk)R0G ' .  -. #%GJ "GH!#GIr$   c                   K   	 t        |      }t        j                         4 d{   }|j                  | ||||      4 d{   }|j                  t
        j                  k\  r#t        j                  d|j                          n|j                  t
        j                  k\  r#t        j                  d|j                          n?|j                  t
        j                  k7  r"t        j                  d|j                          |j                          d{   }ddd      d{    ddd      d{    S 7 7 7 ,7 # 1 d{  7  sw Y   .xY w7 %# 1 d{  7  sw Y   S xY w# t        $ r t        j                  d|       Y yt        $ r }	t        j                  d|	       Y d}	~	yd}	~	ww xY ww)	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*9N,,. : :(#++! #* ,  : : **J*JJ)>x>O'PQ!J,B,BBB8==/R "JMM9K'/1 +3--/$9M%: :: :( ! )::$ %:%: : : :: : : :( !  	=NN6<   	JNNEqII	Js   G$E< EE< E&EE&CEEE#E&.E/E&3E< >E$?E< GE< E&EE&E!	EE!	E&$E< &E9,E/-E94E< 8G9E< <GGG%G ;G GGc
                 f   t        ||D 
cg c]  }
t        |
t              rt        |
j                  j                  d|
j                  j                  d            |
j                  |j                  j                  j                  |
j                  j                  d             c}
t        ||j                  di       |j                  dd      |	      t        |	      ||r|j                  nd
|rt        |d      r|j                  ng | j                        }|j                  d      S c c}
w )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.
        	full_pathsourcepb_checksum)retrieved_fromr   	vector_dbr   r   r   r   )r   r   r   r   )r   r.   	user_auth)r*   r   r!   r   r   useruser_identitiesrQ   Trb   )r   
isinstancer   r   metadatar:   page_contentvectorstorer_   r   r   user_idhasattrr   rQ   ri   )r^   ry   rz   r{   r|   r}   r~   r   r   r   r   qas               r%   r   z1PebbloRetrievalAPIWrapper.build_prompt_qa_payload  s   :    c8, #&<<#3#3#S\\%5%5h%?$ (('33==FF # 0 0 ? (,,Z<+//qA#5	 (#)5%%9k B )22 $ 8 85
8 wwTw**5s   BD.
)F)N   )!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   \   s2   +c]"&&ISM!&}!# #NS NT NR $);V;V (;V 	;V
 ;V {+;V 8n;V c3h;V ;V !;V 
;VN $)<V<V (<V 	<V
 <V {+<V 8n<V c3h<V <V !<V 
<V|0c 0eD$sCx.<P6Q 0B00	tT#s(^#	$0B4 D . 
 #'/// / $	/
 / 
(	/ /b $d $$ $D $ $0 
 #'/// / $	/
 / 
/ /v $)9+9+ (9+ 	9+
 9+ {+9+ 8n9+ c3h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  ! - 6 5 <  & 0   
		8	$1 1 0S$ 0U9g-. >  B+	 B+r$   