Self-contained natural language to Mermaid graph generator using SLMs.
Project description
SLM-Graph
SLM-Graph is a lightweight, production-grade Python library that transforms natural language descriptions into professional Mermaid.js diagrams. By embedding llama-cpp-python and using the Instructor pattern, it provides a reliable, self-contained way to generate structured graphs without external API dependencies or local server installations like Ollama.
Key Features
-
Self-Contained Inference: Uses llama-cpp-python directly in your Python process.
-
Strict Schema Validation: Leverages Pydantic and Instructor to prevent AI hallucinations and ensure valid Mermaid syntax.
-
Multi-Format Export: Generates SVG, PNG, and PDF exports in parallel.
-
Professional Aesthetics: Automatically maps SLM output to optimized Mermaid flowchart layouts.
Installation
-
System Rendering Dependency
You must have the Mermaid CLI installed for image and PDF generation:
npm install -g @mermaid-js/mermaid-cli
-
Python Library
User Warning (Windows Users): Installing llama-cpp-python often triggers a long C++ compilation process that can take 10-20 minutes or fail if build tools are missing. To skip this and ensure a smooth installation, use the pre-compiled binaries:
- For windows:
pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu pip install slm-graph
- For Linux/Mac:
pip install slm-graph
-
Model Weight
To use the library, you need to download a Small Language Model in GGUF format.
Recommended Model:
Setup Instructions:
-
Create a folder named models in your project root directory.
-
Download the .gguf file (e.g., Llama-3.2-1B-Instruct-Q4_K_M.gguf) from the links above.
-
Paste the file into the models/ folder.
Quick Start
from slm_graph import EasyGraph
import asyncio
async def main():
# Initialize with the path to your downloaded GGUF model
eg = EasyGraph(model_path="models/Llama-3.2-1B-Instruct-Q4_K_M.gguf")
# Generate graph files from a prompt
await eg.generate(
prompt="A customer places an order. If payment is successful, the warehouse ships it. If it fails, the order is cancelled.",
output_name="order_process",
formats=["svg", "png", "pdf"]
)
if __name__ == "__main__":
asyncio.run(main())
Disclaimers & Limitations
-
First-Time Install Delay: As mentioned, the first installation of
llama-cpp-pythonmay hang at "Building Wheel." This is expected behavior as it compiles C++ code for your specific CPU. -
Inference Speed: Generation speed depends on your CPU/GPU. A 3B model typically takes 5-15 seconds to "think" before rendering begins.
-
Model Accuracy: While 3B models are highly accurate, they may occasionally hallucinate. We use Pydantic aliases (
name -> label,from -> source) to automatically correct common SLM naming mistakes.
Project Architecture
-
core.py: Orchestrates local inference and Instructor patching. -
schema.py: Defines the data contract for nodes, edges, and graph types. -
renderer.py: Compiles Pydantic objects to Mermaid and handles CLI subprocesses.
License
Distributed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slm_graph-0.1.2.tar.gz.
File metadata
- Download URL: slm_graph-0.1.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.5 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96a861612d1619edd1d69fedc54d70fb15a66c0df7e0ce053312c9f0caa51aa8
|
|
| MD5 |
52f9253a2e6467d704f95308c45f78b0
|
|
| BLAKE2b-256 |
8f811e1c8b6f574f3378d1afb93efb940697dda692c0151b4af3c4aeb2125b1c
|
File details
Details for the file slm_graph-0.1.2-py3-none-any.whl.
File metadata
- Download URL: slm_graph-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.5 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
701c6ff37dbf33618a06802c98c6fbb70deb6ae29e8cd0e0d9fb1f5676f05ded
|
|
| MD5 |
14b1fef046cdfda735433c57cd00b673
|
|
| BLAKE2b-256 |
88759d4eb95e5cf5e9c86c9580deeedf6947d79d5579bb6531112ea71d405106
|