Create OpenSearch Serverless security and network policies
<h2>CreateOpenSearchCollection</h2>security_policy_json={"Rules":[{"ResourceType":"collection","Resource":[f"collection/{kb_collection_name}"]}],"AWSOwnedKey":True}security_policy=open_search_serverless_client.create_security_policy(description='security policy of aoss collection',name=kb_collection_name,policy=json.dumps(security_policy_json),type='encryption')
network_policy_json=[{"Rules":[{"Resource":[f"collection/{kb_collection_name}"],"ResourceType":"dashboard"},{"Resource":[f"collection/{kb_collection_name}"],"ResourceType":"collection"}],"AllowFromPublic":True}]network_policy=open_search_serverless_client.create_security_policy(description='network policy of aoss collection',name=kb_collection_name,policy=json.dumps(network_policy_json),type='network')
Allow Lambda function to invoke OpenSearch Serverless
<h2>CreateIAMRolefortheLambdafunction</h2>try:assume_role_policy_document={"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}assume_role_policy_document_json=json.dumps(assume_role_policy_document)lambda_iam_role=iam_client.create_role(RoleName=lambda_role_name,AssumeRolePolicyDocument=assume_role_policy_document_json)# Pause to make sure role is createdtime.sleep(10)except:lambda_iam_role=iam_client.get_role(RoleName=lambda_role_name)
data_policy_json=[{"Rules":[{"Resource":[f"collection/{kb_collection_name}"],"Permission":["aoss:DescribeCollectionItems","aoss:CreateCollectionItems","aoss:UpdateCollectionItems","aoss:DeleteCollectionItems"],"ResourceType":"collection"},{"Resource":[f"index/{kb_collection_name}/*"],"Permission":["aoss:CreateIndex","aoss:DeleteIndex","aoss:UpdateIndex","aoss:DescribeIndex","aoss:ReadDocument","aoss:WriteDocument"],"ResourceType":"index"}],"Principal":[kb_role_arn,f"arn:aws:sts::{account_id}:assumed-role/Admin/*",current_role,# uncomment if running in sagemaker# sagemaker_execution_role,lambda_iam_role['Role']['Arn']],"Description":""}]data_policy=open_search_serverless_client.create_access_policy(description='data access policy for aoss collection',name=kb_collection_name,policy=json.dumps(data_policy_json),type='data')
<h2>Creatingtheknowledgebase</h2>try:# ensure the index is created and availabletime.sleep(45)kb_obj=bedrock_agent_client.create_knowledge_base(name=kb_name,description='KB that contains product reviews',roleArn=kb_role_arn,knowledgeBaseConfiguration={'type':'VECTOR',# Corrected type'vectorKnowledgeBaseConfiguration':{'embeddingModelArn':embedding_model_arn}},storageConfiguration=storage_configuration)# Pretty print the responsepprint.pprint(kb_obj)exceptExceptionase:print(f"Error occurred: {e}")
<h2>DefinetheS3configurationforyourdatasource</h2>s3_configuration={'bucketArn':bucket_arn,'inclusionPrefixes':[kb_key]}<h2>Definethedatasourceconfiguration</h2>data_source_configuration={'s3Configuration':s3_configuration,'type':'S3'}knowledge_base_id=kb_obj["knowledgeBase"]["knowledgeBaseId"]knowledge_base_arn=kb_obj["knowledgeBase"]["knowledgeBaseArn"]chunking_strategy_configuration={"chunkingStrategy":"FIXED_SIZE","fixedSizeChunkingConfiguration":{"maxTokens":512,"overlapPercentage":20}}<h2>Createthedatasource</h2>try:# ensure that the KB is created and availabletime.sleep(45)data_source_response=bedrock_agent_client.create_data_source(knowledgeBaseId=knowledge_base_id,name=data_source_name,description='DataSource for the insurance claim documents requirements',dataSourceConfiguration=data_source_configuration,vectorIngestionConfiguration={"chunkingConfiguration":chunking_strategy_configuration})# Pretty print the responsepprint.pprint(data_source_response)exceptExceptionase:print(f"Error occurred: {e}")
<h2>CreateAgent</h2>agent_instruction="""You are an agent that can look up product reviews. If an user asks about your functionality, provide guidance in natural language and do not include function names on the output."""response=bedrock_agent_client.create_agent(agentName=agent_name,agentResourceRoleArn=agent_role['Role']['Arn'],description="Agent for searching through product reviews.",idleSessionTTLInSeconds=1800,foundationModel="anthropic.claude-3-haiku-20240307-v1:0",instruction=agent_instruction,)
agent_id=response['agent']['agentId']
Create Bedrock Agent Action Group
agent_functions=[{"name":"retrieve-reviews-opensearch","description":"Gets the list of top n reviews. Do not use this if any product is mentioned.","parameters":{"count":{"description":"count of reviews to return","required":True,"type":"integer"},"end_date":{"description":"end date of range of reviews to query","required":True,"type":"number"},"start_date":{"description":"start date of range of reviews to query","required":True,"type":"number"}}},{"name":"retrieve-reviews-hybrid","description":"Gets the list of top n reviews. Use this if any product is mentioned.","parameters":{"count":{"description":"count of reviews to return","required":True,"type":"integer"},"description":{"description":"description of product","required":True,"type":"string"},"end_date":{"description":"end date of range of reviews to query","required":True,"type":"number"},"reviewer":{"description":"reviewer of product","required":True,"type":"string"},"start_date":{"description":"start date of range of reviews to query","required":True,"type":"number"}}}]
<h2>Pausetomakesureagentiscreated</h2>time.sleep(30)<h2>Now,wecanconfigureandcreateanactiongrouphere:</h2>agent_action_group_response=bedrock_agent_client.create_agent_action_group(agentId=agent_id,agentVersion='DRAFT',actionGroupExecutor={'lambda':lambda_function['FunctionArn']},actionGroupName='GetReviewsActionGroup',functionSchema={'functions':agent_functions},description='Actions for listing product reviews')
<h2>ExtracttheagentAliasIdfromtheresponse</h2>agent_alias_id=agent_alias['agentAlias']['agentAliasId']<h2>createarandomidforsessioninitiatorid</h2>session_id:str=str(uuid.uuid1())enable_trace:bool=Trueend_session:bool=False<h2>invoketheagentAPI</h2>agentResponse=bedrock_agent_runtime_client.invoke_agent(inputText=""" The start date and end date are placed in <start_date></start_date> and <end_date></end_date> tags. Give me the last 2 reviews on hair conditioner from jokic. <start_date>1477808000000</start_date> <end_date>1609430400000</end_date> """,agentId=agent_id,agentAliasId=agent_alias_id,sessionId=session_id,enableTrace=enable_trace,endSession=end_session)logger.info(pprint.pprint(agentResponse))
%%timeevent_stream=agentResponse['completion']try:foreventinevent_stream:if'chunk'inevent:data=event['chunk']['bytes']logger.info(f"Final answer ->\n{data.decode('utf8')}")agent_answer=data.decode('utf8')end_event_received=True# End event indicates that the request finished successfullyelif'trace'inevent:logger.info(json.dumps(event['trace'],indent=2))else:raiseException("unexpected event.",event)exceptExceptionase:raiseException("unexpected event.",e)