Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Fix appropriately setting the route for openrouter when muxing #1029

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions src/codegate/muxing/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ class BodyAdapter:

def _get_provider_formatted_url(self, model_route: rulematcher.ModelRoute) -> str:
"""Get the provider formatted URL to use in base_url. Note this value comes from DB"""
if model_route.endpoint.provider_type in [
db_models.ProviderType.openai,
db_models.ProviderType.openrouter,
]:
if model_route.endpoint.provider_type == db_models.ProviderType.openai:
return urljoin(model_route.endpoint.endpoint, "/v1")
if model_route.endpoint.provider_type == db_models.ProviderType.openrouter:
return urljoin(model_route.endpoint.endpoint, "/api/v1")
return model_route.endpoint.endpoint

def set_destination_info(self, model_route: rulematcher.ModelRoute, data: dict) -> dict:
Expand Down Expand Up @@ -199,7 +198,8 @@ def _format_antropic(self, chunk: str) -> str:
],
)
return open_ai_chunk.model_dump_json(exclude_none=True, exclude_unset=True)
except Exception:
except Exception as e:
logger.warning(f"Error formatting Anthropic chunk: {chunk}. Error: {e}")
return cleaned_chunk.strip()


Expand Down
Empty file added tests/muxing/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions tests/muxing/test_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from codegate.db.models import ProviderType
from codegate.muxing.adapter import BodyAdapter


class MockedEndpoint:
def __init__(self, provider_type: ProviderType, endpoint_route: str):
self.provider_type = provider_type
self.endpoint = endpoint_route


class MockedModelRoute:
def __init__(self, provider_type: ProviderType, endpoint_route: str):
self.endpoint = MockedEndpoint(provider_type, endpoint_route)


@pytest.mark.parametrize(
"provider_type, endpoint_route, expected_route",
[
(ProviderType.openai, "https://api.openai.com/", "https://api.openai.com/v1"),
(ProviderType.openrouter, "https://openrouter.ai/api", "https://openrouter.ai/api/v1"),
(ProviderType.openrouter, "https://openrouter.ai/", "https://openrouter.ai/api/v1"),
(ProviderType.ollama, "http://localhost:11434", "http://localhost:11434"),
],
)
def test_catch_all(provider_type, endpoint_route, expected_route):
body_adapter = BodyAdapter()
model_route = MockedModelRoute(provider_type, endpoint_route)
actual_route = body_adapter._get_provider_formatted_url(model_route)
assert actual_route == expected_route