Apache AGE is a PostgreSQL extension that provides graph database functionality. AGE is an acronym for A Graph Extension, and is inspired by Bitnine’s fork of PostgreSQL 10, AgensGraph, which is a multi-model database. The goal of the project is to create single storage that can handle both relational and graph model data so that users can use standard ANSI SQL along with openCypher, the Graph query language. The data elements Apache AGE
stores are nodes, edges connecting them, and attributes of nodes and edges.
This notebook shows how to use LLMs to provide a natural language interface to a graph database you can query with the Cypher
query language.
Cypher is a declarative graph query language that allows for expressive and efficient data querying in a property graph.
Setting up
You will need to have a runningPostgre
instance with the AGE extension installed. One option for testing is to run a docker container using the official AGE docker image.
You can run a local docker container by running the executing the following script:
Seeding the database
Assuming your database is empty, you can populate it using Cypher query language. The following Cypher statement is idempotent, which means the database information will be the same if you run it one or multiple times.Refresh graph schema information
If the schema of database changes, you can refresh the schema information needed to generate Cypher statements.Querying the graph
We can now use the graph cypher QA chain to ask question of the graphLimit the number of results
You can limit the number of results from the Cypher QA Chain using thetop_k
parameter.
The default is 10.
Return intermediate results
You can return intermediate steps from the Cypher QA Chain using thereturn_intermediate_steps
parameter
Return direct results
You can return direct results from the Cypher QA Chain using thereturn_direct
parameter
Add examples in the Cypher generation prompt
You can define the Cypher statement you want the LLM to generate for particular questionsUse separate LLMs for Cypher and answer generation
You can use thecypher_llm
and qa_llm
parameters to define different llms
Ignore specified node and relationship types
You can useinclude_types
or exclude_types
to ignore parts of the graph schema when generating Cypher statements.
Validate generated Cypher statements
You can use thevalidate_cypher
parameter to validate and correct relationship directions in generated Cypher statements