langchain.chains import GraphCypherQAChainfrom langchain.graphs import MemgraphGraphfrom langchain.prompts import PromptTemplatefrom gqlalchemy import Memgraphimport osWe're utilizing the Python library GQLAlchemy to establish a connection between our Memgraph database and Python script. To execute queries, we can set up a Memgraph instance as follows:memgraph = Memgraph(host='127.0.0.1', port=7687)Populating the database‚ÄãYou can effortlessly populate your new, empty database using the Cypher query language. Don't worry if you don't grasp every line just yet, you can learn Cypher from the documentation here. Running the following script will execute a seeding query on the database, giving us data about a video game, including details like the publisher, available platforms, and genres. This data will serve as a basis for our work.# Creating and executing the seeding queryquery = """    MERGE (g:Game {name: "Baldur's Gate 3"})    WITH g, ["PlayStation 5", "Mac OS", "Windows", "Xbox Series X/S"] AS platforms,            ["Adventure", "Role-Playing Game", "Strategy"] AS genres    FOREACH (platform IN platforms |        MERGE (p:Platform {name: platform})        MERGE (g)-[:AVAILABLE_ON]->(p)    )    FOREACH (genre IN genres |        MERGE (gn:Genre {name: genre})        MERGE (g)-[:HAS_GENRE]->(gn)    )    MERGE (p:Publisher {name: "Larian Studios"})    MERGE (g)-[:PUBLISHED_BY]->(p);"""memgraph.execute(query)Refresh graph schema‚ÄãYou're all set to instantiate the Memgraph-LangChain graph using the following script. This interface will allow us to query our database using LangChain, automatically creating the required graph schema for generating Cypher queries through LLM.graph = MemgraphGraph(url="bolt://localhost:7687", username="", password="")If necessary, you can manually refresh the graph schema as follows.graph.refresh_schema()To familiarize yourself with the data and verify the updated graph schema, you can print it using the following
This notebook shows how to use LLMs to provide a natural language interface to a Memgraph database. To complete this tutorial, you will need Docker and Python 3.x installed.
This notebook shows how to use LLMs to provide a natural language interface to a Memgraph database. To complete this tutorial, you will need Docker and Python 3.x installed. ->: langchain.chains import GraphCypherQAChainfrom langchain.graphs import MemgraphGraphfrom langchain.prompts import PromptTemplatefrom gqlalchemy import Memgraphimport osWe're utilizing the Python library GQLAlchemy to establish a connection between our Memgraph database and Python script. To execute queries, we can set up a Memgraph instance as follows:memgraph = Memgraph(host='127.0.0.1', port=7687)Populating the database‚ÄãYou can effortlessly populate your new, empty database using the Cypher query language. Don't worry if you don't grasp every line just yet, you can learn Cypher from the documentation here. Running the following script will execute a seeding query on the database, giving us data about a video game, including details like the publisher, available platforms, and genres. This data will serve as a basis for our work.# Creating and executing the seeding queryquery = """    MERGE (g:Game {name: "Baldur's Gate 3"})    WITH g, ["PlayStation 5", "Mac OS", "Windows", "Xbox Series X/S"] AS platforms,            ["Adventure", "Role-Playing Game", "Strategy"] AS genres    FOREACH (platform IN platforms |        MERGE (p:Platform {name: platform})        MERGE (g)-[:AVAILABLE_ON]->(p)    )    FOREACH (genre IN genres |        MERGE (gn:Genre {name: genre})        MERGE (g)-[:HAS_GENRE]->(gn)    )    MERGE (p:Publisher {name: "Larian Studios"})    MERGE (g)-[:PUBLISHED_BY]->(p);"""memgraph.execute(query)Refresh graph schema‚ÄãYou're all set to instantiate the Memgraph-LangChain graph using the following script. This interface will allow us to query our database using LangChain, automatically creating the required graph schema for generating Cypher queries through LLM.graph = MemgraphGraph(url="bolt://localhost:7687", username="", password="")If necessary, you can manually refresh the graph schema as follows.graph.refresh_schema()To familiarize yourself with the data and verify the updated graph schema, you can print it using the following