A comprehensive Python package for structured prompt engineering techniques
Project description
Proctor AI: A Python Library for Prompt Engineering Techniques
proctor-ai is a comprehensive Python package designed to implement and explore a variety of text-based prompt engineering techniques. It provides a structured way to apply different prompting strategies to interact with Large Language Models (LLMs), using LiteLLM and OpenRouter as the default backend.
The library is based on the hierarchical structure of prompting techniques outlined in the initial project documentation (docs/protoc.md).
Features
- Hierarchical Technique Implementation: Organizes prompting techniques into categories:
- Zero-Shot (e.g.,
EmotionPrompting,RolePrompting,SelfAsk) - Few-Shot (e.g.,
ExampleGeneration,KNN) - Thought Generation (e.g.,
ChainOfThought,ZeroShotCoT,FewShotCoT) - Decomposition (e.g.,
DECOMP) - Self-Criticism (e.g.,
ChainOfVerification) - Ensembling (e.g.,
SelfConsistency)
- Zero-Shot (e.g.,
- Base Classes: Provides
PromptTechniqueas an extensible base class for creating custom techniques. - Composability: Allows combining multiple techniques sequentially using
CompositeTechnique. - LLM Backend: Uses LiteLLM to interact with various LLM APIs, configured for OpenRouter by default.
- Configuration: Easily configure API keys and models via environment variables or a
.envfile. - Utilities: Includes helper functions like
dedent_prompt. - Logging: Integrated logging using
richfor clear, colorized console output showing inputs, prompts, and responses. - Advanced KNN: Optional implementation of KNN technique with proper text embeddings and semantic similarity (requires additional dependencies).
- Error Handling: Robust error handling with automatic retries for transient API errors.
- Performance Optimization: Caching mechanisms for technique instances and embeddings to improve performance.
Installation
-
Clone the repository (if developing):
# Replace with your actual repository URL git clone https://github.com/svngoku/proctor.git # Updated repo URL cd proctor
-
Create and activate a virtual environment:
# Using venv (requires Python 3) python3 -m venv .venv source .venv/bin/activate # Use activate.fish for fish shell # Or using uv # uv venv # source .venv/bin/activate
-
Install the package:
- For usage:
# Install from PyPI pip install proctor-ai # Or using uv uv pip install proctor-ai # Or install directly from GitHub pip install git+https://github.com/svngoku/proctor.git
- For development (from the cloned repo root):
# Use uv for basic installation uv pip install -e . # With advanced KNN features uv pip install -e ".[knn]" # With development tools (pytest, ruff, etc.) uv pip install -e ".[dev]" # With all features and tools uv pip install -e ".[all]" # Or use pip pip install -e ".[all]"
This installs the package in editable mode with your chosen optional dependencies.
- For usage:
Configuration
The library requires an OpenRouter API key to function.
- Create a
.envfile in the root of theproctorpackage directory (i.e.,proctor/.env). - Add your API key:
# proctor/.env OPENROUTER_API_KEY="YOUR_OPENROUTER_API_KEY_HERE" # Optional: Specify a default model to override the default in config.py # OPENROUTER_MODEL="mistralai/mistral-7b-instruct"
The library usespython-dotenvto automatically load these variables when the example scripts are run or when the library is imported.
Usage
See the examples/ directory (proctor/examples/) for detailed usage patterns.
Basic Example:
import os
from dotenv import load_dotenv
from proctor import get_technique, list_techniques
# Load API key from .env file in the current working directory
# Ensure your .env file is in the directory from where you run the script
load_dotenv()
# List available techniques
print("Available techniques:", list_techniques())
# Get a specific technique instance
technique_name = "zero_shot_cot"
cot_technique = get_technique(technique_name)
if cot_technique:
problem = "Explain the theory of relativity in simple terms."
# Generate the prompt (useful for inspection)
prompt = cot_technique.generate_prompt(problem)
print(f"\n--- Generated {technique_name} Prompt ---")
print(prompt)
print("--- End Prompt ---")
# Execute the technique (calls the LLM via LiteLLM/OpenRouter)
# Check if API key is present before executing
if os.environ.get("OPENROUTER_API_KEY") and os.environ.get("OPENROUTER_API_KEY") != "YOUR_API_KEY_HERE":
print(f"\n--- Executing {technique_name} --- ")
response = cot_technique.execute(problem)
print(f"\n--- LLM Response ---")
print(response)
print("--- End Response ---")
else:
print("\nSkipping LLM execution: OPENROUTER_API_KEY not set or is placeholder in .env file.")
else:
print(f"Technique '{technique_name}' not found.")
Using Composite Techniques:
from proctor import CompositeTechnique, RolePrompting, ChainOfThought
# Define a composite technique
expert_cot = CompositeTechnique(
name="Expert Chain-of-Thought",
identifier="custom-expert-cot",
techniques=[
RolePrompting(), # First, set the role
ChainOfThought() # Then, apply structured CoT
]
)
problem = "Plan a three-day trip to Kyoto, Japan, focusing on historical sites."
# Generate the combined prompt
prompt = expert_cot.generate_prompt(problem, role="experienced travel planner")
print(prompt)
# Execute (requires API key)
# response = expert_cot.execute(problem, role="experienced travel planner")
# print(response)
Logging
The library uses Python's logging module configured with rich to provide colorized output in the console. When techniques are executed, you will see:
- The technique being executed (Magenta)
- Input text (Cyan)
- System prompt, if used (Yellow)
- Generated prompt (Blue)
- LLM response (Green)
Set the logging level via environment variable if needed (e.g., LOG_LEVEL=DEBUG).
Quick Start
After installation, here's a simple example to get started:
# Install the package
# pip install proctor-ai
import os
from dotenv import load_dotenv
from proctor import ZeroShotCoT
# Load your API key
load_dotenv()
# Create a technique instance
cot = ZeroShotCoT()
# Use it with any problem
problem = "What are the benefits of renewable energy?"
response = cot.execute(problem)
print(response)
Development
- Clone the repository.
- Set up a virtual environment (see Installation).
- Install in development mode:
make install-devoruv pip install -e ".[dev,all]".
Using the Makefile
The project includes a comprehensive Makefile for development tasks:
# Install dependencies
make install-dev
# Run linting and formatting
make lint
# Run tests
make test
# Run core tests (excluding optional features)
make test-core
# Run tests with coverage
make test-cov
# Check code style without fixing
make check
# Clean build artifacts
make clean
# Build package
make build
# Deploy to Test PyPI
make deploy-test-permissive
# Deploy to Production PyPI
make deploy-prod-permissive
# See all available commands
make help
Manual Commands
If you prefer running commands directly:
# Running Tests
pytest tests/ -v
# Linting
ruff check proctor/ tests/ examples/
ruff format proctor/ tests/ examples/
# Building
python -m build
Deployment
The project uses automated CI/CD via GitHub Actions:
- Pull Requests: Run tests and linting
- Push to main/master: Deploy to Test PyPI automatically
- Tagged releases: Deploy to Production PyPI automatically
Creating a Release
-
Update the version in
pyproject.toml:make version-bump-patch # or version-bump-minor, version-bump-major
-
Commit and push the version change:
git add pyproject.toml git commit -m "Bump version to X.Y.Z" git push
-
Create and push a tag:
git tag v0.1.2 # Replace with your version git push origin v0.1.2
-
GitHub Actions will automatically:
- Run tests
- Build the package
- Deploy to PyPI
Manual Deployment
For manual deployment using the Makefile:
# Deploy to Test PyPI
make deploy-test-permissive
# Deploy to Production PyPI
make deploy-prod-permissive
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run the tests (
make test-core) - Run the linter (
make lint) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Setup
# Clone the repo
git clone https://github.com/svngoku/proctor.git
cd proctor
# Set up development environment
make install-dev
# Run tests to ensure everything works
make test-core
# See all available commands
make help
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
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 proctor_ai-0.1.3.tar.gz.
File metadata
- Download URL: proctor_ai-0.1.3.tar.gz
- Upload date:
- Size: 60.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abd92827cc7f53abdebd1ebf5e657e5634bf26e2ae200e4c25f205bfc1bf8570
|
|
| MD5 |
eb8267950b2d0e30d9b3847e3a5997cb
|
|
| BLAKE2b-256 |
a89f8780af6dfa06814ea070310c455ef208234252d7f3630c50d650a501b5b9
|
File details
Details for the file proctor_ai-0.1.3-py3-none-any.whl.
File metadata
- Download URL: proctor_ai-0.1.3-py3-none-any.whl
- Upload date:
- Size: 57.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87f7c14b1c1d2b22b40caab252295ac2b5d3cd2589a5050ef497041e6ec7e79d
|
|
| MD5 |
c9fe4d54fa80f22e2fbed82a260aac44
|
|
| BLAKE2b-256 |
23f47da4619611f6d32a73aa50a8d37bc117399613bf5267aecb73b4fffc86d5
|