gen-ai-workshop icon indicating copy to clipboard operation
gen-ai-workshop copied to clipboard

Share data_process_action lambda code

Open pdeshmuk opened this issue 10 months ago • 1 comments

The data_process_action lambda is used in the "Building Agents for Amazon Bedrock" module. The lambda code is not shared in the GitHub project

pdeshmuk avatar Jan 04 '25 01:01 pdeshmuk

hey @pdeshmuk , you can find it in the console and maybe create a PR to add it to the code base if you find it needed. final full and correct code after troubleshooting (check environmental variables too):

`import os import json import pandas import boto3

S3_BUCKET = os.environ["S3_BUCKET"] S3_OBJECT = os.environ["S3_OBJECT"]

def lambda_handler(event, context): # Print the received event to the logs print("Received event: ") print(event)

# Initialize response code to None
response_code = None

# Extract the action group, api path, and parameters from the prediction
action = event["actionGroup"]
api_path = event["apiPath"]
inputText = event["inputText"]
httpMethod = event["httpMethod"]

print(f"inputText: {inputText}")

# Check the api path to determine which tool function to call
if api_path == "/get_num_records":
    s3 = boto3.client("s3")
    s3.download_file(S3_BUCKET, S3_OBJECT, "/tmp/data.csv")
    df = pandas.read_csv("/tmp/data.csv")

    # Get count of dataframe
    count = len(df)

    response_body = {"application/json": {"body": str(count)}}
    response_code = 200
else:
    # If the api path is not recognized, return an error message
    body = {"{}::{} is not a valid api, try another one.".format(action, api_path)}
    response_code = 400
    response_body = {"application/json": {"body": str(body)}}

# Print the response body to the logs
print(f"Response body: {response_body}")

# Create a dictionary containing the response details
action_response = {
    "actionGroup": action,
    "apiPath": api_path,
    "httpMethod": httpMethod,
    "httpStatusCode": response_code,
    "responseBody": response_body,
}

# Return the list of responses as a dictionary
api_response = {"messageVersion": "1.0", "response": action_response}

return api_response

`

Armiyants avatar Feb 23 '25 04:02 Armiyants