4mesolutions / php-auto-prompt
PHPAutoPrompt is a PHP class for generating prompts using OpenAI API or Anthropic API.
Requires
- php: >=8.2
- guzzlehttp/guzzle: ^7.0
This package is auto-updated.
Last update: 2025-06-29 02:18:25 UTC
README
The OpenAIPromptDataGenerator is a PHP library designed to generate data examples using AI language models (OpenAI GPT or Anthropic Claude) based on provided configurations and prompts. This tool is useful for creating datasets, testing prompts, and generating diverse examples for various natural language processing tasks.
Table of Contents
Features
- Generate examples using OpenAI GPT or Anthropic Claude models
- Configurable number of examples, model, and output formats
- Support for single-shot and multi-shot generation
- Option to diversify generated examples
- Automatic retrying with exponential backoff for API requests
- Progress bar to track generation process
- Output as serialized PHP array or CSV file
Requirements
- PHP 7.4 or higher
- Composer (for managing dependencies)
- OpenAI API key or Anthropic API key
Installation
-
Clone the repository:
git clone https://github.com/yourusername/OpenAIPromptDataGenerator.git
-
Install dependencies:
composer install
-
Set up your API key:
- For OpenAI: Set the
OPENAI_API_KEY
environment variable - For Anthropic: Set the
ANTHROPIC_API_KEY
environment variable
- For OpenAI: Set the
Usage
- Create a configuration array with your desired settings.
- Instantiate the
Config
class with your configuration. - Create an instance of
OpenAIPromptDataGenerator
using the config. - Call the
generateExamples()
method to generate your data.
Configuration
The Config
class accepts an associative array with the following options:
api_key
: Your API key (optional if set as an environment variable)prompt
: The prompt to use for generationinput_function
: An array describing the input function and its parametersnumber_of_examples
: Number of examples to generate (default: 5)model_name
: The name of the AI model to use (default: 'gpt-3.5-turbo')output_path
: Path to save the serialized output (optional)output_csv_path
: Path to save the CSV output (optional)single_shot
: Whether to use single-shot generation (default: false)diversify
: Whether to diversify the generated examples (default: false)expected_param_name
: The name of the parameter expected in the outputfixed_input
: Fixed input to be added to all generated examples (optional)api_type
: The type of API to use ('openai' or 'anthropic', default: 'openai')
Classes Overview
OpenAIPromptDataGenerator
: The main class that orchestrates the example generation process.Config
: Handles configuration settings for the generator.APIClient
: Manages API interactions with OpenAI or Anthropic, including error handling and retries.DataProcessor
: Prepares messages for API requests and processes the output.Utilities
: Provides utility functions for saving data to files.Example
: Represents a single generated example.ProgressBar
: Displays a progress bar during the generation process.
Example
<?php require_once 'vendor/autoload.php'; $config = new Config([ 'prompt' => 'Generate a random person's name and age.', 'input_function' => [ 'name' => 'generate_person', 'parameters' => [ 'min_age' => 'int', 'max_age' => 'int', ], ], 'number_of_examples' => 10, 'model_name' => 'gpt-3.5-turbo', 'output_csv_path' => 'output.csv', 'expected_param_name' => 'person', 'api_type' => 'openai', ]); $generator = new OpenAIPromptDataGenerator($config); $examples = $generator->generateExamples(); print_r($examples);
This example will generate 10 random person names and ages using the OpenAI GPT-3.5-turbo model and save the results to output.csv
.