
    dh=                        S SK r S SKJrJr  S SKJrJrJrJrJ	r	J
r
Jr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JrJr  S S	KJr  S
\S\4S jrSSS\4S jr\\\\\   S4   r   " S S\SS9r! " S S\SS9r" " S S\SS9r# " S S\SS9r$ " S S\SS9r% " S S\SS9r& " S S\\SS9r' " S S \'5      r( " S! S"\'5      r) " S# S$\SS9r* " S% S&\SS9r+S'S(S)S*S+S,.r,\" S-S.S/S09 " S1 S2\5      5       r-g)3    N)ABCabstractmethod)AnyCallableDictListLiteralOptionalSequenceUnion)
deprecated)CallbackManagerForRetrieverRun)Document)BaseRetriever)	BaseModelFieldmodel_validator	validator)	Annotatedexcerptreturnc                 h    U (       d  U $ [         R                  " SSU 5      R                  SS5      nU$ )zlClean an excerpt from Kendra.

Args:
    excerpt: The excerpt to clean.

Returns:
    The cleaned excerpt.

z\s+ z... )resubreplace)r   ress     ]/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/retrievers/kendra.pyclean_excerptr       s0     
&&g
&
.
.ub
9CJ    item
ResultItemc                     SnU R                  5       nU(       a	  USU S3-  n[        U R                  5       5      nU(       a	  USU S3-  nU$ )zCombine a ResultItem title and excerpt into a single string.

Args:
    item: the ResultItem of a Kendra search.

Returns:
    A combined text of the title and excerpt of the given item.

r   zDocument Title: 
zDocument Excerpt: 
)	get_titler    get_excerpt)r"   texttitler   s       r   combined_textr*   +   s[     DNNE"5',,D,,./G&wir22Kr!   c                   T    \ rS rSr% Sr\\S'    \\S'    \\   \S'    \\	   \S'   Sr
g)		HighlightG   z8Information that highlights the keywords in the excerpt.BeginOffset	EndOffset	TopAnswerType N)__name__
__module____qualname____firstlineno____doc__int__annotations__r
   boolstr__static_attributes__r2   r!   r   r,   r,   G   s0    BLNJ~7
3-<r!   r,   allow)extrac                   6    \ rS rSr% Sr\\S'    \\   \S'   Sr	g)TextWithHighLightsU   zText with highlights.Text
Highlightsr2   N)
r3   r4   r5   r6   r7   r;   r9   r
   r   r<   r2   r!   r   r@   r@   U   s    
Ir!   r@   c                   $    \ rS rSr% Sr\\S'   Srg)AdditionalResultAttributeValue_   z(Value of an additional result attribute.TextWithHighlightsValuer2   N)r3   r4   r5   r6   r7   r@   r9   r<   r2   r!   r   rE   rE   _   s    2//)r!   rE   c                   R    \ rS rSr% Sr\\S'    \S   \S'    \\S'    S\4S jr	S	r
g
)AdditionalResultAttributeg   zAdditional result attribute.KeyTEXT_WITH_HIGHLIGHTS_VALUE	ValueTypeValuer   c                 B    U R                   R                  R                  $ N)rN   rG   rB   selfs    r   get_value_text(AdditionalResultAttribute.get_value_textq   s    zz11666r!   r2   N)r3   r4   r5   r6   r7   r;   r9   r	   rE   rS   r<   r2   r!   r   rI   rI   g   s1    &	H#344 ))%7 7r!   rI   c                       \ rS rSr% SrSr\\   \S'    Sr	\\
   \S'    Sr\\\      \S'    Sr\\   \S'    \S\4S	 j5       rS
rg)DocumentAttributeValuev   zValue of a document attribute.N	DateValue	LongValueStringListValueStringValuer   c                     U R                   (       a  U R                   $ U R                  (       a  U R                  $ U R                  (       a  U R                  $ U R                  (       a  U R                  $ g)zThe only defined document attribute value or None.
According to Amazon Kendra, you can only provide one
value for a document attribute.
N)rX   rY   rZ   r[   rQ   s    r   valueDocumentAttributeValue.value   sS     >>>>!>>>>!'''###r!   r2   )r3   r4   r5   r6   r7   rX   r
   r;   r9   rY   r8   rZ   r   r[   propertyDocumentAttributeValueTyper]   r<   r2   r!   r   rV   rV   v   sh    (#Ix}#3#Ix}#+/OXd3i(/ !%K#%1  r!   rV   c                   0    \ rS rSr% Sr\\S'    \\S'   Srg)DocumentAttribute   zDocument attribute.rK   rN   r2   N)	r3   r4   r5   r6   r7   r;   r9   rV   r<   r2   r!   r   rb   rb      s    	H#!!%r!   rb   c                      \ rS rSr% Sr\\   \S'    \\   \S'    \\   \S'    / r\\	\
      \S'    \\   \S'    \S\4S	 j5       r\S\4S
 j5       rS\4S jrS\\\4   4S jrS\4S jr\4S\S /\4   S\4S jjrSrg)r#      zBase class of a result item.Id
DocumentIdDocumentURIDocumentAttributesScoreAttributesr   c                     g)zDocument title.Nr2   rQ   s    r   r&   ResultItem.get_title       r!   c                     g)zDDocument excerpt or passage original content as retrieved by Kendra.Nr2   rQ   s    r   r'   ResultItem.get_excerpt   rm   r!   c                     0 $ )zDocument additional metadata dict.
This returns any extra metadata except these:
    * result_id
    * document_id
    * source
    * title
    * excerpt
    * document_attributes
r2   rQ   s    r   get_additional_metadata"ResultItem.get_additional_metadata   s	     	r!   c                     U R                   =(       d    /  Vs0 sH#  oR                  UR                  R                  _M%     sn$ s  snf )zDocument attributes dict.)ri   rK   rN   r]   )rR   attrs     r   get_document_attributes_dict'ResultItem.get_document_attributes_dict   s<    8<8O8O8USU8UW8Ut$*****8UWWWs   )Ac                 <    U R                   b  U R                   S   $ g)zDocument Score ConfidenceScoreConfidenceNOT_AVAILABLE)rj   rQ   s    r   get_score_attributeResultItem.get_score_attribute   s#    +''(9::"r!   page_content_formatterc           
      "   U" U 5      nU R                  5       nUR                  U R                  U R                  U R                  U R                  5       U R                  5       U R                  5       U R                  5       S.5        [        X#S9$ )z!Converts this item to a Document.)	result_iddocument_idsourcer)   r   document_attributesscore)page_contentmetadata)
rq   updaterf   rg   rh   r&   r'   ru   rz   r   )rR   r|   r   r   s       r   to_docResultItem.to_doc   s     .d3//1!WW#**)++-'+'H'H'J113
	
 \EEr!   r2   N)r3   r4   r5   r6   r7   r
   r;   r9   ri   r   rb   dictr   r&   r'   rq   r   r`   ru   rz   r*   r   r   r   r<   r2   r!   r   r#   r#      s    &-#<>&7!89>"d^#%3   SS S S
 
Xd38R3R.S X#S # GTF&.~s/B&CF	F Fr!   c                       \ rS rSr% Sr\\S'    \\   \S'    \\   \S'    \\   \S'    / r	\\
\      \S'    \\   \S'    S	\4S
 jrS	\4S jrS	\4S jrS	\4S jrSrg)QueryResultItem   zQuery API result item.DocumentTitleFeedbackTokenFormatr1   AdditionalAttributesDocumentExcerptr   c                 .    U R                   R                  $ rP   )r   rB   rQ   s    r   r&   QueryResultItem.get_title   s    !!&&&r!   c                     U R                   (       d  gU R                   S   (       d  gU R                   S   R                  5       $ )Nr   r   )r   rS   rQ   s    r   get_attribute_value#QueryResultItem.get_attribute_value   s9    ((((+,,Q/>>@@r!   c                     U R                   (       a/  U R                   S   R                  S:X  a  U R                  5       nU$ U R                  (       a  U R                  R                  nU$ SnU$ )Nr   
AnswerTextr   )r   rK   r   r   rB   )rR   r   s     r   r'   QueryResultItem.get_excerpt   sh    %%))!,00L@..0G  !!**//G  Gr!   c                 "    SU R                   0nU$ )Ntype)r1   )rR   additional_metadatas     r   rq   'QueryResultItem.get_additional_metadata  s    %tyy1""r!   r2   N)r3   r4   r5   r6   r7   r@   r9   r
   r;   r   r   rI   r&   r   r'   r   rq   r<   r2   r!   r   r   r      s     %%C= ASM
 3-?FH(4(A#BCHG011''3 'AS AS # #r!   r   c                   Z    \ rS rSr% Sr\\   \S'    \\   \S'    S\4S jrS\4S jr	Sr
g	)
RetrieveResultItemi  zRetrieve API result item.r   Contentr   c                 ,    U R                   =(       d    S$ Nr   )r   rQ   s    r   r&   RetrieveResultItem.get_title  s    !!'R'r!   c                 ,    U R                   =(       d    S$ r   )r   rQ   s    r   r'   RetrieveResultItem.get_excerpt  s    ||!r!r!   r2   N)r3   r4   r5   r6   r7   r
   r;   r9   r&   r'   r<   r2   r!   r   r   r     s7    #C= c]"(3 ("S "r!   r   c                   *    \ rS rSr% Sr\\   \S'   Srg)QueryResulti!  a	  `Amazon Kendra Query API` search result.

It is composed of:
    * Relevant suggested answers: either a text excerpt or table excerpt.
    * Matching FAQs or questions-answer from your FAQ file.
    * Documents including an excerpt of each document with its title.
ResultItemsr2   N)	r3   r4   r5   r6   r7   r   r   r9   r<   r2   r!   r   r   r   !  s     o&&r!   r   c                   6    \ rS rSr% Sr\\S'    \\   \S'   Sr	g)RetrieveResulti/  z`Amazon Kendra Retrieve API` search result.

It is composed of:
    * relevant passages or text excerpts given an input query.
QueryIdr   r2   N)
r3   r4   r5   r6   r7   r;   r9   r   r   r<   r2   r!   r   r   r   /  s      L())r!   r           g      ?g      ?g      ?      ?)ry   LOWMEDIUMHIGH	VERY_HIGHz0.3.16z1.0z#langchain_aws.AmazonKendraRetriever)sinceremovalalternative_importc                      \ rS rSr% Sr\\S'   Sr\\   \S'   Sr	\\   \S'   Sr
\\S'   Sr\\   \S	'   Sr\\\      \S
'   \r\\/\4   \S'   \\S'   Sr\\   \S'   \\\   \" SSS94   \S'   \" S5      S\S\4S j5       r\" SS9\S\\\4   S\4S j5       5       rS\S\\   4S jrS\\   S\\    4S jr!S\\    S\\    4S jr"S\S\#S\\    4S  jr$S!r%g)"AmazonKendraRetrieveriE  a  `Amazon Kendra Index` retriever.

Args:
    index_id: Kendra index id

    region_name: The aws region e.g., `us-west-2`.
        Fallsback to AWS_DEFAULT_REGION env variable
        or region specified in ~/.aws/config.

    credentials_profile_name: The name of the profile in the ~/.aws/credentials
        or ~/.aws/config files, which has either access keys or role information
        specified. If not specified, the default credential profile or, if on an
        EC2 instance, credentials from IMDS will be used.

    top_k: No of results to return

    attribute_filter: Additional filtering of results based on metadata
        See: https://docs.aws.amazon.com/kendra/latest/APIReference

    document_relevance_override_configurations: Overrides relevance tuning
        configurations of fields/attributes set at the index level
        See: https://docs.aws.amazon.com/kendra/latest/APIReference

    page_content_formatter: generates the Document page_content
        allowing access to all result item attributes. By default, it uses
        the item's title and excerpt.

    client: boto3 client for Kendra

    user_context: Provides information about the user context
        See: https://docs.aws.amazon.com/kendra/latest/APIReference

Example:
    .. code-block:: python

        retriever = AmazonKendraRetriever(
            index_id="c0806df7-e76b-4bce-9b5c-d5582f6b1a03"
        )

index_idNregion_namecredentials_profile_name   top_kattribute_filter*document_relevance_override_configurationsr|   clientuser_contextr   r   )gelemin_score_confidencer]   r   c                 0    US:  a  [        SU S35      eU$ )Nr   top_k () cannot be negative.)
ValueError)clsr]   s     r   validate_top_k$AmazonKendraRetriever.validate_top_k  s#    19wug-BCDDr!   before)modevaluesc                    UR                  S5      nUb  US:  a  [        SU S35      eUR                  S5      b  U$  SS KnUR                  S5      (       a  UR                  US   S9nOUR                  5       n0 nUR                  S5      (       a  US   US'   UR                  " S0 UD6US'   U$ ! [
         a    [        S	5      e[         a  n[        S
5      UeS nAff = f)Nr   r   r   r   r   r   )profile_namer   zRCould not import boto3 python package. Please install it with `pip install boto3`.zCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid.)kendra)getr   boto3Sessionr   ImportError	Exception)r   r   r   r   sessionclient_paramses          r   create_client#AmazonKendraRetriever.create_client  s    

7#wug-BCDD::h+M	zz455--V<V5W-X  --/Mzz-((/5m/Dm,&~~H-HF8M 	>   	* 		s   A3B3 3C"CC"queryc                    U R                   UR                  5       SS U R                  S.nU R                  b  U R                  US'   U R                  b  U R                  US'   U R
                  b  U R
                  US'   U R                  R                  " S0 UD6n[        R                  U5      nUR                  (       a  UR                  $ U R                  R                  " S0 UD6n[        R                  U5      nUR                  $ )Nr   i  )IndexId	QueryTextPageSizeAttributeFilter'DocumentRelevanceOverrideConfigurationsUserContextr2   )r   stripr   r   r   r   r   retriever   	parse_objr   r   r   )rR   r   kendra_kwargsresponser_resultq_results         r   _kendra_query#AmazonKendraRetriever._kendra_query  s    }} q-


   ,/3/D/DM+,::F?? CD (+/+<+<M-(;;''8-8!++H5''' ;;$$5}5((2###r!   result_itemsc                 ~    US U R                     Vs/ sH  nUR                  U R                  5      PM      nnU$ s  snf rP   )r   r   r|   )rR   r   r"   top_docss       r   _get_top_k_docs%AmazonKendraRetriever._get_top_k_docs  sJ     %\tzz2
2 KK3342 	 
 	
s   $:docsc                 B   U R                   (       d  U$ U Vs/ sHz  nUR                  R                  S5      c  M!  [        UR                  S   [        5      (       d  ME  [
        R                  UR                  S   S5      U R                   :  d  Mx  UPM|     nnU$ s  snf )zZ
Filter out the records that have a score confidence
greater than the required threshold.
r   r   )r   r   r   
isinstancer;   KENDRA_CONFIDENCE_MAPPING)rR   r   r"   filtered_docss       r   _filter_by_score_confidence1AmazonKendraRetriever._filter_by_score_confidence  s    
 ((K 	
!!'*  t}}W5s;	 
 .11$--2H#N,,-  	 	
 	
s   B B/BBrun_managerc                h    U R                  U5      nU R                  U5      nU R                  U5      $ )zRun search on Kendra index and get top k documents

Example:
.. code-block:: python

    docs = retriever.invoke('This is my query')

)r   r   r   )rR   r   r   r   
top_k_docss        r   _get_relevant_documents-AmazonKendraRetriever._get_relevant_documents  s5     ))%0)),7
//
;;r!   r2   )&r3   r4   r5   r6   r7   r;   r9   r   r
   r   r   r8   r   r   r   r   r*   r|   r   r#   r   r   r   floatr   r   r   r   classmethodr   r   r   r   r   r   r   r   r<   r2   r!   r   r   r   E  sn   'R M!%K#%.2hsm2E3N'+htn+GK.d0DK:GHj\3%67GK#'L(4.'#HUOUcc5J$JKKw3 3  
 (#"4S> "c "  $"H$3 $8J+? $6HZ,@ T(^ X 4> &<< 4	<
 
h<r!   r   ).r   abcr   r   typingr   r   r   r   r	   r
   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr   pydanticr   r   r   r   typing_extensionsr   r;   r    r*   r8   r`   r,   r@   rE   rI   rV   rb   r#   r   r   r   r   r   r   r2   r!   r   <module>r     s]   	 #	 	 	 7 C - 3  (3 3    ( #3T#Y#<= 
=	 
=' *Yg *7	 7Yg >&	 &>FCw >FB.#j .#b" " 
)7 

Yg 
   
<
f<M f<
f<r!   