Skip to main content

LLM-targeted template engine, built upon Jinja

Project description

Prompt Bottle

LLM-targeted template engine, built upon Jinja.

Features

  • Use Jinja syntax to build template for OpenAI chat completion API.
  • Multimodal support for input - without pain.
  • Simple and easy to use.

Quick Start

Install the package via pip:

pip install prompt_bottle

Then, we can create a "Prompt Bottle" using Jinja syntax:

from prompt_bottle import PromptBottle

bottle = PromptBottle(
    [
        {
            "role": "system",
            "content": "You are a helpful assistant in the domain of {{ domain }}",
        },
        "{% for round in rounds %}",
        {
            "role": "user",
            "content": "Question: {{ round[0] }}",
        },
        {
            "role": "assistant",
            "content": "Answer: {{ round[1] }}",
        },
        "{% endfor %}",
        {"role": "user", "content": "Question: {{ final_question }}"},
    ]
)

Then we can render it as we do with Jinja.

Render the bottle and send to OpenAI:
from prompt_bottle import pb_img_url

prompt = bottle.render(
    domain="math",
    rounds=[
        ("1+1", "2"),
        (
            f"What is this picture? {pb_img_url('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg')}",
            "This is an example image by Wikipedia",
        ),
    ],
    final_question="8*8",
)

from rich import print  # pip install rich

print(prompt)
It prints the rendered prompt:
[
    {
        'content': [{'text': 'You are a helpful assistant in the domain of math', 'type': 'text'}],
        'role': 'system'
    },
    {'content': [{'text': 'Question: 1+1', 'type': 'text'}], 'role': 'user'},
    {'role': 'assistant', 'content': [{'text': 'Answer: 2', 'type': 'text'}]},
    {
        'content': [
            {'text': 'Question: What is this picture? ', 'type': 'text'},
            {
                'image_url': {'url': 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'},
                'type': 'image_url'
            }
        ],
        'role': 'user'
    },
    {
        'role': 'assistant',
        'content': [{'text': 'Answer: This is an example image by Wikipedia', 'type': 'text'}]
    },
    {'content': [{'text': 'Question: 8*8', 'type': 'text'}], 'role': 'user'}
]

Finally, we can send the prompt to OpenAI:

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=prompt,
)

print(response.choices[0].message.content)

The response is:

Answer: 64

Concepts

Prompt Bottle

The template of the prompt. It is a list of Template Messages. It can be rendered as python dict or JSON string.

Template Message

A message is either an OpenAI message dict, or a Jinja control block like {% for ... %} or {% if ... %}.

If it is a text message, it will be rendered by Jinja firstly, like {{ variable }}. And then it will be rendered by Multimodal Tag.

Multimodal Tag

The tag inside a text can render the text message as multimodal parts. The tag looks like <PROMPT_BOTTLE_IMG_URL>https://your.image.url</PROMPT_BOTTLE_IMG_URL>, or using pb_img_url("https://your.image.url") function to get it.

All the Multimodal Tags can be found in prompt_bottle.tags.tags.

Presets

Some common prompt templates are provided in prompt_bottle.presets.

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.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

prompt_bottle-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prompt_bottle-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.20.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for prompt_bottle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31a9796fd18cfc37d23731c140956753f78651561fb125c56ffec1e60c5e8b1f
MD5 623ee076243e0429507a3912a9ff9f55
BLAKE2b-256 3ab850be1ea7994e08ed30e894ec8c73cfac2077677c0e4b662013a9f7b67720

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prompt_bottle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.20.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for prompt_bottle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8b5b25d77deb27b4dc3ba28131772a8a3e49a9126a5684f40daf60bdc5e8a59
MD5 3c70f00022be476174fb5d0ab06eb8da
BLAKE2b-256 cfaeffe89806499dd61dbcbf80ffe5d4d6e88800e4eaa48501f984f46d2fc0fa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page