|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import litellm |
| 4 | +import pytest |
| 5 | + |
| 6 | +from eval_protocol.dataset_logger import default_logger |
| 7 | +from eval_protocol.litellm_compat import allow_litellm_logging_to_start |
| 8 | +from eval_protocol.mcp.execution.policy import LiteLLMPolicy |
| 9 | +from eval_protocol.models import EvaluationRow, Message |
| 10 | +from eval_protocol.pytest.default_single_turn_rollout_process import SingleTurnRolloutProcessor |
| 11 | +from eval_protocol.pytest.exception_config import get_default_exception_handler_config |
| 12 | +from eval_protocol.pytest.types import RolloutProcessorConfig |
| 13 | +from vendor.tau2.data_model.message import UserMessage |
| 14 | +from vendor.tau2.utils.llm_utils import generate |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("index", range(4)) |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_acompletion_across_pytest_event_loops(index: int) -> None: |
| 20 | + response = await litellm.acompletion( |
| 21 | + model="openai/gpt-4o-mini", |
| 22 | + messages=[{"role": "user", "content": "ping"}], |
| 23 | + api_key="test", |
| 24 | + mock_response=f"ok-{index}", |
| 25 | + ) |
| 26 | + await allow_litellm_logging_to_start() |
| 27 | + |
| 28 | + assert response.choices[0].message.content == f"ok-{index}" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize(("index", "stream"), [(0, False), (1, True), (2, False), (3, True)]) |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_single_turn_processor_across_pytest_event_loops(index: int, stream: bool) -> None: |
| 34 | + config = RolloutProcessorConfig( |
| 35 | + completion_params={ |
| 36 | + "model": "openai/gpt-4o-mini", |
| 37 | + "api_key": "test", |
| 38 | + "mock_response": f"single-turn-{index}", |
| 39 | + "stream": stream, |
| 40 | + }, |
| 41 | + mcp_config_path="", |
| 42 | + semaphore=asyncio.Semaphore(1), |
| 43 | + server_script_path=None, |
| 44 | + steps=1, |
| 45 | + logger=default_logger, |
| 46 | + exception_handler_config=get_default_exception_handler_config(), |
| 47 | + ) |
| 48 | + row = EvaluationRow(messages=[Message(role="user", content="ping")]) |
| 49 | + |
| 50 | + result = await SingleTurnRolloutProcessor()([row], config)[0] |
| 51 | + |
| 52 | + assert result.messages[-1].content == f"single-turn-{index}" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize(("index", "stream"), [(0, False), (1, True), (2, False), (3, True)]) |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_litellm_policy_across_pytest_event_loops(index: int, stream: bool) -> None: |
| 58 | + policy = LiteLLMPolicy( |
| 59 | + model_id="openai/gpt-4o-mini", |
| 60 | + use_caching=False, |
| 61 | + api_key="test", |
| 62 | + mock_response=f"policy-{index}", |
| 63 | + stream=stream, |
| 64 | + ) |
| 65 | + |
| 66 | + result = await policy._make_llm_call([{"role": "user", "content": "ping"}], tools=[]) |
| 67 | + |
| 68 | + assert result["choices"][0]["message"]["content"] == f"policy-{index}" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("index", range(2)) |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_tau2_generate_across_pytest_event_loops(index: int) -> None: |
| 74 | + result = await generate( |
| 75 | + model="openai/gpt-4o-mini", |
| 76 | + messages=[UserMessage(role="user", content="ping")], |
| 77 | + api_key="test", |
| 78 | + mock_response=f"tau2-{index}", |
| 79 | + ) |
| 80 | + |
| 81 | + assert result.content == f"tau2-{index}" |
0 commit comments