Evaluator uses to score your prompts, guiding the optimizer towards better performance. A high-quality, representative dataset is the single most important factor for a successful optimization run.
This cookbook demonstrates how to prepare and use datasets from different sources with the agent-opt library.
The Required Data Format: A List of Dictionaries
Regardless of the source, theagent-opt library expects your final dataset to be a Python list of dictionaries. Each dictionary in the list represents a single data point or “row.” The keys of the dictionary are the column names, and the values are the corresponding data.
1. Creating In-Memory Datasets
The simplest way to get started, especially for quick tests or small experiments, is to define your dataset directly in your Python script.Example: A Simple Q&A Dataset
2. Importing Datasets from Files
For larger datasets, you’ll typically load them from files. We recommend using thepandas library as it provides a simple and powerful way to read various formats and convert them into the required list of dictionaries.
a. From a CSV File
This is the most common format. Assuming you have adata.csv file:
pandas:
b. From a JSON File (List of Objects)
If yourdata.json file is a list of objects, you can use either pandas or the built-in json library.
c. From a JSONL File (JSON Lines)
For very large datasets, the JSON Lines (.jsonl) format is common, where each line is a separate JSON object. pandas handles this seamlessly.
3. The DataMapper: Connecting Your Dataset to the Optimizer
The DataMapper is a crucial component that acts as a “translator.” It tells the optimizer and evaluator how to use the columns from your dataset.
You define this translation with a key_map dictionary:
- The keys are the generic names that the
Evaluatorexpects (e.g.,response,context). - The values are the specific column names from your dataset (e.g.,
ground_truth_answer,article_text).
Example
Imagine your dataset has columnsarticle_text and ideal_summary, and you are using the summary_quality evaluator, which expects inputs named input and output.
4. Putting It All Together: A Complete Example
This example shows the full workflow, from loading a dataset to running an optimization.Extras: Handling Large Datasets
Running optimization on a very large dataset can be slow and expensive. Theagent-opt optimizers are designed to work effectively with a representative sample of your data.
You can easily sample your dataset after loading it.
A good sample size for most optimizers is between 30 and 200 examples. This provides a strong enough signal for improvement without excessive cost.
Best Practices for Datasets
Quality over Quantity
Quality over Quantity
A small, high-quality, and diverse dataset of 20-50 examples is often more effective than a large, noisy dataset of thousands of examples. Ensure your ground-truth answers are accurate and consistent.
Represent Edge Cases
Represent Edge Cases
Your dataset should include examples of tricky or unusual inputs that your initial prompt struggles with. The optimizer will use these “hard cases” to learn how to make the prompt more robust.
Keep Column Names Simple
Keep Column Names Simple
Use simple, descriptive column names in your source files (e.g.,
question, context, summary) to make mapping easier. Avoid spaces or special characters in column headers.