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.1.tar.gz (5.0 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slm_graph-0.1.1.tar.gz
  • Upload date:
  • Size: 5.0 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.1.tar.gz
Algorithm Hash digest
SHA256 ec56e6077567940388d82ddac9a09f88cf91e2abd202a39814e0ddd09814da59
MD5 919bfb1fc73698e9b4825c38397ac7c6
BLAKE2b-256 9d1fddf2e0c23c83f304d027a501af95df7c448d50751f1ad40e0f0e407f9e24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slm_graph-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c188d1e8aa4668b7d2118a54a7d19bcba11a02328c15f5fcf89672b6e62dda4
MD5 0dea45d2083fecd46e4d2eed5bd4e2e7
BLAKE2b-256 4bd7b6a1fc27371d5d89e05eeff54df6618e4ef22711ce7a4ad9720f91e36333

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