|
| 1 | +"""OpenRouter-specific fixtures for tests.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import os |
| 5 | + |
| 6 | +pytest.importorskip("openai") |
| 7 | + |
| 8 | +from openai import OpenAI, AsyncOpenAI |
| 9 | +from judgeval.tracer.llm.llm_openai.wrapper import ( |
| 10 | + wrap_openai_client_sync, |
| 11 | + wrap_openai_client_async, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def openrouter_api_key(): |
| 17 | + """OpenRouter API key from environment""" |
| 18 | + api_key = os.getenv("OPENROUTER_API_KEY") |
| 19 | + if not api_key: |
| 20 | + pytest.skip("OPENROUTER_API_KEY environment variable not set") |
| 21 | + return api_key |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def sync_client(openrouter_api_key): |
| 26 | + """Unwrapped sync OpenRouter client (using OpenAI SDK)""" |
| 27 | + return OpenAI( |
| 28 | + api_key=openrouter_api_key, |
| 29 | + base_url="https://openrouter.ai/api/v1", |
| 30 | + default_headers={ |
| 31 | + "HTTP-Referer": os.getenv("OPENROUTER_APP_URL", "https://judgmentlabs.ai"), |
| 32 | + "X-Title": os.getenv("OPENROUTER_APP_NAME", "Judgeval Tests"), |
| 33 | + }, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def async_client(openrouter_api_key): |
| 39 | + """Unwrapped async OpenRouter client (using OpenAI SDK)""" |
| 40 | + return AsyncOpenAI( |
| 41 | + api_key=openrouter_api_key, |
| 42 | + base_url="https://openrouter.ai/api/v1", |
| 43 | + default_headers={ |
| 44 | + "HTTP-Referer": os.getenv("OPENROUTER_APP_URL", "https://judgmentlabs.ai"), |
| 45 | + "X-Title": os.getenv("OPENROUTER_APP_NAME", "Judgeval Tests"), |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def wrapped_sync_client(tracer, sync_client): |
| 52 | + """Wrapped sync OpenRouter client with tracer""" |
| 53 | + return wrap_openai_client_sync(tracer, sync_client) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture |
| 57 | +def wrapped_async_client(tracer, async_client): |
| 58 | + """Wrapped async OpenRouter client with tracer""" |
| 59 | + return wrap_openai_client_async(tracer, async_client) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(params=["wrapped", "unwrapped"], ids=["with_tracer", "without_tracer"]) |
| 63 | +def sync_client_maybe_wrapped(request, tracer, sync_client): |
| 64 | + """Parametrized fixture that yields both wrapped and unwrapped sync clients""" |
| 65 | + if request.param == "wrapped": |
| 66 | + return wrap_openai_client_sync(tracer, sync_client) |
| 67 | + return sync_client |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(params=["wrapped", "unwrapped"], ids=["with_tracer", "without_tracer"]) |
| 71 | +def async_client_maybe_wrapped(request, tracer, async_client): |
| 72 | + """Parametrized fixture that yields both wrapped and unwrapped async clients""" |
| 73 | + if request.param == "wrapped": |
| 74 | + return wrap_openai_client_async(tracer, async_client) |
| 75 | + return async_client |
0 commit comments