Building a Graph PoC using Agents: a case study (part 3 - create data)

Step by step output of using agents to build a graph proof of concept. Part 3 creates sample data.

GenAI isn't just good for writing code. It is also very useful for building data models. This is part 3 of a series covering building a graph proof of concept using agentic AI. It is recommended to start at  part 1 .
In part 1, we created our environment and ran our first agent to take our theme of  Create trip plans for walking tours of cities  and generated a full use case with questions we want the graph to answer. In part 2 we created a data model to answer the questions in the use case. Now in part 3, we will generate sample data in the provided schema to populate the Neptune cluster.
To accomplish this, we run the prompt  @generate-data  in  Kiro CLI  . This launches a fairly complex workflow that does the following:
  1. Create a set of data creation jobs, one for each distinct node label and one for each edge label.
  1. Create a set of worker threads (we find 4 works well assuming standard Bedrock quotas) that will execute these jobs one by one in parallel.
  1. Next it executes the node creation jobs followed by executing the edge creation jobs (edges must be explicitly attached to nodes, so they must exist first)
So what does this look like? First, it can take a significant amount of time. If you have short lived credentials, you may want to refresh them at this point. Second, it will use a lot of tokens, especially the edge generation as we'll see. Third, because we are generating each type of node and edge in isolation, the data may not tell a continuous story (though Part 5 will address this). So let's look at what happens behind the scene generating the POI nodes (Points of Interest).

Node Generation

First, the  Sample Data Generator Agent  will use the Neptune MCP server to determine if any POI nodes alredy exist, running the query  MATCH (n:POI) RETURN count(n) as count .
Next the LLM starts creating batches of MERGE queries to generate the data like this:
WITH {
name: 'The Golden Gate Overlook',
poi_type: 'Viewpoint',
latitude: 37.8199,
longitude: -122.4783,
elevation: 67.5,
popularity_score: 9.2,
opening_time: '00:00',
closing_time: '23:59',
is_must_see: true,
average_visit_duration_minutes: 30,
accessibility_rating: 8,
description: 'Stunning panoramic views of the Golden Gate Bridge and San
Francisco Bay'} as properties
MERGE (n:POI {`~id`: 'POI_501'})
SET n += properties
WITH {
name: 'Fishermans Wharf Pier 39',
poi_type: 'Tourist Attraction',
latitude: 37.8087,
longitude: -122.4098,
elevation: 3.2,
popularity_score: 8.9,
opening_time: '10:00',
closing_time: '21:00',
is_must_see: true,
average_visit_duration_minutes: 90,
accessibility_rating: 10,
description: 'Famous waterfront marketplace with shops, restaurants, and sea
lions'} as properties
MERGE (n:POI {`~id`: 'POI_502'})
SET n += properties
...
However when it executes the query it gets the exception from Neptune  Error executing tool run_opencypher_query: An error occurred (AccessDeniedException) when calling the ExecuteOpenCypherQuery operation: User: arn:aws:sts::{redacted}:assumed-role/{redacted}/{redacted} is not authorized to perform: neptune-db:DeleteDataViaQuery on resource: arn:aws:neptune-db:us-east-1:{redacted}:cluster-{redacted}/* . You probably remember we gave our agent least privilege and the ability to delete was not needed. Fortunately the LLM realizes this and decides  "Let me try using CREATE instead of MERGE:" . It fails again because you can see the query above is reusing the variable n, so it makes another modification to each record format to:
CREATE (:POI
{ `~id`: 'POI_501',
name: 'The Golden Gate Overlook',
poi_type: 'Viewpoint',
latitude: 37.8199,
longitude: -122.4783,
elevation: 67.5,
popularity_score: 9.2,
opening_time: '00:00',
closing_time: '23:59',
is_must_see: true,
average_visit_duration_minutes: 30,
accessibility_rating: 8,
description: 'Stunning panoramic views of the Golden Gate Bridge and
San Francisco Bay'})
CREATE ...
And this time it succeeds. So it generates dozens more batches of data until it thinks it has enough, and then it executes another query using the MCP server to verify the number of nodes it created via  MATCH (n:POI) RETURN count(n) as total_pois , which is sees is 500. Satisfied, it writes a summary of what it did and then exits its task. This agent ran for about 5 minutes and consumed about 350,000 tokens to complete this task.

Edge Generation

After all of the nodes are generated, it moves onto the edge generation. Let's look at what the agent generating WALKING_PATH edges did step by step.
First, it examines the schema and existing data. It calls the  get_graph_schema  tool from the MCP server (this is why we have it permission to  neptune-db:GetGraphSummary  in part 1) and then writes some queries to explore the data, specifically  MATCH (p:POI) RETURN COUNT(p) as poi_count  and  MATCH ()-[w:WALKING_PATH]->() RETURN COUNT(w) as existing_walking_paths  (maybe we want to add some agentic memory or agent to agent calls in the future).
Now armed with the data it needs, it knows edges need to explicitly state their starting and ending nodes, so it is going to query some IDs it can use via the MCP server and query  MATCH (p:POI) RETURN p.~id as id, p.name as name, p.latitude as lat, p.longitude as lon, p.elevation as elevation LIMIT 100 .
It runs the same query again with  SKIP 100 LIMIT 100  and then again with  SKIP 200 LIMIT 150  to get a bigger pool to pull from.
Now with enough data, it starts to create MERGE queries again...another sign we might want to have memory or better prompting to tell it to use CREATE. It also realizes that using SET also seems to trigger a DELETE action, so it tries
MATCH (from {`~id`:'POI_301'}),
(to {`~id`:'POI_302'})
CREATE (from)-[:WALKING_PATH {`~id`:'WALKING_PATH_001', distance_meters: 450, estimated_walking_minutes: 6, elevation_change_meters: 3,
difficulty_level: 'easy', accessibility_score: 9, has_shade: true,
scenic_rating: 8}]->(to)
MATCH (from {`~id`:'POI_302'}),
(to {`~id`:'POI_303'})
CREATE (from)-[:WALKING_PATH {`~id`:'WALKING_PATH_002', distance_meters: 620, estimated_walking_minutes: 8, elevation_change_meters: 1, difficulty_level:
'easy', accessibility_score: 9, has_shade: false, scenic_rating: 7}]->(to)
...
Alas it wasn't meant to be, as it gets the response  Error executing tool run_opencypher_query: An error occurred (MalformedQueryException) when calling the ExecuteOpenCypherQuery operation: WITH is required between CREATE and MATCH (line 3, column 1 (offset: 284)) . Not ready to give up though, it realizes  I need to use UNWIND instead. Let me restructure the query: .
UNWIND [
{from: 'POI_301', to: 'POI_302', id: 'WALKING_PATH_1001',
distance_meters: 450, estimated_walking_minutes: 6,
elevation_change_meters: 3, difficulty_level: 'easy',
accessibility_score: 9, has_shade: true, scenic_rating: 8},
{from: 'POI_302', to: 'POI_303', id: 'WALKING_PATH_1002', ...},
...
] AS path
MATCH (fromNode {`~id`: path.from}),
(toNode {`~id`: path.to})
CREATE (fromNode)-[:WALKING_PATH {`~id`: path.id,
distance_meters: path.distance_meters,
estimated_walking_minutes: path.estimated_walking_minutes,
elevation_change_meters: path.elevation_change_meters,
difficulty_level: path.difficulty_level,
accessibility_score: path.accessibility_score,
has_shade: path.has_shade,
scenic_rating: path.scenic_rating}]->(toNode)
It completes a number of cycles, each batch adding walking paths for a single city (San Francisco, Sydney, Tokyo, Amsterdam, etc.) until it has created 900 walking path edges total. Then it decides it is done and the agent exits the task. About 10 minutes and 3 million tokens later, we have our walking paths complete.
With our data in place, we will now work towards converting our questions into queries using the  @generate-queries  prompt in  part 4 .
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article