mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 20:26:50 +08:00
style(llm-generator): precompile Persian-word heuristic for clarity and performance
This commit is contained in:
parent
0846542c33
commit
a7a074edbb
@ -37,6 +37,13 @@ from models.workflow import Workflow
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Precompiled heuristic to detect common Persian (Farsi) words in short inputs.
|
||||||
|
# Using a compiled regex avoids repeated recompilation on every call.
|
||||||
|
_PERSIAN_HEURISTIC = re.compile(
|
||||||
|
r"\b(سلام|متشکرم|ممنون|خوب|چطور|سپاس)\b",
|
||||||
|
flags=re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class WorkflowServiceInterface(Protocol):
|
class WorkflowServiceInterface(Protocol):
|
||||||
def get_draft_workflow(self, app_model: App, workflow_id: str | None = None) -> Workflow | None:
|
def get_draft_workflow(self, app_model: App, workflow_id: str | None = None) -> Workflow | None:
|
||||||
@ -70,7 +77,8 @@ class LLMGenerator:
|
|||||||
# Log at debug level to aid debugging without failing the linter S110.
|
# Log at debug level to aid debugging without failing the linter S110.
|
||||||
logger.debug("langdetect detection failed: %s", exc)
|
logger.debug("langdetect detection failed: %s", exc)
|
||||||
# Also check for some common Persian words as an additional heuristic
|
# Also check for some common Persian words as an additional heuristic
|
||||||
if bool(re.search(r"\b(سلام|متشکرم|ممنون|خوب|چطور|سپاس)\b", (text or ""), flags=re.IGNORECASE)):
|
# Use precompiled regex for clarity and performance.
|
||||||
|
if _PERSIAN_HEURISTIC.search(text or ""):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user