Kg Gen is a Python library that extracts knowledge graphs from plain text with a language model. You pass in a string or a list of chat messages, and it returns the entities, the relation types, and the subject, relation, object triples it found. Uses include building a graph to support retrieval-augmented generation (RAG), generating synthetic graph data for model training and testing, and analyzing how concepts in a text relate.
Long inputs can be split into chunks, and similar entities and relations can be clustered (for example merging "AI" with "artificial intelligence") during generation or afterward, optionally guided by a context string. Separate graphs can be aggregated into one. For conversations, role information and message order are preserved, so relations can link speakers to the concepts they mention.
Model calls go through LiteLLM, so any provider it supports works, including OpenAI, Anthropic, Gemini, DeepSeek, and local models through Ollama, with an optional custom API base URL. Structured output is generated with DSPy. The project also ships an MCP server that gives AI agents persistent graph memory, a graph visualizer, and MINE, a benchmark for knowledge graph extraction described in the accompanying paper.
Features
- Text to graph: extracts entities, relation types, and triples from plain text in one call
- Conversation input: processes message arrays while keeping roles and message order
- Chunking: splits large texts into chunks of a set size before extraction
- Clustering: merges near-duplicate entities and relations, optionally guided by context
- Graph aggregation: combines several generated graphs into one
- Model choice: any LiteLLM provider, including OpenAI, Anthropic, Gemini, DeepSeek, and Ollama
- Visualization: renders a graph to an HTML view in the browser
- MCP server: exposes graph memory to AI agents and MCP clients
- MINE benchmark: an evaluation for knowledge graph extraction quality



