1
1
from __future__ import annotations
2
2
3
3
import sys
4
- import traceback
5
4
import time
5
+ import traceback
6
6
from re import compile , Match , Pattern
7
- from typing import Callable , Coroutine , Optional , Tuple , Union , Dict
8
- from typing_extensions import TypedDict
9
-
7
+ from re import Match , Pattern , compile
8
+ from typing import Callable , Coroutine , Dict , Optional , Tuple , Union
10
9
11
10
from fastapi import (
11
+ HTTPException ,
12
12
Request ,
13
13
Response ,
14
- HTTPException ,
15
14
)
16
15
from fastapi .responses import JSONResponse
17
16
from fastapi .routing import APIRoute
17
+ from typing_extensions import TypedDict
18
18
19
19
from llama_cpp .server .types import (
20
+ CreateChatCompletionRequest ,
20
21
CreateCompletionRequest ,
21
22
CreateEmbeddingRequest ,
22
- CreateChatCompletionRequest ,
23
23
)
24
24
25
25
@@ -46,7 +46,7 @@ class ErrorResponseFormatters:
46
46
47
47
@staticmethod
48
48
def context_length_exceeded (
49
- request : Union [" CreateCompletionRequest" , " CreateChatCompletionRequest" ],
49
+ request : Union [CreateCompletionRequest , CreateChatCompletionRequest ],
50
50
match , # type: Match[str] # type: ignore
51
51
) -> Tuple [int , ErrorResponse ]:
52
52
"""Formatter for context length exceeded error"""
@@ -84,7 +84,7 @@ def context_length_exceeded(
84
84
85
85
@staticmethod
86
86
def model_not_found (
87
- request : Union [" CreateCompletionRequest" , " CreateChatCompletionRequest" ],
87
+ request : Union [CreateCompletionRequest , CreateChatCompletionRequest ],
88
88
match , # type: Match[str] # type: ignore
89
89
) -> Tuple [int , ErrorResponse ]:
90
90
"""Formatter for model_not_found error"""
@@ -105,11 +105,11 @@ class RouteErrorHandler(APIRoute):
105
105
# key: regex pattern for original error message from llama_cpp
106
106
# value: formatter function
107
107
pattern_and_formatters : Dict [
108
- " Pattern[str]" ,
108
+ Pattern [str ],
109
109
Callable [
110
110
[
111
- Union [" CreateCompletionRequest" , " CreateChatCompletionRequest" ],
112
- " Match[str]" ,
111
+ Union [CreateCompletionRequest , CreateChatCompletionRequest ],
112
+ Match [str ],
113
113
],
114
114
Tuple [int , ErrorResponse ],
115
115
],
@@ -127,14 +127,14 @@ def error_message_wrapper(
127
127
error : Exception ,
128
128
body : Optional [
129
129
Union [
130
- " CreateChatCompletionRequest" ,
131
- " CreateCompletionRequest" ,
132
- " CreateEmbeddingRequest" ,
130
+ CreateChatCompletionRequest ,
131
+ CreateCompletionRequest ,
132
+ CreateEmbeddingRequest ,
133
133
]
134
134
] = None ,
135
135
) -> Tuple [int , ErrorResponse ]:
136
136
"""Wraps error message in OpenAI style error response"""
137
- print (f"Exception: { str ( error ) } " , file = sys .stderr )
137
+ print (f"Exception: { error !s } " , file = sys .stderr )
138
138
traceback .print_exc (file = sys .stderr )
139
139
if body is not None and isinstance (
140
140
body ,
0 commit comments