compatible model daemon request exception

This commit is contained in:
takatost 2024-11-01 19:20:26 -07:00
parent d92cb994a9
commit c100f24f7d
3 changed files with 38 additions and 12 deletions

View File

@ -10,8 +10,15 @@ from core.model_runtime.entities.model_entities import (
PriceInfo, PriceInfo,
PriceType, PriceType,
) )
from core.model_runtime.errors.invoke import InvokeAuthorizationError, InvokeError from core.model_runtime.errors.invoke import (
from core.plugin.entities.plugin_daemon import PluginModelProviderEntity InvokeAuthorizationError,
InvokeBadRequestError,
InvokeConnectionError,
InvokeError,
InvokeRateLimitError,
InvokeServerUnavailableError,
)
from core.plugin.entities.plugin_daemon import PluginDaemonInnerError, PluginModelProviderEntity
from core.plugin.manager.model import PluginModelManager from core.plugin.manager.model import PluginModelManager
@ -31,7 +38,7 @@ class AIModel(BaseModel):
model_config = ConfigDict(protected_namespaces=()) model_config = ConfigDict(protected_namespaces=())
@property @property
def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]: def _invoke_error_mapping(self) -> dict[type[Exception], list[type[Exception]]]:
""" """
Map model invoke error to unified error Map model invoke error to unified error
The key is the error type thrown to the caller The key is the error type thrown to the caller
@ -40,9 +47,17 @@ class AIModel(BaseModel):
:return: Invoke error mapping :return: Invoke error mapping
""" """
raise NotImplementedError return {
InvokeConnectionError: [InvokeConnectionError],
InvokeServerUnavailableError: [InvokeServerUnavailableError],
InvokeRateLimitError: [InvokeRateLimitError],
InvokeAuthorizationError: [InvokeAuthorizationError],
InvokeBadRequestError: [InvokeBadRequestError],
PluginDaemonInnerError: [PluginDaemonInnerError],
ValueError: [ValueError],
}
def _transform_invoke_error(self, error: Exception) -> InvokeError: def _transform_invoke_error(self, error: Exception) -> Exception:
""" """
Transform invoke error to unified error Transform invoke error to unified error
@ -52,13 +67,15 @@ class AIModel(BaseModel):
for invoke_error, model_errors in self._invoke_error_mapping.items(): for invoke_error, model_errors in self._invoke_error_mapping.items():
if isinstance(error, tuple(model_errors)): if isinstance(error, tuple(model_errors)):
if invoke_error == InvokeAuthorizationError: if invoke_error == InvokeAuthorizationError:
return invoke_error( return InvokeAuthorizationError(
description=( description=(
f"[{self.provider_name}] Incorrect model credentials provided, please check and try again." f"[{self.provider_name}] Incorrect model credentials provided, please check and try again."
) )
) )
elif isinstance(invoke_error, InvokeError):
return invoke_error(description=f"[{self.provider_name}] {invoke_error.description}, {str(error)}") return invoke_error(description=f"[{self.provider_name}] {invoke_error.description}, {str(error)}")
else:
return error
return InvokeError(description=f"[{self.provider_name}] Error: {str(error)}") return InvokeError(description=f"[{self.provider_name}] Error: {str(error)}")

View File

@ -53,7 +53,7 @@ class BasePluginManager:
) )
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
logger.exception(f"Request to Plugin Daemon Service failed: {e}") logger.exception(f"Request to Plugin Daemon Service failed: {e}")
raise ValueError("Request to Plugin Daemon Service failed") raise PluginDaemonInnerError(code=-500, message="Request to Plugin Daemon Service failed")
return response return response
@ -157,8 +157,17 @@ class BasePluginManager:
Make a stream request to the plugin daemon inner API and yield the response as a model. Make a stream request to the plugin daemon inner API and yield the response as a model.
""" """
for line in self._stream_request(method, path, params, headers, data, files): for line in self._stream_request(method, path, params, headers, data, files):
line_data = json.loads(line) line_data = None
rep = PluginDaemonBasicResponse[type](**line_data) try:
line_data = json.loads(line)
rep = PluginDaemonBasicResponse[type](**line_data)
except Exception as e:
# TODO modify this when line_data has code and message
if line_data and "error" in line_data:
raise ValueError(line_data["error"])
else:
raise ValueError(line)
if rep.code != 0: if rep.code != 0:
if rep.code == -500: if rep.code == -500:
try: try:

View File

@ -103,7 +103,7 @@ class RetrievalService:
if exceptions: if exceptions:
exception_message = ";\n".join(exceptions) exception_message = ";\n".join(exceptions)
raise Exception(exception_message) raise ValueError(exception_message)
if retrieval_method == RetrievalMethod.HYBRID_SEARCH.value: if retrieval_method == RetrievalMethod.HYBRID_SEARCH.value:
data_post_processor = DataPostProcessor( data_post_processor = DataPostProcessor(