
    dhO                     l   S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
JrJrJr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJrJr  S SKJr  S SKJ r J!r!J"r"J#r#J$r$J%r%J&r&  \RN                  " \(5      r)Sr*Sr+Sr, " S S\-\5      r.S\\#\&4   4S jr/S\-4S jr0 " S S\5      r1g)    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                   $    \ rS rSrSrSrSrSrSrg)Routes%   z2Routes available for the Pebblo API as enumerator.z/v1/app/discoverz
/v1/promptz/v1/prompt/governance N)	__name__
__module____qualname____firstlineno____doc__retrieval_app_discoverpromptprompt_governance__static_attributes__r       m/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chains/pebblo_retrieval/utilities.pyr   r   %   s    </F/r'   r   returnc                     [        5       n [        SU R                  SS5      S9n[        R                  " 5       n[        UR                  [        R                  S   U R                  SS5      UR                  UR                  [        5       U R                  SS5      U R                  S	S5      S
9nSUR                  ;   a  SUl        SUl        [        R                  SU 35        [        R                  SU 35        X4$ )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pathr0   os
os_versioniplanguagelanguage_versionDarwindesktopzMac OSXz
framework zruntime )r   r   getr0   unamer   noder6   environsystemr.   get_iptyper2   loggerdebug)runtime_env	frameworkr>   r2   s       r(   get_runtimerH   -   s     *+K+//2CT"JI NNEZZZZY7<<==8I6$):IF	G 7:: #
LL:i[)*
LL8G9%&r'   c                      SSK n U R                  5       n U R                  U5      nU$ ! [         a    U R                  S5      n U$ f = f)z>Fetch local runtime ip address.

Returns:
    str: IP address
r   N	localhost)socketgethostnamegethostbyname	Exception)rK   r4   	public_ips      r(   rB   rB   L   sY     D6((.	   6((5	6s   ) AAc                   z  ^  \ rS rSr% Sr\\   \S'    Sr\\S'    \\   \S'    \\   \S'    S\	4U 4S	 jjr
S
\SS4S jr S(S\S\S\S\S\\   S\\   S\\\	4   S\S\SS4S jjr S(S\S\S\S\S\\   S\\   S\\\	4   S\S\SS4S jjrS\S\\\\\	4   4   4S jrS\S\\\\\	4   4   4S jrS(S\S\4S jjr\  S)S\S\S\S \\   S!\S\\   4S" jj5       r\S \S#\\   SS4S$ j5       r\  S)S\S\S\S \\   S!\S\	4S% jj5       r S(S\S\S\S\S\\   S\\   S\\\	4   S\S\S\4S& jjr S'r!U =r"$ )*PebbloRetrievalAPIWrapper\   z!Wrapper for Pebblo Retrieval API.api_keylocalclassifier_locationclassifier_url	cloud_urlkwargsc                    > [        USSS5      US'   [        USS[        5      US'   [        USS[        5      US'   [        TU ]  " S	0 UD6  g)
z%Validate that api key in environment.rS   PEBBLO_API_KEY rV   PEBBLO_CLASSIFIER_URLrW   PEBBLO_CLOUD_URLNr   )r   _DEFAULT_CLASSIFIER_URL_DEFAULT_PEBBLO_CLOUD_URLsuper__init__)selfrX   	__class__s     r(   ra   "PebbloRetrievalAPIWrapper.__init__h   sh    0I/
y $8$&=?V$
  3K!35N
{ 	"6"r'   appr)   Nc                 T   SnUR                  SS9nU R                  S:X  aK  U R                  5       nU R                   [        R
                  R                   3nU R                  SXTU5      nU R                  (       a  U R                  SS9nU(       aB  [        R                  " UR                  5      R                  S5      nUR                  SU05        UR                  S[        05        U R                   [        R
                  R                   3nU R                  SXtU5      ngg)	zk
Send app discovery request to Pebblo server & cloud.

Args:
    app (App): App instance to be discovered.
NTexclude_unsetrT   POSTcloud_requestpebblo_server_versionpebblo_client_version)dictrU   _make_headersrV   r   r#   valuemake_requestrS   jsonloadstextr=   updatePLUGIN_VERSIONrW   )	rb   re   pebblo_resppayloadheadersapp_discover_urlrl   pebblo_cloud_url_s	            r(   send_app_discover+PebbloRetrievalAPIWrapper.send_app_discoveru   s    (((.##w.((*G&&'(E(E(K(K'LM  ++F4DwWK<<((t(<G(,

;3C3C(D(H(H+)%  79NOPNN3^DE"&..!1&2O2O2U2U1VW!!&*:WMA r'   app_name	retrieverquestionanswerauth_contextdocsprompt_entitiesprompt_timeprompt_gov_enabledc
                 |   Sn
U R                  UUUUUUUUU	5	      nU R                  S:X  aK  U R                  5       nU R                   [        R
                  R                   3nU R                  SXU5      n
U R                  (       a  U R                  S:X  a*  U
(       a  U
R                  5       OSn
U R                  X5        U R                  SS9nU R                   [        R
                  R                   3nU R                  SXU5      ngU R                  S:X  a   [        R                  S5        [        S5      egar  
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.
NrT   ri   Trj   zpebblo-cloudz6API key is missing for sending prompt to Pebblo cloud.)build_prompt_qa_payloadrU   ro   rV   r   r$   rp   rq   rS   rr   update_cloud_payloadrW   rD   warning	NameErrorrb   r   r   r   r   r   r   r   r   r   rw   rx   ry   
prompt_urlpebblo_cloud_prompt_urlr|   s                   r(   send_prompt%PebbloRetrievalAPIWrapper.send_prompt   s1   6 ..

 ##w.((*G //01D1D0EFJ++FJQK<<''72 5@k..0T))'?((t(<G)-(89L9L8M&N#!!&*AGTA%%7NNSTTUU 8r'   c
                 z  #    Sn
U R                  UUUUUUUUU	5	      nU R                  S:X  aS  U R                  5       nU R                   [        R
                  R                   3nU R                  SXU5      I Sh  vN n
U R                  (       at  U R                  S:X  a  U R                  X5        U R                  SS9nU R                   [        R
                  R                   3nU R                  SXU5      I Sh  vN ngU R                  S:X  a   [        R                  S5        [        S5      eg N N87fr   )r   rU   ro   rV   r   r$   rp   amake_requestrS   r   rW   rD   r   r   r   s                   r(   asend_prompt&PebbloRetrievalAPIWrapper.asend_prompt   s:    6 ..

 ##w.((*G //01D1D0EFJ $ 2 26:PW XXK<<''72 ))'?((t(<G)-(89L9L8M&N#((/' A %%7NNSTTUU 8 Ys%   A:D;<D7=BD; D97D;9D;c                 n   SU0n0 SS.nSnU R                   S:X  a  U R                  5       nU R                   [        R                  R
                   3nU R                  SXeU5      nU(       aF  UR                  5       R                  S0 5      US'   UR                  5       R                  SS5      US'   XC4$ )	  
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entityCountTrT   ri   r   r   )	rU   ro   rV   r   r%   rp   rq   rr   r=   rb   r   prompt_payloadr   is_valid_promptry   prompt_gov_api_urlrw   s           r(   check_prompt_validity/PebbloRetrievalAPIWrapper.check_prompt_validity  s     #H--/ B $##w.((*G&&'(@(@(F(F'GH  ++*^K .9.>.>.@.D.DZQS.T
+1<1A1A1C1G1G!12. //r'   c                 R  #    SU0n0 SS.nSnU R                   S:X  a  U R                  5       nU R                   [        R                  R
                   3nU R                  SXeU5      I Sh  vN nU(       a*  UR                  S0 5      US'   UR                  S	S5      US	'   XC4$  N87f)
r   r$   r   r   TrT   ri   Nr   r   )rU   ro   rV   r   r%   rp   r   r=   r   s           r(   acheck_prompt_validity0PebbloRetrievalAPIWrapper.acheck_prompt_validity0  s     " #H--/ B $##w.((*G&&'(@(@(F(F'GH  !% 2 2*^! K .9ooj".M
+1<PQ1R.//s   A*B',B%-9B'rk   c                     SSS.nU(       aE  U R                   (       a  UR                  SU R                   05        U$ [        R                  S5        U$ )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.)rS   ru   rD   r   )rb   rk   ry   s      r(   ro   'PebbloRetrievalAPIWrapper._make_headersQ  sM     ).
 ||T\\:;  MNr'   methodurlry   rx   timeoutc           
      H    [        XX#US9n[        R                  SU UR                   R                  [	        [        UR                   R                  (       a  UR                   R                  O/ 5      5      [	        UR                  5      5        UR                  [        R                  :  a$  [        R                  SUR                   35        U$ UR                  [        R                  :  a$  [        R                  SUR                   35        U$ UR                  [        R                  :w  a"  [        R                  SUR                   35        U$ ! [         a    [        R                  SU5         g[         a   n[        R                  SU5         SnAgSnAff = f)	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   ry   rr   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   rD   rE   r   strlenbodystatus_coder   INTERNAL_SERVER_ERRORr   BAD_REQUESTrt   OKr   rN   )r   r   ry   rx   r   responsees          r(   rq   &PebbloRetrievalAPIWrapper.make_requesth  s`   *	Iw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!8	F!FF!rw   c                    U(       a  U R                  S0 5      nUR                  UR                  S0 5      R                  S0 5      5        UR                  SS5        U R                  S0 5      nUR                  UR                  S0 5      R                  S0 5      5        UR                  SS5        U R                  S/ 5      nU H  nUR                  SS5        M     g0 U S'   0 U S'   / U S'   g)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=   ru   pop)rx   rw   r   r$   r   context_datas         r(   r   .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           	        #     [        US9n[        R                  " 5        ISh  vN nUR                  U UUUUS9 ISh  vN nUR                  [
        R                  :  a#  [        R                  SUR                   35        OUR                  [
        R                  :  a#  [        R                  SUR                   35        O@UR                  [
        R                  :w  a"  [        R                  SUR                   35        UR                  5       I Sh  vN nSSS5      ISh  vN   SSS5      ISh  vN   W$  GN GN N, N! , ISh  vN  (       d  f       N3= f N*! , ISh  vN  (       d  f       W$ = f! [         a    [        R                  SU5         g[         a   n	[        R                  SU	5         Sn	A	gSn	A	ff = f7f)	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   rr   ry   r   r   r   r   r   z(An Exception caught in amake_request: %s)r
   aiohttpClientSessionr   statusr   r   rD   r   r   rt   r   rr   r   rN   )
r   r   ry   rx   r   client_timeoutasessionr   response_jsonr   s
             r(   r   '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"F EF E)EE)CEE	E!E),E-E)1F <E'=F GF E)	EE)E$	EE$	 E)'F )F/E20F<F  GF  G$G&	G/G
G
GGc
                 x   [        UU V
s/ sH  n
[        U
[        5      (       d  M  [        U
R                  R                  SU
R                  R                  S5      5      U
R                  UR                  R                  R                  U
R                  R                  S5      S9PM     sn
[        UUR                  S0 5      UR                  SS5      U	S9[        US	9UU(       a  UR                  OS
U(       a  [        US5      (       a  UR                  O/ U R                  S9nUR                  SS9$ s  sn
f )ac  
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   r1   	user_auth)r-   r   r$   r   r   useruser_identitiesrU   Trg   )r   
isinstancer   r   metadatar=   page_contentvectorstorerc   r   r   user_idhasattrr   rU   rn   )rb   r   r   r   r   r   r   r   r   r   r   qas               r(   r   1PebbloRetrievalAPIWrapper.build_prompt_qa_payload  s#   :     Cc8,#&<<#3#3#S\\%5%5h%?$ (('33==FF # 0 0 ?   (,,Z<+//qA#5	 (#)5%%9k B B )22 $ 8 85
8 wwTw**5s   D7
BD7
r   )F)N   )#r   r   r    r!   r"   r   r   __annotations__rU   r   ra   r   r}   r   r   r   r   r   boolr   r   r	   r   r   rn   ro   staticmethodintr   rq   r   r   r   r&   __classcell__)rc   s   @r(   rQ   rQ   \   s7   +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+ 9+r'   rQ   )2rr   loggingr6   r0   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   rD   rv   r^   r_   r   r   rH   rB   rQ   r   r'   r(   <module>r      s      	    3 3  ! - 6 5 <  & 0   
		8	$1 1 0S$ 0U9g-. >  B+	 B+r'   