@@ -79,7 +79,7 @@ def get_proxy_target(model, path):
79
79
"""
80
80
if os .getenv ("PROXY_TARGET" ):
81
81
return os .getenv ("PROXY_TARGET" )
82
- elif model in known_chat_models and path .endswith ("/chat/completions" )
82
+ elif model in known_chat_models and path .endswith ("/chat/completions" ):
83
83
return f"https://{ location } -aiplatform.googleapis.com/v1/projects/{ project_id } /locations/{ location } /endpoints/openapi/chat/completions"
84
84
else :
85
85
return f"https://{ location } -aiplatform.googleapis.com/v1/projects/{ project_id } /locations/{ location } /{ model } :rawPredict"
@@ -114,12 +114,12 @@ def to_vertex_anthropic(openai_messages):
114
114
"messages" : message
115
115
}
116
116
117
- def from_anthropic_to_openai_response (msg ):
117
+ def from_anthropic_to_openai_response (msg , model ):
118
118
msg_json = json .loads (msg )
119
119
return json .dumps ({
120
120
"id" : msg_json ["id" ],
121
121
"object" : "chat.completion" ,
122
- "model" : msg_json . get ( " model" , "claude" ) ,
122
+ "model" : model ,
123
123
"choices" : [
124
124
{
125
125
"index" : 0 ,
@@ -136,21 +136,23 @@ def from_anthropic_to_openai_response(msg):
136
136
"usage" : msg_json .get ("usage" , {})
137
137
})
138
138
139
+ def get_chat_completion_model_name (model_alias ):
140
+ if model_alias in known_chat_models :
141
+ # publishers/google/models/gemini-2.0-flash-lite-001 -> "google/gemini-2.0-flash-lite-001"
142
+ model_alias = model_alias .replace ("publishers/" , "" ).replace ("models/" , "" )
143
+
144
+ return model_alias
145
+
139
146
async def handle_proxy (request : Request , path : str ):
140
147
try :
141
148
content = await request .body ()
142
149
content_json = json .loads (content )
150
+ model_alias = content_json .get ("model" , "default" )
151
+ model = get_model ("gcp" , model_alias )
143
152
144
153
if USE_MODEL_MAPPING :
145
154
if "model" in content_json :
146
- request_model = content_json .get ("model" , None )
147
- model = get_model ("gcp" , request_model )
148
- model_name = model
149
-
150
- if model != None and model != request_model and "publishers/google/" in model :
151
- model_name = f"google/{ model .split ('/' )[- 1 ]} "
152
-
153
- content_json ["model" ]= model_name
155
+ content_json ["model" ]= get_chat_completion_model_name (model )
154
156
155
157
needs_conversion = False
156
158
if not model in known_chat_models :
@@ -175,7 +177,7 @@ async def handle_proxy(request: Request, path: str):
175
177
if needs_conversion :
176
178
# convert vertex response to openai format
177
179
if "anthropic" in model :
178
- content = from_anthropic_to_openai_response (response .content )
180
+ content = from_anthropic_to_openai_response (response .content , model_alias )
179
181
180
182
except httpx .RequestError as e :
181
183
logging .error (f"Proxy request failed: { e } " )
0 commit comments