support openrouter qwen model (#3630)

This commit is contained in:
Shuchang Zheng
2025-10-06 18:55:52 -07:00
committed by GitHub
parent ccc49902e8
commit ea92ca4c51
4 changed files with 88 additions and 12 deletions

View File

@@ -1,10 +1,11 @@
import importlib
import json
import types
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, MagicMock
import pytest
from skyvern import config
from skyvern.config import Settings
from skyvern.forge import app
from skyvern.forge.sdk.api.llm import api_handler_factory, config_registry
@@ -19,6 +20,9 @@ class DummyResponse(dict):
def model_dump_json(self, indent: int = 2):
return json.dumps(self, indent=indent)
def model_dump(self):
return self
class DummyArtifactManager:
async def create_llm_artifact(self, *args, **kwargs):
@@ -49,27 +53,36 @@ async def test_openrouter_basic_completion(monkeypatch):
@pytest.mark.asyncio
async def test_openrouter_dynamic_model(monkeypatch):
settings = Settings(
ENABLE_OPENROUTER=True,
OPENROUTER_API_KEY="key",
OPENROUTER_MODEL="base-model",
LLM_KEY="OPENROUTER",
)
SettingsManager.set_settings(settings)
# Update settings via monkeypatch to ensure config_registry sees them
monkeypatch.setattr(config.settings, "ENABLE_OPENROUTER", True)
monkeypatch.setattr(config.settings, "OPENROUTER_API_KEY", "key")
monkeypatch.setattr(config.settings, "OPENROUTER_MODEL", "base-model")
monkeypatch.setattr(config.settings, "OPENROUTER_API_BASE", "https://openrouter.ai/api/v1")
# Clear existing configs before reload
config_registry.LLMConfigRegistry._configs.clear()
importlib.reload(config_registry)
monkeypatch.setattr(app, "ARTIFACT_MANAGER", DummyArtifactManager())
# Mock the AsyncOpenAI client
async_mock = AsyncMock(return_value=DummyResponse('{"status": "ok"}'))
monkeypatch.setattr(api_handler_factory.litellm, "acompletion", async_mock)
mock_client = MagicMock()
mock_client.chat.completions.create = async_mock
# Patch AsyncOpenAI to return our mock client
monkeypatch.setattr(api_handler_factory, "AsyncOpenAI", lambda **kwargs: mock_client)
base_handler = api_handler_factory.LLMAPIHandlerFactory.get_llm_api_handler("OPENROUTER")
override_handler = api_handler_factory.LLMAPIHandlerFactory.get_override_llm_api_handler(
"openrouter/other-model", default=base_handler
)
result = await override_handler("hi", "test")
assert result == {"status": "ok"}
called_model = async_mock.call_args.kwargs.get("model")
assert called_model == "openrouter/other-model"
assert called_model == "other-model"
@pytest.mark.asyncio