Skip to content

Latest commit

 

History

History
208 lines (144 loc) · 11.1 KB

File metadata and controls

208 lines (144 loc) · 11.1 KB

Invoking agents present in AWS and also creation of Agents using python.

Prerequisites

  1. For generating AWS Access keys follow this->https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html
  2. For generating Google API keys follow this->README
  3. For generating Airtable API keys follow this->README

First we create the Lambda Function in AWS

  1. Login to AWS platform(https://aws.amazon.com/console/)

  2. Firstly we will created a Lambda function that our agent will use to generate the output. Search Lambda in the AWS search bar. image Click on the create function image

  3. Create the name of the lambda function, then select Python 3.11 as the runtime. Click on create function

  4. Copy the code from the lambda_function.py here: image

  5. Next step Go to the Lambda->Configuration->Permission Click Add Permissions image image image Specify a statement id.(It can be anything that you want it to be.) Specify the principle as bedrock.amazonaws.com Select lambda:InvokeFunction from the Action Dropdown. Click on Save image

    Go to Configuration->General Configuration. Click on Edit. image image

    Change the Timeout value to 1 min and hit save.

  6. Go to Configurations->Environment Variables

    image

    Click on Edit to add environment variables. Here you can store your API keys that you generated for Airtable and Google, for the lambda function to access.

    Save the Airtable API as AIRTABLE_API_KEY AND Google API key as GOOGLE_API
    image

  7. Click on Deploy.

Creating an Agent

  1. Now we need to upload the OpenAPI spec in the S3 bucket. Search for S3 in the search bar. image Click on the Create bucket option.

  2. Give the name for the bucket. Give the Object Owner as ACL enabled, with Bucket owner preferred. image Click on the Create Bucket option.

  3. Now go back to the Bucket list page and click on the bucket that you just created image Click the upload option to upload the Open API spec. Upload the lead_generation.json file here. image

  4. Now we create the agent. Search AWS bedrock and select the agents option. image

  5. Click on the create agents option image Specify the Agent name and the description (Note: you can find the descriptions in the agents_description.txt file in this directory.) You can create the IAM permission automatically for new user, or use an existing IAM permission if available. Click Next.

  6. Select model from the drop down and give the descriptions Description for claude: You are a solar panel financial advisor, your job is to extract address and monthly bills for calling functions to calculate solar potential. You also are a solar panel lead generation employee, your job is to extract personal details like name, phone number and address for returning as response for calling functions to generate leads. Strictly respond to the question for calling the function that is required to answer. While returning the summarized solar potential calculation, the assistant should add a statement in the end as "Please provide you name and phone number for contacting you". If the user provides the name, address and phone number, extract them for creating leads.

    We have selected Claude v2:1. Click Next image

  7. Create an action group. image Give the action group name. Select the lambda function created earlier from the drop down menu. image Select the file from the Browse option for S3. Click on the bucket then the Open API .json file inside it. image image Click on Choose then click on next.

  8. Knowledge base is optional, it uses Open Search service of AWS and might charge for the uses, or you can just skip it.

  9. Create Agent. image

Our Agent is now fully setup and can be used with functionalities such as creating data entries and generating solar potential through google apis.

Now you are ready to use the agents.

Invoking agents present in AWS

Once an agent is created in the AWS Bedrock console, it can be accessed through python using the agent ID and agent alias ID.

Here is an example of an agent already created and existing in AWS Bedrock.

image In this case lead_gen is the agent that is already created and existing in AWS Bedrock. We need its agentId and aliasId. image image Now we can invoke this agent as such:

image image

The response contains completion message as event byte chunks. (This is the message from the agents) image

Creating agents and Invoking them

Now, creating a new agent using python. You must retrieve the agentResourceRoleArn from the AWS platform:

  1. Go to AWS console and search for 'IAM'-> Go to roles in the Side panel image

  2. Your IAM role will be present here, with a prefix 'AmazonBedrockExecutionRoleForAgents_', Click on the role and copy its ARN. image

Now paste the ARN in the code while creating an agent.

aws_agent = bedrock_agent.create_agent(
    agentName='Lead_gen_notebook',
    description='Extract name, phone number and address for lead generation. Or extract address and monthly bill for calculating solar potential',
    foundationModel='anthropic.claude-v2',
    instruction="""You are a solar panel financial advisor, your job is to extract address and monthly bills for calling functions to calculate solar potential. You also are a solar panel lead generation employee, your job is to extract personal details like name, phone number and address for returning as response for calling functions to generate leads. Strictly respond to the question for calling the function that is required to answer.""",
    agentResourceRoleArn='arn:aws:iam::674564987265:role/service-role/AmazonBedrockExecutionRoleForAgents_KT4F6CHPJV'
)

This will create a new agent in our AWS bedrock as Lead_gen_notebook image

Then we give this agent an action group that stores our S3 bucket and Lambda Function informations

#Creating the action group for the agent
response = bedrock_agent.create_agent_action_group(
    agentId=aws_agent['agent'].get('agentId'),
    agentVersion="DRAFT",
    actionGroupName='LeadGenAction',
    actionGroupExecutor={
        'lambda': 'arn:aws:lambda:us-east-1:674564987265:function:lead_gen_solar'
    },
    apiSchema={
        's3': {
            's3BucketName': 'legalgraphbucket',
            's3ObjectKey': 'lead_generation.json'
        }
    },
    actionGroupState='ENABLED'
)

image

After this we give an alias to the agent while passing the agent id

# Creating the alias
agent_alias = bedrock_agent.create_agent_alias(
    agentId=aws_agent['agent'].get('agentId'),
    agentAliasName='Dummy',
)

image

Now the process of Agent creation with minimal configuration is done and ready to be invoked as done earlier.

Testing the Agent

At the bottom right of the agent screen you will find a place to enter your message for the agent image

Example Question:

  1. Calculate solar potential for 9813 Sherborne Ave, Bakersfield, CA 93311, USA with monthly bill as 300 USD
  2. Create a Lead for Jhon Doe, California, 336-2126-7543

Solar Potential Calculation and Creating a lead.

image

Entry in Airtable

image