All the cool kids are converting natural language text to knowledge graphs, so why not me!? 😊

In this article I show how to use a “1-liner” using Concerto Graph to create nodes and edges in a Knowledge Graph from text, whilst ensuring that the nodes and edges conforms to a Concerto data model (schema).

I’ve extended Concerto Graph to add a new method mergeTextToGraph which makes converting a block of text to nodes and edges to a Knowledge Graph very simple.

Simply pass a block of text to the mergeTextToGraph method and it will take care of extracting the nodes/edges from the text and adding them to the Knowledge Graph, ensuring that the structure of the nodes and edges conforms to the Concerto data model that is associated with the Graph Model.

    const textToGraph = `Kingdom of the Planet of the Apes

Maze Runner director Wes Ball returns to the Planet of the Apes franchise, with a story set 300 years after the events of
War for the Planet of the Apes. Pitched by Ball as Apocalypto but with apes and focusing more on action and adventure, 
the story follows a new protagonist Noa (Owen Teague) as he tries to steer the apes away from the totalitarian future 
they are headed towards. Freya Allan, Kevin Durand, Dichen Lachman, and William H. Macy star.`;
    await graphModel.mergeTextToGraph(textToGraph);
Graph nodes and edges created from Kingdom of the Planet of the Apes text snippet

The debug output should be similar to that below. In this case a single Movie node is created, a single Director node is created and 5 Actor nodes. Relationships are then created to link the Movie, Director and Actors.

Creating {"type":"node","label":"Movie","properties":{"identifier":"Kingdom of the Planet of the Apes","summary":"Maze Runner director Wes Ball returns to the Planet of the Apes franchise, with a story set 300 years after the events of War for the Planet of the Apes. Pitched by Ball as Apocalypto but with apes and focusing more on action and adventure, the story follows a new protagonist Noa (Owen Teague) as he tries to steer the apes away from the totalitarian future they are headed towards. Freya Allan, Kevin Durand, Dichen Lachman, and William H. Macy star."}}... []
EmbeddingCacheNode cache miss []
Created cache node 6174ef3f2803595fd0e0b2ab99b0a758c158458f5068668d1e7f5be50950e7c6 []
Creating {"type":"node","label":"Director","properties":{"identifier":"Wes Ball"}}... []
Creating {"type":"relationship","startNodeLabel":"Director","startNodeIdentifier":"Wes Ball","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"directed"}... []
Creating {"type":"node","label":"Actor","properties":{"identifier":"Owen Teague"}}... []
Creating {"type":"relationship","startNodeLabel":"Actor","startNodeIdentifier":"Owen Teague","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"actedIn"}... []
Creating {"type":"node","label":"Actor","properties":{"identifier":"Freya Allan"}}... []
Creating {"type":"relationship","startNodeLabel":"Actor","startNodeIdentifier":"Freya Allan","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"actedIn"}... []
Creating {"type":"node","label":"Actor","properties":{"identifier":"Kevin Durand"}}... []
Creating {"type":"relationship","startNodeLabel":"Actor","startNodeIdentifier":"Kevin Durand","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"actedIn"}... []
Creating {"type":"node","label":"Actor","properties":{"identifier":"Dichen Lachman"}}... []
Creating {"type":"relationship","startNodeLabel":"Actor","startNodeIdentifier":"Dichen Lachman","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"actedIn"}... []
Creating {"type":"node","label":"Actor","properties":{"identifier":"William H. Macy"}}... []
Creating {"type":"relationship","startNodeLabel":"Actor","startNodeIdentifier":"William H. Macy","endNodeLabel":"Movie","endNodeIdentifier":"Kingdom of the Planet of the Apes","startNodePropertyName":"actedIn"}... []
Created 7 nodes and 6 edges. []

The implementation uses a pretty simple prompt, which is parameterised using the Concerto model.

The code is committed and the demo code has been updated. I’d be grateful for testing with other data models and any feedback.