Spaces:
Paused
Paused
Commit ·
9fde8ed
1
Parent(s): 93e5da8
added [pay] prefix for preview openai mode. removed model check
Browse filesFiles changed (2)
- app/routes/chat_api.py+17-40
- app/routes/models_api.py+21-12
35 unmodified lines363738394041424344454647484950515253545556575859606162636465666768697071727374757677785 unmodified lines8485868788899028 unmodified lines11912012112212312412512612712812935 unmodified linescredential_manager_instance = fastapi_request.app.state.credential_managerOPENAI_DIRECT_SUFFIX = "-openai"EXPERIMENTAL_MARKER = "-exp-"# Dynamically fetch allowed models for validationvertex_model_ids = await get_vertex_models()# Suffixes that can be appended to base models.# The remote model config should ideally be the source of truth for all valid permutations.standard_suffixes = ["-search", "-encrypt", "-encrypt-full", "-auto"]# No longer using special_suffix_map, will use prefix check insteadall_allowed_model_ids = set(vertex_model_ids) # Start with base models from configfor base_id in vertex_model_ids: # Iterate over base models to add suffixed versions# Apply standard suffixes only if not gemini-2.0if not base_id.startswith("gemini-2.0"):for suffix in standard_suffixes:all_allowed_model_ids.add(f"{base_id}{suffix}")# Apply special suffixes for models starting with "gemini-2.5-flash"if base_id.startswith("gemini-2.5-flash"):special_flash_suffixes = ["-nothinking", "-max"]for special_suffix in special_flash_suffixes:all_allowed_model_ids.add(f"{base_id}{special_suffix}")# Add express models to the allowed list as well.# These should be full names from the remote config.vertex_express_model_ids = await get_vertex_express_models()all_allowed_model_ids.update(vertex_express_model_ids)# Add potential -openai models if they contain -exp-potential_openai_direct_models = set()for base_id in vertex_model_ids: # vertex_model_ids are base modelsif EXPERIMENTAL_MARKER in base_id:potential_openai_direct_models.add(f"{base_id}{OPENAI_DIRECT_SUFFIX}")all_allowed_model_ids.update(potential_openai_direct_models)if not request.model or request.model not in all_allowed_model_ids:return JSONResponse(status_code=400, content=create_openai_error_response(400, f"Model '{request.model}' not found or not supported by this adapter. Valid models are: {sorted(list(all_allowed_model_ids))}", "invalid_request_error"))is_openai_direct_model = request.model.endswith(OPENAI_DIRECT_SUFFIX) and EXPERIMENTAL_MARKER in request.modelis_auto_model = request.model.endswith("-auto")is_grounded_search = request.model.endswith("-search")is_encrypted_model = request.model.endswith("-encrypt")5 unmodified lines# Determine base_model_name by stripping known suffixes# This order matters if a model could have multiple (e.g. -encrypt-auto, though not currently a pattern)if is_openai_direct_model:base_model_name = request.model[:-len(OPENAI_DIRECT_SUFFIX)]elif is_auto_model: base_model_name = request.model[:-len("-auto")]elif is_grounded_search: base_model_name = request.model[:-len("-search")]elif is_encrypted_full_model: base_model_name = request.model[:-len("-encrypt-full")] # Must be before -encrypt28 unmodified linesif client_to_use is None:print(f"WARNING: All {len(express_api_keys_list)} Vertex Express API key(s) failed to initialize for model {base_model_name}. Falling back.")# else:# if not express_api_keys_list:# print(f"DEBUG: No Vertex Express API keys configured. Skipping Express Mode attempt for model {base_model_name}.")# elif base_model_name not in vertex_express_model_ids:# print(f"DEBUG: Model {base_model_name} is not in the Vertex Express model list. Skipping Express Mode attempt.")if client_to_use is None:rotated_credentials, rotated_project_id = credential_manager_instance.get_random_credentials()35 unmodified lines3637383940414243444546474849505152535455565 unmodified lines626364656667686970717228 unmodified lines10110210310410510635 unmodified linescredential_manager_instance = fastapi_request.app.state.credential_managerOPENAI_DIRECT_SUFFIX = "-openai"EXPERIMENTAL_MARKER = "-exp-"PAY_PREFIX = "[PAY]"# Model validation based on a predefined list has been removed as per user request.# The application will now attempt to use any provided model string.# We still need to fetch vertex_express_model_ids for the Express Mode logic.vertex_express_model_ids = await get_vertex_express_models()# Updated logic for is_openai_direct_modelis_openai_direct_model = Falseif request.model.endswith(OPENAI_DIRECT_SUFFIX):temp_name_for_marker_check = request.model[:-len(OPENAI_DIRECT_SUFFIX)]if temp_name_for_marker_check.startswith(PAY_PREFIX):is_openai_direct_model = Trueelif EXPERIMENTAL_MARKER in temp_name_for_marker_check:is_openai_direct_model = Trueis_auto_model = request.model.endswith("-auto")is_grounded_search = request.model.endswith("-search")is_encrypted_model = request.model.endswith("-encrypt")5 unmodified lines# Determine base_model_name by stripping known suffixes# This order matters if a model could have multiple (e.g. -encrypt-auto, though not currently a pattern)if is_openai_direct_model:temp_base_name = request.model[:-len(OPENAI_DIRECT_SUFFIX)]if temp_base_name.startswith(PAY_PREFIX):base_model_name = temp_base_name[len(PAY_PREFIX):]else:base_model_name = temp_base_nameelif is_auto_model: base_model_name = request.model[:-len("-auto")]elif is_grounded_search: base_model_name = request.model[:-len("-search")]elif is_encrypted_full_model: base_model_name = request.model[:-len("-encrypt-full")] # Must be before -encrypt28 unmodified linesif client_to_use is None:print(f"WARNING: All {len(express_api_keys_list)} Vertex Express API key(s) failed to initialize for model {base_model_name}. Falling back.")if client_to_use is None:rotated_credentials, rotated_project_id = credential_manager_instance.get_random_credentials()
13 unmodified lines14151617181963 unmodified lines838485868788899091929394959697989910010113 unmodified linesOPENAI_DIRECT_SUFFIX = "-openai"EXPERIMENTAL_MARKER = "-exp-"# Access credential_manager from app statecredential_manager_instance: CredentialManager = fastapi_request.app.state.credential_manager63 unmodified lines})# Ensure uniqueness again after adding suffixes# Add OpenAI direct variations for experimental models if SA creds are availableif has_sa_creds: # OpenAI direct mode only works with SA credentials# We should iterate through the base models that could be experimental.# `raw_vertex_models` should contain these.for model_id in raw_vertex_models: # Iterate through the original list of base modelsif EXPERIMENTAL_MARKER in model_id:suffixed_id = f"{model_id}{OPENAI_DIRECT_SUFFIX}"# Check if already added (e.g. if remote config somehow already listed it)if not any(m['id'] == suffixed_id for m in dynamic_models_data):dynamic_models_data.append({"id": suffixed_id, "object": "model", "created": current_time, "owned_by": "google","permission": [], "root": model_id, "parent": None})# final_models_data_map = {m["id"]: m for m in dynamic_models_data}# model_list = list(final_models_data_map.values())# model_list.sort()13 unmodified lines1415161718192063 unmodified lines8485868788899091929394959697989910010110210310410510610710810911013 unmodified linesOPENAI_DIRECT_SUFFIX = "-openai"EXPERIMENTAL_MARKER = "-exp-"PAY_PREFIX = "[PAY]"# Access credential_manager from app statecredential_manager_instance: CredentialManager = fastapi_request.app.state.credential_manager63 unmodified lines})# Ensure uniqueness again after adding suffixes# Add OpenAI direct variations if SA creds are availableif has_sa_creds: # OpenAI direct mode only works with SA credentials# `all_model_ids` contains the comprehensive list of base models that are eligible based on current credentials# We iterate through this to determine which ones get an -openai variation.# `raw_vertex_models` is used here to ensure we only add -openai suffix to models that are# fundamentally Vertex models, not just any model that might appear in `all_model_ids` (e.g. from Express list exclusively)# if express only key is provided.# We iterate through the base models from the main Vertex list.for base_model_id_for_openai in raw_vertex_models: # Iterate through original list of GAIA/Vertex base modelsdisplay_model_id = ""if EXPERIMENTAL_MARKER in base_model_id_for_openai:display_model_id = f"{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}"else:display_model_id = f"{PAY_PREFIX}{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}"# Check if already added (e.g. if remote config somehow already listed it or added as a base model)if display_model_id and not any(m['id'] == display_model_id for m in dynamic_models_data):dynamic_models_data.append({"id": display_model_id, "object": "model", "created": current_time, "owned_by": "google","permission": [], "root": base_model_id_for_openai, "parent": None})# final_models_data_map = {m["id"]: m for m in dynamic_models_data}# model_list = list(final_models_data_map.values())# model_list.sort()
app/routes/chat_api.py+17-40
app/routes/models_api.py+21-12