上一节我们在控制台访问了knowledge base,本节我们使用python来访问。
首先找到它的ID:
以及用于generate回答的model ARN:
aws bedrock list-foundation-models --query modelSummaries[].modelArn
# 使用 arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0
和上一节一样,在python中调用retrieve_and_generate
函数:
import boto3
import json
AWS_REGION_BEDROCK = "us-west-2"
client = boto3.client(
service_name="bedrock-agent-runtime", region_name=AWS_REGION_BEDROCK
)
question = "GDPR是什么?"
response = client.retrieve_and_generate(
input={"text": question},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": "HOJ21MHDDF",
"modelArn": "arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0",
},
},
)
print(response)
answer = response.get("output").get("text")
print(answer)
输出结果: