-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐍 Add basic error wrapping [Part. 1] (#628)
- Loading branch information
1 parent
3ec7329
commit 35d3a40
Showing
9 changed files
with
98 additions
and
25 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
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
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
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
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
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 |
---|---|---|
@@ -1 +1,31 @@ | ||
from typing import TypeVar | ||
|
||
from langchain import LLMChain | ||
from langchain.schema import OutputParserException, BaseOutputParser | ||
|
||
from reworkd_platform.web.api.agent.model_settings import create_model, ModelSettings | ||
from reworkd_platform.web.api.errors import OpenAIError | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def parse_with_handling(parser: BaseOutputParser[T], completion: str) -> T: | ||
try: | ||
return parser.parse(completion) | ||
except OutputParserException as e: | ||
raise OpenAIError( | ||
e, "There was an issue parsing the response from the AI model." | ||
) | ||
|
||
|
||
async def call_model_with_handling( | ||
model_settings: ModelSettings, prompt: str, args: dict | ||
) -> str: | ||
try: | ||
model = create_model(model_settings) | ||
chain = LLMChain(llm=model, prompt=prompt) | ||
return await chain.arun(args) | ||
except Exception as e: | ||
raise OpenAIError( | ||
e, "There was an issue getting a response from the AI model." | ||
) |
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,17 @@ | ||
from fastapi import Request | ||
from fastapi.responses import JSONResponse | ||
|
||
from reworkd_platform.web.api.errors import PlatformaticError | ||
|
||
|
||
async def platformatic_exception_handler( | ||
_: Request, | ||
platform_exception: PlatformaticError, | ||
): | ||
return JSONResponse( | ||
status_code=409, | ||
content={ | ||
"error": platform_exception.__class__.__name__, | ||
"detail": platform_exception.detail, | ||
}, | ||
) |
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,27 @@ | ||
# Parent exception class for all expected backend exceptions | ||
# Will be caught and handled by the platform_exception_handler | ||
# Shoutout https://platformatic.dev/ | ||
class PlatformaticError(Exception): | ||
detail: str | ||
|
||
def __init__(self, base_exception: Exception, detail: str = ""): | ||
super().__init__(base_exception) | ||
self.detail = detail | ||
|
||
|
||
class OpenAIError(PlatformaticError): | ||
def __init__(self, exception: Exception, detail: str = ""): | ||
super().__init__(exception, detail) | ||
|
||
# (Replicate) ModelError: NSFW content detected. Try running it again, or try a different prompt. | ||
# ReplicateError: You've hit your monthly spend limit. You can change or remove your limit at https://replicate.com/account/billing#limits. | ||
|
||
|
||
# UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in position 445: surrogates not allowed | ||
|
||
# InvalidRequestError: Engine not found | ||
# TimeoutError -> https://reworkd.sentry.io/issues/4206041431/?project=4505228628525056&query=is%3Aunresolved&referrer=issue-stream&stream_index=17 | ||
# ServiceUnavailableError: The server is overloaded or not ready yet. | ||
# ClientPayloadError: Response payload is not completed -> https://reworkd.sentry.io/issues/4209422945/?project=4505228628525056&query=is%3Aunresolved&referrer=issue-stream&stream_index=21 | ||
# InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 5238 tokens. Please reduce the length of the messages. | ||
# InvalidRequestError: This model's maximum context length is 8192 tokens. However, you requested 8451 tokens (451 in the messages, 8000 in the completion). Please reduce the length of the messages or completion. |
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
35d3a40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-git-main-reworkd.vercel.app
docs.reworkd.ai
docs-reworkd.vercel.app