llm_util

A module that contains procedures describing graphs in a format best suited for large language models (LLMs). The module was originally built for schema generation in LangChain (opens in a new tab) - a framework for developing applications powered by language models.

TraitValue
Module typeutil
ImplementationPython
Parallelismsequential

Procedures

schema()

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. The parameter is case-insensitive.

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:

To get the prompt-ready graph schema, use the following query:

CALL llm_util.schema() YIELD schema RETURN schema;

To get the raw graph schema, use the following query:

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

Example

Below are examples on how to get a prompt ready graph schema, and a raw graph schema.

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);

Database schema

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

Prompt-ready graph schema

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;

Results

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)']

Raw graph schema

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

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

Results

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"
      }
   ]
}