# Knowledge graph creation

A knowledge graph stores the entities of your domain as nodes and the facts
that connect them as relationships, with labels and properties that say what
each one means. GraphRAG, AI memory and agents retrieve their context from it,
so the quality of their answers depends on the quality of the graph.

The meaning is what separates a knowledge graph from any other graph. In the
top graph, the labels and relationship types say what a report, a symptom and
a disease are and how they relate; the bottom graph only connects nodes:

![A knowledge graph of patients, reports, symptoms, diseases and treatments with meaningful relationship types, compared with a graph of energy sources and transformers that has no added meaning](https://memgraph.com/docs/pages/ai-ecosystem/graph-rag/knowledge-graph-creation/kg-or-no.jpg)

Start by designing the graph: which entities become nodes, which facts become
relationships and which properties a query needs. See [Model a knowledge
graph](https://memgraph.com/docs/data-modeling/modeling-guides/model-a-knowledge-graph) and [Data
modeling](https://memgraph.com/docs/data-modeling).

## Choose how to create the graph

How you create the graph depends on where your data is:

| Your data | Use |
| --- | --- |
| A MySQL or PostgreSQL database | [SQL2Graph](https://memgraph.com/docs/ai-ecosystem/knowledge-graph-creation/sql2graph-agent), which proposes a graph model and migrates the data |
| Another relational database | [Migrate from a relational database](https://memgraph.com/docs/data-migration/migrate-from-rdbms) |
| CSV, JSON, Parquet or Cypher files | [Data migration](https://memgraph.com/docs/data-migration), for example [`LOAD CSV`](https://memgraph.com/docs/querying/clauses/load-csv) |
| Documents such as PDF, DOCX or text | [Unstructured2Graph](https://memgraph.com/docs/ai-ecosystem/knowledge-graph-creation/unstructured2graph-agent), which extracts entities and relationships with an LLM |
| Neo4j | [Migrate from Neo4j](https://memgraph.com/docs/data-migration/migrate-from-neo4j) |
| A stream, such as Kafka | [Data streams](https://memgraph.com/docs/data-migration/data-streams) |

For large imports, follow the [import best
practices](https://memgraph.com/docs/data-migration/best-practices).

## Structured data

Structured data, such as tables and CSV files, already has explicit entities
and fields, so you decide the graph model and map the data to it:

1. Map tables or files to nodes, and foreign keys or references to
   relationships.
2. Choose the properties to keep on each node and relationship, including
   metadata that adds context for the LLM.
3. Import the data into Memgraph.

If your data is in MySQL or PostgreSQL, SQL2Graph can do the mapping for you,
either by rules or with an LLM, and you review the model before it migrates
the data.

## Unstructured data

Unstructured data, such as text, PDFs or logs, has no explicit entities, so
the entities and relationships have to be extracted first. You choose how much
to automate:

- **Unstructured2Graph** parses and chunks documents, extracts entities and
  relationships with an LLM and writes them to Memgraph.
- **Framework integrations** build a knowledge graph from documents in your
  [LangChain](https://memgraph.com/docs/ai-ecosystem/integrations/langchain) or
  [LlamaIndex](https://memgraph.com/docs/ai-ecosystem/integrations/llamaindex) application.
- **A custom pipeline** gives you the most control over accuracy, which
  matters in production.

A custom extraction pipeline usually has these steps:

1. **Preprocessing:** Clean and split the raw data.
2. **Named entity recognition (NER):** Find and classify the entities.
3. **Relationship extraction:** Find the connections between the entities.
4. **Disambiguation:** Merge mentions of the same entity and resolve
   ambiguous ones from their context.
5. **Post-processing:** Validate the entities and relationships before you
   write them to the graph.

For an example, see [How to Extract Entities and Build a Knowledge Graph with
Memgraph and spaCy](https://memgraph.com/blog/extract-entities-build-knowledge-graph-memgraph-spacy).

Read more:

- [Improved Knowledge Graph Creation with LangChain and LlamaIndex](https://memgraph.com/blog/improved-knowledge-graph-creation-langchain-llamaindex)
- [Constructing a Knowledge Graph with LlamaIndex and Memgraph](https://www.llamaindex.ai/blog/constructing-a-knowledge-graph-with-llamaindex-and-memgraph)

When the graph is ready, retrieve from it with
[GraphRAG](https://memgraph.com/docs/ai-ecosystem/graph-rag).
