Skip to main content

Type-safe prompt template builder for LLM APIs

Project description

philiprehberger-prompt-builder

Tests PyPI version License

Type-safe prompt template builder for LLM APIs.

Installation

pip install philiprehberger-prompt-builder

Usage

Fluent Builder

from philiprehberger_prompt_builder import Prompt

messages = (
    Prompt()
    .system("You are a helpful assistant.")
    .user("Summarize this article: {article}")
    .render(article="Long article text here...")
)
# [{"role": "system", "content": "You are a helpful assistant."},
#  {"role": "user", "content": "Summarize this article: Long article text here..."}]

Few-Shot Examples

messages = (
    Prompt()
    .system("Classify the sentiment.")
    .example(user="I love this!", assistant="positive")
    .example(user="Terrible product.", assistant="negative")
    .user("{text}")
    .render(text="Pretty good actually")
)

Reusable Templates

from philiprehberger_prompt_builder import PromptTemplate

summarizer = PromptTemplate(
    system="You are a {tone} summarizer. Output in {format}.",
    user="Summarize: {content}",
    defaults={"tone": "concise", "format": "bullet points"},
)

# Use with defaults
messages = summarizer.render(content="Article text...")

# Override defaults
messages = summarizer.render(content="...", tone="detailed", format="paragraphs")

# Create variant
verbose = summarizer.extend(tone="thorough", format="essay")

Token Estimation

prompt = Prompt().system("...").user("{text}")
estimated = prompt.estimate_tokens(text="Hello world")

API

Method Description
.system(content) Add a system message
.user(content) Add a user message
.assistant(content) Add an assistant message
.example(user, assistant) Add a few-shot example pair
.render(**kwargs) Render with variable substitution → list of dicts
.estimate_tokens(**kwargs) Rough token count (~4 chars/token)

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_prompt_builder-0.1.7.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file philiprehberger_prompt_builder-0.1.7.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_prompt_builder-0.1.7.tar.gz
Algorithm Hash digest
SHA256 df5d0900a4879ac60f1293589711c40b28ec241a508f5195a2dee10b96ea0f80
MD5 4c1cfef2a520e3f95844b721191edd07
BLAKE2b-256 cbf5f95a83d752adc3a1ea5528ae1b202fcd47fe735bd300cc6222e3d69c1061

See more details on using hashes here.

File details

Details for the file philiprehberger_prompt_builder-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_prompt_builder-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4f90eea0b5800c2665dbbf79875e267cf43d404f9f9ab2252dc8a17df9ee94
MD5 a52e47ace801145e9127135ffff3081c
BLAKE2b-256 b69ddb71424c8e788f74c581d542164ac28212aa3b350c759b9019dde5e35c55

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