与 AWS SDK CreateKnowledgeBase 配合使用 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK CreateKnowledgeBase 配合使用

以下代码示例演示了如何使用 CreateKnowledgeBase

Python
适用于 Python 的 SDK(Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

创建 Amazon Bedrock 知识库。

def create_knowledge_base(bedrock_agent_client, name, role_arn, description=None): """ Creates a new knowledge base. Args: bedrock_agent_client: The Boto3 Bedrock Agent client. name (str): The name of the knowledge base. role_arn (str): The ARN of the IAM role that the knowledge base assumes to access resources. description (str, optional): A description of the knowledge base. Returns: dict: The details of the created knowledge base. """ try: kwargs = { "name": name, "roleArn": role_arn, "knowledgeBaseConfiguration": { "type": "VECTOR", "vectorKnowledgeBaseConfiguration": { "embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v1" } }, "storageConfiguration": { "type": "OPENSEARCH_SERVERLESS", # Note: You will need to create an OpenSearch Serverless collection first and replace this ARN # with your actual collection ARN from the OpenSearch console. If you use the console instead, # you can use the quick-create flow to have Knowledge Bases create the collection for you. "opensearchServerlessConfiguration": { "collectionArn": "arn:aws:aoss:us-east-1::123456789012:collection/abcdefgh12345678defgh", "fieldMapping": { "metadataField": "metadata", "textField": "text", "vectorField": "vector" }, "vectorIndexName": "test-uuid" }, }, "clientToken": "test-client-token-" + str(uuid.uuid4()) } if description: kwargs["description"] = description response = bedrock_agent_client.create_knowledge_base(**kwargs) logger.info("Created knowledge base with ID: %s", response["knowledgeBase"]["knowledgeBaseId"]) return response["knowledgeBase"] except ClientError as err: logger.error( "Couldn't create knowledge base. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 有关 API 的详细信息,请参阅适用CreateKnowledgeBasePython 的AWS SDK (Boto3) API 参考

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将 Amazon Bedrock 与 SD AWS K 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。