Skip to main content

A framework for automated LLM prompt optimization.

Project description

Prompt Forge

简体中文 | English

Prompt Forge is a Python framework designed for the automated optimization of prompts for large language models (LLMs). Inspired by the iterative training processes used in machine learning, Prompt Forge aims to systematically discover highly effective prompts based on evaluation metrics and datasets, particularly for scenarios involving API-only LLM access.

This framework helps users move beyond manual prompt tuning, which can be time-consuming and prone to suboptimal results, especially on large datasets. By treating prompt structures as architectures and using an LLM-based optimizer, Prompt Forge iteratively refines prompts to maximize performance according to user-defined metrics.

Features (Planned/Included)

  • Structured Prompt Templates: Define prompts using composable sections (e.g., Role, Task, Constraints, Examples). Includes built-in templates (RCTCRE, APE, CRISPE, BROKE).
  • Automated Optimization Loop: Mimics ML training with epochs and batches.
  • Two-Step LLM-Based Optimizer: Utilizes a powerful LLM (OptimizerLLM) to first analyze feedback and generate optimization directions, then apply those directions to modify the prompt, using structured JSON communication.
  • Configurable Update Granularity: Control the scope and intensity of prompt changes (from micro-adjustments to full rewrites).
  • Extensible Components: Easily add custom datasets, LLM clients, evaluation metrics, prompt templates, and update strategies.
  • Multi-Metric Evaluation: Define and weight multiple metrics to guide optimization towards complex goals, returning detailed results.
  • API-Only Focus: Designed to work with LLMs accessible only via APIs.

Installation

pip install ptforge

Quick Start

import os
import logging
from ptforge import (
    PromptOptimizer, OptimizationConfig, get_template, JsonlDataset,
    ExactMatchAccuracy, Evaluator, LLMBasedUpdater, OpenAIClient, UpdateGranularity
)

# Configure logging
logging.basicConfig(level=logging.INFO)

# Ensure API keys are set (e.g., via environment variables)
# os.environ["OPENAI_API_KEY"] = "your-key"
# os.environ["OPENAI_API_BASE"] = "your-base-url-if-needed" # Optional

try:
    # 1. Setup Components
    target_llm = OpenAIClient(model="gpt-3.5-turbo") # Or your preferred model/client
    optimizer_llm = OpenAIClient(model="gpt-4o")    # Powerful model recommended for optimization

    dataset = JsonlDataset("examples/dummy_data.jsonl") # Path relative to execution

    initial_template = get_template(
        "RCTCRE",
        initial_values={
            "role": "Assistant answering questions.",
            "task": "Answer concisely: {{input}}",
            "context": "General knowledge question.",
            "constraints": None,
            "response_format": "Plain text.",
            "examples": None,
        },
        optimizable_sections={"role", "task", "constraints", "context", "response_format"}
    )

    evaluator = Evaluator(metrics=[(ExactMatchAccuracy(case_sensitive=False), 1.0)])

    updater = LLMBasedUpdater(
        optimizer_llm_client=optimizer_llm,
        task_description="Answer general knowledge questions concisely and accurately."
    )

    config = OptimizationConfig(epochs=2, batch_size=4, update_granularity=UpdateGranularity.SECTION_REPHRASE)

    # 2. Initialize Optimizer
    optimizer = PromptOptimizer(target_llm, initial_template, dataset, evaluator, updater, config)

    # 3. Run Optimization
    # Use context managers if your client supports them (OpenAIClient does)
    with target_llm, optimizer_llm:
        best_template, result = optimizer.optimize()

    # 4. Use Results
    print(f"Optimization finished. Best score: {result.best_score}")
    print("Best template sections:")
    for section in best_template.list_sections():
        print(f"--- {section} ---")
        print(best_template.get_section(section) or "(Empty/None)")

except Exception as e:
    logging.error(f"Optimization failed: {e}", exc_info=True)

Contributing

Contributions are welcome! Please refer to the contributing guidelines.

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

ptforge-0.1.3.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

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

ptforge-0.1.3-py3-none-any.whl (48.4 kB view details)

Uploaded Python 3

File details

Details for the file ptforge-0.1.3.tar.gz.

File metadata

  • Download URL: ptforge-0.1.3.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for ptforge-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8795adc11e299a3234cd233bf4e7c9e3e994a1ff9490a903257f4e578d62b262
MD5 d6a607159aa8957dac5c998571e5efc9
BLAKE2b-256 c4bc5aa684669f5aa63814a8934101d937df3e8f45eb16c120587b7d0368aea7

See more details on using hashes here.

File details

Details for the file ptforge-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ptforge-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 48.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for ptforge-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 81e2338fe6f5df08141118c73af7c55c5c7c38cd3c30e93cd3b2d6f57376b6c7
MD5 7dc6eeb95b7f13c29cb492592bf71f52
BLAKE2b-256 8d542241fdb67c1a1ae43a5879b3646bb8220397f8e573cfc0e31300c63eddf6

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