1111)
1212from uuid import uuid4
1313
14- from fastapi import HTTPException
15-
1614from aidial_sdk .chat_completion .choice import Choice
1715from aidial_sdk .chat_completion .chunks import (
1816 BaseChunk ,
2321 UsagePerModelChunk ,
2422)
2523from aidial_sdk .chat_completion .request import Request
26- from aidial_sdk .exceptions import HTTPException as DialHttpException
27- from aidial_sdk .exceptions import request_validation_error
28- from aidial_sdk .utils .errors import json_error , runtime_error
24+ from aidial_sdk .exceptions import HTTPException as DIALException
25+ from aidial_sdk .exceptions import request_validation_error , runtime_server_error
26+ from aidial_sdk .utils .errors import RUNTIME_ERROR_MESSAGE , runtime_error
2927from aidial_sdk .utils .logging import log_error , log_exception
3028from aidial_sdk .utils .merge_chunks import merge
3129from aidial_sdk .utils .streaming import DONE_MARKER , format_chunk
@@ -101,35 +99,22 @@ async def _generate_stream(
10199 end_chunk_generated = False
102100 try :
103101 self .user_task .result ()
104- except DialHttpException as e :
102+ except DIALException as e :
105103 if self .request .stream :
106104 self ._queue .put_nowait (EndChunk (e ))
107105 end_chunk_generated = True
108106 else :
109- raise HTTPException (
110- status_code = e .status_code ,
111- detail = json_error (
112- message = e .message ,
113- type = e .type ,
114- param = e .param ,
115- code = e .code ,
116- display_message = e .display_message ,
117- ),
118- )
107+ raise e .to_fastapi_exception ()
119108 except Exception as e :
120- log_exception ("Error during processing the request" )
109+ log_exception (RUNTIME_ERROR_MESSAGE )
121110
122111 if self .request .stream :
123112 self ._queue .put_nowait (EndChunk (e ))
124113 end_chunk_generated = True
125114 else :
126- raise HTTPException (
127- status_code = 500 ,
128- detail = json_error (
129- message = "Error during processing the request" ,
130- type = "runtime_error" ,
131- ),
132- )
115+ raise runtime_server_error (
116+ RUNTIME_ERROR_MESSAGE
117+ ).to_fastapi_exception ()
133118
134119 if not end_chunk_generated :
135120 self ._queue .put_nowait (EndChunk ())
@@ -168,41 +153,26 @@ async def _generate_stream(
168153 yield chunk
169154
170155 if item .exc :
171- if isinstance (item .exc , DialHttpException ):
172- formatted_chunk = format_chunk (
173- json_error (
174- message = item .exc .message ,
175- type = item .exc .type ,
176- param = item .exc .param ,
177- code = item .exc .code ,
178- display_message = item .exc .display_message ,
179- )
180- )
156+ if isinstance (item .exc , DIALException ):
157+ formatted_chunk = format_chunk (item .exc .json_error ())
181158 else :
182159 formatted_chunk = format_chunk (
183- json_error (
184- message = "Error during processing the request" ,
185- type = "runtime_error" ,
186- )
160+ runtime_server_error (
161+ RUNTIME_ERROR_MESSAGE
162+ ).json_error ()
187163 )
188164 yield formatted_chunk
189165 else :
190166 if self ._last_choice_index != (self .request .n or 1 ):
191167 log_error ("Not all choices were generated" )
192168
193- error = json_error (
194- message = "Error during processing the request" ,
195- type = "runtime_error" ,
196- )
169+ error = runtime_server_error (RUNTIME_ERROR_MESSAGE )
197170
198171 if self .request .stream :
199- formatted_chunk = format_chunk (error )
172+ formatted_chunk = format_chunk (error . json_error () )
200173 yield formatted_chunk
201174 else :
202- raise HTTPException (
203- status_code = 500 ,
204- detail = error ,
205- )
175+ raise error .to_fastapi_exception ()
206176
207177 if self .request .stream :
208178 yield format_chunk (DONE_MARKER )
@@ -229,26 +199,13 @@ async def _generator(
229199 if self .user_task in done :
230200 try :
231201 self .user_task .result ()
232- except DialHttpException as e :
233- raise HTTPException (
234- status_code = e .status_code ,
235- detail = json_error (
236- message = e .message ,
237- type = e .type ,
238- param = e .param ,
239- code = e .code ,
240- display_message = e .display_message ,
241- ),
242- )
202+ except DIALException as e :
203+ raise e .to_fastapi_exception ()
243204 except Exception :
244- log_exception ("Error during processing the request" )
245- raise HTTPException (
246- status_code = 500 ,
247- detail = json_error (
248- message = "Error during processing the request" ,
249- type = "runtime_error" ,
250- ),
251- )
205+ log_exception (RUNTIME_ERROR_MESSAGE )
206+ raise runtime_server_error (
207+ RUNTIME_ERROR_MESSAGE
208+ ).to_fastapi_exception ()
252209
253210 return get_task .result () if get_task in done else await get_task
254211
0 commit comments