aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.97k stars 556 forks source link

client-bedrock-agent: enableTrace: true returns no answer #6001

Closed mpoferl closed 2 months ago

mpoferl commented 2 months ago

Checkboxes for prior research

Describe the bug

I'm using @aws-sdk/client-bedrock-agent-runtime in my lambda function to send prompts to my bedrock agent. For my use case, I need to set enableTrace: true because I have a knowledge base connected. Unfortunately, I don't get a response from my agent after doing this.

For example, in the knowledge base I have a document which contains "Mona was at the zoo in 2005.". When I ask the agent in the AWS console, it returns "Mona was in the zoo in 2005.". But when I invoke my lambda I receive "Sorry, I don't have enough information to answer that."

SDK version number

@aws-sdk/client-bedrock-agent-runtime@3.554.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

20

Reproduction Steps

import { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import {
  InvokeAgentCommand,
  InvokeAgentCommandInput,
} from '@aws-sdk/client-bedrock-agent-runtime';
import {BedrockAgentRuntime} from '@aws-sdk/client-bedrock-agent-runtime';
import {v4 as uuidv4} from 'uuid';
import middy from "@middy/core";

const bedrockAgentRuntime = new BedrockAgentRuntime({});

const handler = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
  const {inputText} = JSON.parse(event.body ?? '')

  const sessionId = uuidv4();
  const invokeAgentInput: InvokeAgentCommandInput = {
    agentId: 'XXXXX',
    agentAliasId: 'XXXXX',
    inputText,
    sessionId,
    enableTrace: true,
  };

  try {
    const invokeResponse = await invokeAgent(invokeAgentInput);
    console.log('invokeResponse:', JSON.stringify(invokeResponse));

    return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'InvokeAgentCommand: command has been sent.',
        commandResponse: invokeResponse,
        sessionId
      }),
      headers: {'Content-Type': 'application/json'},
    }

  } catch (e) {
    return {
      statusCode: 500,
      body: JSON.stringify({
        message: 'InvokeAgentCommand: an error occurred.',
        error: e.message,
      }),
      headers: {'Content-Type': 'application/json'},
    }
  }
}

async function invokeAgent(input: InvokeAgentCommandInput) {
  return new Promise(async (resolve, reject) => {
    const command = new InvokeAgentCommand(input);
    const response = await bedrockAgentRuntime.send(command);

    if (response && response.completion) {
      let completion = "";
      for await (const chunkEvent of response.completion) {
        if (chunkEvent.chunk) {
          const chunk = chunkEvent.chunk;
          let decoded = new TextDecoder("utf-8").decode(chunk.bytes);
          completion += decoded;
        }
      }
      resolve({ ...response, completion });
    }

    reject(new Error("No completion response"));
  });
}

export const lambdaHandler = middy(handler)

Observed Behavior

{
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "28902d58-eab5-400d-8d8c-3a20e0306c36",
        "attempts": 1,
        "totalRetryDelay": 0
    },
    "contentType": "application/json",
    "sessionId": "94e834c8-bf23-4be0-ac74-ebdefa8a5a8d",
    "completion": "Sorry, I don't have enough information to answer that."
}

Expected Behavior

{
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "XXXXXX",
        "attempts": 1,
        "totalRetryDelay": 0
    },
    "contentType": "application/json",
    "sessionId": "XXXXX",
    "completion": "Mona was in the zoo in 2005."
}

Possible Solution

No response

Additional Information/Context

No response

RanVaknin commented 2 months ago

Hi @mpoferl ,

You mentioned you are using @aws-sdk/client-bedrock-agent-runtime@3.554.0 but also running your code from lambda. Are you providing your own SDK to lambda? Or using the Lambda provided SDK?

Im asking because Lambda does not provide the latest version of the SDK. Lambda updates their SDK version roughly twice a year. Since Bedrock added the enableTrace feature a few months ago, my first instinct is that your Lambda is not using the most up to date version containing this feature.

You can check which SDK version your Lambda provides by doing the following.

Thanks, Ran~

mpoferl commented 2 months ago

Hey @RanVaknin,

I followed your advice and the log points out the version to 3.515.0.

Could it be related to CodeCatalyst? I created my repo with a blueprint. In the logs are some warnings related to the node version: CDK CLI version parameter found. Installed CDK CLI v2.137.0 Node dependencies file exists: ./package.json npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@datastream/core@0.0.35', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.2', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@middy/core@5.3.2', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.2', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'aws-sdk-mock@5.9.0', npm WARN EBADENGINE required: { node: '>=18.0.0' }, npm WARN EBADENGINE current: { node: 'v16.20.2', npm: '8.19.4' } npm WARN EBADENGINE }

workflow.yml `Name: onPushToMainDeploymentWorkflow SchemaVersion: "1.0" Triggers:

RanVaknin commented 2 months ago

Hi @mpoferl ,

I'm not sure how you are creating your lambda function. I'm not too familiar with CodeCatalyst. Whichever way you create your lambda function, you need to ensure you provide your own SDK version by bundling and uploading to lambda.

Regarding the warning, I'm not entirely sure why you are seeing this. This is likely unrelated but this warning might be showing because Node 16 is officially deprecated, and Lambda will also be deprecating that runtime starting June of 2024. I suggest that when you create your Lambda, you choose Node 20.

Let me know if this helps. Thanks Ran~

mpoferl commented 2 months ago

I create the lambda function in the cdk: const bedrockAgentHandler = new nodejsfunction.NodejsFunction(this, 'BedrockAgentFunction', { runtime: Runtime.NODEJS_18_X, entry: path.join(__dirname, '../lambda/bedrock-agent-handler.handler.ts'), handler: 'lambdaHandler', memorySize: 256, timeout: Duration.seconds(90) });

mpoferl commented 2 months ago

Okay, it had nothing to do with my lambda or cdk. I deleted my previous agent alias and created a new one and now it is working.

github-actions[bot] commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.