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 can process both single files and entire directories, automatically chunking content and generating multiple instruction-input-output pairs for each chunk.
Features
- 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
- Configurable number of QA pairs per chunk
- 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
llm_model='your-model-selection' # defult using gpt-4o
)
# Generate dataset from a single file
chunks, dataset = ag.generate(
input_path='your_file.txt',
output_path='output.jsonl',
language='zhtw', # or 'en'
entries_per_chunk=3
)
Advanced Configuration
# Process an entire directory with custom settings
chunks, dataset = ag.generate(
input_path='your_directory/',
output_path='output.jsonl',
language='en',
gen_prompt_path='custom_prompt.txt', # Optional: Use custom prompt template
chunk_size=4096, # Customize chunk size
entries_per_chunk=5 # Generate more QA pairs per chunk
)
Understanding Chunks and Dataset
Chunks
Chunks are sections of your input text that have been automatically split for processing. Each chunk contains:
content: The actual text contentsource: The source file pathidx: A formatted string showing the chunk's position (e.g., "01/17" means chunk 1 of 17)
Example chunks:
# Example chunks from a technical document
[
Chunk(
content="Introduction to Machine Learning\nMachine learning is a subset of artificial intelligence...",
source="ml_guide.txt",
idx="01/03"
),
Chunk(
content="Supervised Learning Methods\nIn supervised learning, algorithms learn from labeled data...",
source="ml_guide.txt",
idx="02/03"
),
Chunk(
content="Practical Applications\nMachine learning is used in various fields including...",
source="ml_guide.txt",
idx="03/03"
)
]
Dataset (QA Pairs)
The dataset consists of QA pairs generated from each chunk. Each QA pair contains:
instruction: The task or questioninput: Additional context or input dataoutput: The expected response or answer
Example QA pairs:
[
QAPair(
instruction="Explain the basic concept of machine learning in simple terms",
input="Consider the following introduction to machine learning",
output="Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed. It's similar to how humans learn from experience, but using data and algorithms instead."
),
QAPair(
instruction="What is the main characteristic of supervised learning?",
input="Read about supervised learning methods",
output="The main characteristic of supervised learning is that it uses labeled data for training. This means the algorithm learns from examples where the correct answers are already known, allowing it to make predictions on new, unseen data."
),
QAPair(
instruction="List three practical applications of machine learning",
input="Based on the section about practical applications",
output="Three practical applications of machine learning include: 1) Email spam filtering, 2) Medical diagnosis and image analysis, and 3) Recommendation systems in e-commerce platforms. These applications demonstrate how machine learning can solve real-world problems."
)
]
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
- Start with a small test file before processing large directories
- Monitor the generated output quality
- Adjust chunk size based on your content
- Use appropriate language setting for your source material
- Consider using custom prompts for specific use cases
Limitations
- Requires valid API credentials for OpenAI or Azure OpenAI
- Processing speed depends on API rate limits
- Large directories may take significant time to process
- Memory usage scales with chunk size and batch size
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 alpacagen-0.1.1.tar.gz.
File metadata
- Download URL: alpacagen-0.1.1.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2d6122fd09a55dfbf0fca9c9e9d631d35ce7acd78d343a2d66e3b2b5e0b4919
|
|
| MD5 |
74f11d474c42eaa5a686bd776eb13e77
|
|
| BLAKE2b-256 |
e5a8be5cd47f26b43cf1ce02715f9fb6ab3d55063e88e786832a9d915640809f
|
File details
Details for the file alpacagen-0.1.1-py3-none-any.whl.
File metadata
- Download URL: alpacagen-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5c88a7ead8aa9dc16c3626712108e2a2b19c7feb32cee9507536a778abe5473
|
|
| MD5 |
56187c98997a586c3186bb5b62914335
|
|
| BLAKE2b-256 |
fce495e781df27906f2c55c7d1ce232f6abfa531537879263e9239134ff363d0
|