调用AWS Bedrock Agent时出现resourceNotFoundException:指定ID知识库不存在的问题求助
调用AWS Bedrock Agent时出现resourceNotFoundException:指定ID知识库不存在的问题求助
我最近在尝试用Python代码调用自己在AWS Bedrock Agent Builder里创建的Agent,这个Agent在AWS控制台的测试区域运行完全正常,但用代码调用时一直报错。
我的调用代码如下:
def invoke_agent(prompt: str): """ 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. """ agents_runtime_client = boto3.client('bedrock-agent-runtime', aws_access_key_id=all_values['aws_access_key_id'], aws_secret_access_key=all_values['aws_secret_access_key'], region_name="us-east-1") try: # Note: The execution time depends on the foundation model, complexity of the agent, # and the length of the prompt. In some cases, it can take up to a minute or more to # generate a response. response = agents_runtime_client.invoke_agent( agentId='**********', agentAliasId='**********', sessionId='session_id', inputText=prompt, ) completion = "" for event in response.get("completion"): chunk = event["chunk"] completion = completion + chunk["bytes"].decode() except ClientError as e: logger.error(f"Couldn't invoke agent. {e}") raise e return completion
报错的栈跟踪末尾是:
File "/opt/homebrew/lib/python3.11/site-packages/botocore/eventstream.py", line 619, in _parse_event raise EventStreamError(parsed_response, self._operation_name) botocore.exceptions.EventStreamError: An error occurred (resourceNotFoundException) when calling the InvokeAgent operation: Knowledge Base with id 7M7ACWA9BQ does not exist
这个错误特别让人困惑:我现在的Agent根本没有关联任何知识库(之前关联过一个,但因为OpenSearch成本太高早就删掉了,而且我不确定那个删掉的知识库是不是这个ID),并且在控制台测试时完全正常。后来我尝试给Agent重新关联了一个新的知识库,但报错还是一样,问题没解决。
想请教大家我哪里操作错了?该怎么解决这个问题?
可能的原因和解决方法
我之前遇到过几乎一模一样的问题,大概率是Agent Alias没有同步Agent的最新配置导致的,下面是具体分析和解决步骤:
核心问题:当你最初创建Agent Alias时,它会绑定当时Agent的完整配置(包括关联的知识库信息)。后来你删除了旧知识库并更新了Agent,但Agent Alias并不会自动同步这个变更,依然保留着指向旧知识库ID的缓存配置。而控制台测试默认使用的是Agent的最新版本,这就造成了控制台正常、代码调用报错的差异。
具体解决步骤:
- 登录AWS Bedrock控制台,找到你的目标Agent
- 切换到Aliases标签页,找到代码中使用的
agentAliasId对应的别名 - 点击该别名的Update alias按钮
- 在更新页面,确保选择的是Agent的最新版本(也就是已经移除旧知识库的版本)
- 保存更新后,再用代码重新调用试试
额外检查点:
- 确认代码里的
agentId和agentAliasId完全正确,没有拼写或格式错误 - 如果更新Alias后还是不行,尝试直接创建一个全新的Agent Alias,用新的Alias ID替换代码中的值再测试
- 检查IAM权限:确保调用Bedrock Agent Runtime的角色,拥有访问当前Agent及关联资源(如果有新知识库的话)的完整权限
- 确认代码里的
另外,如果你之后重新关联了新的知识库,也要记得先发布Agent的新版本,再更新Alias指向这个新版本,否则Alias还是会沿用旧配置。
备注:内容来源于stack exchange,提问作者olyjosh
相关产品推荐
相关产品推荐

