Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share data_process_action lambda code #5

Open
pdeshmuk opened this issue Jan 4, 2025 · 1 comment
Open

Share data_process_action lambda code #5

pdeshmuk opened this issue Jan 4, 2025 · 1 comment

Comments

@pdeshmuk
Copy link

pdeshmuk commented Jan 4, 2025

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

@Armiyants
Copy link

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

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants