Skip to main content

Prompt template engine, built upon Jinja

Project description

Prompt Bottle

A powerful prompt template engine built upon Jinja

PyPI version Python 3.10+ License: MIT

Features ✨

  • 🎯 Jinja-Powered Templates: Leverage the full power of Jinja2 templating for dynamic prompt generation
  • 🎭 Role-Based Messaging: Structure conversations with system, user, assistant, and tool roles
  • 🤖 Pydantic-AI Compatible: Returns structured pydantic-ai ModelMessage objects for seamless integration
  • 🔄 Format Conversion: Convert to OpenAI chat API (or other APIs) through pydantic-ai's message mapping

Installation 📦

pip install prompt-bottle

Quick Start 🚀

Basic Usage

from prompt_bottle import render

# Simple template
template = """
You are a helpful assistant.
<div role="user">{{ user_message }}</div>
"""

messages = render(template, user_message="Hello, world!")
print(messages)

Core Concepts 📚

Template Example

You are a helpful assistant.
{{ system_instructions }}

{% for item in conversation %}
<div role="user">{{ item.message }}</div>
<div role="assistant">
  <think>{{ item.reasoning }}</think>
  {{ item.response }}
</div>
{% endfor %}

<div role="user">
  <Instruction>
  Now you are required to answer the query based on the context.
  Your output must be quoted in <Answer></Answer> tags.
  </Instruction>
  <Context>
  {{ context }}
  </Context>
  <Query>
  {{ query }}
  </Query>
</div>

💡 Check out example.py and example.jinja for a comprehensive example with all features!

[!WARNING] You can use any HTML-like tags in your prompt, other than the reserved tags. However, all tags must be properly closed (e.g., <instruct> content </instruct> not <instruct> content) to avoid parsing errors.

Roles

Prompt Bottle supports four main roles:

  • system: System instructions and configuration. Default of raw text.
  • user: User messages and queries
  • assistant: AI assistant responses
  • tool: Tool execution results

Response Types

Assistant responses can include multiple content tags:

  • <text>: Regular text content. Default of raw text.
  • <tool_call>: Function/tool invocations
  • <think>: Reasoning and thought processes

Request Types

TODO: Will support multimodal input in the future.

API Reference 📖

render(template: str, **kwargs) -> list[ModelMessage]

Renders a Jinja template with the provided variables and returns structured messages.

Parameters:

  • template: Jinja template string
  • **kwargs: Template variables

Returns: List of pydantic-ai ModelMessage objects (ModelRequest/ModelResponse)

to_openai_chat(messages: list[ModelMessage], **model_kwargs) -> list[dict]

Converts structured messages to OpenAI chat completion format.

Parameters:

  • messages: List of ModelMessage objects from render()
  • **model_kwargs: OpenAI model configuration (default model: gpt-4o)

Returns: List of OpenAI-formatted message dictionaries

Contributing 🤝

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

License 📄

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

Links 🔗

velin: Vue.js based prompt template engine

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

prompt_bottle-0.4.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

prompt_bottle-0.4.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file prompt_bottle-0.4.0.tar.gz.

File metadata

  • Download URL: prompt_bottle-0.4.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.9 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for prompt_bottle-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c25a5da169de8a9154dd77eea7c3cd1b7e1ed72a9a56435beead9e0d233bfc9e
MD5 d0a05452c326810b788e4d5ef0b07dfc
BLAKE2b-256 45c92744c7dd955bb78473c68bc62c1690a36ad30b28c376f83283db9eaf3837

See more details on using hashes here.

File details

Details for the file prompt_bottle-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: prompt_bottle-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.9 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for prompt_bottle-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d62fc41a3b6ca33b7b5822d9bbd8c96802cd09dc6bae29674536847c68e6a3a8
MD5 e38605ff1c45019b686d25d68fbbbdb7
BLAKE2b-256 9f6741f7d49ee5644bd3aef0c812420b75b4e04bdfc67a4dc7b8d9abb05f14d9

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