yachty66 / openai_pricing_logger

🚀 openai_pricing_logger: Track & log OpenAI API costs with ease. A Python package for monitoring GPT-3.5-turbo & GPT-4 usage costs. Lightweight, automatic cost logging. Ideal for developers, data scientists. OpenAI cost tracking, API logging, Python, GPT models
https://pypi.org/project/openai-pricing-logger/0.0.2/
MIT License
52 stars 1 forks source link

Error when using with AzureOpenAI: TypeError: __name__ must be set to a string object #2

Closed talrejanikhil closed 2 months ago

talrejanikhil commented 2 months ago

Code to reproduce

from openai_pricing_logger import openai_api_listener

client = AzureOpenAI(
        api_key="mykey",
        azure_endpoint="my-endpoint",
        api_version="my-version",
        azure_deployment="my-deployment-name"
    )

with openai_api_listener():
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role":"user",
                "content": "Tell me a joke"
            }
        ]
    )

Stacktrace:

ypeError                                 Traceback (most recent call last)
Cell In[13], line 3
      1 from openai_pricing_logger import openai_api_listener
----> 3 with openai_api_listener():
      4     response = client.chat.completions.create(
      5         model=os.getenv(model + "OPENAI_DEPLOYMENT"),
      6         messages=[
   (...)
     11         ]
     12     )

File ~/.conda/envs/text2sql/lib/python3.10/contextlib.py:135, in _GeneratorContextManager.__enter__(self)
    133 del self.args, self.kwds, self.func
    134 try:
--> 135     return next(self.gen)
    136 except StopIteration:
    137     raise RuntimeError("generator didn't yield") from None

File ~/.conda/envs/text2sql/lib/python3.10/site-packages/openai_pricing_logger/logger.py:68, in openai_api_listener()
     65 @contextlib.contextmanager
     66 def openai_api_listener():
     67     original_create = openai.Completion.create
---> 68     openai.Completion.create = log_cost_and_timestamp(original_create)
     69     original_chat_create = openai.ChatCompletion.create
     70     openai.ChatCompletion.create = log_cost_and_timestamp(original_chat_create)

File ~/.conda/envs/text2sql/lib/python3.10/site-packages/openai_pricing_logger/logger.py:33, in log_cost_and_timestamp(func)
     31 def log_cost_and_timestamp(func):
     32     @functools.wraps(func)
---> 33     def wrapper(*args, **kwargs):
     34         model = kwargs.get("model", "unknown")
     35         response = func(*args, **kwargs)

File ~/.conda/envs/text2sql/lib/python3.10/functools.py:56, in update_wrapper(wrapper, wrapped, assigned, updated)
     54         pass
     55     else:
---> 56         setattr(wrapper, attr, value)
     57 for attr in updated:
     58     getattr(wrapper, attr).update(getattr(wrapped, attr, {}))

TypeError: __name__ must be set to a string object

library version: openai_pricing_logger-0.0.2

yachty66 commented 2 months ago

Hey. I don't develop this fun project anymore.