Automatic Prompt Selection for Large Language Models

Viet-Tung Do, Xuan-Quang Nguyen, Van-Khanh Hoang, Duy-Hung Nguyen, Shahab Sabahi, Jeff Yang, Hajime Hotta, Minh-Tien Nguyen, Hung Le

TL;DR

This paper introduces Automatic Prompt Selection (APS), a novel method to automatically find the best prompt for a given input to a Large Language Model (LLM). APS balances prompt generality and specificity by clustering data, generating prompts per cluster, and training a lightweight prompt evaluator, achieving competitive zero-shot QA performance without expensive LLM fine-tuning.

What Problem Does This Paper Address?

Large Language Models (LLMs) are incredibly powerful, but their performance heavily relies on the quality of the prompts provided. Crafting these prompts manually is a time-consuming and labor-intensive process. While automatic prompt optimization methods exist, they often struggle to balance prompt generality (one prompt for all inputs) with specificity (a unique prompt for each input), leading to either inflexibility or an explosion in the search space. Furthermore, many existing methods are computationally expensive, requiring extensive LLM interactions during training or inference. This paper aims to address these limitations by proposing an efficient and flexible approach to automatically select the optimal prompt for a given input.

Methods

The proposed Automatic Prompt Selection (APS) method consists of three main steps:

  1. Prompt Database Generation:
    • Clustering: The training data is first clustered into coherent groups using Sentence-Transformers for embeddings and K-Means for clustering. The intuition is that similar inputs should benefit from similar prompts.
  2. Meta-Prompt Generation: For each cluster, an LLM-based prompt generator (A1) is used to create a set of diverse candidate prompts. This is done by constructing a "meta-prompt" with a few example input-output pairs from the cluster and instructing the LLM to generate prompts. Duplicate prompts are filtered out. The union of all generated prompts forms a versatile prompt database.
  3. Prompt Evaluator Training:
    • Data Synthesis: A second LLM (A2) is used to generate synthetic data. For a given input (question-context pair) and its ground-truth answer, the LLM generates outputs using both "good" prompts (those that produce answers similar to the ground truth) and "bad" prompts (those that produce different answers).
  4. Preference Learning: A prompt evaluator model (E) is trained on this synthetic data using a preference loss. The evaluator learns to assign higher scores to prompts that lead to outputs closer to the ground truth and lower scores to prompts that lead to dissimilar outputs. This training is done offline and does not require fine-tuning the downstream LLM.
  5. Prompt Ranking:
    • Inference: At test time, given a new input (question-context pair), the prompt evaluator (E) is used to score all candidate prompts in the pre-generated database.
  6. Selection: The prompt with the highest score is selected as the optimal prompt for that specific input. The paper also explores a voting mechanism where the top-k highest-scoring prompts are used, and the final answer is determined by a majority vote, which can improve robustness.

Datasets

The paper evaluates its approach on three challenging question-answering datasets:

  • GSM8K: A dataset of high-quality, linguistically diverse grade school math word problems requiring multi-step arithmetic calculations.
  • MultiArith: A subset of the Mawps repository, containing mathematical problems that require complex arithmetic operations and logical reasoning.
  • AQUA: A dataset of algebraic word problems with answers and natural language rationales, including multiple-choice options.

Key Insights and Ideas

  • Clustering for Prompt Specificity: Grouping similar inputs and generating prompts tailored to each cluster is crucial for balancing prompt generality and specificity, leading to better performance than a single prompt for all inputs.
  • Prompt Evaluator as a Lightweight Alternative: Training a dedicated prompt evaluator model significantly reduces computational cost and latency compared to methods that require querying the downstream LLM for every candidate prompt during training or inference.
  • Preference Learning for Prompt Quality: Using a preference loss to train the evaluator, where it learns to distinguish between good and bad prompts based on their output quality, is an effective way to learn prompt relevance without needing exact score labels.
  • Voting for Robustness: Employing a voting mechanism with the top-k selected prompts can further enhance performance and robustness by mitigating potential biases or outliers from individual prompts.

Key Results

  • The proposed APS method consistently outperforms several strong baselines across all three QA datasets.
  • On GSM8K, APS achieves an accuracy of 81.49%, outperforming the second-best baseline by 1.28%.
  • On MultiArith, APS achieves a perfect score of 100%, surpassing baselines by up to 0.55%.
  • On AQUA, APS achieves 64.57% accuracy, a 2.76% improvement over the next best baseline.
  • The voting mechanism (top-5 voting) shows consistent improvement over no voting, with gains of approximately 2% on GSM8K, 0.5% on MultiArith, and 1% on AQUA.
  • The cost of training APS is estimated to be between $5-$10 for prompt and synthetic data generation, and around $10 for evaluation on all test sets, making it cost-effective.

Limitations and Future Work

  • Prompt Duplication and Diversity: While efforts are made to eliminate exact prompt matches, semantically equivalent prompts with minor token variations might persist, potentially diminishing prompt database diversity. The authors suggest assessing semantic similarity for future improvements.
  • Hyperparameter Tuning: The approach involves several hyperparameters, and tuning them can be time-consuming. Practitioners are encouraged to explore varying hyperparameter values for their specific tasks.
  • Future Work: The authors plan to extend their method to few-shot in-context learning and apply it to various NLP tasks beyond question answering. They also aim to explore ways to further improve prompt quality and diversity.

Summary updated

BibTeX
@inproceedings{do2025automatic,
  author = {Viet-Tung Do and Xuan-Quang Nguyen and Van-Khanh Hoang and Duy-Hung Nguyen and Shahab Sabahi and Jeff Yang and Hajime Hotta and Minh-Tien Nguyen and Hung Le},
  title = {{Automatic Prompt Selection for Large Language Models}},
  booktitle = {PAKDD},
  year = {2025},
  url = {https://openreview.net/forum?id=MhP45xYcvb},
}
Figure