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 targets prompt engineering workflows where clarity, versioning, and correctness matter.

| 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.marg contains the template, and helloworld.json contains the data.

// file:helloworld.marg

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

{
    "name": "World"
}

Run the following command:

margarita render helloworld.marg

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.marg",
        "snippets/task_context.marg",
        "snippets/chain_of_thought.marg",
        "snippets/output_format.marg"
    ],
    context={
        "role": "data scientist",
        "user_name": "Bob",
        "task": "Analyze customer churn",
        "format": "JSON",
        "tone": "analytical"
    }
)

Documentation

Full documentation is available at https://banyango.github.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.1.0.tar.gz (722.4 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.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: margarita-0.1.0.tar.gz
  • Upload date:
  • Size: 722.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 b7c20f018b51d994b2e70cb55e67fd57fe91a0313ac295846d9018e09ac97dff
MD5 0a3ae361bfdcd38bed4329869a7a8a96
BLAKE2b-256 96ec3fde3da83aaef7a33fd3a24cf58b96a7ec19464fb274a37e7349a676c671

See more details on using hashes here.

File details

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

File metadata

  • Download URL: margarita-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5078324a57bc2e02125a52e4a82508b780ed198dac4f02f1d99501e58fb04d37
MD5 0bc9462d1901a0e80cbc535bbb4b7918
BLAKE2b-256 9441f767b9aca5f9ca6df078c5a487ab122bd1c0d8cea4d2914f68fd5bf954c5

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