-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAI request
NotFoundError
response adds additional context to in…
…dicate potential lack of funds (#784) When an OpenAI token is used that either does not have billing enabled or does not have enough credits to call the default model, their API will raise an exceptionof type `NotFoundError`. When this happens, we will catch the errors of this type and modify the message to include additional context that indicates that the issue may be related to insufficient credits. We will then re-raise an `APUException`. Fixes issue #709 --------- Co-authored-by: Luke Lalor <lukehlalor@gmail.com>
- Loading branch information
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
from pytest import fail | ||
from httpx import Response, Request | ||
from openai import AsyncOpenAI, NotFoundError | ||
|
||
from eidolon_ai_sdk.apu.apu import APUException | ||
from eidolon_ai_sdk.apu.llm.open_ai_connection_handler import OpenAIConnectionHandler | ||
|
||
|
||
class TestOpenAIConnectionHandler(OpenAIConnectionHandler): | ||
def makeClient(self) -> AsyncOpenAI: | ||
raise NotFoundError( | ||
self.connection_response_message(), | ||
response=Response( | ||
404, | ||
request=Request("POST", "https://example.com") | ||
), | ||
body=None, | ||
) | ||
|
||
def connection_response_message(self): | ||
return "The model `gpt-4-turbo` does not exist or you do not have access to it." | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def connection(): | ||
# noinspection PyTypeChecker | ||
return TestOpenAIConnectionHandler(spec=None) | ||
|
||
|
||
async def test_not_found_response(connection, **kwargs): | ||
""" | ||
Asserts that when a NotFoundException is thrown by the OpenAI client then an appropriate message is returned | ||
to the user as part of an APUException | ||
""" | ||
try: | ||
await connection.completion(**kwargs) | ||
fail("Expected to raise APUException") | ||
except APUException as e: | ||
message = str(e) | ||
assert connection.connection_response_message() in message | ||
assert "OpenAI accounts that do not have sufficient credits may not have access to some models." in message |