-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy patherrors.py
More file actions
62 lines (52 loc) · 2.11 KB
/
Copy patherrors.py
File metadata and controls
62 lines (52 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""OpenAI-shaped API errors.
Raising these lets the server return a structured `{"error": {...}}` body with
the right HTTP status instead of dropping the connection.
"""
from typing import Optional
class APIError(Exception):
def __init__(
self, status: int, message: str, err_type: str, code: Optional[str] = None
):
super().__init__(message)
self.status = status
self.message = message
self.err_type = err_type
self.code = code
def body(self) -> dict:
return {
"error": {"message": self.message, "type": self.err_type, "code": self.code}
}
class ContextLengthExceeded(APIError):
def __init__(self, num_tokens: int, max_context: int, completion_tokens: int = 0):
# completion_tokens > 0: the prompt fits but prompt + requested
# max_tokens would run past the window — reject up front rather than
# fail (or truncate) mid-generation.
if completion_tokens > 0:
message = (
f"This model's maximum context length is {max_context} tokens. "
f"However, you requested {num_tokens + completion_tokens} tokens "
f"({num_tokens} in the messages, {completion_tokens} in the "
f"completion). Please reduce the length of the messages or "
f"completion."
)
else:
message = (
f"This model's maximum context length is {max_context} tokens, "
f"but the request has {num_tokens} prompt tokens."
)
super().__init__(
status=400,
message=message,
err_type="invalid_request_error",
code="context_length_exceeded",
)
class GenerationError(APIError):
def __init__(self, detail: str):
super().__init__(
status=500, message=f"Generation failed: {detail}", err_type="server_error"
)