Compare commits

...
Author SHA1 Message Date
9qeklajc f160384e14 price 2025-12-21 21:06:38 +01:00
9qeklajc 4ee6801413 price 2025-12-21 21:03:03 +01:00
9qeklajc e4153920a9 price 2025-12-21 20:58:52 +01:00
9qeklajc 1b4e092242 add print 2025-12-21 12:33:14 +01:00
9qeklajc 5416cefd87 use cost field if available 2025-12-20 16:16:23 +01:00
shroominicandGitHub 8edc3512c1 Merge pull request #258 from Routstr/remove-flaky-wallet-tests
Remove flaky wallet tests
2025-12-19 10:54:26 +01:00
3 changed files with 62 additions and 6 deletions
+44 -2
View File
@@ -5,6 +5,7 @@ from pydantic.v1 import BaseModel
from ..core import get_logger
from ..core.db import AsyncSession
from ..core.settings import settings
from .price import sats_usd_price
logger = get_logger(__name__)
@@ -64,6 +65,42 @@ async def calculate_cost( # todo: can be sync
)
return cost_data
usage_data = response_data["usage"]
if "cost" in usage_data and usage_data["cost"] is not None:
try:
usd_cost = float(usage_data["cost"])
sats_per_usd = 1.0 / sats_usd_price()
cost_in_sats = usd_cost * sats_per_usd
cost_in_msats = math.ceil(cost_in_sats * 1000)
logger.info(
"Using cost field from usage data",
extra={
"usd_cost": usd_cost,
"cost_in_sats": cost_in_sats,
"cost_in_msats": cost_in_msats,
"model": response_data.get("model", "unknown"),
},
)
return CostData(
base_msats=0,
input_msats=0, # Cost field doesn't break down by token type
output_msats=0,
total_msats=cost_in_msats,
)
except Exception as e:
logger.warning(
"Error using cost field, falling back to token-based calculation",
extra={
"error": str(e),
"cost_value": usage_data.get("cost"),
"model": response_data.get("model", "unknown"),
},
)
# Fall through to token-based calculation
MSATS_PER_1K_INPUT_TOKENS: float = (
float(settings.fixed_per_1k_input_tokens) * 1000.0
)
@@ -129,10 +166,15 @@ async def calculate_cost( # todo: can be sync
)
return cost_data
input_tokens = response_data.get("usage", {}).get("prompt_tokens", 0)
output_tokens = response_data.get("usage", {}).get("completion_tokens", 0)
input_tokens = usage_data.get("prompt_tokens", 0)
output_tokens = usage_data.get("completion_tokens", 0)
# added for response api
input_tokens = input_tokens if input_tokens != 0 else usage_data.get("input_tokens", 0)
output_tokens = output_tokens if output_tokens != 0 else usage_data.get("output_tokens", 0)
input_msats = round(input_tokens / 1000 * MSATS_PER_1K_INPUT_TOKENS, 3)
output_msats = round(output_tokens / 1000 * MSATS_PER_1K_OUTPUT_TOKENS, 3)
token_based_cost = math.ceil(input_msats + output_msats)
+1
View File
@@ -528,4 +528,5 @@ async def models(session: AsyncSession = Depends(get_session)) -> dict:
from ..proxy import get_unique_models
items = get_unique_models()
print(items)
return {"data": items}
+17 -4
View File
@@ -85,6 +85,7 @@ async def _fetch_btc_usd_price() -> float:
_binance_btc_usdt(client),
)
valid_prices = [price for price in prices if price is not None]
print(valid_prices)
if not valid_prices:
logger.error("No valid BTC prices obtained from any exchange")
raise ValueError("Unable to fetch BTC price from any exchange")
@@ -102,14 +103,26 @@ async def _update_prices() -> None:
global BTC_USD_PRICE, SATS_USD_PRICE
try:
btc_price = await _fetch_btc_usd_price()
BTC_USD_PRICE = btc_price
SATS_USD_PRICE = btc_price / 100_000_000
logger.info(
"Updated BTC/SATS prices successfully",
extra={"btc_usd": btc_price, "sats_usd": SATS_USD_PRICE},
)
except Exception as e:
logger.warning(
"Skipping price update; unable to fetch BTC price",
"Failed to fetch live prices, using fallback if available",
extra={"error": str(e), "error_type": type(e).__name__},
)
return
BTC_USD_PRICE = btc_price
SATS_USD_PRICE = btc_price / 100_000_000
# Use fallback price if no price is set
if BTC_USD_PRICE is None or SATS_USD_PRICE is None:
fallback_btc_price = getattr(settings, "fallback_btc_usd_price", 50000.0)
BTC_USD_PRICE = fallback_btc_price
SATS_USD_PRICE = fallback_btc_price / 100_000_000
logger.info(
"Using fallback BTC price",
extra={"fallback_btc_usd": fallback_btc_price, "sats_usd": SATS_USD_PRICE},
)
def btc_usd_price() -> float: