Skip to main content

A tool for generating instruction-following datasets in Alpaca format

Project description

AlpacaGen

AlpacaGen is a powerful tool for generating instruction-following datasets in the Alpaca format using large language models (LLMs). It supports processing of various file types (PDF, DOCX, TXT, etc.) and can handle both individual files and entire directories, automatically chunking content and generating multiple instruction-input-output pairs for each chunk.

Features

  • Process multiple file formats (PDF, DOCX, TXT, and more)
  • Recursive directory processing - automatically handles all supported files in a directory
  • Support for multiple LLM providers (Azure OpenAI, OpenAI)
  • Multilingual support (Traditional Chinese, English)
  • Automatic text chunking with customizable size and overlap
  • Batch processing with progress bars
  • Source tracking for generated QA pairs
  • JSONL output format

Installation

pip install alpacagen

Usage

Basic Usage

from alpacagen import AlpacaGen

# Initialize AlpacaGen
ag = AlpacaGen(
    llm_client='azure',  # or 'openai'
    api_key='your-api-key',
    base_url='your-api-base-url'  # Required for Azure OpenAI
)

# Process a single file
chunks = ag.get_chunks('documentation.pdf')

# Or process an entire directory
chunks = ag.get_chunks('path/to/your/docs/')  # Will process all supported files in the directory

# Generate dataset from chunks
dataset = ag.get_datasets(
    chunks,
    language='zhtw',  # or 'en'
    entries_per_chunk=3,
    output_path='output.jsonl'
)

Processing Different File Types

AlpacaGen can handle various file formats:

# PDF documents
chunks = ag.get_chunks('technical_manual.pdf')

# Word documents
chunks = ag.get_chunks('specifications.docx')

# Text files
chunks = ag.get_chunks('notes.txt')

# Mixed directory
chunks = ag.get_chunks(
    'project_docs/',  # Contains PDFs, DOCXs, TXTs
    chunk_size=4096
)

Advanced Configuration

# Process with custom settings
ag = AlpacaGen(
    llm_client='azure',
    api_key='your-api-key',
    base_url='your-api-base-url',
    llm_model='your-model-selection'  # default is gpt-4o
)

# Generate chunks with custom size
chunks = ag.get_chunks(
    input_path='docs_directory/',  # Can be directory or single file
    chunk_size=4096
)

# Generate dataset with custom prompt
dataset = ag.get_datasets(
    chunks,
    language='en',
    gen_prompt_path='custom_prompt.txt',  # Optional: Use custom prompt template
    entries_per_chunk=5,  # Generate more QA pairs per chunk
    output_path='output.jsonl'
)

Understanding Data Structures

Chunks

Chunks represent sections of your input text that have been automatically split for processing. Each chunk contains:

  • content: The actual text content
  • source: The source file path (preserves original file path for directory processing)
  • idx: A formatted string showing the chunk's position (e.g., "01/17" means chunk 1 of 17)

Example chunk structure:

@dataclass
class Chunk:
    content: str
    source: str  # e.g., "project_docs/specifications.docx"
    idx: str     # format: "01/17"

QA Pairs

The dataset consists of QA pairs generated from each chunk. Each QA pair contains:

  • instruction: The task or question
  • input: Additional context or input data
  • output: The expected response or answer
  • source: Reference to the original chunk that generated this pair

Example QA pair structure:

@dataclass
class QAPair:
    instruction: str
    input: str
    output: str
    source: Chunk  # Maintains link to original file and position

Example dataset output:

[
    QAPair(
        instruction="What are the key components of a distributed system?",
        input="Based on the chapter about distributed systems",
        output="The key components include nodes, communication networks, middleware, and coordination mechanisms.",
        source=Chunk(
            content="Chapter 1: Distributed Systems\nA distributed system consists of...",
            source="project_docs/distributed_systems.pdf",
            idx="01/12"
        )
    )
]

Configuration Options

  • llm_client: Choose between 'azure' or 'openai'
  • llm_model: Specify custom model (defaults available for each client)
  • chunk_size: Control the size of text chunks (default: 4096)
  • entries_per_chunk: Number of QA pairs to generate per chunk (default: 3)
  • language: Choose between 'zhtw' (Traditional Chinese) or 'en' (English)

Best Practices

  1. Start with a small test file before processing large directories
  2. Organize your input files in a structured directory
  3. Monitor the generated output quality
  4. Adjust chunk size based on your content type and length
  5. Use appropriate language setting for your source material
  6. Consider using custom prompts for specific domains or file types
  7. Keep track of source chunks for better data traceability

Recent Updates

  • Added support for processing entire directories recursively
  • Enhanced file type support (PDF, DOCX, TXT, etc.)
  • Added source tracking for QA pairs through the new source attribute
  • Updated chunking mechanism with improved overlap handling
  • Enhanced dataset generation with better error handling
  • Simplified API with separate get_chunks and get_datasets methods

Contact Me

Have questions or suggestions? Feel free to reach out!

Email: qqandy0120@gmail.com

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

alpacagen-1.1.1.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

alpacagen-1.1.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file alpacagen-1.1.1.tar.gz.

File metadata

  • Download URL: alpacagen-1.1.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for alpacagen-1.1.1.tar.gz
Algorithm Hash digest
SHA256 53fbd985bcd21ecbb031887b678df4ef2d3dafc13b3b92c449fc8d0639da48f4
MD5 089838b3abe0c952b0ca22a49d4df78b
BLAKE2b-256 effb7f05132b7c9c3d3f6f129f38d3384d739e5a00560b5dfa4259be1f4f3bab

See more details on using hashes here.

File details

Details for the file alpacagen-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: alpacagen-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for alpacagen-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ad36d8328d9702a6615b63c8a4574617fa29d97026d04d763e22509c20e4f36
MD5 3d68b4a98dc7fa1b421ede37fcc94d41
BLAKE2b-256 c40c15ff0b28ee2d12661851140b8ff7df0861e81533dbdaf3ea26b5542f7774

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