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.

It renders out to Markdown format by default so you can use this anywhere Markdown is supported.

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.

Conditionals Example

// file:conditional_example.mg

if is_admin:
    <<Welcome, Admin ${name}>>
else:
    <<Welcome, User {{name}}!>>

Includes

// file: role_example.mg
You are a fancy AI assistant.
// files:json_output_format_template.mg
<<
Output the response in the following JSON format:
{
    "response": "<your response here>"
}
>>
// file:include_example.mg
[[ role ]]

if output_json:
    [[ json_output_format_template ]]

Passing Context

You can pass context variables to your templates to make them dynamic.

// file:role.mg
You are a ${ type } AI assistant.
// file:context_example.mg
[[ role type="fancy" ]]

Metadata

Organize your templates with metadata headers.

// file:metadata_example.mg
---
title: "Greeting Template"
version: "1.0"
author: "Batman"
---
<<
Hello, {{name}}!
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.3.2.tar.gz (732.7 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.3.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: margarita-0.3.2.tar.gz
  • Upload date:
  • Size: 732.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.3.2.tar.gz
Algorithm Hash digest
SHA256 33287c6f5014bb1f1eaa4582b3defe87a91a36da9557ce00c2fd1c02c51e97bf
MD5 340b791ac87b12be14a12634398e2094
BLAKE2b-256 3e7e08e3eb96138a3f29be9c1a82e0ce07bdd2d1e88784325a2670f1cec6245b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: margarita-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8dcae1911b495d8d4555780ed077bad13ee7b1e13dd5309e727e120b49d15b8
MD5 a5b8405ebe14f9f833423483eed25cf2
BLAKE2b-256 e28b5cd3c94c80973eea8082fe02ebb8d1ed97ae1802c18e68a41132138ebd2a

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