eigent/backend/app/model/model_platform.py

146 lines
5.5 KiB
Python

# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
from typing import Annotated, Final
from pydantic import BeforeValidator
PLATFORM_ALIAS_MAPPING: Final[dict[str, str]] = {
"z.ai": "zhipuai",
"ant-ling": "openai-compatible-model",
"ModelArk": "openai-compatible-model",
"grok": "openai-compatible-model",
"ernie": "qianfan",
"llama.cpp": "openai-compatible-model",
"nebius": "openai-compatible-model",
"orcarouter": "openai-compatible-model",
}
# Bedrock Converse requires a region during model initialization.
BEDROCK_CONVERSE_REGION: Final[str] = "us-west-2"
# Azure OpenAI requires an api_version. The cloud proxy accepts any modern
# version; this default keeps cloud-mode requests working when the frontend
# does not surface api_version in extra_params.
AZURE_DEFAULT_API_VERSION: Final[str] = "2024-10-21"
EIGENT_CLOUD_MODEL_ENDPOINT_MARKERS: Final[tuple[str, ...]] = (
"eigent-proxy",
"proxy.eigent.ai",
)
OPENAI_COMPATIBLE_MODEL_PLATFORM: Final[str] = "openai-compatible-model"
# Azure's GPT-5.6 model family rejects function tools combined with
# `reasoning_effort` on the legacy chat-completions transport. CAMEL supports
# Azure's Responses API explicitly, so select that transport for this known
# incompatible combination instead of asking users to disable either tools or
# thinking effort.
AZURE_RESPONSES_REASONING_TOOL_MODEL_PREFIXES: Final[tuple[str, ...]] = (
"gpt-5.6",
)
def patch_bedrock_cloud_config(
api_url: str, extra_params: dict
) -> tuple[str, dict]:
"""Patch API URL and extra_params for Bedrock Converse in cloud mode.
Appends '/bedrock' to the proxy URL and defaults the region.
Returns the updated (api_url, extra_params).
"""
extra_params = dict(extra_params)
extra_params.setdefault("region_name", BEDROCK_CONVERSE_REGION)
if not api_url.rstrip("/").endswith("/bedrock"):
api_url = api_url + "/bedrock"
return api_url, extra_params
def patch_azure_cloud_config(extra_params: dict) -> dict:
"""Default Azure `api_version` for cloud mode.
The cloud proxy fronts Azure OpenAI but the frontend sends an empty
`extra_params` for cloud, leaving `api_version` unset. Camel's
`AzureOpenAIModel` raises if neither the kwarg nor `AZURE_API_VERSION`
env var is provided — inject a sensible default here so cloud-mode
GPT models (gpt-5.4, gpt-5.5, gpt-5-mini, ...) construct cleanly.
"""
extra_params = dict(extra_params)
extra_params.setdefault("api_version", AZURE_DEFAULT_API_VERSION)
return extra_params
def is_eigent_cloud_model_endpoint(api_url: object) -> bool:
"""Return whether ``api_url`` targets Eigent's model proxy."""
return isinstance(api_url, str) and any(
marker in api_url for marker in EIGENT_CLOUD_MODEL_ENDPOINT_MARKERS
)
def resolve_cloud_model_runtime_platform(
*, model_platform: str, api_url: object, api_mode: object
) -> str:
"""Select the client protocol used to call an Eigent Cloud model.
Eigent Cloud exposes an OpenAI-compatible Responses endpoint even when
the selected deployment is backed by Azure. Using ``AzureOpenAI`` for
that request produces ``/openai/responses``; LiteLLM reserves that path
for OpenAI pass-through. The proxy's routed endpoint is ``/responses``
(or ``/v1/responses``), which is generated by the OpenAI-compatible
client. Direct customer Azure endpoints retain the native Azure client.
"""
normalized_platform = normalize_model_platform(model_platform)
if (
normalized_platform == "azure"
and api_mode == "responses"
and is_eigent_cloud_model_endpoint(api_url)
):
return OPENAI_COMPATIBLE_MODEL_PLATFORM
return normalized_platform
def azure_reasoning_tools_require_responses_api(
*, model_platform: str, model_type: str
) -> bool:
"""Return whether Azure requires Responses for reasoning plus tools.
Keep this compatibility rule transport-specific and model-family-specific:
older Azure deployments continue using chat completions unless their own
configuration explicitly opts into Responses.
"""
normalized_platform = model_platform.strip().lower()
normalized_model = model_type.strip().lower()
return normalized_platform == "azure" and normalized_model.startswith(
AZURE_RESPONSES_REASONING_TOOL_MODEL_PREFIXES
)
def normalize_model_platform(platform: str) -> str:
"""Normalize provider aliases to supported model platform names."""
return PLATFORM_ALIAS_MAPPING.get(platform, platform)
def normalize_optional_model_platform(platform: str | None) -> str | None:
"""Optional variant of normalize_model_platform."""
if platform is None:
return None
return normalize_model_platform(platform)
NormalizedModelPlatform = Annotated[
str, BeforeValidator(normalize_model_platform)
]
NormalizedOptionalModelPlatform = Annotated[
str | None, BeforeValidator(normalize_optional_model_platform)
]