Add your description here
Project description
broprompt
Lightweight Python library for prompt template management with dynamic parameter handling.
Features
- Load prompt templates from markdown files
- Dynamic parameter access via dot notation
- Template + parameter combination into final prompt strings
- Export/import parameters as dictionaries
- Function-to-tool conversion utilities
- Parameter validation and extraction
- Examples repository with prompt techniques and integration patterns
Usage
from broprompt.prompt_engineering import Prompt
# Load template from markdown file
prompts = Prompt.from_markdown("system_prompt.md")
# Set parameters
prompts.params.role = "assistant"
prompts.params.domain = "coding"
# Get final prompt string
final_prompt = prompts.str
# Export parameters
params_dict = prompts.to_dict()
# Import parameters
prompts.from_dict({"role": "expert", "tone": "professional"})
Tools Module
from broprompt import Prompt, list_tools, parse_codeblock_to_dict, get_yaml_function_definition, validate_parameters
# Define functions with <|start|><|end|> tokens for descriptions
def save_file(filename: str, content: str):
"""
<|start|>This will be used only when a user ask for saving something<|end|>
Args:
filename (str): The name of the file to save.
content (str): The content to write into the file.
"""
return f"File {filename} saved with content length {len(content)}"
def add_calendar(date: str, time: str, description: str):
"""
<|start|>This will be used only when a user ask for adding something to their calendar<|end|>
Args:
date (str): The date to add to the calendar in YYYY-MM-DD format.
time (str): The time to add to the calendar in HH:MM format.
description (str): A brief description of the event.
"""
return f"Event '{description}' added to calendar on {date} at {time}"
tools = [save_file, add_calendar]
# Load tool selector prompt and populate with tools
tool_selector_prompt = Prompt.from_markdown("./tool_selector.md")
tool_selector_prompt.from_dict({"tools": list_tools(tools)})
# Select tool based on user message
message = "I have to visit my aunt on 2024-10-01 at 15:00. Add it to my calendar"
tool_response = model.run(system_prompt=tool_selector_prompt.str, messages=[model.UserMessage(message)])
tool = parse_codeblock_to_dict(tool_response, codeblock="yaml")
# Get selected tool and its YAML definition
selected_tool = [t for t in tools if t.__name__ == tool.get("tool", "nope")][0]
tool_definition = get_yaml_function_definition(selected_tool)
# Extract parameters using tool call prompt
tool_call_prompt = Prompt.from_markdown("./tool_call.md")
tool_call_prompt.from_dict({"definition": tool_definition})
params_response = model.run(tool_call_prompt.str, [model.UserMessage(message)])
params = parse_codeblock_to_dict(params_response, codeblock="yaml")
# Validate and execute
is_valid, error = validate_parameters(params, selected_tool)
if is_valid:
result = selected_tool(**params)
Structured Output
from broprompt import Prompt, parse_codeblock_to_dict, get_yaml_schema_definition
from pydantic import BaseModel, Field
from typing import Literal
# Define Pydantic models
class User(BaseModel):
"""This is a user model that contains name and age."""
name: str = Field(..., description="the name of the user")
age: int = Field(..., description="the age of the user")
class Sentiment(BaseModel):
"""This is a sentiment model that extract sentiment from INPUT with its explanation."""
sentiment: Literal["positive", "neutral", "negative"] = Field(..., description="read INPUT and classify the sentiment")
evidence: str = Field(..., description="provide evidence from INPUT to support the sentiment")
explanation: str = Field(..., description="what is the reason for the sentiment")
# Generate YAML schema definitions
user_schema = get_yaml_schema_definition(User, as_array=True) # For multiple users
sentiment_schema = get_yaml_schema_definition(Sentiment, as_array=False) # For single sentiment
# Load structured output prompt template
so_prompt = Prompt.from_markdown("./structured_output.md")
so_prompt.from_dict({"definition": user_schema})
# Extract structured data
response = model.run(system_prompt=so_prompt.str, messages=[model.UserMessage("Jake is 30 years old, but Kate is 5 year younger than him.")])
users_data = parse_codeblock_to_dict(response)
users = [User(**item) for item in users_data]
Context Module
from broprompt.context import Context
# Create a context (LangChain Document equivalent)
ctx = Context(
context="This is the document content",
metadata={"source": "file.txt", "type": "text"}
)
# Access content
print(ctx.context) # Direct access
# Automatic fields
print(ctx.id) # Auto-generated UUID
print(ctx.created_at) # UTC timestamp
Template Format
Use {parameter_name} placeholders in your markdown files:
# System Prompt
You are {role}, specialized in {domain}.
Respond in {tone} tone.
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
broprompt-0.1.5.tar.gz
(9.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file broprompt-0.1.5.tar.gz.
File metadata
- Download URL: broprompt-0.1.5.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b00930dcd0a79ba209ab9b83734de6d5a3467f91f80bc8b8e4f88d2b9328f9e
|
|
| MD5 |
3564e5613d74e7435bdaa3f965d9c87c
|
|
| BLAKE2b-256 |
cccb844548ec1bf6909e6b52248e26a5a49e7a24230de3d737f5e63db5c012fb
|
File details
Details for the file broprompt-0.1.5-py3-none-any.whl.
File metadata
- Download URL: broprompt-0.1.5-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12ef093d48748ee890e93dfe1251924338c4d64baeaee647156ec215386f3b0a
|
|
| MD5 |
d39272f7a39148c941ae71034057c8a0
|
|
| BLAKE2b-256 |
6ec830b56aee5e081ff71e9d44576e6c8a5b73e6c773b9970a75fc7ccb98a495
|