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

Commit 0bec802

Browse files
Remove code for mapping origin to destination in muxing (#1010)
The initial muxing implementation assumed that we would need to transform the input body from one provider to another. e.g. from athropic to openai. This changed. The final implementation is assuming that is receiving OpenAI format and will respond with OpenAI format. This PR is removing unused code.
1 parent 9f542c3 commit 0bec802

File tree

2 files changed

+5
-55
lines changed

2 files changed

+5
-55
lines changed

src/codegate/muxing/adapter.py

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,12 @@ class MuxingAdapterError(Exception):
2222

2323
class BodyAdapter:
2424
"""
25-
Map the body between OpenAI and Anthropic.
25+
Format the body to the destination provider format.
2626
27-
TODO: This are really knaive implementations. We should replace them with more robust ones.
27+
We expect the body to always be in OpenAI format. We need to configure the client
28+
to send and expect OpenAI format. Here we just need to set the destination provider info.
2829
"""
2930

30-
def _from_openai_to_antrhopic(self, openai_body: dict) -> dict:
31-
"""Map the OpenAI body to the Anthropic body."""
32-
new_body = copy.deepcopy(openai_body)
33-
messages = new_body.get("messages", [])
34-
system_prompt = None
35-
system_msg_idx = None
36-
if messages:
37-
for i_msg, msg in enumerate(messages):
38-
if msg.get("role", "") == "system":
39-
system_prompt = msg.get("content")
40-
system_msg_idx = i_msg
41-
break
42-
if system_prompt:
43-
new_body["system"] = system_prompt
44-
if system_msg_idx is not None:
45-
del messages[system_msg_idx]
46-
return new_body
47-
48-
def _from_anthropic_to_openai(self, anthropic_body: dict) -> dict:
49-
"""Map the Anthropic body to the OpenAI body."""
50-
new_body = copy.deepcopy(anthropic_body)
51-
system_prompt = anthropic_body.get("system")
52-
messages = new_body.get("messages", [])
53-
if system_prompt:
54-
messages.insert(0, {"role": "system", "content": system_prompt})
55-
if "system" in new_body:
56-
del new_body["system"]
57-
return new_body
58-
5931
def _get_provider_formatted_url(self, model_route: rulematcher.ModelRoute) -> str:
6032
"""Get the provider formatted URL to use in base_url. Note this value comes from DB"""
6133
if model_route.endpoint.provider_type in [
@@ -65,35 +37,13 @@ def _get_provider_formatted_url(self, model_route: rulematcher.ModelRoute) -> st
6537
return f"{model_route.endpoint.endpoint}/v1"
6638
return model_route.endpoint.endpoint
6739

68-
def _set_destination_info(self, data: dict, model_route: rulematcher.ModelRoute) -> dict:
40+
def set_destination_info(self, model_route: rulematcher.ModelRoute, data: dict) -> dict:
6941
"""Set the destination provider info."""
7042
new_data = copy.deepcopy(data)
7143
new_data["model"] = model_route.model.name
7244
new_data["base_url"] = self._get_provider_formatted_url(model_route)
7345
return new_data
7446

75-
def _identify_provider(self, data: dict) -> db_models.ProviderType:
76-
"""Identify the request provider."""
77-
if "system" in data:
78-
return db_models.ProviderType.anthropic
79-
else:
80-
return db_models.ProviderType.openai
81-
82-
def map_body_to_dest(self, model_route: rulematcher.ModelRoute, data: dict) -> dict:
83-
"""
84-
Map the body to the destination provider.
85-
86-
We only need to transform the body if the destination or origin provider is Anthropic.
87-
"""
88-
origin_prov = self._identify_provider(data)
89-
if model_route.endpoint.provider_type == db_models.ProviderType.anthropic:
90-
if origin_prov != db_models.ProviderType.anthropic:
91-
data = self._from_openai_to_antrhopic(data)
92-
else:
93-
if origin_prov == db_models.ProviderType.anthropic:
94-
data = self._from_anthropic_to_openai(data)
95-
return self._set_destination_info(data, model_route)
96-
9747

9848
class StreamChunkFormatter:
9949
"""

src/codegate/muxing/router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def route_to_dest_provider(
9797

9898
# 2. Map the request body to the destination provider format.
9999
rest_of_path = self._ensure_path_starts_with_slash(rest_of_path)
100-
new_data = self._body_adapter.map_body_to_dest(model_route, data)
100+
new_data = self._body_adapter.set_destination_info(model_route, data)
101101

102102
# 3. Run pipeline. Selecting the correct destination provider.
103103
provider = self._provider_registry.get_provider(model_route.endpoint.provider_type)

0 commit comments

Comments
 (0)