llm_util

docs-source (opens in a new tab)

TraitValue
Module typeutil
ImplementationPython
Parallelismsequential

Procedures

schema(output_type)

The schema() procedure generates the graph database schema in a prompt-ready or raw format. The prompt-ready format is optimized to describe the database schema in words best recognized by large language models (LLMs). The raw format offers all the necessary information about the graph schema in a format that can be customized for later use with LLMs.

Input:

  • output_type: str (default='prompt_ready') ➡ By default, the graph schema will include additional context and it will be prompt-ready. If set to 'raw', it will produce a simpler version that can be adjusted for the prompt.

Output:

  • schema: mgp.Anystr containing prompt-ready graph schema description in a format suitable for large language models (LLMs), or mgp.List containing information on graph schema in raw format which can customized for LLMs.

Usage:

Get prompt-ready graph schema:

CALL llm_util.schema() YIELD schema RETURN schema;

or

CALL llm_util.schema('prompt_ready') YIELD schema RETURN schema;

Get raw graph schema:

CALL llm_util.schema('raw') YIELD schema RETURN schema;

The output_type is case-insensitive.

Example - Get prompt-ready graph schema

Step 1 Create a graph

Create a graph by running the following Cypher query:

CREATE (n:Person {name: "Kate", age: 27})-[:IS_FRIENDS_WITH {since: "2023-06-21"}]->(m:Person:Student {name: "James", age: 30, year: "second"})-[:STUDIES_AT]->(:University {name: "University of Zagreb"}) CREATE (p:Person:Student {name: "Anthony", age: 25})-[:STUDIES_AT]->(:University {name: "University of Vienna"}) 
WITH n, m 
CREATE (n)-[:LIVES_IN]->(:City {name: "Zagreb"})<-[:LIVES_IN]-(m);

Step 2 Database schema

The schema of the created graph can be seen in Memgraph Lab, under the Graph Schema tab:

Step 3 Run the command

Once the graph is created, run the following code to call the schema procedure:

CALL llm_util.schema() YIELD schema RETURN schema;

or

CALL llm_util.schema('prompt_ready') YIELD schema RETURN schema;

Step 4 Result

Below is the result of running the schema procedure:

Node properties are the following:
Node name: 'Person', Node properties: [{'property': 'name', 'type': 'str'}, {'property': 'age', 'type': 'int'}, {'property': 'year', 'type': 'str'}]
Node name: 'Student', Node properties: [{'property': 'name', 'type': 'str'}, {'property': 'age', 'type': 'int'}, {'property': 'year', 'type': 'str'}]
Node name: 'University', Node properties: [{'property': 'name', 'type': 'str'}]
Node name: 'City', Node properties: [{'property': 'name', 'type': 'str'}]

Relationship properties are the following:
Relationship Name: 'IS_FRIENDS_WITH', Relationship Properties: [{'property': 'since', 'type': 'str'}]

The relationships are the following:
['(:Person)-[:IS_FRIENDS_WITH]->(:Person)']
['(:Person)-[:IS_FRIENDS_WITH]->(:Student)']
['(:Person)-[:LIVES_IN]->(:City)']
['(:Person)-[:STUDIES_AT]->(:University)']
['(:Student)-[:STUDIES_AT]->(:University)']
['(:Student)-[:LIVES_IN]->(:City)']

Example - Get raw graph schema

Step 1 Create a graph

Create a graph by running the following Cypher query:

CREATE (n:Person {name: "Kate", age: 27})-[:IS_FRIENDS_WITH {since: "2023-06-21"}]->(m:Person:Student {name: "James", age: 30, year: "second"})-[:STUDIES_AT]->(:University {name: "University of Zagreb"}) CREATE (p:Person:Student {name: "Anthony", age: 25})-[:STUDIES_AT]->(:University {name: "University of Vienna"}) 
WITH n, m 
CREATE (n)-[:LIVES_IN]->(:City {name: "Zagreb"})<-[:LIVES_IN]-(m);

Step 2 Database schema

The schema of the created graph can be seen in Memgraph Lab, under the Graph Schema tab:

Step 3 Run the command

Once the graph is created, run the following code to call the schema procedure:

CALL llm_util.schema('raw') YIELD schema RETURN schema;

Step 4 Result

Below is the result of running the schema procedure:

{
   "node_props": {
      "City": [
         {
            "property": "name",
            "type": "str"
         }
      ],
      "Person": [
         {
            "property": "name",
            "type": "str"
         },
         {
            "property": "age",
            "type": "int"
         },
         {
            "property": "year",
            "type": "str"
         }
      ],
      "Student": [
         {
            "property": "name",
            "type": "str"
         },
         {
            "property": "age",
            "type": "int"
         },
         {
            "property": "year",
            "type": "str"
         }
      ],
      "University": [
         {
            "property": "name",
            "type": "str"
         }
      ]
   },
   "rel_props": {
      "IS_FRIENDS_WITH": [
         {
            "property": "since",
            "type": "str"
         }
      ]
   },
   "relationships": [
      {
         "end": "Person",
         "start": "Person",
         "type": "IS_FRIENDS_WITH"
      },
      {
         "end": "Student",
         "start": "Person",
         "type": "IS_FRIENDS_WITH"
      },
      {
         "end": "City",
         "start": "Person",
         "type": "LIVES_IN"
      },
      {
         "end": "University",
         "start": "Person",
         "type": "STUDIES_AT"
      },
      {
         "end": "University",
         "start": "Student",
         "type": "STUDIES_AT"
      },
      {
         "end": "City",
         "start": "Student",
         "type": "LIVES_IN"
      }
   ]
}