Skip to content

LLM Extract graph using mermaid

Published: at 10:30 AM
Author: Sebastian Talamoni

One of the many things you can do with LLMs is IE (Information Extraction). You are not limited to NER (Named Entity Recognition) but you can easily tweak your prompt to include other “for you” relevant information.

I have been doing this kind of AI workflows for a while now. Most of the times you have another workflow step that consumes this information, so 95% of the times you would want to use a structured json output (even better if PydanticAI output_type is used).

But just for fun today tested this alternative output and was impressed. Off course you can still do the extraction with structure output and then convert that into the diagram, also possible. For simplicity I decided to do it directly. So decided to test this idea with 2 articles from today’s newspaper.

F1 Example 🏎️

URL : Horner verlaat Red Bull ‘met pijn in het hart’: ‘Heb van dit team gehouden’

based on “summary” (not full article) using local LLM

I am using a very small local model for this: google’s gemma-3n-e4b alt text

For simple texts and simpler outputs local LLMs do really fine. The complex the task off course the smarter the model.

Include feelings and actions (gpt-4.1-mini)

This second screenshot is with a small variation in the prompt to include “feelings” and “actions”: alt text ⚠️ this was done with openAI chatgp4-mini and full article text

Include feelings and actions (gpt-4.1)

This is clearly better, grouping feelings and actions into 1 relations per tuple. alt text

Improving relationship grouping (gpt-4.1)

Last one had too many lines since it tried to map each action and feeling in its own relationship, prompt modified to group them alt text

Example 2

Here is the screenshot from LM-Studio, that shows the model used, the prompt and the output. alt text

What is mermaid ?

Mermaid is a text-based diagramming tool that lets you create flowcharts, sequence diagrams, entity-relationship diagrams, and more using a simple plain-text syntax. It’s especially popular among developers because it integrates well with tools like Markdown, Obsidian and others.

mermaid supported in Obsidian

Obsidian has been my PKM system for years, but i was not aware that it already supported mermaid syntax. Just using the ``` (and then close them) does the trick : alt text

System Prompt

The system prompt is here, pretty simple and allows a lot of room for improvement.

LLM System prompt

You are an expert in transforming relationship data into visual graphs using Mermaid syntax. Your task is to: a) Extract entities and relationships from a given text. b) Normalize entity IDs using lowercase with underscores (e.g., Sebastian_Talamoni).

For each entity:

  • Choose an appropriate emoji icon based on the role (e.g., 👨‍💼 for person, 🏎️ for organization).
  • Use the full label with spaces (e.g., “Sebastian Talamoni”), and display the icon before the name.
  • Generate the Mermaid diagram using graph TD.
  • Add clear relationship labels to arrows.
  • Use classDef and class to style nodes based on entity type:
    • person: light teal fill, bold dark teal border
    • organization: light orange fill, bold orange border

📌 Output format: mermaid

graph TD entity_id[”🔍 Entity Label”] … entity1 —>|relationship| entity2

  classDef person fill:#e0f7fa,stroke:#00796b,stroke-width:2px,font-size:14px
  classDef org fill:#fff3e0,stroke:#f57c00,stroke-width:2px,font-size:14px

  class entity1,entity2 person
  class entity3 org

Make sure the output is ready to render in Mermaid, does not contain extra text, and is cleanly formatted.

Conclusion : Why I am excited about this ?

I was able to understand the context of the article much faster than reading it. At least worked like that for me. I was able to on the fly define what ontology I wanted for the extracted entities , relationships , and where to place the focus of the extraction. In any other system would require quite some work of “adapting” the Ontology for example.

References


Next Post
Build Smart Agents with Docker MCP Server Toolkit & PydanticAI