|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import logging
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
|
from typing import Any, cast
|
|
|
|
|
from typing import Any, Optional, cast
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
@ -9,13 +9,19 @@ from configs import dify_config
|
|
|
|
|
from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfig
|
|
|
|
|
from core.app.apps.base_app_queue_manager import AppQueueManager
|
|
|
|
|
from core.app.apps.workflow_app_runner import WorkflowBasedAppRunner
|
|
|
|
|
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, InvokeFrom
|
|
|
|
|
from core.app.entities.app_invoke_entities import (
|
|
|
|
|
AdvancedChatAppGenerateEntity,
|
|
|
|
|
AppGenerateEntity,
|
|
|
|
|
InvokeFrom,
|
|
|
|
|
)
|
|
|
|
|
from core.app.entities.queue_entities import (
|
|
|
|
|
QueueAnnotationReplyEvent,
|
|
|
|
|
QueueStopEvent,
|
|
|
|
|
QueueTextChunkEvent,
|
|
|
|
|
)
|
|
|
|
|
from core.app.features.annotation_reply.annotation_reply import AnnotationReplyFeature
|
|
|
|
|
from core.moderation.base import ModerationError
|
|
|
|
|
from core.moderation.input_moderation import InputModeration
|
|
|
|
|
from core.variables.variables import VariableUnion
|
|
|
|
|
from core.workflow.callbacks import WorkflowCallback, WorkflowLoggingCallback
|
|
|
|
|
from core.workflow.entities.variable_pool import VariablePool
|
|
|
|
|
@ -24,7 +30,7 @@ from core.workflow.variable_loader import VariableLoader
|
|
|
|
|
from core.workflow.workflow_entry import WorkflowEntry
|
|
|
|
|
from extensions.ext_database import db
|
|
|
|
|
from models.enums import UserFrom
|
|
|
|
|
from models.model import App, Conversation, EndUser, Message
|
|
|
|
|
from models.model import App, Conversation, EndUser, Message, MessageAnnotation
|
|
|
|
|
from models.workflow import ConversationVariable, WorkflowType
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
@ -241,3 +247,51 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
|
self._publish_event(QueueTextChunkEvent(text=text))
|
|
|
|
|
|
|
|
|
|
self._publish_event(QueueStopEvent(stopped_by=stopped_by))
|
|
|
|
|
|
|
|
|
|
def query_app_annotations_to_reply(
|
|
|
|
|
self, app_record: App, message: Message, query: str, user_id: str, invoke_from: InvokeFrom
|
|
|
|
|
) -> Optional[MessageAnnotation]:
|
|
|
|
|
"""
|
|
|
|
|
Query app annotations to reply
|
|
|
|
|
:param app_record: app record
|
|
|
|
|
:param message: message
|
|
|
|
|
:param query: query
|
|
|
|
|
:param user_id: user id
|
|
|
|
|
:param invoke_from: invoke from
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
annotation_reply_feature = AnnotationReplyFeature()
|
|
|
|
|
return annotation_reply_feature.query(
|
|
|
|
|
app_record=app_record, message=message, query=query, user_id=user_id, invoke_from=invoke_from
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def moderation_for_inputs(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
app_id: str,
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
app_generate_entity: AppGenerateEntity,
|
|
|
|
|
inputs: Mapping[str, Any],
|
|
|
|
|
query: str | None = None,
|
|
|
|
|
message_id: str,
|
|
|
|
|
) -> tuple[bool, Mapping[str, Any], str]:
|
|
|
|
|
"""
|
|
|
|
|
Process sensitive_word_avoidance.
|
|
|
|
|
:param app_id: app id
|
|
|
|
|
:param tenant_id: tenant id
|
|
|
|
|
:param app_generate_entity: app generate entity
|
|
|
|
|
:param inputs: inputs
|
|
|
|
|
:param query: query
|
|
|
|
|
:param message_id: message id
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
moderation_feature = InputModeration()
|
|
|
|
|
return moderation_feature.check(
|
|
|
|
|
app_id=app_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
app_config=app_generate_entity.app_config,
|
|
|
|
|
inputs=dict(inputs),
|
|
|
|
|
query=query or "",
|
|
|
|
|
message_id=message_id,
|
|
|
|
|
trace_manager=app_generate_entity.trace_manager,
|
|
|
|
|
)
|
|
|
|
|
|