Gunakan CreateKnowledgeBase dengan AWS SDK - Amazon Bedrock

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan CreateKnowledgeBase dengan AWS SDK

Contoh kode berikut menunjukkan cara menggunakanCreateKnowledgeBase.

Python
SDK untuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.

Buat basis pengetahuan 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

Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMenggunakan Amazon Bedrock dengan SDK AWS. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.