Skip to content

使用者模擬器

使用可配置行為模擬多個標註者的自動化測試工具。

使用者模擬器通過模擬具有可配置行為和能力水平的多個使用者,實現 Potato 標註任務的自動化測試。

概述

模擬器適用於:

  • 品質控制測試:測試注意力檢查、金標準和遮蔽行為
  • 儀表板測試:為管理員儀表板生成真實的標註資料
  • 可擴充套件性測試:使用大量併發使用者對伺服器進行壓力測試
  • AI 輔助評估:將 LLM 準確率與類人行為進行比較
  • 主動學習測試:模擬迭代標註工作流

快速開始

bash
# Basic random simulation with 10 users
python -m potato.simulator --server http://localhost:8000 --users 10
 
# With configuration file
python -m potato.simulator --config simulator-config.yaml --server http://localhost:8000
 
# Fast scalability test (no waiting between annotations)
python -m potato.simulator --server http://localhost:8000 --users 50 --parallel 10 --fast-mode

配置

YAML 配置檔案

建立包含模擬器設定的 YAML 檔案:

yaml
simulator:
  # User configuration
  users:
    count: 20
    competence_distribution:
      good: 0.5      # 50% will be "good" annotators (80-90% accuracy)
      average: 0.3   # 30% "average" (60-70% accuracy)
      poor: 0.2      # 20% "poor" (40-50% accuracy)
 
  # Annotation strategy
  strategy: random  # random, biased, llm, pattern
 
  # Timing configuration
  timing:
    annotation_time:
      min: 2.0
      max: 45.0
      mean: 12.0
      std: 6.0
      distribution: normal  # uniform, normal, exponential
 
  # Execution
  execution:
    parallel_users: 5
    delay_between_users: 0.5
    max_annotations_per_user: 50
 
server:
  url: http://localhost:8000

能力水平

水平準確率描述
perfect100%始終匹配金標準
good80-90%高品質標註者
average60-70%典型眾包工人
poor40-50%低品質標註者
random~1/N從標籤中隨機選擇
adversarial0%故意選錯(用於測試品質控制)

標註策略

隨機策略(預設)

均勻隨機選擇標籤:

yaml
strategy: random

偏向策略

基於標籤偏好的加權選擇:

yaml
strategy: biased
biased_config:
  label_weights:
    positive: 0.6
    negative: 0.3
    neutral: 0.1

LLM 策略

使用 LLM 根據文本內容生成標註:

yaml
strategy: llm
llm_config:
  endpoint_type: openai
  model: gpt-4o-mini
  api_key: ${OPENAI_API_KEY}
  temperature: 0.1
  add_noise: true
  noise_rate: 0.05

使用 Ollama 的本地 LLM:

yaml
strategy: llm
llm_config:
  endpoint_type: ollama
  model: llama3.2
  base_url: http://localhost:11434

CLI 選項

text
Usage: python -m potato.simulator [OPTIONS]

Required:
  --server, -s URL        Potato server URL

User Configuration:
  --users, -u NUM         Number of simulated users (default: 10)
  --competence DIST       Competence distribution

Strategy:
  --strategy TYPE         Strategy: random, biased, llm, pattern
  --llm-endpoint TYPE     LLM endpoint: openai, anthropic, ollama
  --llm-model NAME        LLM model name

Execution:
  --parallel, -p NUM      Max concurrent users (default: 5)
  --max-annotations, -m   Max annotations per user
  --fast-mode             Disable waiting between annotations

Output:
  --output-dir, -o DIR    Output directory (default: simulator_output)

品質控制測試

測試注意力檢查檢測:

yaml
simulator:
  users:
    count: 10
    competence_distribution:
      adversarial: 1.0  # All users will fail
  quality_control:
    attention_check_fail_rate: 0.5
    respond_fast_rate: 0.3

輸出檔案

模擬完成後,結果匯出到輸出目錄:

  • summary_{timestamp}.json - 彙總統計
  • user_results_{timestamp}.json - 每使用者詳細結果
  • annotations_{timestamp}.csv - 所有標註的扁平格式

摘要示例

json
{
  "user_count": 20,
  "total_annotations": 400,
  "total_time_seconds": 125.3,
  "attention_checks": {
    "passed": 18,
    "failed": 2,
    "pass_rate": 0.9
  }
}

程式設計方式使用

python
from potato.simulator import SimulatorManager, SimulatorConfig
 
# Create configuration
config = SimulatorConfig(
    user_count=10,
    strategy="random",
    competence_distribution={"good": 0.5, "average": 0.5}
)
 
# Create and run simulator
manager = SimulatorManager(config, "http://localhost:8000")
results = manager.run_parallel(max_annotations_per_user=20)
 
# Print summary and export
manager.print_summary()
manager.export_results()

與測試整合

模擬器可以在 pytest fixture 中使用:

python
import pytest
from potato.simulator import SimulatorManager, SimulatorConfig
 
@pytest.fixture
def simulated_annotations(flask_test_server):
    config = SimulatorConfig(user_count=5, strategy="random")
    manager = SimulatorManager(config, flask_test_server.base_url)
    return manager.run_parallel(max_annotations_per_user=10)
 
def test_dashboard_shows_annotations(simulated_annotations, flask_test_server):
    response = requests.get(f"{flask_test_server.base_url}/admin/api/overview")
    assert response.json()["total_annotations"] > 0

故障排除

登入失敗

  • 確保伺服器允許匿名註冊或設定了 require_password: false
  • 檢查伺服器日誌中的身份驗證錯誤

沒有可用實例

  • 驗證資料檔案是否正確載入
  • 檢查分配策略設定

LLM 策略不工作

  • 驗證 API 金鑰已設定
  • 對於 Ollama,確保伺服器正在執行
  • 檢查模型名稱是否正確

延伸閱讀

有關實現細節,請參閱原始碼文件