Skip to content

basic strands code

0

now everything is correct there is no error done everything now i have a 2files lambda.py and strands_agent.py give me 2 file basic codes what output is give that file give me everything in detail those 2 files can upload my lambda

asked a year ago209 views

2 Answers
0
Accepted Answer

I'll provide you with basic code examples for both files that you can upload to your Lambda function.

strands_agent.py:

from strands import Agent
from typing import Dict, Any

# Define a system prompt for your agent
SYSTEM_PROMPT = """You are a helpful assistant that provides clear and concise answers to user questions."""

class StrandsAgent:
def __init__(self):
# Initialize the agent with the system prompt
self.agent = Agent(system_prompt=SYSTEM_PROMPT)

def process_message(self, message: str) -> str:
# Process the input message and return the agent's response
response = self.agent.process(message)
return response.text

lambda.py:

from strands_agent import StrandsAgent
from typing import Dict, Any

# Initialize the agent once outside the handler for better performance
agent = StrandsAgent()

def handler(event: Dict[str, Any], context):
# Extract the input text from the event
input_text = event.get("text", "")

if not input_text:
return {
"statusCode": 400,
"body": "No input text provided"
}

# Process the input text with the Strands agent
response_text = agent.process_message(input_text)

# Return the response
return {
"statusCode": 200,
"body": response_text
}

To deploy these files to Lambda:

  1. Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate
pip install strands
  1. Create a package directory and copy files:
mkdir package
cd package
cp -r ../venv/lib/python*/site-packages/* .
cp ../lambda.py .
cp ../strands_agent.py .
  1. Create a zip file:
zip -r strands_agent_lambda.zip .
  1. Upload the zip file to your Lambda function.

  2. Set the handler to: lambda.handler

When you invoke this Lambda function with an event like:

{
"text": "What is AWS Lambda?"
}

The Lambda will:

  1. Extract the "text" field from the event
  2. Pass it to the Strands agent for processing
  3. Return a response with the agent's answer about AWS Lambda

The output will be a JSON object with:

  • statusCode: 200 for success, 400 if no input text is provided
  • body: The text response from the Strands agent

You can customize the system prompt in strands_agent.py to make your agent more specialized for specific tasks or domains.
Sources
connect the strands agent to lambda | AWS re:Post
lambda connect to strands | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

0

what is the output above code?

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.