66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
# SPDX-FileCopyrightText: 2026 Art Sea LLC
|
|
#
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Callable
|
|
|
|
from band_aider.ai_client.completion_client import CompletionClient, OpenAIConfig
|
|
from band_aider.ai_client.openai_client_kind import OpenAIClientKind
|
|
from band_aider.ai_client.openai_compatible_client import OpenAICompatibleClient
|
|
from band_aider.ai_client.openai_responses_client import OpenAIResponsesClient
|
|
|
|
def _compatible_builder(
|
|
config: OpenAIConfig,
|
|
) -> CompletionClient:
|
|
return OpenAICompatibleClient(
|
|
model=config.model,
|
|
api_base=config.api_base,
|
|
api_key=config.api_key,
|
|
temperature=config.temperature,
|
|
max_tokens=config.max_tokens,
|
|
max_attempts=config.max_attempts,
|
|
retry_temperature_step=config.retry_temperature_step,
|
|
max_retry_temperature=config.max_retry_temperature,
|
|
verify_ssl=config.verify_ssl,
|
|
)
|
|
|
|
|
|
def _responses_builder(
|
|
config: OpenAIConfig,
|
|
) -> CompletionClient:
|
|
return OpenAIResponsesClient(
|
|
model=config.model,
|
|
api_base=config.api_base,
|
|
api_key=config.api_key,
|
|
temperature=config.temperature,
|
|
max_tokens=config.max_tokens,
|
|
max_attempts=config.max_attempts,
|
|
retry_temperature_step=config.retry_temperature_step,
|
|
max_retry_temperature=config.max_retry_temperature,
|
|
verify_ssl=config.verify_ssl,
|
|
)
|
|
|
|
|
|
_PROXY_BUILDERS: dict[OpenAIClientKind, Callable[[OpenAIConfig], CompletionClient]] = {
|
|
OpenAIClientKind.COMPLETION: _compatible_builder,
|
|
OpenAIClientKind.RESPONSE: _responses_builder,
|
|
}
|
|
|
|
|
|
class OpenAIProxyFactory:
|
|
"""OpenAIProxyFactory constructs root OpenAI-compatible proxies from configs."""
|
|
|
|
@classmethod
|
|
def from_config(cls, config: OpenAIConfig) -> CompletionClient:
|
|
"""Return one proxy from one config."""
|
|
|
|
builder = _PROXY_BUILDERS.get(config.client, _PROXY_BUILDERS[OpenAIClientKind.COMPLETION])
|
|
return builder(config)
|
|
|
|
@classmethod
|
|
def from_configs(cls, configs: tuple[OpenAIConfig, ...]) -> tuple[CompletionClient, ...]:
|
|
"""Return one proxy per config in order."""
|
|
|
|
return tuple(cls.from_config(config) for config in configs)
|