|
25 | 25 | Pricing, |
26 | 26 | _calculate_usd_max_costs, |
27 | 27 | _update_model_sats_pricing, |
28 | | - async_fetch_openrouter_models, |
29 | 28 | ) |
30 | 29 | from ..payment.price import sats_usd_price |
31 | 30 | from ..wallet import recieve_token, send_token |
32 | 31 |
|
33 | 32 | logger = get_logger(__name__) |
34 | 33 |
|
35 | 34 |
|
36 | | -class UpstreamProvider: |
| 35 | +class BaseUpstreamProvider: |
37 | 36 | """Provider for forwarding requests to an upstream AI service API.""" |
38 | 37 |
|
39 | 38 | base_url: str |
@@ -1702,111 +1701,3 @@ def get_cached_model_by_id(self, model_id: str) -> Model | None: |
1702 | 1701 | Model object or None if not found |
1703 | 1702 | """ |
1704 | 1703 | return self._models_by_id.get(model_id) |
1705 | | - |
1706 | | - |
1707 | | -class OpenAIUpstreamProvider(UpstreamProvider): |
1708 | | - """Upstream provider specifically configured for OpenAI API.""" |
1709 | | - |
1710 | | - def __init__(self, api_key: str, provider_fee: float = 1.01): |
1711 | | - self.upstream_name = "openai" |
1712 | | - super().__init__( |
1713 | | - base_url="https://api.openai.com/v1", |
1714 | | - api_key=api_key, |
1715 | | - provider_fee=provider_fee, |
1716 | | - ) |
1717 | | - |
1718 | | - def transform_model_name(self, model_id: str) -> str: |
1719 | | - """Strip 'openai/' prefix for OpenAI API compatibility.""" |
1720 | | - return model_id.removeprefix("openai/") |
1721 | | - |
1722 | | - async def fetch_models(self) -> list[Model]: |
1723 | | - """Fetch OpenAI models from OpenRouter API filtered by openai source.""" |
1724 | | - models_data = await async_fetch_openrouter_models(source_filter="openai") |
1725 | | - return [Model(**model) for model in models_data] # type: ignore |
1726 | | - |
1727 | | - |
1728 | | -class AnthropicUpstreamProvider(UpstreamProvider): |
1729 | | - """Upstream provider specifically configured for Anthropic API.""" |
1730 | | - |
1731 | | - def __init__(self, api_key: str, provider_fee: float = 1.01): |
1732 | | - self.upstream_name = "anthropic" |
1733 | | - super().__init__( |
1734 | | - base_url="https://api.anthropic.com/v1", |
1735 | | - api_key=api_key, |
1736 | | - provider_fee=provider_fee, |
1737 | | - ) |
1738 | | - |
1739 | | - def transform_model_name(self, model_id: str) -> str: |
1740 | | - """Strip 'anthropic/' prefix for Anthropic API compatibility.""" |
1741 | | - return model_id.removeprefix("anthropic/") |
1742 | | - |
1743 | | - async def fetch_models(self) -> list[Model]: |
1744 | | - """Fetch Anthropic models from OpenRouter API filtered by anthropic source.""" |
1745 | | - models_data = await async_fetch_openrouter_models(source_filter="anthropic") |
1746 | | - return [Model(**model) for model in models_data] # type: ignore |
1747 | | - |
1748 | | - |
1749 | | -class AzureUpstreamProvider(UpstreamProvider): |
1750 | | - """Upstream provider specifically configured for Azure OpenAI Service.""" |
1751 | | - |
1752 | | - def __init__( |
1753 | | - self, |
1754 | | - base_url: str, |
1755 | | - api_key: str, |
1756 | | - api_version: str, |
1757 | | - provider_fee: float = 1.01, |
1758 | | - ): |
1759 | | - """Initialize Azure provider with API key and version. |
1760 | | -
|
1761 | | - Args: |
1762 | | - base_url: Azure OpenAI endpoint base URL |
1763 | | - api_key: Azure OpenAI API key for authentication |
1764 | | - api_version: Azure OpenAI API version (e.g., "2024-02-15-preview") |
1765 | | - provider_fee: Provider fee multiplier (default 1.01 for 1% fee) |
1766 | | - """ |
1767 | | - super().__init__( |
1768 | | - base_url=base_url, |
1769 | | - api_key=api_key, |
1770 | | - provider_fee=provider_fee, |
1771 | | - ) |
1772 | | - self.api_version = api_version |
1773 | | - |
1774 | | - def prepare_params( |
1775 | | - self, path: str, query_params: Mapping[str, str] | None |
1776 | | - ) -> Mapping[str, str]: |
1777 | | - """Prepare query parameters for Azure OpenAI, adding API version. |
1778 | | -
|
1779 | | - Args: |
1780 | | - path: Request path |
1781 | | - query_params: Original query parameters from the client |
1782 | | -
|
1783 | | - Returns: |
1784 | | - Query parameters dict with Azure API version added for chat completions |
1785 | | - """ |
1786 | | - params = dict(query_params or {}) |
1787 | | - if path.endswith("chat/completions"): |
1788 | | - params["api-version"] = self.api_version |
1789 | | - return params |
1790 | | - |
1791 | | - |
1792 | | -class OpenRouterUpstreamProvider(UpstreamProvider): |
1793 | | - """Upstream provider specifically configured for OpenRouter API.""" |
1794 | | - |
1795 | | - def __init__(self, api_key: str, provider_fee: float = 1.06): |
1796 | | - """Initialize OpenRouter provider with API key. |
1797 | | -
|
1798 | | - Args: |
1799 | | - api_key: OpenRouter API key for authentication |
1800 | | - provider_fee: Provider fee multiplier (default 1.06 for 6% fee) |
1801 | | - """ |
1802 | | - self.upstream_name = "openrouter" |
1803 | | - super().__init__( |
1804 | | - base_url="https://openrouter.ai/api/v1", |
1805 | | - api_key=api_key, |
1806 | | - provider_fee=provider_fee, |
1807 | | - ) |
1808 | | - |
1809 | | - async def fetch_models(self) -> list[Model]: |
1810 | | - """Fetch all OpenRouter models.""" |
1811 | | - models_data = await async_fetch_openrouter_models() |
1812 | | - return [Model(**model) for model in models_data] # type: ignore |
0 commit comments