Skip to main content
Model selection tuning enables you to automatically identify the best performing model from a set of candidates based on evaluation metrics. This method is ideal when you have multiple models available and want to systematically compare them against your specific tasks, cost constraints, and latency requirements — without manual trial-and-error. You will learn the following core features step by step:

Core Components

The model selection process involves three core components that work together:
  • Workflow Function: An async function that executes your agent logic with a given model and returns the result.
  • Judge Function: Evaluates the workflow output and returns a reward indicating performance (higher is better).
  • Task Dataset: A collection of tasks for evaluating and comparing models.
WorkflowOutput and JudgeOutput are framework-provided data classes. Your workflow and judge functions must return instances of these types. Do not define your own output classes.

Prerequisites

Before running the examples, install the required dependencies and set up your API key:
Never commit your API key to version control. Use environment variables or a .env file (with python-dotenv) for local development.

Setup & Configuration

Define your candidate models that will be evaluated:

Defining the Workflow Function

The workflow function executes your agent logic with a given model and returns a standardized result. It must:
  • Accept a task (e.g., a question or input) and a model instance
  • Run inference using that model
  • Return a WorkflowOutput object containing the model’s response
This pattern is identical to the one introduced in Overview. For a complete implementation example — including how to set up an agent, format messages, and return structured output — please refer to that guide.
Your function must return WorkflowOutput. Do not define custom output classes.

Implementing the Judge Function

The judge function evaluates the output of the workflow and assigns a numerical reward (higher = better) along with optional diagnostic metrics. It must:
  • Accept the original task and the response from the workflow
  • Compute a scalar reward (e.g., accuracy, BLEU score, or inverse latency)
  • Return a JudgeOutput object with reward and metrics
This follows the same contract described in Overview. That guide provides a step-by-step example of building a correctness-based judge.

Using Built-in Judges

AgentScope provides built-in judge functions for common efficiency metrics:

Running Model Selection

With your components defined, run the model selection process:
Key configurations include:
  • workflow_func: The workflow function that executes tasks with different models.
  • judge_func: The judge function that evaluates performance.
  • train_dataset: Configuration for the evaluation dataset.
  • candidate_models: List of models to compare.

Supported Dataset Formats

DatasetConfig supports multiple data sources:

Minimal JSON Example (tasks.json)

Complete Examples

Example 1: Token Usage Optimization

This example selects the best model based on token consumption:

Example 2: Translation Quality with BLEU Score

This example selects the best model for translation tasks based on BLEU score:

Key Benefits

Performance optimization

Identify the model that achieves the highest accuracy on your specific task.

Cost efficiency

Select models that achieve desired performance with lower computational costs.

Latency control

Choose models that meet your speed constraints without sacrificing quality.

Resource awareness

Find the best model that fits within your infrastructure limitations.

Best Practices

Start small to save cost and time: Model selection evaluates every candidate model on every task. Use total_steps=10 in DatasetConfig for initial testing — a full run with 3 models and 100 tasks may cost 300× a single inference.
  • Choose appropriate metrics: Align your judge function with your actual goals (accuracy, efficiency, cost, etc.)
  • Monitor detailed metrics: Use detailed metrics to understand the trade-offs between different models
  • Validate results: Manually check a few outputs from your selected model to ensure quality meets expectations