From 22d084264c9857768d9b68c13ba64755454d8d1a Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 14:24:06 -0700 Subject: [PATCH 1/8] Add in infra as code --- bedrock/README.md | 15 ++++++ bedrock/main.tf | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 bedrock/README.md create mode 100644 bedrock/main.tf diff --git a/bedrock/README.md b/bedrock/README.md new file mode 100644 index 00000000..27e864ab --- /dev/null +++ b/bedrock/README.md @@ -0,0 +1,15 @@ +# Exploration of AWS bedrock + +This is to document the exploration of AWS bedrock and the resources created + +## Knowledge Base + +1. Log into SCCE dev aws account +1. Create knowledge base (nftc-kb) with a service role +1. See a scratch template in `main.tf` that is untested currently + +## Agent +1. Create an agent +1. create a service role for the agent +1. After creation, link it to the knowledge base +1. prepare the agent \ No newline at end of file diff --git a/bedrock/main.tf b/bedrock/main.tf new file mode 100644 index 00000000..99169c45 --- /dev/null +++ b/bedrock/main.tf @@ -0,0 +1,113 @@ +resource "aws_s3_bucket" "nftc_kb_bucket" { + bucket = "nftc-kb-bucket" + + tags = { + Name = "nftc-kb-bucket" + } +} + +resource "aws_iam_role" "nftc_kb_role" { + name = "nftc-kb-role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17", + Statement = [ + { + Effect = "Allow", + Principal = { + Service = "bedrock.amazonaws.com" + }, + Action = "sts:AssumeRole" + } + ] + }) + + tags = { + Name = "nftc-kb-role" + } +} + +resource "aws_iam_role_policy" "nftc_kb_policy" { + name = "nftc-kb-policy" + role = aws_iam_role.nftc_kb_role.id + + policy = jsonencode({ + Version = "2012-10-17", + Statement = [ + { + Effect = "Allow", + Action = [ + "s3:ListBucket", + "s3:GetObject", + "s3:PutObject" + ], + Resource = [ + aws_s3_bucket.nftc_kb_bucket.arn, + "${aws_s3_bucket.nftc_kb_bucket.arn}/*" + ] + } + ] + }) +} + +resource "awscc_bedrock_knowledge_base" "nftc_kb" { + knowledge_base_name = "nftc-kb" + s3_bucket = aws_s3_bucket.nftc_kb_bucket.bucket + service_role = aws_iam_role.nftc_kb_role.arn + + tags = { + Name = "nftc-kb" + } +} + +resource "aws_iam_role" "nftc_agent_role" { + name = "nftc-agent-role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17", + Statement = [ + { + Effect = "Allow", + Principal = { + Service = "bedrock.amazonaws.com" + }, + Action = "sts:AssumeRole" + } + ] + }) + + tags = { + Name = "nftc-agent-role" + } +} + +resource "aws_iam_role_policy" "nftc_agent_policy" { + name = "nftc-agent-policy" + role = aws_iam_role.nftc_agent_role.id + + policy = jsonencode({ + Version = "2012-10-17", + Statement = [ + { + Effect = "Allow", + Action = [ + "bedrock:DescribeKnowledgeBase", + "bedrock:QueryKnowledgeBase" + ], + Resource = [ + awscc_bedrock_knowledge_base.nftc_kb.arn + ] + } + ] + }) +} + +resource "awscc_bedrock_agent" "nftc_agent" { + agent_name = "nftc-agent" + service_role = aws_iam_role.nftc_agent_role.arn + knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id + + tags = { + Name = "nftc-agent" + } +} From b4ab1c1e1dd4e57904815402babfea91bff6690c Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 17:30:25 -0700 Subject: [PATCH 2/8] Add in tf script --- bedrock/main.tf | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/bedrock/main.tf b/bedrock/main.tf index 99169c45..c832e5fe 100644 --- a/bedrock/main.tf +++ b/bedrock/main.tf @@ -50,7 +50,7 @@ resource "aws_iam_role_policy" "nftc_kb_policy" { }) } -resource "awscc_bedrock_knowledge_base" "nftc_kb" { +resource "aws_bedrock_knowledge_base" "nftc_kb" { knowledge_base_name = "nftc-kb" s3_bucket = aws_s3_bucket.nftc_kb_bucket.bucket service_role = aws_iam_role.nftc_kb_role.arn @@ -102,12 +102,32 @@ resource "aws_iam_role_policy" "nftc_agent_policy" { }) } -resource "awscc_bedrock_agent" "nftc_agent" { - agent_name = "nftc-agent" - service_role = aws_iam_role.nftc_agent_role.arn - knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id +resource "aws_bedrockagent_agent_knowledge_base_association" "nftc_kb_association" { + agent_id = aws_bedrockagent_agent.nftc_agent.id + description = "Example Knowledge base" + knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id + knowledge_base_state = "ENABLED" +} - tags = { - Name = "nftc-agent" - } +resource "aws_bedrockagent_agent" "nftc_agent" { + agent_name = "nftc-agent" + service_role = aws_iam_role.nftc_agent_role.arn + knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id + foundation_model = "anthropic.claude-v3-sonnet" + instruction = """ + Your task is to extract data about research tools, such as animal models and cell lines biobanks from scientific publications. When provided with a name or synonym for a research tool, you will generate a comprehensive list of temporal "observations" about the research tool that describe the natural history of the model as they relate to development or age. For example, an observation could be "The pigs developed tumor type X at Y months of age." Do not include observations about humans with NF1. Your response must be formatted to be compliant with the following JSON: + [ + { + resourceType: [Animal Model, Cell Line], + observationText: This is an example sentence., + observationType: [Body Length, Body weight, Coat Color, Disease Susceptibility, Feed Intake, Feeding Behavior, Growth rate, Motor Activity, Organ Development, Reflex Development, Reproductive Behavior, Social Behavior, Swimming Behavior, Tumor Growth, Issue, Depositor Comment, Usage Instructions, General Comment or Review, Other], + observationPhase: [prenatal, postnatal, null], + observationTime: a double; the time during the development of the organism which the observation occurred, + observationTimeUnits: [days, weeks, months, years] + } + ] + """ + tags = { + Name = "nftc-agent" + } } From d8d3d8ce2821b3852b41315c3f72d9700fe08ace Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 17:35:33 -0700 Subject: [PATCH 3/8] reconfigure resources --- bedrock/main.tf | 87 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/bedrock/main.tf b/bedrock/main.tf index c832e5fe..ec80d32b 100644 --- a/bedrock/main.tf +++ b/bedrock/main.tf @@ -51,15 +51,30 @@ resource "aws_iam_role_policy" "nftc_kb_policy" { } resource "aws_bedrock_knowledge_base" "nftc_kb" { - knowledge_base_name = "nftc-kb" - s3_bucket = aws_s3_bucket.nftc_kb_bucket.bucket - service_role = aws_iam_role.nftc_kb_role.arn - + name = "nftc-kb" + role_arn = aws_iam_role.nftc_kb_role.arn + knowledge_base_configuration { + vector_knowledge_base_configuration { + embedding_model_arn = "arn:aws:bedrock:us-west-2::foundation-model/amazon.titan-embed-text-v1" + } + type = "VECTOR" + } tags = { Name = "nftc-kb" } } +resource "aws_bedrockagent_data_source" "example" { + knowledge_base_id = aws_bedrock_knowledge_base.nftc_kb.id + name = "nftc-kb-datasource" + data_source_configuration { + type = "S3" + s3_configuration { + bucket_arn = aws_s3_bucket.nftc_kb_bucket.arn + } + } +} + resource "aws_iam_role" "nftc_agent_role" { name = "nftc-agent-role" @@ -81,27 +96,54 @@ resource "aws_iam_role" "nftc_agent_role" { } } -resource "aws_iam_role_policy" "nftc_agent_policy" { - name = "nftc-agent-policy" - role = aws_iam_role.nftc_agent_role.id - policy = jsonencode({ - Version = "2012-10-17", - Statement = [ - { - Effect = "Allow", - Action = [ - "bedrock:DescribeKnowledgeBase", - "bedrock:QueryKnowledgeBase" - ], - Resource = [ - awscc_bedrock_knowledge_base.nftc_kb.arn - ] - } + +data "aws_caller_identity" "current" {} + +data "aws_partition" "current" {} + +data "aws_region" "current" {} + +data "aws_iam_policy_document" "example_agent_trust" { + statement { + actions = ["sts:AssumeRole"] + principals { + identifiers = ["bedrock.amazonaws.com"] + type = "Service" + } + condition { + test = "StringEquals" + values = [data.aws_caller_identity.current.account_id] + variable = "aws:SourceAccount" + } + condition { + test = "ArnLike" + values = ["arn:${data.aws_partition.current.partition}:bedrock:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:agent/*"] + variable = "AWS:SourceArn" + } + } +} + +data "aws_iam_policy_document" "example_agent_permissions" { + statement { + actions = ["bedrock:InvokeModel"] + resources = [ + "arn:${data.aws_partition.current.partition}:bedrock:${data.aws_region.current.name}::foundation-model/anthropic.claude-v2", ] - }) + } +} + +resource "aws_iam_role" "example" { + assume_role_policy = data.aws_iam_policy_document.example_agent_trust.json + name_prefix = "AmazonBedrockExecutionRoleForAgents_" +} + +resource "aws_iam_role_policy" "example" { + policy = data.aws_iam_policy_document.example_agent_permissions.json + role = aws_iam_role.example.id } + resource "aws_bedrockagent_agent_knowledge_base_association" "nftc_kb_association" { agent_id = aws_bedrockagent_agent.nftc_agent.id description = "Example Knowledge base" @@ -111,8 +153,7 @@ resource "aws_bedrockagent_agent_knowledge_base_association" "nftc_kb_associatio resource "aws_bedrockagent_agent" "nftc_agent" { agent_name = "nftc-agent" - service_role = aws_iam_role.nftc_agent_role.arn - knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id + agent_resource_role_arn = aws_iam_role.example.arn foundation_model = "anthropic.claude-v3-sonnet" instruction = """ Your task is to extract data about research tools, such as animal models and cell lines biobanks from scientific publications. When provided with a name or synonym for a research tool, you will generate a comprehensive list of temporal "observations" about the research tool that describe the natural history of the model as they relate to development or age. For example, an observation could be "The pigs developed tumor type X at Y months of age." Do not include observations about humans with NF1. Your response must be formatted to be compliant with the following JSON: From cbce2d3715b76806cc5b86f8ad507300a9176bdc Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 19:26:40 -0700 Subject: [PATCH 4/8] add invoke code --- bedrock/invoke.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 bedrock/invoke.py diff --git a/bedrock/invoke.py b/bedrock/invoke.py new file mode 100644 index 00000000..5965ae4a --- /dev/null +++ b/bedrock/invoke.py @@ -0,0 +1,48 @@ +import boto3 +from botocore.exceptions import ClientError + + +def invoke_agent(agents_runtime_client, agent_id, agent_alias_id, session_id, prompt): + """ + Sends a prompt for the agent to process and respond to. + + :param agent_id: The unique identifier of the agent to use. + :param agent_alias_id: The alias of the agent to use. + :param session_id: The unique identifier of the session. Use the same value across requests + to continue the same conversation. + :param prompt: The prompt that you want Claude to complete. + :return: Inference response from the model. + """ + + try: + response = agents_runtime_client.invoke_agent( + agentId=agent_id, + agentAliasId=agent_alias_id, + sessionId=session_id, + inputText=prompt, + ) + + completion = "" + + for event in response.get("completion"): + chunk = event["chunk"] + completion = completion + chunk["bytes"].decode() + + except ClientError as e: + print(f"Couldn't invoke agent. {e}") + raise + + return completion + + +runtime_client=boto3.client( + service_name="bedrock-agent-runtime", region_name="us-east-1" +) + +invoke_agent( + agents_runtime_client=runtime_client, + agent_id="7O1Q74HUYJ", + agent_alias_id="YQ6O9PQNP8", + session_id="my_session", + prompt="give me some observations about NF1fl/fl;Dhh-Cre" +) From 231b9e46c803fea9ec2b0186e39735dbbcd140cb Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 19:35:14 -0700 Subject: [PATCH 5/8] Update invoke script --- bedrock/invoke.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/bedrock/invoke.py b/bedrock/invoke.py index 5965ae4a..94cadc08 100644 --- a/bedrock/invoke.py +++ b/bedrock/invoke.py @@ -1,3 +1,7 @@ +"""Invokes the NF bedrock agent""" +import json +import uuid + import boto3 from botocore.exceptions import ClientError @@ -6,12 +10,15 @@ def invoke_agent(agents_runtime_client, agent_id, agent_alias_id, session_id, pr """ Sends a prompt for the agent to process and respond to. - :param agent_id: The unique identifier of the agent to use. - :param agent_alias_id: The alias of the agent to use. - :param session_id: The unique identifier of the session. Use the same value across requests - to continue the same conversation. - :param prompt: The prompt that you want Claude to complete. - :return: Inference response from the model. + Args: + agent_id: The unique identifier of the agent to use. + agent_alias_id: The alias of the agent to use. + session_id: The unique identifier of the session. Use the same value across requests + to continue the same conversation. + prompt: The prompt that you want Claude to complete. + + Returns: + Inference response from the model. """ try: @@ -39,10 +46,12 @@ def invoke_agent(agents_runtime_client, agent_id, agent_alias_id, session_id, pr service_name="bedrock-agent-runtime", region_name="us-east-1" ) -invoke_agent( + +response = invoke_agent( agents_runtime_client=runtime_client, agent_id="7O1Q74HUYJ", - agent_alias_id="YQ6O9PQNP8", - session_id="my_session", + agent_alias_id="EGWXPZWTLS", + session_id=str(uuid.uuid1()), prompt="give me some observations about NF1fl/fl;Dhh-Cre" ) +json.loads(response) From 2ccc20c1166db86f063d92f012b9f8e954e4cc6b Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Thu, 23 May 2024 19:36:30 -0700 Subject: [PATCH 6/8] Update --- bedrock/invoke.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bedrock/invoke.py b/bedrock/invoke.py index 94cadc08..9772a1cd 100644 --- a/bedrock/invoke.py +++ b/bedrock/invoke.py @@ -42,16 +42,15 @@ def invoke_agent(agents_runtime_client, agent_id, agent_alias_id, session_id, pr return completion -runtime_client=boto3.client( - service_name="bedrock-agent-runtime", region_name="us-east-1" -) - - -response = invoke_agent( - agents_runtime_client=runtime_client, - agent_id="7O1Q74HUYJ", - agent_alias_id="EGWXPZWTLS", - session_id=str(uuid.uuid1()), - prompt="give me some observations about NF1fl/fl;Dhh-Cre" -) -json.loads(response) +if __name__ == "__main__": + runtime_client = boto3.client( + service_name="bedrock-agent-runtime", region_name="us-east-1" + ) + response = invoke_agent( + agents_runtime_client=runtime_client, + agent_id="7O1Q74HUYJ", + agent_alias_id="EGWXPZWTLS", + session_id=str(uuid.uuid1()), + prompt="give me some observations about NF1fl/fl;Dhh-Cre" + ) + print(json.loads(response)) From 42eb362254f4b00181b2cbfef6955a1a93763da3 Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Sat, 25 May 2024 10:52:22 -0700 Subject: [PATCH 7/8] Add in response generation template --- bedrock/README.md | 2 +- bedrock/main.tf | 125 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 116 insertions(+), 11 deletions(-) diff --git a/bedrock/README.md b/bedrock/README.md index 27e864ab..12e8b57f 100644 --- a/bedrock/README.md +++ b/bedrock/README.md @@ -12,4 +12,4 @@ This is to document the exploration of AWS bedrock and the resources created 1. Create an agent 1. create a service role for the agent 1. After creation, link it to the knowledge base -1. prepare the agent \ No newline at end of file +1. prepare the agent diff --git a/bedrock/main.tf b/bedrock/main.tf index ec80d32b..e41a7833 100644 --- a/bedrock/main.tf +++ b/bedrock/main.tf @@ -1,3 +1,11 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} resource "aws_s3_bucket" "nftc_kb_bucket" { bucket = "nftc-kb-bucket" @@ -67,6 +75,7 @@ resource "aws_bedrock_knowledge_base" "nftc_kb" { resource "aws_bedrockagent_data_source" "example" { knowledge_base_id = aws_bedrock_knowledge_base.nftc_kb.id name = "nftc-kb-datasource" + data_deletion_policy = "DELETE" data_source_configuration { type = "S3" s3_configuration { @@ -155,19 +164,115 @@ resource "aws_bedrockagent_agent" "nftc_agent" { agent_name = "nftc-agent" agent_resource_role_arn = aws_iam_role.example.arn foundation_model = "anthropic.claude-v3-sonnet" - instruction = """ - Your task is to extract data about research tools, such as animal models and cell lines biobanks from scientific publications. When provided with a name or synonym for a research tool, you will generate a comprehensive list of temporal "observations" about the research tool that describe the natural history of the model as they relate to development or age. For example, an observation could be "The pigs developed tumor type X at Y months of age." Do not include observations about humans with NF1. Your response must be formatted to be compliant with the following JSON: - [ + instruction = < + $search_results$ + + If you reference information from a search result within your answer, you must include a citation to source where the information was found. Each result has a corresponding source ID that you should reference. + Note that may contain multiple if you include information from multiple results in your answer. + Do NOT directly quote the in your answer. Your job is to answer the user's question as concisely as possible. + You must output your answer in the following format. Pay attention and follow the formatting and spacing exactly: + + + + [ + { + "resourceName": "the resource name, likely the same as the input concept from the user", + "resourceType": ["Animal Model", "Cell Line"], + "observationText": "This is an example sentence.", + "observationType": [ + "Body Length", + "Body weight", + "Coat Color", + "Disease Susceptibility", + "Feed Intake", + "Feeding Behavior", + "Growth rate", + "Motor Activity", + "Organ Development", + "Reflex Development", + "Reproductive Behavior", + "Social Behavior", + "Swimming Behavior", + "Tumor Growth", + "Issue", + "Depositor Comment", + "Usage Instructions", + "General Comment or Review", + "Other" + ], + "observationPhase": ["prenatal", "postnatal", null], + "observationTime": "a double; the time during the development of the organism at which the observation occurred", + "observationTimeUnits": ["days", "weeks", "months", "years"], + "sourcePublication": "pubmed ID or DOI" + }, + ] + + + source ID + + + + + [ { - resourceType: [Animal Model, Cell Line], - observationText: This is an example sentence., - observationType: [Body Length, Body weight, Coat Color, Disease Susceptibility, Feed Intake, Feeding Behavior, Growth rate, Motor Activity, Organ Development, Reflex Development, Reproductive Behavior, Social Behavior, Swimming Behavior, Tumor Growth, Issue, Depositor Comment, Usage Instructions, General Comment or Review, Other], - observationPhase: [prenatal, postnatal, null], - observationTime: a double; the time during the development of the organism which the observation occurred, - observationTimeUnits: [days, weeks, months, years] + "resourceName": "the resource name, likely the same as the input concept from the user", + "resourceType": ["Animal Model", "Cell Line"], + "observationText": "This is an example sentence.", + "observationType": [ + "Body Length", + "Body weight", + "Coat Color", + "Disease Susceptibility", + "Feed Intake", + "Feeding Behavior", + "Growth rate", + "Motor Activity", + "Organ Development", + "Reflex Development", + "Reproductive Behavior", + "Social Behavior", + "Swimming Behavior", + "Tumor Growth", + "Issue", + "Depositor Comment", + "Usage Instructions", + "General Comment or Review", + "Other" + ], + "observationPhase": ["prenatal", "postnatal", null], + "observationTime": "a double; the time during the development of the organism at which the observation occurred", + "observationTimeUnits": ["days", "weeks", "months", "years"], + "sourcePublication": "pubmed ID or DOI" + }, + ] + + + source ID + + + + EOT + inference_configuration = { + max_length = 2048 + stop_sequences=["Human"] + temperature= 0 + top_k= 250 + top_p= 1 } + } ] - """ tags = { Name = "nftc-agent" } From fe4461139b4a2afe36d21984bbd676f1c5d853c8 Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Sat, 25 May 2024 11:26:27 -0700 Subject: [PATCH 8/8] Update policies --- bedrock/main.tf | 213 ++++++++++++++++++++++-------------------------- 1 file changed, 98 insertions(+), 115 deletions(-) diff --git a/bedrock/main.tf b/bedrock/main.tf index e41a7833..38cdd936 100644 --- a/bedrock/main.tf +++ b/bedrock/main.tf @@ -58,22 +58,25 @@ resource "aws_iam_role_policy" "nftc_kb_policy" { }) } -resource "aws_bedrock_knowledge_base" "nftc_kb" { +resource "aws_bedrockagent_knowledge_base" "nftc_kb" { name = "nftc-kb" role_arn = aws_iam_role.nftc_kb_role.arn knowledge_base_configuration { vector_knowledge_base_configuration { - embedding_model_arn = "arn:aws:bedrock:us-west-2::foundation-model/amazon.titan-embed-text-v1" + embedding_model_arn = "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v1" } type = "VECTOR" } + storage_configuration { + type = "OPENSEARCH_SERVERLESS" + } tags = { Name = "nftc-kb" } } resource "aws_bedrockagent_data_source" "example" { - knowledge_base_id = aws_bedrock_knowledge_base.nftc_kb.id + knowledge_base_id = aws_bedrockagent_knowledge_base.nftc_kb.id name = "nftc-kb-datasource" data_deletion_policy = "DELETE" data_source_configuration { @@ -133,147 +136,127 @@ data "aws_iam_policy_document" "example_agent_trust" { } } -data "aws_iam_policy_document" "example_agent_permissions" { +data "aws_iam_policy_document" "AmazonBedrockAgentBedrockFoundationModelPolicy" { statement { actions = ["bedrock:InvokeModel"] resources = [ "arn:${data.aws_partition.current.partition}:bedrock:${data.aws_region.current.name}::foundation-model/anthropic.claude-v2", + "arn:${data.aws_partition.current.partition}:bedrock:${data.aws_region.current.name}::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0", + ] + } +} + +data "aws_iam_policy_document" "AmazonBedrockAgentRetrieveKnowledgeBasePolicy" { + statement { + actions = ["bedrock:Retrieve"] + resources = [ + "arn:${data.aws_partition.current.partition}:bedrock:${data.aws_region.current.name}::${data.aws_caller_identity.current.account_id}:knowledge-base/${aws_bedrockagent_knowledge_base.nftc_kb.id}" ] } } -resource "aws_iam_role" "example" { +resource "aws_iam_role" "agent" { assume_role_policy = data.aws_iam_policy_document.example_agent_trust.json name_prefix = "AmazonBedrockExecutionRoleForAgents_" } -resource "aws_iam_role_policy" "example" { - policy = data.aws_iam_policy_document.example_agent_permissions.json - role = aws_iam_role.example.id +resource "aws_iam_role_policy" "foundation_model_policy" { + policy = data.aws_iam_policy_document.AmazonBedrockAgentBedrockFoundationModelPolicy.json + role = aws_iam_role.agent.id +} + +resource "aws_iam_role_policy" "rag_policy" { + policy = data.aws_iam_policy_document.AmazonBedrockAgentRetrieveKnowledgeBasePolicy.json + role = aws_iam_role.agent.id } resource "aws_bedrockagent_agent_knowledge_base_association" "nftc_kb_association" { agent_id = aws_bedrockagent_agent.nftc_agent.id description = "Example Knowledge base" - knowledge_base_id = awscc_bedrock_knowledge_base.nftc_kb.id + knowledge_base_id = aws_bedrockagent_knowledge_base.nftc_kb.id knowledge_base_state = "ENABLED" } resource "aws_bedrockagent_agent" "nftc_agent" { - agent_name = "nftc-agent" - agent_resource_role_arn = aws_iam_role.example.arn - foundation_model = "anthropic.claude-v3-sonnet" + agent_name = "nftc-agent" + agent_resource_role_arn = aws_iam_role.agent.arn + foundation_model = "anthropic.claude-v3-sonnet" instruction = < - $search_results$ - - If you reference information from a search result within your answer, you must include a citation to source where the information was found. Each result has a corresponding source ID that you should reference. - Note that may contain multiple if you include information from multiple results in your answer. - Do NOT directly quote the in your answer. Your job is to answer the user's question as concisely as possible. - You must output your answer in the following format. Pay attention and follow the formatting and spacing exactly: - - - - [ - { - "resourceName": "the resource name, likely the same as the input concept from the user", - "resourceType": ["Animal Model", "Cell Line"], - "observationText": "This is an example sentence.", - "observationType": [ - "Body Length", - "Body weight", - "Coat Color", - "Disease Susceptibility", - "Feed Intake", - "Feeding Behavior", - "Growth rate", - "Motor Activity", - "Organ Development", - "Reflex Development", - "Reproductive Behavior", - "Social Behavior", - "Swimming Behavior", - "Tumor Growth", - "Issue", - "Depositor Comment", - "Usage Instructions", - "General Comment or Review", - "Other" - ], - "observationPhase": ["prenatal", "postnatal", null], - "observationTime": "a double; the time during the development of the organism at which the observation occurred", - "observationTimeUnits": ["days", "weeks", "months", "years"], - "sourcePublication": "pubmed ID or DOI" - }, - ] - - - source ID - - - - - [ - { - "resourceName": "the resource name, likely the same as the input concept from the user", - "resourceType": ["Animal Model", "Cell Line"], - "observationText": "This is an example sentence.", - "observationType": [ - "Body Length", - "Body weight", - "Coat Color", - "Disease Susceptibility", - "Feed Intake", - "Feeding Behavior", - "Growth rate", - "Motor Activity", - "Organ Development", - "Reflex Development", - "Reproductive Behavior", - "Social Behavior", - "Swimming Behavior", - "Tumor Growth", - "Issue", - "Depositor Comment", - "Usage Instructions", - "General Comment or Review", - "Other" - ], - "observationPhase": ["prenatal", "postnatal", null], - "observationTime": "a double; the time during the development of the organism at which the observation occurred", - "observationTimeUnits": ["days", "weeks", "months", "years"], - "sourcePublication": "pubmed ID or DOI" - }, - ] - - - source ID - - - - EOT - inference_configuration = { - max_length = 2048 - stop_sequences=["Human"] - temperature= 0 - top_k= 250 - top_p= 1 - } + prompt_configurations = [{ + prompt_type = "KNOWLEDGE_BASE_RESPONSE_GENERATION", + prompt_state = "ENABLED", + prompt_creation_mode = "OVERRIDDEN", + parser_mode = "DEFAULT", + base_prompt_template = < + $search_results$ + + If you reference information from a search result within your answer, you must include a citation to source where the information was found. Each result has a corresponding source ID that you should reference. + Do NOT directly quote the in your answer. Your job is to answer the user's question as concisely as possible. + You must output your answer in the following format. Pay attention and follow the formatting and spacing exactly: + + + + [ + { + "resourceName": "the resource name, likely the same as the input concept from the user", + "resourceType": ["Animal Model", "Cell Line"], + "observationText": "This is an example sentence.", + "observationType": [ + "Body Length", + "Body weight", + "Coat Color", + "Disease Susceptibility", + "Feed Intake", + "Feeding Behavior", + "Growth rate", + "Motor Activity", + "Organ Development", + "Reflex Development", + "Reproductive Behavior", + "Social Behavior", + "Swimming Behavior", + "Tumor Growth", + "Issue", + "Depositor Comment", + "Usage Instructions", + "General Comment or Review", + "Other" + ], + "observationPhase": ["prenatal", "postnatal", null], + "observationTime": "a double; the time during the development of the organism at which the observation occurred", + "observationTimeUnits": ["days", "weeks", "months", "years"], + "sourcePublication": "pubmed ID or DOI" + }, + ] + + + source ID + + + + + EOT + inference_configuration = [{ + max_length = 2048 + stop_sequences = ["Human"] + temperature = 0 + top_k = 250 + top_p = 1 + }] + }], + override_lambda = null } ] tags = { Name = "nftc-agent" } } +