Skip to main content

A comprehensive Python package for structured prompt engineering techniques

Project description

Proctor: A Python Library for Prompt Engineering Techniques

Proctor Logo

PyPI version CI

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)
  • Base Classes: Provides PromptTechnique as 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 .env file.
  • Utilities: Includes helper functions like dedent_prompt.
  • Logging: Integrated logging using rich for 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

  1. Clone the repository (if developing):

    # Replace with your actual repository URL
    git clone https://github.com/svngoku/proctor.git # Updated repo URL
    cd proctor
    
  2. 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
    
  3. 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.

Configuration

The library requires an OpenRouter API key to function.

  1. Create a .env file in the root of the proctor package directory (i.e., proctor/.env).
  2. 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 uses python-dotenv to 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

  1. Clone the repository.
  2. Set up a virtual environment (see Installation).
  3. Install in editable mode: uv pip install -e ..
  4. 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

proctor_ai-0.1.1.tar.gz (54.9 kB view details)

Uploaded Source

Built Distribution

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

proctor_ai-0.1.1-py3-none-any.whl (50.3 kB view details)

Uploaded Python 3

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

Hashes for proctor_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 79956a1a641608e5358c0955e93df1e7fcd6d2908d901184c92a70b0e32f897c
MD5 944b8bcde2d4dcd998bdf4b8408d6848
BLAKE2b-256 90d9c63bb0ef6f53c478f3692a77528d513209d792dda74c98484daa6ba293a3

See more details on using hashes here.

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

Hashes for proctor_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1708bd14da681fa80a215c38bf1dbc344db6ed5a965291b907267c69ef353af6
MD5 8c2946c67e377ef34896fde23463a7c0
BLAKE2b-256 88d329421c5f99afcd55ff3ced2ba3886345ca207076cbe8a402ea85805c2c66

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