Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(question_classifier): improve error handling with custom exceptions #10365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/core/workflow/nodes/question_classifier/exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class QuestionClassifierNodeError(ValueError):
"""Base class for QuestionClassifierNode errors."""


class InvalidModelTypeError(QuestionClassifierNodeError):
"""Raised when the model is not a Large Language Model."""
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Any, Optional, cast

from core.app.entities.app_invoke_entities import ModelConfigWithCredentialsEntity
from core.llm_generator.output_parser.errors import OutputParserError
from core.memory.token_buffer_memory import TokenBufferMemory
from core.model_manager import ModelInstance
from core.model_runtime.entities import LLMUsage, ModelPropertyKey, PromptMessageRole
Expand All @@ -24,6 +25,7 @@
from models.workflow import WorkflowNodeExecutionStatus

from .entities import QuestionClassifierNodeData
from .exc import InvalidModelTypeError
from .template_prompts import (
QUESTION_CLASSIFIER_ASSISTANT_PROMPT_1,
QUESTION_CLASSIFIER_ASSISTANT_PROMPT_2,
Expand Down Expand Up @@ -124,7 +126,7 @@ def _run(self):
category_name = classes_map[category_id_result]
category_id = category_id_result

except Exception:
except OutputParserError:
logging.error(f"Failed to parse result text: {result_text}")
try:
process_data = {
Expand Down Expand Up @@ -309,4 +311,4 @@ def _get_prompt_template(
)

else:
raise ValueError(f"Model mode {model_mode} not support.")
raise InvalidModelTypeError(f"Model mode {model_mode} not support.")
2 changes: 1 addition & 1 deletion api/libs/json_in_md_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def parse_json_markdown(json_string: str) -> dict:
starts = ["```json", "```", "``", "`", "{"]
ends = ["```", "``", "`", "}"]
end_index = -1
start_index = 0
for s in starts:
start_index = json_string.find(s)
if start_index != -1:
Expand All @@ -24,7 +25,6 @@ def parse_json_markdown(json_string: str) -> dict:
break
if start_index != -1 and end_index != -1 and start_index < end_index:
extracted_content = json_string[start_index:end_index].strip()
print("content:", extracted_content, start_index, end_index)
parsed = json.loads(extracted_content)
else:
raise Exception("Could not find JSON block in the output.")
Expand Down