mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 01:18:05 +08:00
Co-authored-by: 聂政 <niezheng@pjlab.org.cn>
This commit is contained in:
@ -684,7 +684,9 @@ class AdvancedChatAppGenerateTaskPipeline:
|
||||
)
|
||||
elif isinstance(event, QueueMessageReplaceEvent):
|
||||
# published by moderation
|
||||
yield self._message_cycle_manager._message_replace_to_stream_response(answer=event.text)
|
||||
yield self._message_cycle_manager._message_replace_to_stream_response(
|
||||
answer=event.text, reason=event.reason
|
||||
)
|
||||
elif isinstance(event, QueueAdvancedChatMessageEndEvent):
|
||||
if not graph_runtime_state:
|
||||
raise ValueError("graph runtime state not initialized.")
|
||||
@ -695,7 +697,8 @@ class AdvancedChatAppGenerateTaskPipeline:
|
||||
if output_moderation_answer:
|
||||
self._task_state.answer = output_moderation_answer
|
||||
yield self._message_cycle_manager._message_replace_to_stream_response(
|
||||
answer=output_moderation_answer
|
||||
answer=output_moderation_answer,
|
||||
reason=QueueMessageReplaceEvent.MessageReplaceReason.OUTPUT_MODERATION,
|
||||
)
|
||||
|
||||
# Save message
|
||||
|
||||
@ -264,8 +264,16 @@ class QueueMessageReplaceEvent(AppQueueEvent):
|
||||
QueueMessageReplaceEvent entity
|
||||
"""
|
||||
|
||||
class MessageReplaceReason(StrEnum):
|
||||
"""
|
||||
Reason for message replace event
|
||||
"""
|
||||
|
||||
OUTPUT_MODERATION = "output_moderation"
|
||||
|
||||
event: QueueEvent = QueueEvent.MESSAGE_REPLACE
|
||||
text: str
|
||||
reason: str
|
||||
|
||||
|
||||
class QueueRetrieverResourcesEvent(AppQueueEvent):
|
||||
|
||||
@ -148,6 +148,7 @@ class MessageReplaceStreamResponse(StreamResponse):
|
||||
|
||||
event: StreamEvent = StreamEvent.MESSAGE_REPLACE
|
||||
answer: str
|
||||
reason: str
|
||||
|
||||
|
||||
class AgentThoughtStreamResponse(StreamResponse):
|
||||
|
||||
@ -126,12 +126,12 @@ class BasedGenerateTaskPipeline:
|
||||
if self._output_moderation_handler:
|
||||
self._output_moderation_handler.stop_thread()
|
||||
|
||||
completion = self._output_moderation_handler.moderation_completion(
|
||||
completion, flagged = self._output_moderation_handler.moderation_completion(
|
||||
completion=completion, public_event=False
|
||||
)
|
||||
|
||||
self._output_moderation_handler = None
|
||||
|
||||
return completion
|
||||
if flagged:
|
||||
return completion
|
||||
|
||||
return None
|
||||
|
||||
@ -182,10 +182,12 @@ class MessageCycleManage:
|
||||
from_variable_selector=from_variable_selector,
|
||||
)
|
||||
|
||||
def _message_replace_to_stream_response(self, answer: str) -> MessageReplaceStreamResponse:
|
||||
def _message_replace_to_stream_response(self, answer: str, reason: str = "") -> MessageReplaceStreamResponse:
|
||||
"""
|
||||
Message replace to stream response.
|
||||
:param answer: answer
|
||||
:return:
|
||||
"""
|
||||
return MessageReplaceStreamResponse(task_id=self._application_generate_entity.task_id, answer=answer)
|
||||
return MessageReplaceStreamResponse(
|
||||
task_id=self._application_generate_entity.task_id, answer=answer, reason=reason
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user