Skip to main content

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

  1. Python Library

    pip install slm-graph

  2. System Rendering Dependency

    You must have the Mermaid CLI installed for image and PDF generation:

    npm install -g @mermaid-js/mermaid-cli

  3. Model Weight

    To use the library, you need to download a Small Language Model in GGUF format.

    Recommended Models:

    • Bartowski/Llama-3.2-1B-Instruct-GGUF (Recommended: Q4_K_M or Q6_K versions)
    • Unsloth/Llama-3.2-1B-Instruct-GGUF

Setup Instructions:

  1. Create a folder named models in your project root directory.

  2. Download the .gguf file (e.g., Llama-3.2-1B-Instruct-Q4_K_M.gguf) from the links above.

  3. 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())

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slm_graph-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slm_graph-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file slm_graph-0.1.0.tar.gz.

File metadata

  • Download URL: slm_graph-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.5 Windows/11

File hashes

Hashes for slm_graph-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c6921a6f21f8e5a02fd1ac43dcf85cde0c3d5c9544aed4159555a78278b6b7f
MD5 530d34b9913254a45d5ef2447803e3b9
BLAKE2b-256 69576c45f35e51b8b616bd9cef473e3029ea69860843a8f943bd8ae7da57f1e2

See more details on using hashes here.

File details

Details for the file slm_graph-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slm_graph-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.5 Windows/11

File hashes

Hashes for slm_graph-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 242e80b4b6e33dfcadf76b93e19ded4588b6821cac66a2714f1de7c4634edcf1
MD5 a380bd177798e1e7c0aa1739ba5dd927
BLAKE2b-256 47fbe26b7404483962e3d0d01148b3259b60bc83ae5554ab2a60925297903b56

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page