AWS Public Sector Blog

Battling the food security crisis with Agents for Amazon Bedrock

AWS branded background design with text overlay that says "Battling the food security crisis with Agents for Amazon Bedrock"

The 2024 version of the United Nations (UN) annual report The State of Food Security and Nutrition in the World found that about 29.6 percent of the global population, or about 2.4 billion people, were moderately or severely food insecure in 2022, meaning they did not have adequate access to food. Food security can be caused by a number of factors, including poverty, inflationary factors, violent conflict, and the effects of climate change. In this post, I demonstrate how generative artificial intelligence (AI) can help organizations better understand the food security crisis.

The Food and Agriculture Organization of the United Nations (FAO) has made available a Data in Emergencies (DIEM) system. Data is regularly collected and made available to help users understand the impact of shocks in food crisis areas. This data is collected in a number of areas, including crop and livestock production and food security. Decision-makers active in crisis-affected geographic locations can use this data to respond to the crisis. You can learn more about the data that is available and gain access to it through the DIEM data access portal.

For this solution, I want to be able to use generative AI from Amazon Web Services (AWS) to better understand the food security crisis. I will demonstrate how a generative AI model can reason about food security data and provide results that are relevant and meaningful.

Solution overview

Agents for Amazon Bedrock is a feature of Amazon Bedrock that allows a large language model (LLM) to use your data to answer questions from users. It understands a user’s request and breaks the task down into smaller tasks that the agent can perform. It can collect additional information from a user through natural language conversation, and it can take action to fulfill the user’s request by making API calls.

The agent understands what API calls are available, what those APIs do, the data that each API is expecting to receive, and the output of each API call. The agent understands your APIs by examining an API specification definition you provide that meets the OpenAPI specification. An OpenAPI schema defines a set of HTTP APIs that the agent can call. The schema defines the input, output, and description of each API method. Using these definitions, the agent can reason about which APIs to call and in which order to best fulfill the user’s request.

For this solution, I have created an OpenAPI specification for a set of APIs that retrieve food security information from the UN’s DIEM dataset. When the user asks a question about food security, the agent will reason how to best answer the question, given the APIs it has access to.

The solution works as follows:

  1. After installing the solution using an AWS CloudFormation template, an Amazon Elastic Compute Cloud (Amazon EC2) Image Builder task builds a container that is used to retrieve the UN DIEM data. The container is stored in Amazon Elastic Container Registry (Amazon ECR).
  2. A scheduler periodically launches the AWS Lambda data processor. This function pulls authentication credentials for the UN DIEM data from AWS Secrets Manager, pulls the UN DIEM down using API calls, performs some deduplication, and saves the results in Amazon Simple Storage Service (Amazon S3).
  3. When a decision-maker wants to better understand the food security crisis, they invoke an agent, which connects to a Lambda function that performs data querying functions. The Lambda function is configured to respond to three different API calls: /countries, which returns a list of countries and their ISO3 codes; /countries/food-insecurity, which returns a list of all countries and their food security status; and /countries/detail, which returns food security data for a specific country indicated by the three-character ISO3 code. The agent reasons which API to call and sends a request to the Lambda function for fulfillment of each API.
  4. The Lambda function reads in the processed DIEM data from Amazon S3 to return data for each API call.

Figure 1 shows the solution architecture.

Data is pulled from the UN DIEM data, processed, and saved in Amazon S3. The Amazon Bedrock agent then uses a Lambda function to perform queries against the data.

Figure 1. The solution architecture, which allows Agents for Amazon Bedrock to query data about global food security.

The agent can use the following OpenAPI schema to understand what functions it can perform:

openapi: 3.0.0
info:
    title: Food insecurity API
    version: 1.0.0
    description: APIs for evaluating food insecurity across the world
paths:
    /countries:
      get:
        summary: Returns the list of countries and their ISO3 code
        description: Returns the list of countries and their ISO 3-character code
        operationId: getCountryList
        responses:
          "200":
            description: The list of countries in the database
            content:
              application/json:
                schema:
                  type: array
                  items:
                    type: object
                    properties:
                      country_name:
                        type: string 
                        description: The name of the country
                      ISO3:
                        type: string
                        description: The 3-character ISO code to uniquely identify the country
    
    /countries/food-insecurity:
      get:
        summary: Returns a list of all countries and their food security status. 
        description: Returns a list containing the country name and a FIES raw score, which identifies the food insecurity status. A score less than 4 indicates mild food insecurity, meaning the participants worry about food and the household may reduce other essential expenses to pay for food. A value between 4 and 7 indicates moderate food insecurity, which means the household is compromising food quality, variety, and quantity. The household may also suffer from malnutrition (meaning obesity or micronutrient deficiencies) or under nutrition (meaning stunting or wasting). Finally, a score of 7 or above indicates severe food insecurity. The household is experiencing hunger and a severe lack of undernutrition due to a lack of inadequate intake of essential nutrients. This may lead to starvation and death. FIES stands for Food Insecurity Experience Scale.
        operationId: getCountrySecurity
        responses:
          "200":
            description: The list of countries and food insecurity score from the food insecurity database
            content:
              application/json:
                schema:
                  type: array
                  items:
                    type: object
                    properties:
                      country_name:
                        type: string
                        description: The name of the country
                      FIES_score:
                        type: number
                        description: The FIES raw score for food insecurity. A value between 0-4 indicates mild food insecurity, a value between 4-7 indicates moderate food insecurity, and a value of 7 or above indicates severe food insecurity. FIES stands for Food Insecurity Experience Scale.
    /countries/detail/:
      get:
        summary: Returns food insecurity data about the specific country indicated by the 3-character ISO code
        description: Returns specific food insecurity indicators about the specific country identified in the ISO3 code
        operationId: getCountryDetails
        parameters:
          - name: ISO3
            in: path
            description: The 3-character ISO code to uniquely identify the country
            required: true
            schema:
              type: string
        responses:
          "200":
            description: Food insecurity statistics for a specific country
            content:
              application/json:
                schema:
                  type: object
                  properties:
                    FIES_hungry_percent: 
                      type: number
                      description: Percentage of households that went to sleep hungry at least once in the past 30 days because there was not enough food
                    FIES_ranout_percent:
                      type: number
                      description: Percentage of households that ran out of food at least once in the past 30 days because there were not sufficient resources to get food
                    FIES_skipped_percent:
                      type: number
                      description: Percentage of households that had to skip a meal at least once in the past 30 days because of a lack of money or other resources to get food
                    FIES_wholeday_percent:
                      type: number
                      description: Percentage of households within the past 30 days that had to go a whole day and night without eating anything at all because there was not enough food
                    FIES_score:
                      type: number
                      description: The FIES raw score for food insecurity. A value between 0-4 indicates mild food insecurity, a value between 4-7 indicates moderate food insecurity, and a value of 7 or above indicates severe food insecurity. FIES stands for Food Insecurity Experience Scale.

Prerequisites

To use the solution, you must first gain access to the UN’s DIEM system. To create an account, select Create a DIEM account. Once you’ve been granted access, you will receive a notification that your account has been authorized to access the DIEM API. You need your DIEM credentials for this solution.

Download DIEM_2023_-_Fields_descriptions for the field descriptions of the DIEM data.

Deploying the solution

To build the infrastructure that supports this solution, you can use an AWS CloudFormation template, which deploys all the necessary components and the appropriate AWS Identity and Access Management (IAM) permissions.

1. Deploy this solution in your environment, or select Launch Stack. This solution will deploy in the AWS US East (N. Virginia) us-east-1 Region.

launch stack button

2. After selecting Launch Stack , the Create stack page from the AWS CloudFormation dashboard appears. At the bottom of the page, select Next.

3. Continue to select Next until you arrive at the Review

4. Check the box that says I acknowledge that AWS CloudFormation might create IAM resources and select Submit. The installation begins, which usually takes about 5 minutes to be completed.

As part of the installation process, the solution deployed a placeholder for your DIEM authentication credentials in Secrets Manager. You must update Secrets Manager with your DIEM credentials so the solution can retrieve DIEM data into your AWS account.

  1. Go to Secrets Manager and select the secret called StackName-secrets, where StackName is the name of the CloudFormation stack that was just deployed. My secret is called food-agent-secrets.
  2. To change the username and password to the username and password from your DIEM data access credentials, select Edit.
  3. Select Save.

Figure 2 shows the Secrets Manager Secrets page with a username and a password under Secret key.

The Secrets Manager page shows a secret value with a username and a password. An arrow shows the Edit button which must be clicked to update the values.

Figure 2. Modifying the food-agent-secrets with DIEM data access credentials.

Once the secret is changed, the Lambda function that ingests the DIEM data will automatically run. Wait about 5 minutes before proceeding to the next step. You can also look in your S3 buckets for a bucket name that ends in -diem. If you see a file called data.csv in the bucket, the data has been successfully processed, and you can begin using the solution.

Using the solution

For this demonstration, I use the test functionality available within the Amazon Bedrock interface for Agents. For a production system, connect the agent with a user interface.

To access the test interface, on the Amazon Bedrock console, in the left navigation under Builder tools, select Agents. On the screen that appears, under Name, select the agent. In my case, the agent is called food-agent_Agent.

Ask the agent questions on the right-hand panel, under Test, in the message field, as shown in Figure 3.

The Bedrock Agent console shows details of the agent, including the name, creation date, and versions. An arrow points to a box where you can type questions that are sent to the agent.

Figure 3. The Amazon Bedrock Agents console, which allows testing of the agent.

Through the test interface, I can ask the agent a question like “What countries have the highest level of food insecurity?”

The agent responds with the results in Figure 4.

The user asks the question: “What countries have the highest level of food insecurity?” The Agent responds with: “Based on the food insecurity scores (FIES scores) from the data, the countries with the highest levels of food insecurity are: 1. Central African Republic (FIES score: 7.68) 2. Somalia (FIES score: 7.54) 3. Afghanistan (FIES score: 6.70) 4. Haiti (FIES score: 6.36) 5. Mozambique (FIES score: 5.86) A FIES score above 7 indicates severe food insecurity, where households are experiencing hunger and severe lack of nutrition that can lead to starvation and death. The Central African Republic and Somalia have the highest scores above 7, indicating extremely severe levels of food insecurity in those countries.”

Figure 4. Example output from an agent that has access to food security data.

To answer this question, the agent follows these steps:

  1. It calls /countries/food-insecurity to get a list of countries and their food security score.
  2. The agent identifies that the countries with the highest FIES scores (a score that identifies the amount of food insecurity present) are the Central African Republic, Somalia, and Afghanistan. It decides to call /countries/detail for each of these countries to get more detail on what these scores mean for each of these countries.
  3. The agent calls the /countries/detail API for the Central African Republic, Somalia, and Afghanistan.
  4. The agent summarizes and returns the results.

As another example, I can ask, “How many people around the world missed a meal due to lack of food?”

One of the values returned from the /countries/detail API is called FIES_skipped_percent, which represents the percentage of people who missed a meal in each country. The agent reasons that to answer the question, it must take this value and multiply it by the total population of each country to get an estimated number of people who skipped meals. The agent performs the reasoning and returns this result in Figure 5.

The user asks the question: “How many people around the world missed a meal due to lack of food?” and the agent responds with: “Based on the data available, here are some key estimates of the number of people around the world who missed meals due to lack of food: Afghanistan: 28.9 million Bangladesh: 25.6 million Burkina Faso: 5.3 million To get the total global number, I would need to calculate and sum the values for each country by multiplying the "FIES_skipped_percent" by the total population. This involves repeating this process for all countries in the database. While I cannot provide the precise global total here, the data shows that tens of millions of people, particularly in countries with high food insecurity, are regularly skipping meals and going hungry due to lack of access to adequate food. This highlights the urgent need to address global food insecurity through humanitarian aid, sustainable agriculture, economic development and other interventions.”

Figure 5. An agent responds to a more complex question about food security.

Clean up

To avoid ongoing charges in your AWS account, remove the resources created for this solution. Use the CloudFormation console to delete the stack created as part of this demo. Select the stack, select Delete, and then select Delete stack.

Conclusion

In this post, I demonstrated how you can use Agents for Amazon Bedrock to better understand your organization’s data. I demonstrated that by using data from the UN’s Food and Agriculture Organization, I was able to better understand the food security crisis. The agent performs reasoning about the question the user is asking, breaks the problem down into smaller tasks that it can complete using the APIs it has access to, and returns meaningful results that answer the question.

While the example here is based on food security, it can be extended to other use cases by defining a set of APIs and writing code in Lambda to perform the processing. By following this pattern, you can extend Agents for Amazon Bedrock to solve problems from better understanding your donors to querying external data sources to automating business processes.

Get started today by creating an agent in Amazon Bedrock so you can use generative AI to begin solving more complicated, multi-step tasks.