Skip to content

Commit ccd566a

Browse files
committed
.
1 parent 7ac31c2 commit ccd566a

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

agentle/responses/pricing/default_pricing_service.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Default pricing service with static pricing data."""
22

3-
from typing import Any
4-
from rsb.models.base_model import BaseModel
3+
from typing import Any, Literal
4+
5+
from rsb.models import BaseModel, Field
56

67
from agentle.responses.pricing.modality import Modality
78

@@ -242,24 +243,16 @@ class DefaultPricingService(BaseModel):
242243
Prices are approximate and based on publicly available pricing information.
243244
"""
244245

245-
type: str = "default"
246-
247-
def __init__(
248-
self,
249-
custom_pricing: dict[str, dict[str, dict[str, float]]] | None = None,
250-
):
251-
"""
252-
Initialize the default pricing service.
246+
type: Literal["default"] = Field(default="default")
247+
custom_pricing: dict[str, dict[str, dict[str, float]]] | None = Field(default=None)
248+
pricing: dict[str, dict[str, dict[str, Any]]] = Field(default_factory=dict)
253249

254-
Args:
255-
custom_pricing: Optional dictionary to override or extend default pricing.
256-
Format: {"model": {"modality": {"input": float, "cached_input": float, "output": float}}}
257-
"""
258-
super().__init__()
250+
def model_post_init(self, context: Any, /) -> None:
251+
"""Initialize pricing after model creation."""
259252
self.pricing = MODEL_PRICING.copy()
260-
if custom_pricing:
253+
if self.custom_pricing:
261254
# Deep merge custom pricing
262-
for model, modalities in custom_pricing.items():
255+
for model, modalities in self.custom_pricing.items():
263256
if model in self.pricing:
264257
self.pricing[model].update(modalities)
265258
else:

agentle/responses/pricing/openrouter_pricing_service.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""OpenRouter pricing service with dynamic pricing from API."""
22

33
import logging
4-
from typing import Any
4+
from typing import Any, Literal
55

66
import httpx
7-
from rsb.models.base_model import BaseModel
7+
from pydantic import PrivateAttr
8+
from rsb.models import BaseModel, Field
89

910
from agentle.responses.pricing.modality import Modality
1011

@@ -28,31 +29,25 @@ class OpenRouterPricingService(BaseModel):
2829
_models_cache: Internal cache of model pricing data
2930
"""
3031

31-
type: str = "openrouter"
32+
type: Literal["openrouter"] = Field(default="openrouter")
3233
api_key: str | None = None
3334
base_url: str = "https://openrouter.ai/api/v1"
34-
http_client: httpx.AsyncClient | None = None
35+
_http_client: httpx.AsyncClient | None = PrivateAttr(default=None)
3536
_models_cache: dict[str, dict[str, Any]] | None = None
3637

37-
def __init__(
38-
self,
39-
api_key: str | None = None,
40-
base_url: str = "https://openrouter.ai/api/v1",
41-
http_client: httpx.AsyncClient | None = None,
42-
):
43-
"""
44-
Initialize the OpenRouter pricing service.
38+
@property
39+
def http_client(self) -> httpx.AsyncClient:
40+
if self._http_client is None:
41+
raise ValueError("Client is None.")
4542

46-
Args:
47-
api_key: OpenRouter API key. If not provided, reads from OPENROUTER_API_KEY env var.
48-
base_url: Base URL for OpenRouter API
49-
http_client: Optional custom HTTP client for requests
50-
"""
51-
super().__init__()
52-
self.api_key = api_key
53-
self.base_url = base_url
54-
self.http_client = http_client
55-
self._models_cache = None
43+
return self._http_client
44+
45+
def model_post_init(self, context: Any, /) -> None:
46+
super().model_post_init(context)
47+
self._http_client = httpx.AsyncClient()
48+
49+
def change_http_client(self, client: httpx.AsyncClient) -> None:
50+
self._http_client = client
5651

5752
async def _fetch_models(self) -> dict[str, dict[str, Any]]:
5853
"""
@@ -109,8 +104,7 @@ async def _fetch_models(self) -> dict[str, dict[str, Any]]:
109104
self._models_cache = {}
110105
return self._models_cache
111106
finally:
112-
if self.http_client is None:
113-
await client.aclose()
107+
await client.aclose()
114108

115109
async def get_input_price_per_million(
116110
self,

examples/openrouter_responses_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def add(a: int, b: int) -> int:
1919

2020
async def main():
2121
"""Basic text generation example."""
22+
2223
responder = Responder.openai()
2324

2425
print("Starting...")

0 commit comments

Comments
 (0)