Skip to main content
This tutorial focuses on the Prompt Tuning method, which optimizes your agent’s system prompt without modifying model weights. This is a lightweight alternative to model weight tuning, ideal for rapid prototyping and scenarios with limited computational resources.

Prerequisites

Before proceeding, ensure you have:
  • AgentScope v1.0.14 or higher
  • A DashScope API key (or another supported provider)
Install the required dependencies:
Set your API key:
Never commit your API key to version control. Use environment variables or a .env file (with python-dotenv) for local development.

Refine Workflow Function

Continuing from Overview, assume you have a task dataset and judge function ready. Now, refine your workflow function for prompt tuning:
The workflow function for prompt tuning accepts system_prompt (a str) instead of model. The system_prompt is optimized iteratively during the tuning process.

Configuration & Tuning

After refining the workflow function, set up the tuning configuration and start the tuning process. Key configurations include:
  • DatasetConfig: Specifies the task dataset for tuning.
    • path: The path to the dataset, which can be a local path or a Hugging Face dataset.
    • split: The split to use for tuning (e.g., "train", "test").
    • name: (Optional) The subset name for Hugging Face datasets with multiple subsets.
  • PromptTuneConfig: Configures the optimization process.
    • lm_model_name: The model name for the prompt proposer (teacher model). Default is "dashscope/qwen-plus".
    • optimization_level: Optimization intensity — "light", "medium", or "heavy". Default is "light".
    • eval_display_progress: Whether to display progress during evaluation. Default is True.
    • eval_display_table: Number of table rows to display during evaluation. Default is 5.
    • eval_num_threads: Number of threads for parallel evaluation. Default is 16.
    • compare_performance: Whether to compare baseline vs. optimized performance. Default is True.
Here is a complete example:
Save the code to a Python file (e.g., main.py) and run it:
The lm_model_name in PromptTuneConfig specifies the teacher model used to generate candidate prompts. You need to provide an API key for the corresponding provider when running the script.

Output

The following example selects a subset from GSM8K and optimizes a ReAct agent on it. The results include the optimized prompt and its evaluation score:
A well-optimized prompt improved task accuracy from 92.67 to 96.88 — a gain of +4.21 points — with no changes to model weights.