AgentOps-AI / tokencost

Easy token price estimates for 400+ LLMs. TokenOps.
https://agentops.ai
MIT License
1.48k stars 61 forks source link

Poor Internet connection will result in an ImportError #51

Closed scrpr closed 5 months ago

scrpr commented 5 months ago

When I try to import this module, it crashes with this:

ERROR:root:Failed to update TOKEN_COSTS: Cannot connect to host raw.githubusercontent.com:443 ssl:default [getaddrinfo failed]
Traceback (most recent call last):
  File "pre_translate.py", line 9, in <module>
    from tokencost import count_message_tokens, count_string_tokens
  File "site-packages\tokencost\__init__.py", line 1, in <module>
    from .costs import (
  File "site-packages\tokencost\costs.py", line 7, in <module>
    from .constants import TOKEN_COSTS
ImportError: cannot import name 'TOKEN_COSTS' from 'tokencost.constants' (site-packages\tokencost\constants.py)

I believe it is caused by:

async def update_token_costs():
    """Update the TOKEN_COSTS dictionary with the latest costs from the LiteLLM cost tracker asynchronously."""
    global TOKEN_COSTS
    try:
        TOKEN_COSTS = await fetch_costs()
    except Exception as e:
        logging.error(f"Failed to update TOKEN_COSTS: {e}")

This function simply not throwing any exception so that

try:
    asyncio.run(update_token_costs())
except Exception:
    logging.error('Failed to update token costs. Using static costs.')
    TOKEN_COSTS = TOKEN_COSTS_STATIC

This block won't catch anything. TLDR. #52 fixes this by adding a raise.

areibman commented 5 months ago

The title of this issue is hilarious 😂

Looking at your PR now, thanks!