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
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 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
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
380 lines
13 KiB
Python
380 lines
13 KiB
Python
from typing import Dict, Any
|
|
|
|
import opik
|
|
from opik.api_objects.experiment import experiment_item
|
|
from opik.evaluation.metrics import score_result
|
|
from opik.types import FeedbackScoreDict
|
|
|
|
from . import verifiers
|
|
from ..testlib import assert_equal, ANY_BUT_NONE, generate_project_name
|
|
|
|
PROJECT_NAME = generate_project_name("e2e", __name__)
|
|
|
|
|
|
def llm_task(item: Dict[str, Any]):
|
|
if item["input"] == {"question": "What is the capital of Ukraine?"}:
|
|
return {"output": "Kyiv"}
|
|
if item["input"] == {"question": "What is the capital of France?"}:
|
|
return {"output": "Paris"}
|
|
if item["input"] == {"question": "What is the capital of Germany?"}:
|
|
return {"output": "Berlin"}
|
|
if item["input"] == {"question": "What is the capital of Poland?"}:
|
|
return {"output": "Krakow"}
|
|
|
|
raise AssertionError(
|
|
f"Task received dataset item with an unexpected input: {item['input']}"
|
|
)
|
|
|
|
|
|
def equals_scoring_function(dataset_item: Dict[str, Any], task_outputs: Dict[str, Any]):
|
|
reference = dataset_item["expected_model_output"]["output"]
|
|
prediction = task_outputs["output"]
|
|
if reference == prediction:
|
|
value = 1.0
|
|
else:
|
|
value = 0.0
|
|
return score_result.ScoreResult(
|
|
name="equals_scoring_function",
|
|
value=value,
|
|
reason="Correct output value" if value == 1.0 else "Incorrect output value",
|
|
)
|
|
|
|
|
|
def test__find_experiment_items_for_dataset__happy_path(
|
|
opik_client: opik.Opik, dataset_name: str, experiment_name: str
|
|
):
|
|
dataset = opik_client.create_dataset(dataset_name, project_name=PROJECT_NAME)
|
|
|
|
dataset.insert(
|
|
[
|
|
{
|
|
"input": {"question": "What is the capital of Ukraine?"},
|
|
"expected_model_output": {"output": "Kyiv"},
|
|
},
|
|
{
|
|
"input": {"question": "What is the capital of Poland?"},
|
|
"expected_model_output": {"output": "Warsaw"},
|
|
},
|
|
]
|
|
)
|
|
|
|
evaluation_result = opik.evaluate(
|
|
dataset=dataset,
|
|
task=llm_task,
|
|
scoring_functions=[equals_scoring_function],
|
|
experiment_name=experiment_name,
|
|
experiment_config={
|
|
"model_name": "gpt-3.5",
|
|
},
|
|
scoring_key_mapping={
|
|
"reference": lambda x: x["expected_model_output"]["output"],
|
|
},
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
opik.flush_tracker()
|
|
|
|
# make sure experiments saved and available
|
|
verifiers.verify_experiment(
|
|
opik_client=opik_client,
|
|
id=evaluation_result.experiment_id,
|
|
experiment_name=evaluation_result.experiment_name,
|
|
experiment_metadata={"model_name": "gpt-3.5"},
|
|
traces_amount=2, # one trace per dataset item
|
|
feedback_scores_amount=1,
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
# find experiment items for dataset
|
|
retrieved_experiment = opik_client.get_experiment_by_name(
|
|
experiment_name, project_name=PROJECT_NAME
|
|
)
|
|
experiments = opik_client.get_experiments_client()
|
|
experiment_items_contents = experiments.find_experiment_items_for_dataset(
|
|
dataset_name=dataset_name,
|
|
experiment_ids=[retrieved_experiment.id],
|
|
project_name=opik_client.project_name,
|
|
)
|
|
assert retrieved_experiment.project_name == PROJECT_NAME
|
|
assert len(experiment_items_contents) == 2
|
|
|
|
EXPECTED_EXPERIMENT_ITEMS_CONTENT = [
|
|
experiment_item.ExperimentItemContent(
|
|
id=ANY_BUT_NONE,
|
|
dataset_item_id=ANY_BUT_NONE,
|
|
trace_id=ANY_BUT_NONE,
|
|
dataset_item_data={
|
|
"expected_model_output": {"output": "Warsaw"},
|
|
"id": ANY_BUT_NONE,
|
|
"input": {"question": "What is the capital of Poland?"},
|
|
},
|
|
evaluation_task_output={"output": "Krakow"},
|
|
feedback_scores=[
|
|
FeedbackScoreDict(
|
|
category_name=None,
|
|
name="equals_scoring_function",
|
|
reason="Incorrect output value",
|
|
value=0.0,
|
|
)
|
|
],
|
|
),
|
|
experiment_item.ExperimentItemContent(
|
|
id=ANY_BUT_NONE,
|
|
dataset_item_id=ANY_BUT_NONE,
|
|
trace_id=ANY_BUT_NONE,
|
|
dataset_item_data={
|
|
"expected_model_output": {"output": "Kyiv"},
|
|
"id": ANY_BUT_NONE,
|
|
"input": {"question": "What is the capital of Ukraine?"},
|
|
},
|
|
evaluation_task_output={"output": "Kyiv"},
|
|
feedback_scores=[
|
|
FeedbackScoreDict(
|
|
category_name=None,
|
|
name="equals_scoring_function",
|
|
reason="Correct output value",
|
|
value=1.0,
|
|
)
|
|
],
|
|
),
|
|
]
|
|
|
|
assert_equal(
|
|
expected=sorted(
|
|
EXPECTED_EXPERIMENT_ITEMS_CONTENT,
|
|
key=lambda item: str(item.evaluation_task_output),
|
|
),
|
|
actual=sorted(
|
|
experiment_items_contents, key=lambda item: str(item.evaluation_task_output)
|
|
),
|
|
)
|
|
|
|
|
|
def test__find_experiment_items_for_dataset__filtered__happy_path(
|
|
opik_client: opik.Opik, dataset_name: str, experiment_name: str
|
|
):
|
|
dataset = opik_client.create_dataset(dataset_name, project_name=PROJECT_NAME)
|
|
|
|
dataset.insert(
|
|
[
|
|
{
|
|
"input": {"question": "What is the capital of Ukraine?"},
|
|
"expected_model_output": {"output": "Kyiv"},
|
|
},
|
|
{
|
|
"input": {"question": "What is the capital of Poland?"},
|
|
"expected_model_output": {"output": "Warsaw"},
|
|
},
|
|
]
|
|
)
|
|
|
|
evaluation_result = opik.evaluate(
|
|
dataset=dataset,
|
|
task=llm_task,
|
|
scoring_functions=[equals_scoring_function],
|
|
experiment_name=experiment_name,
|
|
experiment_config={
|
|
"model_name": "gpt-3.5",
|
|
},
|
|
scoring_key_mapping={
|
|
"reference": lambda x: x["expected_model_output"]["output"],
|
|
},
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
opik.flush_tracker()
|
|
|
|
# make sure experiments saved and available
|
|
verifiers.verify_experiment(
|
|
opik_client=opik_client,
|
|
id=evaluation_result.experiment_id,
|
|
experiment_name=evaluation_result.experiment_name,
|
|
experiment_metadata={"model_name": "gpt-3.5"},
|
|
traces_amount=2, # one trace per dataset item
|
|
feedback_scores_amount=1,
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
# find experiment items for dataset
|
|
retrieved_experiment = opik_client.get_experiment_by_name(
|
|
experiment_name, project_name=PROJECT_NAME
|
|
)
|
|
experiments = opik_client.get_experiments_client()
|
|
experiment_items_contents = experiments.find_experiment_items_for_dataset(
|
|
dataset_name=dataset_name,
|
|
experiment_ids=[retrieved_experiment.id],
|
|
filter_string="feedback_scores.equals_scoring_function = 0.0",
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
assert retrieved_experiment.project_name == PROJECT_NAME
|
|
|
|
assert len(experiment_items_contents) == 1
|
|
|
|
EXPECTED_EXPERIMENT_ITEMS_CONTENT = [
|
|
experiment_item.ExperimentItemContent(
|
|
id=ANY_BUT_NONE,
|
|
dataset_item_id=ANY_BUT_NONE,
|
|
trace_id=ANY_BUT_NONE,
|
|
dataset_item_data={
|
|
"expected_model_output": {"output": "Warsaw"},
|
|
"id": ANY_BUT_NONE,
|
|
"input": {"question": "What is the capital of Poland?"},
|
|
},
|
|
evaluation_task_output={"output": "Krakow"},
|
|
feedback_scores=[
|
|
FeedbackScoreDict(
|
|
category_name=None,
|
|
name="equals_scoring_function",
|
|
reason="Incorrect output value",
|
|
value=0.0,
|
|
)
|
|
],
|
|
)
|
|
]
|
|
|
|
assert_equal(
|
|
expected=EXPECTED_EXPERIMENT_ITEMS_CONTENT,
|
|
actual=experiment_items_contents,
|
|
)
|
|
|
|
|
|
def test__experiment_scores__happy_path(
|
|
opik_client: opik.Opik, dataset_name: str, experiment_name: str
|
|
):
|
|
"""Test that experiment scoring functions are executed and scores are logged."""
|
|
|
|
def compute_experiment_scores(test_results):
|
|
"""Aggregate scores across all test results."""
|
|
# Extract all scoring function values
|
|
all_scores = []
|
|
for result in test_results:
|
|
if result.score_results:
|
|
all_scores.extend([score.value for score in result.score_results])
|
|
|
|
if not all_scores:
|
|
return []
|
|
|
|
# Compute aggregate metrics
|
|
return [
|
|
score_result.ScoreResult(
|
|
name="max_score",
|
|
value=max(all_scores),
|
|
reason=f"Maximum score across {len(all_scores)} measurements",
|
|
),
|
|
score_result.ScoreResult(
|
|
name="min_score",
|
|
value=min(all_scores),
|
|
reason=f"Minimum score across {len(all_scores)} measurements",
|
|
),
|
|
score_result.ScoreResult(
|
|
name="avg_score",
|
|
value=sum(all_scores) / len(all_scores),
|
|
reason=f"Average score across {len(all_scores)} measurements",
|
|
),
|
|
]
|
|
|
|
# Create dataset
|
|
dataset = opik_client.create_dataset(dataset_name, project_name=PROJECT_NAME)
|
|
dataset.insert(
|
|
[
|
|
{
|
|
"input": {"question": "What is the capital of Ukraine?"},
|
|
"expected_model_output": {"output": "Kyiv"},
|
|
},
|
|
{
|
|
"input": {"question": "What is the capital of Poland?"},
|
|
"expected_model_output": {"output": "Warsaw"},
|
|
},
|
|
]
|
|
)
|
|
|
|
# Run evaluation with experiment scoring functions
|
|
evaluation_result = opik.evaluate(
|
|
dataset=dataset,
|
|
task=llm_task,
|
|
scoring_functions=[equals_scoring_function],
|
|
experiment_scoring_functions=[compute_experiment_scores],
|
|
experiment_name=experiment_name,
|
|
experiment_config={
|
|
"model_name": "test-model",
|
|
},
|
|
scoring_key_mapping={
|
|
"reference": lambda x: x["expected_model_output"]["output"],
|
|
},
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
opik.flush_tracker()
|
|
|
|
# Verify experiment was created with experiment scores
|
|
verifiers.verify_experiment(
|
|
opik_client=opik_client,
|
|
id=evaluation_result.experiment_id,
|
|
experiment_name=evaluation_result.experiment_name,
|
|
experiment_metadata={"model_name": "test-model"},
|
|
traces_amount=2,
|
|
feedback_scores_amount=1,
|
|
project_name=PROJECT_NAME,
|
|
)
|
|
|
|
# Verify experiment scores are present in evaluation result
|
|
assert evaluation_result.experiment_scores is not None, (
|
|
"Experiment scores should not be None"
|
|
)
|
|
assert len(evaluation_result.experiment_scores) == 3, (
|
|
f"Expected 3 experiment scores, got {len(evaluation_result.experiment_scores)}"
|
|
)
|
|
|
|
score_names = {score.name for score in evaluation_result.experiment_scores}
|
|
assert score_names == {
|
|
"max_score",
|
|
"min_score",
|
|
"avg_score",
|
|
}, f"Expected score names {{max_score, min_score, avg_score}}, got {score_names}"
|
|
|
|
# Verify experiment scores are retrievable via SDK API
|
|
retrieved_experiment = opik_client.get_experiment_by_name(
|
|
experiment_name, project_name=PROJECT_NAME
|
|
)
|
|
rest_client = opik_client._rest_client
|
|
experiment_content = rest_client.experiments.get_experiment_by_id(
|
|
retrieved_experiment.id
|
|
)
|
|
assert retrieved_experiment.project_name == PROJECT_NAME
|
|
|
|
assert experiment_content.experiment_scores is not None, (
|
|
"Experiment scores should be persisted in backend"
|
|
)
|
|
assert len(experiment_content.experiment_scores) == 3, (
|
|
f"Expected 3 experiment scores in backend, got {len(experiment_content.experiment_scores)}"
|
|
)
|
|
|
|
backend_score_names = {score.name for score in experiment_content.experiment_scores}
|
|
assert backend_score_names == {"max_score", "min_score", "avg_score"}, (
|
|
f"Expected backend score names {{max_score, min_score, avg_score}}, got {backend_score_names}"
|
|
)
|
|
|
|
# Verify score values are reasonable
|
|
max_score = next(
|
|
s for s in evaluation_result.experiment_scores if s.name == "max_score"
|
|
)
|
|
min_score = next(
|
|
s for s in evaluation_result.experiment_scores if s.name == "min_score"
|
|
)
|
|
avg_score = next(
|
|
s for s in evaluation_result.experiment_scores if s.name == "avg_score"
|
|
)
|
|
|
|
assert 0.0 <= max_score.value <= 1.0, (
|
|
f"max_score should be in [0,1], got {max_score.value}"
|
|
)
|
|
assert 0.0 <= min_score.value <= 1.0, (
|
|
f"min_score should be in [0,1], got {min_score.value}"
|
|
)
|
|
assert 0.0 <= avg_score.value <= 1.0, (
|
|
f"avg_score should be in [0,1], got {avg_score.value}"
|
|
)
|
|
assert min_score.value <= avg_score.value <= max_score.value, (
|
|
f"Score ordering should be min <= avg <= max, got {min_score.value} <= {avg_score.value} <= {max_score.value}"
|
|
)
|