Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch timeout override #3501

Merged
merged 21 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
eb112d2
Update changelog
diondrapeck Jul 3, 2024
7fa3e7d
Expose batch timeout environment variable
diondrapeck Jul 3, 2024
d94ac65
Add test
diondrapeck Jul 3, 2024
a26d27a
Merge branch 'main' into batch-timeout
diondrapeck Jul 8, 2024
fe96132
Merge branch 'main' into batch-timeout
diondrapeck Jul 11, 2024
8440556
Change PF_BATCH_TIMEOUT_DEFAULT constant name to PF_BATCH_TIMEOUT_SEC…
diondrapeck Jul 11, 2024
ac0d64a
Change PF_BATCH_TIMEOUT_DEFAULT constant name to PF_BATCH_TIMEOUT_SEC…
diondrapeck Jul 11, 2024
1181b28
Copy get_int_env_var to promptflow-evals utils
diondrapeck Jul 12, 2024
6c109df
Remove timeout logic from proxy_client to batch_run_context
diondrapeck Jul 13, 2024
d00ceb4
Add tests for batch context
diondrapeck Jul 13, 2024
35c5c54
Merge branch 'main' into batch-timeout
diondrapeck Jul 13, 2024
0e6d5f6
Update __exit__ to only reset if environment variable is default
diondrapeck Jul 13, 2024
3953b38
Update CHANGELOG to use promptflow format
diondrapeck Jul 13, 2024
e274e90
Merge branch 'batch-timeout' of https://github.com/microsoft/promptfl…
diondrapeck Jul 13, 2024
fef7729
Lower test timeout value to be under limit
diondrapeck Jul 14, 2024
988aeed
Merge branch 'main' into batch-timeout
diondrapeck Jul 15, 2024
736b39f
Add flag and check if batch timeout was set by system
diondrapeck Jul 19, 2024
668d289
Resolve merge conflicts
diondrapeck Jul 19, 2024
bdb7fa7
Make changelog more clear
diondrapeck Jul 19, 2024
f9867bc
Replace set by system environment variable with class attribute
diondrapeck Jul 22, 2024
ba0ee3a
Merge branch 'main' into batch-timeout
diondrapeck Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/promptflow-evals/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features Added
- Introduced `JailbreakAdversarialSimulator` for customers who need to do run jailbreak and non jailbreak adversarial simulations at the same time. More info in the README.md in `/promptflow/evals/synthetic/README.md#jailbreak-simulator`
- Exposed batch evaluation run timeout via "PF_BATCH_TIMEOUT_SEC" environment variable. This variable can be used to set the timeout for the batch evaluation for each evaluator and target separately only, not the entire API call.

### Bugs Fixed
- Large simulation was causing a jinja exception, this has been fixed.
Expand Down
4 changes: 3 additions & 1 deletion src/promptflow-evals/promptflow/evals/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class Prefixes:

CONTENT_SAFETY_DEFECT_RATE_THRESHOLD_DEFAULT = 4

BATCH_RUN_TIMEOUT = 3600
PF_BATCH_TIMEOUT_SEC_DEFAULT = 3600
PF_BATCH_TIMEOUT_SEC = "PF_BATCH_TIMEOUT_SEC"
PF_IS_BATCH_TIMEOUT_SYSTEM_SET = "PF_IS_BATCH_TIMEOUT_SYSTEM_SET"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

from promptflow._sdk._constants import PF_FLOW_ENTRY_IN_TMP, PF_FLOW_META_LOAD_IN_SUBPROCESS
from promptflow._utils.user_agent_utils import ClientUserAgentUtil
from promptflow.evals._constants import (
PF_BATCH_TIMEOUT_SEC,
PF_BATCH_TIMEOUT_SEC_DEFAULT,
PF_IS_BATCH_TIMEOUT_SYSTEM_SET,
)
from promptflow.tracing._integrations._openai_injector import inject_openai_api, recover_openai_api

from ..._user_agent import USER_AGENT
Expand All @@ -26,6 +31,12 @@ def __enter__(self):
os.environ[PF_FLOW_ENTRY_IN_TMP] = "true"
os.environ[PF_FLOW_META_LOAD_IN_SUBPROCESS] = "false"

if os.environ.get(PF_BATCH_TIMEOUT_SEC) is None:
diondrapeck marked this conversation as resolved.
Show resolved Hide resolved
os.environ[PF_BATCH_TIMEOUT_SEC] = str(PF_BATCH_TIMEOUT_SEC_DEFAULT)
os.environ[PF_IS_BATCH_TIMEOUT_SYSTEM_SET] = "true"
diondrapeck marked this conversation as resolved.
Show resolved Hide resolved
else:
os.environ[PF_IS_BATCH_TIMEOUT_SYSTEM_SET] = "false"

# For addressing the issue of asyncio event loop closed on Windows
set_event_loop_policy()

Expand All @@ -36,3 +47,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(self.client, ProxyClient):
os.environ.pop(PF_FLOW_ENTRY_IN_TMP, None)
os.environ.pop(PF_FLOW_META_LOAD_IN_SUBPROCESS, None)

if os.environ.get(PF_IS_BATCH_TIMEOUT_SYSTEM_SET) == "true":
os.environ.pop(PF_BATCH_TIMEOUT_SEC, None)
os.environ[PF_IS_BATCH_TIMEOUT_SYSTEM_SET] = "false"
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import pandas as pd

from promptflow.contracts.types import AttrDict
from promptflow.evals.evaluate._utils import _apply_column_mapping, _has_aggregator, load_jsonl
from promptflow.evals.evaluate._utils import _apply_column_mapping, _has_aggregator, get_int_env_var, load_jsonl
from promptflow.tracing import ThreadPoolExecutorWithContext as ThreadPoolExecutor

from ..._constants import BATCH_RUN_TIMEOUT
from ..._constants import PF_BATCH_TIMEOUT_SEC, PF_BATCH_TIMEOUT_SEC_DEFAULT

LOGGER = logging.getLogger(__name__)

Expand All @@ -24,15 +24,17 @@ def __init__(self, run, input_data, evaluator_name=None, aggregated_metrics=None
self.aggregated_metrics = aggregated_metrics

def get_result_df(self, exclude_inputs=False):
result_df = self.run.result(timeout=BATCH_RUN_TIMEOUT)
batch_run_timeout = get_int_env_var(PF_BATCH_TIMEOUT_SEC, PF_BATCH_TIMEOUT_SEC_DEFAULT)
result_df = self.run.result(timeout=batch_run_timeout)
if exclude_inputs:
result_df = result_df.drop(columns=[col for col in result_df.columns if col.startswith("inputs.")])
return result_df

def get_aggregated_metrics(self):
try:
batch_run_timeout = get_int_env_var(PF_BATCH_TIMEOUT_SEC, PF_BATCH_TIMEOUT_SEC_DEFAULT)
aggregated_metrics = (
self.aggregated_metrics.result(timeout=BATCH_RUN_TIMEOUT)
self.aggregated_metrics.result(timeout=batch_run_timeout)
if self.aggregated_metrics is not None
else None
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from promptflow.client import PFClient
from promptflow.tracing import ThreadPoolExecutorWithContext as ThreadPoolExecutor

from ..._constants import BATCH_RUN_TIMEOUT

LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -41,13 +39,13 @@ def run(self, flow, data, column_mapping=None, **kwargs):
return ProxyRun(run=eval_future)

def get_details(self, proxy_run, all_results=False):
run = proxy_run.run.result(timeout=BATCH_RUN_TIMEOUT)
run = proxy_run.run.result()
result_df = self._pf_client.get_details(run, all_results=all_results)
result_df.replace("(Failed)", np.nan, inplace=True)
return result_df

def get_metrics(self, proxy_run):
run = proxy_run.run.result(timeout=BATCH_RUN_TIMEOUT)
run = proxy_run.run.result()
return self._pf_client.get_metrics(run)

@staticmethod
Expand Down
16 changes: 16 additions & 0 deletions src/promptflow-evals/promptflow/evals/evaluate/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ def _has_aggregator(evaluator):
return hasattr(evaluator, "__aggregate__")


def get_int_env_var(env_var_name, default_value=None):
"""
The function `get_int_env_var` retrieves an integer environment variable value, with an optional
default value if the variable is not set or cannot be converted to an integer.

:param env_var_name: The name of the environment variable you want to retrieve the value of
:param default_value: The default value is the value that will be returned if the environment
variable is not found or if it cannot be converted to an integer
:return: an integer value.
"""
try:
return int(os.environ.get(env_var_name, default_value))
except Exception:
return default_value


def set_event_loop_policy():
import asyncio
import platform
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
from unittest.mock import MagicMock

import pytest

from promptflow.client import PFClient
from promptflow.evals._constants import PF_BATCH_TIMEOUT_SEC, PF_BATCH_TIMEOUT_SEC_DEFAULT
from promptflow.evals._user_agent import USER_AGENT
from promptflow.evals.evaluate._batch_run_client import BatchRunContext, CodeClient
from promptflow.evals.evaluate._batch_run_client import BatchRunContext, CodeClient, ProxyClient


@pytest.fixture
Expand Down Expand Up @@ -50,3 +52,27 @@ def test_with_pfclient(self, mocker, pf_client_mock):
pass

mock_recover_openai_api.assert_not_called()

def test_batch_timeout_default(self):
before_timeout = os.environ.get(PF_BATCH_TIMEOUT_SEC)
assert before_timeout is None

with BatchRunContext(ProxyClient(PFClient)):
during_timeout = int(os.environ.get(PF_BATCH_TIMEOUT_SEC))
assert during_timeout == PF_BATCH_TIMEOUT_SEC_DEFAULT

# Default timeout should be reset after exiting BatchRunContext
after_timeout = os.environ.get(PF_BATCH_TIMEOUT_SEC)
assert after_timeout is None

def test_batch_timeout_custom(self):
custom_timeout = 1000
os.environ[PF_BATCH_TIMEOUT_SEC] = str(custom_timeout)

with BatchRunContext(ProxyClient(PFClient)):
during_timeout = int(os.environ.get(PF_BATCH_TIMEOUT_SEC))
assert during_timeout == custom_timeout

# Custom timeouts should not be reset after exiting BatchRunContext
after_timeout = int(os.environ.get(PF_BATCH_TIMEOUT_SEC))
assert after_timeout == custom_timeout
Loading