Skip to main content

A lightweight markup language and Python library for writing, composing, and rendering structured LLM prompts.

Project description

MARGARITA

PyPI version Python Support

Margarita is a lightweight markup language and Python library for writing, composing, and rendering structured LLM prompts.

Margarita extends Markdown with templating features like variables, conditionals, loops, and includes, making it easy to create dynamic prompts for large language models (LLMs).

| FOR NOW! CLI tool is WIP you will need to download the source and install it locally to use the margarita command. |

Features

  • ✨ Framework agnostic — works with any LLM or API
  • 🚀 Composable — prompts can be split, reused, and nested
  • 🎯 Static-first — templates are validated before execution
  • 📦 Metadata — version, and provide metadata alongside your prompts.

Get Started

Here's a Hello World example. helloworld.mg contains the template, and helloworld.json contains the data.

// file:helloworld.mg

Hello, {{name}}!
Welcome to Margarita templating.
// file:helloworld.json

{
    "name": "World"
}

Run the following command:

margarita render helloworld.mg

Output:

Hello, World!
Welcome to Margarita templating.

Python Library

Install the package via pip/poetry/uv/etc or whatever package manager you prefer:

pip install margarita
poetry add margarita
uv add margarita

Use the library in your Python code:

from margarita.parser import Parser
from margarita.renderer import Renderer

template = """
You are a helpful assistant.

Task: {{task}}

{% if context %}
Context:
{{context}}
{% endif %}

Please provide a detailed response.
"""

# Parse the template
parser = Parser()
metadata, nodes = parser.parse(template)

# Create a renderer with context
renderer = Renderer(
    context={"task": "Summarize the key points", "context": "User is researching AI agents"}
)

# Render the output
prompt = renderer.render(nodes)
print(prompt)

Use the Composer to manage multiple templates:

from margarita.composer import Composer
from pathlib import Path

manager = Composer(Path("./templates"))

# Compose a complex prompt from multiple snippets
prompt = manager.compose_prompt(
    snippets=[
        "snippets/system_role.mg",
        "snippets/task_context.mg",
        "snippets/chain_of_thought.mg",
        "snippets/output_format.mg"
    ],
    context={
        "role": "data scientist",
        "user_name": "Bob",
        "task": "Analyze customer churn",
        "format": "JSON",
        "tone": "analytical"
    }
)

Documentation

Full documentation is available at https://banyango.mgithub.io/margarita/latest

Development

This project uses uv for dependency management.

Setup Development Environment

uv sync # Install dependencies

Running Tests

# Run tests with pytest
uv run pytest

# Run tests with coverage
uv run pytest --cov=margarita --cov-report=html

Code Quality

# Format code with ruff
uv run ruff format .

# Lint code
uv run ruff check .

# Type checking with mypy
uv run mypy src/margarita

Building the Package

# Build the package
uv build

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to:

  • Update tests as appropriate
  • Follow the existing code style
  • Update documentation for any changed functionality

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors

Acknowledgments

  • Markdown

Changelog

See CHANGELOG.md for a history of changes to this project.

Support

If you encounter any problems or have questions, please open an issue.

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

margarita-0.2.0.tar.gz (718.3 kB view details)

Uploaded Source

Built Distribution

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

margarita-0.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file margarita-0.2.0.tar.gz.

File metadata

  • Download URL: margarita-0.2.0.tar.gz
  • Upload date:
  • Size: 718.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for margarita-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9a107986a7275995d8bf5bbc30f056d2b1012fdf31380f80fd34b592dd01216a
MD5 6c59a13ea04a8426ad74441be8d35969
BLAKE2b-256 4b7885a3f5f165f46b4b6c6975bda99e95253802316547a05c5aa716c6803b0b

See more details on using hashes here.

File details

Details for the file margarita-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: margarita-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for margarita-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 220254fe11ca66847b45f2c7a19eea9e1939b6dee090b9eddba67a311bc031a8
MD5 bd236b575195cd51cd3e5c5ca53967a1
BLAKE2b-256 11c6b8121e8e4515688a544d4f7c760439d7df2324d1107ccddcd4e6e6729831

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