
    dh;                         S SK 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   " S S\S	S
9r " S S\S	S
9r\" SSSS9 " S S\5      5       rg)    )AnyDictListOptional)
deprecated)CallbackManagerForRetrieverRun)Document)BaseRetriever)	BaseModelmodel_validatorc                   (    \ rS rSr% SrSr\\S'   Srg)VectorSearchConfig
   z Configuration for vector search.   numberOfResults N)	__name__
__module____qualname____firstlineno____doc__r   int__annotations____static_attributes__r       ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/retrievers/bedrock.pyr   r   
   s    *OSr   r   allow)extrac                   $    \ rS rSr% Sr\\S'   Srg)RetrievalConfig   zConfiguration for retrieval.vectorSearchConfigurationr   N)r   r   r   r   r   r   r   r   r   r   r   r    r       s    &11r   r    z0.3.16z1.0z+langchain_aws.AmazonKnowledgeBasesRetriever)sinceremovalalternative_importc                       \ rS rSr% Sr\\S'   Sr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   \\S'   \\S	'   \" S
S9\S\\\4   S\4S j5       5       rS\S\S\\   4S jrSrg)AmazonKnowledgeBasesRetriever   a  Amazon Bedrock Knowledge Bases retriever.

See https://aws.amazon.com/bedrock/knowledge-bases for more info.

Setup:
    Install ``langchain-aws``:

    .. code-block:: bash

        pip install -U langchain-aws

Key init args:
    knowledge_base_id: Knowledge Base ID.
    region_name: The aws region e.g., `us-west-2`.
        Fallback 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.
    client: boto3 client for bedrock agent runtime.
    retrieval_config: Configuration for retrieval.

Instantiate:
    .. code-block:: python

        from langchain_community.retrievers import AmazonKnowledgeBasesRetriever

        retriever = AmazonKnowledgeBasesRetriever(
            knowledge_base_id="<knowledge-base-id>",
            retrieval_config={
                "vectorSearchConfiguration": {
                    "numberOfResults": 4
                }
            },
        )

Usage:
    .. code-block:: python

        query = "..."

        retriever.invoke(query)

Use within a chain:
    .. code-block:: python

        from langchain_aws import ChatBedrockConverse
        from langchain_core.output_parsers import StrOutputParser
        from langchain_core.prompts import ChatPromptTemplate
        from langchain_core.runnables import RunnablePassthrough
        from langchain_openai import ChatOpenAI

        prompt = ChatPromptTemplate.from_template(
            """Answer the question based only on the context provided.

        Context: {context}

        Question: {question}"""
        )

        llm = ChatBedrockConverse(
            model_id="anthropic.claude-3-5-sonnet-20240620-v1:0"
        )

        def format_docs(docs):
            return "\n\n".join(doc.page_content for doc in docs)

        chain = (
            {"context": retriever | format_docs, "question": RunnablePassthrough()}
            | prompt
            | llm
            | StrOutputParser()
        )

        chain.invoke("...")

knowledge_base_idNregion_namecredentials_profile_nameendpoint_urlclientretrieval_configbefore)modevaluesreturnc                 
   UR                  S5      b  U$  SS KnSSKJn  SSKJn  UR                  S5      (       a  UR                  US   S9nOUR                  5       nSU" SSS	S0S
90nUR                  S5      (       a  US   US'   UR                  S5      (       a  US   US'   UR                  " S0 UD6US'   U$ ! [         a    [        S5      eW a  n[        S5      UeS nAf[         a  n[        S5      UeS nAff = f)Nr-   r   )Config)UnknownServiceErrorr+   )profile_nameconfigx   max_attempts)connect_timeoutread_timeoutretriesr*   r,   zRCould not import boto3 python package. Please install it with `pip install boto3`.zjEnsure that you have installed the latest boto3 package that contains the API for `bedrock-runtime-agent`.zCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid.)zbedrock-agent-runtime)getboto3botocore.clientr4   botocore.exceptionsr5   Sessionr-   ImportError	Exception
ValueError)clsr1   r>   r4   r5   sessionclient_paramses           r   create_client+AmazonKnowledgeBasesRetriever.create_clientr   s8    ::h+M(	.?zz455--V<V5W-X  --/ &$'cNTUCVM
 zz-((/5m/Dm,zz.))06~0Fn-&~~WWF8M 	>  # 	E   	* 		s$   B'B> >DC$$D1C==Dqueryrun_managerc                n   U R                   R                  SUR                  5       0U R                  U R                  R                  5       S9nUS   n/ nU HZ  nUS   S   nUR                  S5        SU;  a  SUS'   SU;   a  UR                  S5      US'   UR                  [        UUS	95        M\     U$ )
Ntext)retrievalQueryknowledgeBaseIdretrievalConfigurationretrievalResultscontentscorer   metadatasource_metadata)page_contentrU   )	r-   retrievestripr)   r.   dictpopappendr	   )selfrK   rL   responseresults	documentsresultrS   s           r   _get_relevant_documents5AmazonKnowledgeBasesRetriever._get_relevant_documents   s     ;;''"EKKM2 22#'#8#8#=#=#? ( 

 -.	FY'/GJJy!f$"#wV#,2JJz,B()!(#  r   r   )r   r   r   r   r   strr   r*   r   r+   r,   r   r    r   classmethodr   rI   r   r   r	   rb   r   r   r   r   r'   r'      s    M^ !%K#%.2hsm2"&L(3-&K%%(#,4S> ,c ,  $,\*H	hr   r'   N)typingr   r   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr   langchain_core.documentsr	   langchain_core.retrieversr
   pydanticr   r   r   r    r'   r   r   r   <module>rl      sb    , , 6 C - 3 /' 2iw 2 
D
_M _
_r   