Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RateLimitError returned when error does not seem to be related to rate limits #648

Open
3 tasks done
maxyousif15 opened this issue Nov 15, 2023 · 1 comment
Open
3 tasks done
Labels
bug Something isn't working

Comments

@maxyousif15
Copy link

maxyousif15 commented Nov 15, 2023

First check

  • I added a descriptive title to this issue.
  • I used the GitHub search to try to find a similar issue and didn't find one.
  • I searched the Marvin documentation for this issue.

Bug summary

I'm getting a strange RateLimitError associated with my credentials, when I do not think this is a suitable response. I can access the OpenAI API just fine using the openai client and do not get a rate limit error when sending a request via the openai client.

Reproduction

# -- This raises the error
import marvin
from marvin import ai_model
from pydantic import (
    BaseModel,
    Field,
)

from app.llm.openai import _get_openai_credentials

print(marvin.__version__)
>> 1.5.6
credentials = _get_openai_credentials()
marvin.settings.openai.api_key = credentials['api-key']
marvin.settings.openai.organization = credentials['organisation-id']
marvin.settings.llm_model = 'openai/gpt-3.5-turbo-1106'
marvin.settings.llm_temperature = 1
marvin.settings.llm_max_context_tokens = 16385


@ai_model
class Camera(BaseModel):
    """
    Parse out the numeric representing the megapixels from the text.
    """
    megapixels: float = Field(
        description='The megapixels of the camera. Measured in MP.',
    )

Camera("15 megapixels")


# -- This works fine
from app.llm.openai import create_openai_llm
from app.llm.utils import HumanMessage


llm = create_openai_llm()
resp = llm(messages=[HumanMessage(content='hello!')])
print(resp.content)
>> Hello! How can I assist you today?


### Error

```python3
RateLimitError                            Traceback (most recent call last)
Cell In[22], line 28
     21     """
     22     Parse out the numeric representing the megapixels from the text.
     23     """
     24     megapixels: float = Field(
     25         description='The megapixels of the camera. Measured in MP.',
     26     )
---> 28 Camera("15 megapixels")
     29 # Camera.model_fields

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/components/ai_model.py:75, in AIModel.__init__(self, text, instructions_, **kwargs)
     67 def __init__(
     68     self,
     69     text: Optional[str] = None,
   (...)
     72     **kwargs: Any,
     73 ):
     74     if text:
---> 75         kwargs.update(self.__class__.call(text, instructions=instructions_))
     77     super().__init__(**kwargs)

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/components/ai_model.py:271, in AIModel.call(cls, text, ctx, instructions, response_model_name, response_model_description, response_model_field_name, model, **model_kwargs)
    267     if local_vars.get(key, None) is None:
    268         local_vars[key] = metadata.get(key, {})
    270 _model: Self = (  # type: ignore
--> 271     cls.as_chat_completion(
    272         text,
    273         ctx=ctx,
    274         instructions=instructions,
    275         response_model_name=response_model_name,
    276         response_model_description=response_model_description,
    277         response_model_field_name=response_model_field_name,
    278         model=model,
    279         **model_kwargs,
    280     )
    281     .create()
    282     .to_model(cls)
    283 )
    284 return _model

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/core/ChatCompletion/abstract.py:152, in AbstractChatCompletion.create(self, response_model, **kwargs)
    150 request = self._create_request(**kwargs, response_model=response_model)
    151 serialized_request = self._serialize_request(request=request)
--> 152 response_data = self._send_request(**serialized_request)
    153 response = self._parse_response(response_data)
    155 return Turn(
    156     request=Request(
    157         **serialized_request
   (...)
    162     response=response,
    163 )

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/core/ChatCompletion/providers/openai.py:198, in OpenAIChatCompletion._send_request(self, **serialized_request)
    192 """
    193 Send the serialized request to OpenAI's endpoint/service.
    194 """
    195 # Use openai's library functions to send the request and get a response
    196 # Example:
--> 198 return run_sync(
    199     self._send_request_async(**serialized_request),
    200 )

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/utilities/async_utils.py:65, in run_sync(coroutine)
     63     with ThreadPoolExecutor() as executor:
     64         future = executor.submit(asyncio.run, coroutine)
---> 65         return future.result()
     66 else:
     67     return asyncio.run(coroutine)

File ~/anaconda3/envs/py3.9/lib/python3.9/concurrent/futures/_base.py:446, in Future.result(self, timeout)
    444     raise CancelledError()
    445 elif self._state == FINISHED:
--> 446     return self.__get_result()
    447 else:
    448     raise TimeoutError()

File ~/anaconda3/envs/py3.9/lib/python3.9/concurrent/futures/_base.py:391, in Future.__get_result(self)
    389 if self._exception:
    390     try:
--> 391         raise self._exception
    392     finally:
    393         # Break a reference cycle with the exception in self._exception
    394         self = None

File ~/anaconda3/envs/py3.9/lib/python3.9/concurrent/futures/thread.py:58, in _WorkItem.run(self)
     55     return
     57 try:
---> 58     result = self.fn(*self.args, **self.kwargs)
     59 except BaseException as exc:
     60     self.future.set_exception(exc)

File ~/anaconda3/envs/py3.9/lib/python3.9/asyncio/runners.py:44, in run(main, debug)
     42     if debug is not None:
     43         loop.set_debug(debug)
---> 44     return loop.run_until_complete(main)
     45 finally:
     46     try:

File ~/anaconda3/envs/py3.9/lib/python3.9/asyncio/base_events.py:647, in BaseEventLoop.run_until_complete(self, future)
    644 if not future.done():
    645     raise RuntimeError('Event loop stopped before Future completed.')
--> 647 return future.result()

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/marvin/core/ChatCompletion/providers/openai.py:211, in OpenAIChatCompletion._send_request_async(self, **serialized_request)
    208 if handler_fn := serialized_request.pop("stream_handler", {}):
    209     serialized_request["stream"] = True
--> 211 response = await openai.ChatCompletion.acreate(**serialized_request)  # type: ignore # noqa
    213 if handler_fn:
    214     response = await OpenAIStreamHandler(
    215         callback=handler_fn,
    216     ).handle_streaming_response(response)

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/openai/api_resources/chat_completion.py:45, in ChatCompletion.acreate(cls, *args, **kwargs)
     43 while True:
     44     try:
---> 45         return await super().acreate(*args, **kwargs)
     46     except TryAgain as e:
     47         if timeout is not None and time.time() > start + timeout:

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:219, in EngineAPIResource.acreate(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)
    194 @classmethod
    195 async def acreate(
    196     cls,
   (...)
    203     **params,
    204 ):
    205     (
    206         deployment_id,
    207         engine,
   (...)
    217         api_key, api_base, api_type, api_version, organization, **params
    218     )
--> 219     response, _, api_key = await requestor.arequest(
    220         "post",
    221         url,
    222         params=params,
    223         headers=headers,
    224         stream=stream,
    225         request_id=request_id,
    226         request_timeout=request_timeout,
    227     )
    229     if stream:
    230         # must be an iterator
    231         assert not isinstance(response, OpenAIResponse)

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/openai/api_requestor.py:384, in APIRequestor.arequest(self, method, url, params, headers, files, stream, request_id, request_timeout)
    373 try:
    374     result = await self.arequest_raw(
    375         method.lower(),
    376         url,
   (...)
    382         request_timeout=request_timeout,
    383     )
--> 384     resp, got_stream = await self._interpret_async_response(result, stream)
    385 except Exception:
    386     # Close the request before exiting session context.
    387     if result is not None:

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/openai/api_requestor.py:738, in APIRequestor._interpret_async_response(self, result, stream)
    735 except aiohttp.ClientError as e:
    736     util.log_warn(e, body=result.content)
    737 return (
--> 738     self._interpret_response_line(
    739         (await result.read()).decode("utf-8"),
    740         result.status,
    741         result.headers,
    742         stream=False,
    743     ),
    744     False,
    745 )

File ~/anaconda3/envs/py3.9/lib/python3.9/site-packages/openai/api_requestor.py:775, in APIRequestor._interpret_response_line(self, rbody, rcode, rheaders, stream)
    773 stream_error = stream and "error" in resp.data
    774 if stream_error or not 200 <= rcode < 300:
--> 775     raise self.handle_error_response(
    776         rbody, rcode, resp.data, rheaders, stream_error=stream_error
    777     )
    778 return resp

RateLimitError: You exceeded your current quota, please check your plan and billing details.


### Versions

Version:		1.5.6
Python version:		3.9.18
OS/Arch:		darwin/x86_64


### Additional context

_No response_
@maxyousif15 maxyousif15 added the bug Something isn't working label Nov 15, 2023
@zzstoatzz
Copy link
Collaborator

hmm, this is strange - is this something you've encountered in marvin 2 at all yet? (if you've had the chance to try it out?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants