Files
comet-ml--opik/apps/opik-python-backend/tests/unit/test_studio_optimizer_model.py
T
wehub-resource-sync 5a558eb09e
Backend Tests / discover-tests (push) Waiting to run
Backend Tests / ${{ matrix.name }} (push) Blocked by required conditions
Build and Publish SDK / build-and-publish (push) Waiting to run
Build Opik Docker Images / set-version (push) Waiting to run
Build Opik Docker Images / build-backend (push) Blocked by required conditions
Build Opik Docker Images / build-sandbox-executor-python (push) Blocked by required conditions
Build Opik Docker Images / build-python-backend (push) Blocked by required conditions
Build Opik Docker Images / build-frontend (push) Blocked by required conditions
Build Opik Docker Images / create-git-tag (push) Blocked by required conditions
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Waiting to run
Docs - Publish / run (push) Waiting to run
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Waiting to run
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Blocked by required conditions
Frontend Unit Tests / Test on Node 20 (push) Waiting to run
Guardrails E2E Tests / Select Python version matrix (push) Waiting to run
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Blocked by required conditions
Guardrails E2E Tests / 📢 Slack Notification (push) Blocked by required conditions
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Waiting to run
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Blocked by required conditions
SDK Library Integration Tests Runner / Check Secrets (push) Waiting to run
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Blocked by required conditions
SDK Library Integration Tests Runner / Build (push) Blocked by required conditions
SDK Library Integration Tests Runner / openai_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / langchain_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / llama_index_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / anthropic_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / mistral_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / groq_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / aisuite_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / haystack_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / dspy_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / crewai_v0_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / crewai_v1_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / genai_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / adk_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / bedrock_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / litellm_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / harbor_tests (push) Blocked by required conditions
SDK Library Integration Tests Runner / Slack Notification (push) Blocked by required conditions
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Waiting to run
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Waiting to run
Lint Opik Helm Chart / unittest-helm-chart (push) Waiting to run
Lint Opik Helm Chart / render-equality (push) Waiting to run
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Waiting to run
Python BE E2E Tests / Python BE E2E (push) Waiting to run
Python Backend Tests / run-python-backend-tests (push) Waiting to run
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Waiting to run
Release Drafter / update_release_draft (push) Waiting to run
SDK E2E Libraries Integration Tests / Check Secrets (push) Waiting to run
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / build-opik (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Blocked by required conditions
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Waiting to run
TypeScript SDK Library Integration Tests / Check Secrets (push) Waiting to run
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Blocked by required conditions
TypeScript SDK Build & Publish / build-and-publish (push) Waiting to run
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Waiting to run
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

133 lines
4.8 KiB
Python

"""Unit tests for the studio model wiring.
The Optimization Studio lets the optimizer/algorithm (GEPA's reflection LM,
hierarchical's reasoning model) run on a different model than the prompt. These
tests verify, deterministically and offline:
- the separate algorithm model is parsed out of the optimizer parameters,
- the prompt is built with its configured model + parameters,
- the optimizer is built with its configured model + parameters,
- the optimizer defaults to the prompt model when none is set.
"""
from llm_constants import (
ANTHROPIC_CLAUDE_HAIKU,
ANTHROPIC_CLAUDE_OPUS,
GATEWAY_CLAUDE_HAIKU,
GATEWAY_CLAUDE_OPUS,
)
from opik_backend.jobs import optimizer_runner
from opik_backend.studio.types import OptimizationConfig
def _config(
task_model: str = ANTHROPIC_CLAUDE_HAIKU,
task_params: dict | None = None,
optimizer_params: dict | None = None,
) -> dict:
return {
"dataset_name": "ds",
"prompt": {"messages": [{"role": "user", "content": "{{text}}"}]},
"llm_model": {"model": task_model, "parameters": task_params or {}},
"evaluation": {
"metrics": [{"type": "equals", "parameters": {"reference_key": "label"}}]
},
"optimizer": {"type": "gepa", "parameters": optimizer_params or {"seed": 42}},
}
def test_optimizer_model_extracted_from_optimizer_params():
config = OptimizationConfig.from_dict(
_config(
optimizer_params={
"seed": 42,
"model": ANTHROPIC_CLAUDE_OPUS,
"model_parameters": {"temperature": 0.5},
}
)
)
# The separate algorithm model + its params are surfaced...
assert config.optimizer_model == ANTHROPIC_CLAUDE_OPUS
assert config.optimizer_model_params == {"temperature": 0.5}
# ...and removed from the kwargs passed to the optimizer constructor.
assert config.optimizer_params == {"seed": 42}
# The prompt/task model is untouched.
assert config.model == ANTHROPIC_CLAUDE_HAIKU
def test_optimizer_model_defaults_to_none_when_absent():
config = OptimizationConfig.from_dict(_config(optimizer_params={"seed": 7}))
assert config.optimizer_model is None
assert config.optimizer_model_params is None
assert config.optimizer_params == {"seed": 7}
def test_prompt_and_algorithm_use_their_configured_models_and_params():
config = OptimizationConfig.from_dict(
_config(
task_model=ANTHROPIC_CLAUDE_HAIKU,
task_params={"temperature": 0.3},
optimizer_params={
"seed": 42,
"model": ANTHROPIC_CLAUDE_OPUS,
"model_parameters": {"temperature": 0.7},
},
)
)
optimizer, prompt = optimizer_runner.build_optimizer_and_prompt(config)
# Prompt (task evaluation) uses the configured prompt model + params,
# gateway-routed, with the studio defaults applied.
assert prompt.model == GATEWAY_CLAUDE_HAIKU
assert prompt.model_kwargs.get("temperature") == 0.3
assert prompt.model_kwargs.get("stream") is False
assert "max_tokens" in prompt.model_kwargs
# Optimizer (algorithm) uses its own configured model + params.
assert optimizer.model == GATEWAY_CLAUDE_OPUS
assert optimizer.model_parameters.get("temperature") == 0.7
assert optimizer.model_parameters.get("stream") is False
assert "max_tokens" in optimizer.model_parameters
def test_algorithm_defaults_to_prompt_model_when_not_set():
config = OptimizationConfig.from_dict(
_config(
task_model=ANTHROPIC_CLAUDE_HAIKU,
task_params={"temperature": 0.3},
optimizer_params={"seed": 42},
)
)
optimizer, prompt = optimizer_runner.build_optimizer_and_prompt(config)
assert prompt.model == GATEWAY_CLAUDE_HAIKU
# No separate algorithm model → optimizer falls back to the prompt model
# and its parameters.
assert optimizer.model == GATEWAY_CLAUDE_HAIKU
assert optimizer.model_parameters.get("temperature") == 0.3
def test_optimizer_params_preserved_without_separate_model():
# model_parameters set on the optimizer but no model — the optimizer should
# still default to the prompt model yet keep its own configured params
# (not silently drop them).
config = OptimizationConfig.from_dict(
_config(
task_model=ANTHROPIC_CLAUDE_HAIKU,
task_params={"temperature": 0.3},
optimizer_params={"seed": 42, "model_parameters": {"temperature": 0.9}},
)
)
optimizer, prompt = optimizer_runner.build_optimizer_and_prompt(config)
assert optimizer.model == GATEWAY_CLAUDE_HAIKU
assert optimizer.model_parameters.get("temperature") == 0.9
# The prompt keeps its own params, independent of the optimizer's.
assert prompt.model_kwargs.get("temperature") == 0.3