A comprehensive Python package for structured prompt engineering techniques
Project description
Proctor: A Python Library for Prompt Engineering Techniques
proctor 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 (once published) # uv pip install proctor # Or install directly from GitHub (replace with your repo URL) # uv 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).
Development
- Clone the repository.
- Set up a virtual environment (see Installation).
- Install in editable mode:
uv pip install -e .. - Install development dependencies (if any are added later, e.g., for testing):
# Example: uv pip install -e ".[dev]"
Running Tests (Placeholder)
# pytest
Linting (Placeholder)
# ruff check .
# ruff format .
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details (if one is added, or refer to pyproject.toml).
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.1.tar.gz.
File metadata
- Download URL: proctor_ai-0.1.1.tar.gz
- Upload date:
- Size: 54.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79956a1a641608e5358c0955e93df1e7fcd6d2908d901184c92a70b0e32f897c
|
|
| MD5 |
944b8bcde2d4dcd998bdf4b8408d6848
|
|
| BLAKE2b-256 |
90d9c63bb0ef6f53c478f3692a77528d513209d792dda74c98484daa6ba293a3
|
File details
Details for the file proctor_ai-0.1.1-py3-none-any.whl.
File metadata
- Download URL: proctor_ai-0.1.1-py3-none-any.whl
- Upload date:
- Size: 50.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1708bd14da681fa80a215c38bf1dbc344db6ed5a965291b907267c69ef353af6
|
|
| MD5 |
8c2946c67e377ef34896fde23463a7c0
|
|
| BLAKE2b-256 |
88d329421c5f99afcd55ff3ced2ba3886345ca207076cbe8a402ea85805c2c66
|