[docs]classSearchFilter(BaseModel):"""Filter configuration for retrieval."""andAll:Optional[List["SearchFilter"]]=NoneorAll:Optional[List["SearchFilter"]]=Noneequals:Optional[Filter]=NonegreaterThan:Optional[Filter]=NonegreaterThanOrEquals:Optional[Filter]=Nonein_:Optional[Filter]=Field(None,alias="in")lessThan:Optional[Filter]=NonelessThanOrEquals:Optional[Filter]=NonelistContains:Optional[Filter]=NonenotEquals:Optional[Filter]=NonenotIn:Optional[Filter]=Field(None,alias="notIn")startsWith:Optional[Filter]=NonestringContains:Optional[Filter]=NoneclassConfig:allow_population_by_field_name=True
[docs]classVectorSearchConfig(BaseModel,extra="allow"):# type: ignore[call-arg]"""Configuration for vector search."""numberOfResults:int=4filter:Optional[SearchFilter]=NoneoverrideSearchType:Optional[Literal["HYBRID","SEMANTIC"]]=None
[docs]classRetrievalConfig(BaseModel,extra="allow"):# type: ignore[call-arg]"""Configuration for retrieval."""vectorSearchConfiguration:VectorSearchConfignextToken:Optional[str]=None
[docs]classAmazonKnowledgeBasesRetriever(BaseRetriever):"""`Amazon Bedrock Knowledge Bases` retrieval. See https://aws.amazon.com/bedrock/knowledge-bases for more info. 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. Example: .. code-block:: python from langchain_community.retrievers import AmazonKnowledgeBasesRetriever retriever = AmazonKnowledgeBasesRetriever( knowledge_base_id="<knowledge-base-id>", retrieval_config={ "vectorSearchConfiguration": { "numberOfResults": 4 } }, ) """knowledge_base_id:strregion_name:Optional[str]=Nonecredentials_profile_name:Optional[str]=Noneendpoint_url:Optional[str]=Noneclient:Anyretrieval_config:RetrievalConfigmin_score_confidence:Annotated[Optional[float],Field(ge=0.0,le=1.0)]@root_validator(pre=True)defcreate_client(cls,values:Dict[str,Any])->Dict[str,Any]:ifvalues.get("client")isnotNone:returnvaluestry:ifvalues.get("credentials_profile_name"):session=boto3.Session(profile_name=values["credentials_profile_name"])else:# use default credentialssession=boto3.Session()client_params={"config":Config(connect_timeout=120,read_timeout=120,retries={"max_attempts":0})}ifvalues.get("region_name"):client_params["region_name"]=values["region_name"]ifvalues.get("endpoint_url"):client_params["endpoint_url"]=values["endpoint_url"]values["client"]=session.client("bedrock-agent-runtime",**client_params)returnvaluesexceptImportError:raiseModuleNotFoundError("Could not import boto3 python package. ""Please install it with `pip install boto3`.")exceptUnknownServiceErrorase:raiseModuleNotFoundError("Ensure that you have installed the latest boto3 package ""that contains the API for `bedrock-runtime-agent`.")fromeexceptExceptionase:raiseValueError("Could not load credentials to authenticate with AWS client. ""Please check that credentials in the specified ""profile name are valid.")fromedef_filter_by_score_confidence(self,docs:List[Document])->List[Document]:""" Filter out the records that have a score confidence less than the required threshold. """ifnotself.min_score_confidence:returndocsfiltered_docs=[itemforitemindocsif(item.metadata.get("score")isnotNoneanditem.metadata.get("score",0.0)>=self.min_score_confidence)]returnfiltered_docsdef_get_relevant_documents(self,query:str,*,run_manager:CallbackManagerForRetrieverRun,)->List[Document]:response=self.client.retrieve(retrievalQuery={"text":query.strip()},knowledgeBaseId=self.knowledge_base_id,retrievalConfiguration=self.retrieval_config.dict(exclude_none=True),)results=response["retrievalResults"]documents=[]forresultinresults:content=result["content"]["text"]result.pop("content")if"score"notinresult:result["score"]=0if"metadata"inresult:result["source_metadata"]=result.pop("metadata")documents.append(Document(page_content=content,metadata=result,))returnself._filter_by_score_confidence(docs=documents)