AI features¶
Goal: stand up a Bedrock knowledge base that can retrieve and cite content from an Amorphic dataset, under one shared content-moderation policy. Resolve a guardrail first so the knowledge base (and later Bedrock calls) reuse that policy, create the knowledge base with an embedding model, then attach the dataset as a source so Bedrock can index and answer from it.
APIs used here¶
ArtificialIntelligenceApi—list_ai_guard_rails,get_ai_guard_rail,create_knowledge_base,add_resources_to_knowledge_base,get_ai_knowledge_base,delete_knowledge_baseDatasetsApi—delete_dataset,delete_dataset_files
Assume ai_api = ArtificialIntelligenceApi(client) and
datasets_api = DatasetsApi(client) from Use Cases.
Look up an AI guardrail¶
Resolve the guardrail by name so you have its id and version before creating the knowledge base. Attaching that guardrail at create time means the KB and downstream Bedrock calls share one content-moderation policy instead of configuring filtering separately for each call.
listed = ai_api.list_ai_guard_rails(
role_id=role_id,
filter_expression=f"GuardRailName:{guardrail_name}",
)
if listed.guardrails:
guardrail_id = listed.guardrails[0].guard_rail_id
detail = ai_api.get_ai_guard_rail(role_id=role_id, id=guardrail_id)
guardrail_version = detail.guard_rail_version
else:
guardrail_id = ""
guardrail_version = "DRAFT"
Create a knowledge base and attach a dataset¶
Create the knowledge base with an embedding model and the resolved guardrails,
then add an existing dataset as a resource. Attaching the dataset is what lets
Bedrock retrieve and cite that content when answering questions. Chunking is
part of the attach payload; FIXED_SIZE is shown here. Other strategies
(for example SEMANTIC) use different fields — see the API reference.
from openapi_client.models.knowledge_base_create_request import KnowledgeBaseCreateRequest
from openapi_client.models.resource_addition_request import ResourceAdditionRequest
kb_request = KnowledgeBaseCreateRequest.from_dict({
"KnowledgebaseName": knowledge_base_name,
"Description": description,
"EmbeddingModel": embedding_model_id,
"GuardRails": [{"Id": guardrail_id, "Version": guardrail_version}],
})
kb = ai_api.create_knowledge_base(
role_id=role_id,
knowledge_base_create_request=kb_request,
)
knowledge_base_id = kb.knowledgebase_id
add_request = ResourceAdditionRequest.from_dict({
"ResourceType": "Dataset",
"Description": "Dataset source",
"ResourceKey": dataset_id,
"ChunkingConfiguration": {
"ChunkingStrategy": "FIXED_SIZE",
"MaxTokens": 1000,
"OverlapPercentage": 25,
},
"ParsingConfiguration": {
"ParsingStrategy": "DEFAULT",
},
})
added = ai_api.add_resources_to_knowledge_base(
id=knowledge_base_id,
role_id=role_id,
resource_addition_request=add_request,
)
data_source_id = added.source_id
Delete AI resources¶
Tear down the knowledge base when the RAG setup is no longer needed, then delete the dataset if it existed only to feed that knowledge base.
ai_api.delete_knowledge_base(id=knowledge_base_id, role_id=role_id)
# Optionally poll get_ai_knowledge_base until the KB is gone, then:
datasets_api.delete_dataset(id=dataset_id, role_id=role_id)
To remove selected files from the dataset without deleting it, call
delete_dataset_files with TruncateDataset: False.