@@ -22,40 +22,12 @@ class MuxingAdapterError(Exception):
22
22
23
23
class BodyAdapter :
24
24
"""
25
- Map the body between OpenAI and Anthropic .
25
+ Format the body to the destination provider format .
26
26
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.
28
29
"""
29
30
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
-
59
31
def _get_provider_formatted_url (self , model_route : rulematcher .ModelRoute ) -> str :
60
32
"""Get the provider formatted URL to use in base_url. Note this value comes from DB"""
61
33
if model_route .endpoint .provider_type in [
@@ -65,35 +37,13 @@ def _get_provider_formatted_url(self, model_route: rulematcher.ModelRoute) -> st
65
37
return f"{ model_route .endpoint .endpoint } /v1"
66
38
return model_route .endpoint .endpoint
67
39
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 :
69
41
"""Set the destination provider info."""
70
42
new_data = copy .deepcopy (data )
71
43
new_data ["model" ] = model_route .model .name
72
44
new_data ["base_url" ] = self ._get_provider_formatted_url (model_route )
73
45
return new_data
74
46
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
-
97
47
98
48
class StreamChunkFormatter :
99
49
"""
0 commit comments