Building a Graph PoC using Agents: a case study (part 4 - create queries)

Step by step output of using agents to build a graph proof of concept. Part 4 creates queries.

GenAI isn't just good for writing code. It is also very useful for building data models. This is part 4 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. Part 3 walked us through how the agents generated sample data in the provided schema and populated the Neptune cluster with it. Now part 4 covers how we take the questions from our use case and generates queries from them.
You may recall from part 1 that our use case ended with a list of questions we wanted the graph to answer. Those questions were:
  • What is the optimal walking route that visits 5 historical landmarks within 3
hours while minimizing elevation changes and including a lunch stop?
  • Which lesser-known attractions are located along the path between two popular
tourist destinations and match a visitor's interest in art galleries?
  • How can a multi-day itinerary be optimized to minimize repeated walking
through the same neighborhoods while covering all requested POI categories?
Questions are great, but databases talk in query languages and these don't feel like easy queries to write. This is where the  @generate-queries  prompt we can run in  Kiro CLI  comes in. Running this prompt executes a workflow that iterates over each question in the list, calling the  Query Generation Agent , which performs the following steps:
First it uses the  get_graph_schema  tool in the Neptune MCP server to query the database schema.
Next it uses the run_opencypher_query tool from the Neptune MCP server to experiment with data the queries:
 MATCH (p:POI) WHERE p.poi_type = 'historical_landmark' RETURN p.name, p.average_visit_duration_minutes, p.elevation LIMIT 5  (this doesn't return anything because the poi_type doesn't match)
and
 MATCH (p:POI) RETURN p.name, p.poi_type LIMIT 10 
With some data in hand, it takes its first shot at the first question, Question 1: Optimal walking route with 5 historical landmarks within 3 hours, minimizing elevation changes and including a lunch stop, trying the query:
MATCH (start:POI)
WHERE start.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
WITH start
LIMIT 1
MATCH path = (start)-[:WALKING_PATH*4]->(end:POI)
WHERE ALL(n IN nodes(path)
WHERE n:POI AND
n.poi_type IN ['Landmark', 'Historical Site', 'Cathedral', 'Museum'] OR
n.poi_type = 'Restaurant')
WITH path,
nodes(path) AS pois,
relationships(path) AS walks,
reduce(total = 0,
r IN relationships(path) | total + r.estimated_walking_minutes)
AS total_walk_time,
reduce(total = 0, r IN relationships(path) | total +
abs(r.elevation_change_meters))
AS total_elevation_change,
reduce(total = 0, n IN nodes(path) | total +
n.average_visit_duration_minutes)
AS total_visit_time
WHERE total_walk_time + total_visit_time <= 180 AND
ANY(poi IN pois WHERE poi.poi_type = 'Restaurant' OR poi.poi_type = 'Cafe')
RETURN [poi IN pois | poi.name] AS route, total_walk_time, total_visit_time, total_elevation_change, (total_walk_time + total_visit_time) AS total_time
ORDER BY total_elevation_change ASC, total_time ASC LIMIT 5
Phew, that is a lot of query! Unfortunately, one of the subtleties that the LLM doesn't realize is that there are two flavors of the Cypher query language. There is the proprietary  Neo4J Cypher language , and there is the open source  openCypher query language . Neptune supports the latter, and the openCypher specification does not have the  ALL  or  ANY  functions on lines 6 and 23, respectively. The LLM learns this when it executes the query and gets the response  Error executing tool run_opencypher_query: An error occurred (UnsupportedOperationException) when calling the ExecuteOpenCypherQuery operation: 'all' predicate function is not supported . Then it simplifies the query a bit to the following and has success.
MATCH (start:POI)-[w1:WALKING_PATH]->(poi1:POI)-[w2:WALKING_PATH]->(poi2:POI)
-[w3:WALKING_PATH]->(poi3:POI)-[w4:WALKING_PATH]->(poi4:POI)
-[w5:WALKING_PATH]->(lunch:POI)
WHERE start.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
AND poi1.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
AND poi2.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
AND poi3.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
AND poi4.poi_type IN ['Landmark', 'Historical Site', 'Cathedral']
AND lunch.poi_type IN ['Restaurant', 'Cafe']
WITH start, poi1, poi2, poi3, poi4, lunch,
(w1.estimated_walking_minutes + w2.estimated_walking_minutes + w3.estimated_walking_minutes + w4.estimated_walking_minutes + w5.estimated_walking_minutes) AS total_walk_time,
(start.average_visit_duration_minutes + poi1.average_visit_duration_minutes
+ poi2.average_visit_duration_minutes
+ poi3.average_visit_duration_minutes
+ poi4.average_visit_duration_minutes) AS total_visit_time,
(abs(w1.elevation_change_meters) + abs(w2.elevation_change_meters)
+ abs(w3.elevation_change_meters) + abs(w4.elevation_change_meters)
+ abs(w5.elevation_change_meters)) AS total_elevation
WHERE total_walk_time + total_visit_time <= 180
RETURN [start.name, poi1.name, poi2.name, poi3.name,
poi4.name, lunch.name] AS route,
total_walk_time,
total_visit_time,
total_elevation
ORDER BY total_elevation ASC,
(total_walk_time + total_visit_time) ASC
LIMIT 5
It does the same for the other queries, even making some subtle changes to improve them a bit at times until it is happy with the results and then exits.
We are now close to the end, but we have one more thing to do. As you might imagine, some queries like our first one (or anti-money laundering patterns as another example) detect very nuanced scenarios that are unlikely to appear randomly in data. For these reasons, we have our  @generate-scenario-data  agent. This step will analyze the query to see if it returns any results, and if it does not, then it will create data specific for the scenario. We'll explore it more in  part 5 .
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