[Python] I am getting "An error occurred (resourceNotFoundException) when calling the...

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to invoke an agent I built in an agent builder. The agent works fine in the aws console and was trying to make a call to it using a Python code as is below

    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


    but I keep getting this error with the ending part of stack trace below

    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


    The error was even more confusing as the line that says Knowledge Base with id 7M7ACWA9BQ does not exist is strange as I don't have a knowledge base attached while it works fine in aws console(in the bedrock play area where I set the agent up). I had set the agent up earlier with a knowledge base which I removed when the OpenSearch cost was not all that cool for me to retain but not sure it has the said ID either as I removed it long before starting to build out a code to invoke the agent. I have added a knowledge base to the agent when I get the error but it still doesn't solve it.

    What am I doing wrong and how to solve it?

    Continue reading...

Compartilhe esta Página