marvin
marvin copied to clipboard
Ai Models Quickstart example misleading exception handling
First check
- [X] I added a descriptive title to this issue.
- [X] I used the GitHub search to look for a similar issue and didn't find it.
- [X] I searched the Marvin documentation for this feature.
Describe the current behavior
While going through the docs for the first time and testing the package I tried the example in Marvin's Quickstart :
import marvin
marvin.settings.openai.api_key = YOUR_API_KEY
##
from marvin import ai_model
from pydantic import BaseModel, Field
@ai_model
class Location(BaseModel):
city: str
state: str = Field(..., description="The two-letter state abbreviation")
Location("The Big Apple")
While in those conditions :
- If you don't have GPT-4 permissions (fixed after being billed at least 1$ for the API since july 23 says OpenAI)
- If you run your code on python 3.10.4 (As I did, and on 3.10 and above I suppose)
- (in my case on Windows 10)
It resulted in a kinda verbose exception handling list of encountered exceptions :
- "RuntimeError: no running event loop" | marvin/utilities/async_utils.py
- "openai.error.InvalidRequestError: The model
gpt-4
does not exist or you do not have access to it. Learn more: https://help.openai.com/en/articles/7102672-how-can-i-access-gpt-4. " - "RuntimeError: Event loop is closed" (Most recent call last) | asyncio/base_events.py
And if you used quite a few code samples with deprecated asyncio like me, you most likely missed the OpenAI GPT-4's permission issue first.
Describe the proposed behavior
Even guilty, I guess we could improve the first user experience simply here to make it smoother (it could lead to minor edits in other parts of the documentation too. It's a dummy issue as it mainly happened because I tend to scan docs and test code directly instead of reading more in depth on my first tests but "details".
Whatever, even if the last exception raised seems to happen because of the openai package's own asyncio implementation (?), you can solve the real issue by only setting your marvin.settings.llm_model to openai/gpt-3.5-turbo instead of the default gpt-4 one like so :
import marvin
marvin.settings.openai.api_key = YOUR_API_KEY
marvin.settings.llm_model = 'openai/gpt-3.5-turbo' # Fixes the main issue # does not fix the remaining non blocking exception
Example Use
No response
Additional context
While digging into marvin's code, I found out the marvin/utilities/async_utils.py uses deprecated (or so) async code. While it was not the main issue, I solved the "RuntimeError: no running event loop" by getting rid of the whole loop/get_event_loop/etc. stuff with their shorter 3.10+ implementations : "return await functools.partial(func, *args, **kwargs)" and "asyncio.run(coroutine)". Remains what seems to be an older OpenAI package code too also throwing exception on a 'loop.close()' statement but anyway.
I'll let more experienced devs figure out the underlying lessons of this nooby mistake, but I suggest editing the docs where necessary to improve the first time user experience and make it smoother. I love the way this package's API is design, that's why I thought it could be interesting to contribute on improving its doc (and make the solution more SEO friendly too)