Generativ AI Prompt Processor
Project description
Generative AI Prompt extensions
A flexible, composable library for constructing and transforming LLM prompts with a fluent interface.
Installation
pip install aipp
Development Setup
uv venv
source .venv/bin/activate
uv sync --group e
pytest -s
code .
Overview
Generative AI Prompt extensions helps you build complex prompts with:
- Chain-of-thought reasoning instructions
- Variable substitution
- Few-shot examples
- Role/persona assignment
- JSON data formatting
- Structured output requirements
- And more...
Quick Start
from aipp import PromptBuilder
# Simple example
prompt = (
PromptBuilder()
.configure_variables(model="GPT-4", task="summarization")
.step_by_step(detailed=True)
.prompt("Create a summary using {model} for {task} task.")
)
print(str(prompt))
Output:
Please provide a detailed, step-by-step explanation with thorough reasoning at each step.
Create a summary using GPT-4 for summarization task.
Features
Variable Substitution
prompt = (
PromptBuilder()
.configure_variables(model="GPT-4", task="summarization", length="short")
.prompt("Create a {length} summary using {model} for {task} task.")
)
JSON Data
data = {
"user": {"name": "Alice", "preferences": ["science", "history"]},
"settings": {"language": "en", "format": "paragraph"}
}
prompt = (
PromptBuilder()
.prompt("Generate content based on user preferences")
.with_json(data)
)
Few-Shot Examples
examples = [
("Summarize: The quick brown fox jumps over the lazy dog.",
"A fox quickly jumps over a dog that is resting."),
("Summarize: The study showed significant results with p<0.05.",
"The research findings were statistically significant.")
]
prompt = (
PromptBuilder()
.prompt("Summarize the following text")
.with_few_shot(examples)
)
Structured Output
schema = {
"type": "object",
"properties": {
"summary": {"type": "string"},
"length": {"type": "integer"},
"topics": {"type": "array", "items": {"type": "string"}}
}
}
prompt = (
PromptBuilder()
.prompt("Analyze the following text")
.structured_output(schema)
)
Chain of Thought
prompt = (
PromptBuilder()
.prompt("Solve this math problem")
.chain_of_thought()
)
Role/Persona Assignment
prompt = (
PromptBuilder()
.prompt("Explain quantum computing")
.as_role("quantum physicist", "with expertise in quantum information theory")
)
Lambda Processors
Use inline lambda functions for custom transformations:
prompt = (
PromptBuilder()
.prompt("Standard prompt text")
.process(lambda content, _: f"IMPORTANT: {content}")
.process(lambda content, _: f"{content}\n\nPlease be thorough.")
)
Combining Processors
Processors can be combined in any order:
prompt = (
PromptBuilder()
.prompt("Summarize the following article")
.chain_of_thought()
.with_few_shot(examples)
.structured_output(schema)
.as_role("professional writer")
.process(lambda content, _: f"{content}\n\nLimit response to 3 paragraphs.")
)
Advanced Usage
Custom Processors
Create your own processors:
from aipp.processors.base import SimpleProcessor
def create_custom_processor(options):
def transform(content, context=None):
# Transform the content based on options
return f"Custom prefix: {content}"
return SimpleProcessor(transform, name="custom_processor")
prompt = (
PromptBuilder()
.prompt("Base content")
.add_processor(create_custom_processor({"option": "value"}))
)
Processing Context
Processors can share context:
def create_metadata_processor():
def transform(content, context=None):
if context is not None:
context['processed_at'] = "2023-01-01"
return content
return SimpleProcessor(transform, name="metadata")
prompt = PromptBuilder()
prompt.add_processor(create_metadata_processor())
prompt.prompt("Content")
print(prompt.metadata) # Contains processed_at
Contributing
We welcome contributions to improve Generative AI Prompt extensions! See CONTRIBUTING.md for detailed information on:
- Architecture overview
- Core concepts
- How to extend the library
- Design patterns used
- Best practices
- Testing guidelines
Please submit issues and pull requests following our guidelines.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 aipp-0.1.3.tar.gz.
File metadata
- Download URL: aipp-0.1.3.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd0c0df7944f4ead741c28f00d44c966a214f94b58c7e1e04e1bb806260a50a0
|
|
| MD5 |
4da2ccde7f46d0d52243e7399022ba3b
|
|
| BLAKE2b-256 |
3c18bdcac6c9e87be455e45af42162e3101a7439843a928e3b4411792302cb46
|
File details
Details for the file aipp-0.1.3-py3-none-any.whl.
File metadata
- Download URL: aipp-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95e5b8caedb4f8f437174f29e1ec35f0345fffb4fece15034d870edad7d63a85
|
|
| MD5 |
b7e614fd86b00aa189ddd1128c66210b
|
|
| BLAKE2b-256 |
2887349c4075c37112d402b82e91fb61159f5339d79c1a81ba003373ab10c224
|