| [ Web Proxy ] |
| Viewing: https://memgraph.com/docs/mage/query-modules/python/llm-util | [Back] [Original] |
Deprecated: The llm_util.schema() procedure is now deprecated. Please
use the SHOW SCHEMA INFO; query instead for retrieving schema
information. Make sure the --schema-info-enabled=true flag
is set for this query to work.
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 - a framework for developing applications powered by language models.
| Trait | Value |
|---|---|
| Module type | util |
| Implementation | Python |
| Parallelism | sequential |
You can execute this algorithm on graph projections, subgraphs or portions of the graph.
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.
subgraph: Graph (OPTIONAL) A specific subgraph, which is an object of type Graph returned by the project() function, on which the algorithm is run.
If subgraph is not specified, the algorithm is computed on the entire graph by default.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.schema: mgp.Any str 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.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;Below are examples on how to get a prompt ready graph schema, and a raw graph schema.
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);The schema of the created graph can be seen in Memgraph Lab, under the Graph Schema tab:
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;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)']Once the graph is created, run the following code to call the schema procedure:
CALL llm_util.schema('raw') YIELD schema RETURN schema;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"
}
]
}| Web Proxy Viewer | New URL | Original Page |