|
|
|
@ -29,8 +29,9 @@ from core.workflow.system_variable import SystemVariable
|
|
|
|
from core.workflow.variable_loader import VariableLoader
|
|
|
|
from core.workflow.variable_loader import VariableLoader
|
|
|
|
from core.workflow.workflow_entry import WorkflowEntry
|
|
|
|
from core.workflow.workflow_entry import WorkflowEntry
|
|
|
|
from extensions.ext_database import db
|
|
|
|
from extensions.ext_database import db
|
|
|
|
|
|
|
|
from models import Workflow
|
|
|
|
from models.enums import UserFrom
|
|
|
|
from models.enums import UserFrom
|
|
|
|
from models.model import App, Conversation, EndUser, Message, MessageAnnotation
|
|
|
|
from models.model import App, Conversation, Message, MessageAnnotation
|
|
|
|
from models.workflow import ConversationVariable, WorkflowType
|
|
|
|
from models.workflow import ConversationVariable, WorkflowType
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@ -43,21 +44,29 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
|
|
|
|
|
*,
|
|
|
|
application_generate_entity: AdvancedChatAppGenerateEntity,
|
|
|
|
application_generate_entity: AdvancedChatAppGenerateEntity,
|
|
|
|
queue_manager: AppQueueManager,
|
|
|
|
queue_manager: AppQueueManager,
|
|
|
|
conversation: Conversation,
|
|
|
|
conversation: Conversation,
|
|
|
|
message: Message,
|
|
|
|
message: Message,
|
|
|
|
dialogue_count: int,
|
|
|
|
dialogue_count: int,
|
|
|
|
variable_loader: VariableLoader,
|
|
|
|
variable_loader: VariableLoader,
|
|
|
|
|
|
|
|
workflow: Workflow,
|
|
|
|
|
|
|
|
system_user_id: str,
|
|
|
|
|
|
|
|
app: App,
|
|
|
|
) -> None:
|
|
|
|
) -> None:
|
|
|
|
super().__init__(queue_manager, variable_loader)
|
|
|
|
super().__init__(
|
|
|
|
|
|
|
|
queue_manager=queue_manager,
|
|
|
|
|
|
|
|
variable_loader=variable_loader,
|
|
|
|
|
|
|
|
app_id=application_generate_entity.app_config.app_id,
|
|
|
|
|
|
|
|
)
|
|
|
|
self.application_generate_entity = application_generate_entity
|
|
|
|
self.application_generate_entity = application_generate_entity
|
|
|
|
self.conversation = conversation
|
|
|
|
self.conversation = conversation
|
|
|
|
self.message = message
|
|
|
|
self.message = message
|
|
|
|
self._dialogue_count = dialogue_count
|
|
|
|
self._dialogue_count = dialogue_count
|
|
|
|
|
|
|
|
self._workflow = workflow
|
|
|
|
def _get_app_id(self) -> str:
|
|
|
|
self.system_user_id = system_user_id
|
|
|
|
return self.application_generate_entity.app_config.app_id
|
|
|
|
self._app = app
|
|
|
|
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
def run(self) -> None:
|
|
|
|
app_config = self.application_generate_entity.app_config
|
|
|
|
app_config = self.application_generate_entity.app_config
|
|
|
|
@ -86,14 +95,14 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
if self.application_generate_entity.single_iteration_run:
|
|
|
|
if self.application_generate_entity.single_iteration_run:
|
|
|
|
# if only single iteration run is requested
|
|
|
|
# if only single iteration run is requested
|
|
|
|
graph, variable_pool = self._get_graph_and_variable_pool_of_single_iteration(
|
|
|
|
graph, variable_pool = self._get_graph_and_variable_pool_of_single_iteration(
|
|
|
|
workflow=workflow,
|
|
|
|
workflow=self._workflow,
|
|
|
|
node_id=self.application_generate_entity.single_iteration_run.node_id,
|
|
|
|
node_id=self.application_generate_entity.single_iteration_run.node_id,
|
|
|
|
user_inputs=dict(self.application_generate_entity.single_iteration_run.inputs),
|
|
|
|
user_inputs=dict(self.application_generate_entity.single_iteration_run.inputs),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif self.application_generate_entity.single_loop_run:
|
|
|
|
elif self.application_generate_entity.single_loop_run:
|
|
|
|
# if only single loop run is requested
|
|
|
|
# if only single loop run is requested
|
|
|
|
graph, variable_pool = self._get_graph_and_variable_pool_of_single_loop(
|
|
|
|
graph, variable_pool = self._get_graph_and_variable_pool_of_single_loop(
|
|
|
|
workflow=workflow,
|
|
|
|
workflow=self._workflow,
|
|
|
|
node_id=self.application_generate_entity.single_loop_run.node_id,
|
|
|
|
node_id=self.application_generate_entity.single_loop_run.node_id,
|
|
|
|
user_inputs=dict(self.application_generate_entity.single_loop_run.inputs),
|
|
|
|
user_inputs=dict(self.application_generate_entity.single_loop_run.inputs),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@ -104,7 +113,7 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
|
|
|
|
|
|
|
|
# moderation
|
|
|
|
# moderation
|
|
|
|
if self.handle_input_moderation(
|
|
|
|
if self.handle_input_moderation(
|
|
|
|
app_record=app_record,
|
|
|
|
app_record=self._app,
|
|
|
|
app_generate_entity=self.application_generate_entity,
|
|
|
|
app_generate_entity=self.application_generate_entity,
|
|
|
|
inputs=inputs,
|
|
|
|
inputs=inputs,
|
|
|
|
query=query,
|
|
|
|
query=query,
|
|
|
|
@ -114,7 +123,7 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
|
|
|
|
|
|
|
|
# annotation reply
|
|
|
|
# annotation reply
|
|
|
|
if self.handle_annotation_reply(
|
|
|
|
if self.handle_annotation_reply(
|
|
|
|
app_record=app_record,
|
|
|
|
app_record=self._app,
|
|
|
|
message=self.message,
|
|
|
|
message=self.message,
|
|
|
|
query=query,
|
|
|
|
query=query,
|
|
|
|
app_generate_entity=self.application_generate_entity,
|
|
|
|
app_generate_entity=self.application_generate_entity,
|
|
|
|
@ -134,7 +143,7 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
ConversationVariable.from_variable(
|
|
|
|
ConversationVariable.from_variable(
|
|
|
|
app_id=self.conversation.app_id, conversation_id=self.conversation.id, variable=variable
|
|
|
|
app_id=self.conversation.app_id, conversation_id=self.conversation.id, variable=variable
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for variable in workflow.conversation_variables
|
|
|
|
for variable in self._workflow.conversation_variables
|
|
|
|
]
|
|
|
|
]
|
|
|
|
session.add_all(db_conversation_variables)
|
|
|
|
session.add_all(db_conversation_variables)
|
|
|
|
# Convert database entities to variables.
|
|
|
|
# Convert database entities to variables.
|
|
|
|
@ -147,7 +156,7 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
query=query,
|
|
|
|
query=query,
|
|
|
|
files=files,
|
|
|
|
files=files,
|
|
|
|
conversation_id=self.conversation.id,
|
|
|
|
conversation_id=self.conversation.id,
|
|
|
|
user_id=user_id,
|
|
|
|
user_id=self.system_user_id,
|
|
|
|
dialogue_count=self._dialogue_count,
|
|
|
|
dialogue_count=self._dialogue_count,
|
|
|
|
app_id=app_config.app_id,
|
|
|
|
app_id=app_config.app_id,
|
|
|
|
workflow_id=app_config.workflow_id,
|
|
|
|
workflow_id=app_config.workflow_id,
|
|
|
|
@ -158,25 +167,25 @@ class AdvancedChatAppRunner(WorkflowBasedAppRunner):
|
|
|
|
variable_pool = VariablePool(
|
|
|
|
variable_pool = VariablePool(
|
|
|
|
system_variables=system_inputs,
|
|
|
|
system_variables=system_inputs,
|
|
|
|
user_inputs=inputs,
|
|
|
|
user_inputs=inputs,
|
|
|
|
environment_variables=workflow.environment_variables,
|
|
|
|
environment_variables=self._workflow.environment_variables,
|
|
|
|
# Based on the definition of `VariableUnion`,
|
|
|
|
# Based on the definition of `VariableUnion`,
|
|
|
|
# `list[Variable]` can be safely used as `list[VariableUnion]` since they are compatible.
|
|
|
|
# `list[Variable]` can be safely used as `list[VariableUnion]` since they are compatible.
|
|
|
|
conversation_variables=cast(list[VariableUnion], conversation_variables),
|
|
|
|
conversation_variables=cast(list[VariableUnion], conversation_variables),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# init graph
|
|
|
|
# init graph
|
|
|
|
graph = self._init_graph(graph_config=workflow.graph_dict)
|
|
|
|
graph = self._init_graph(graph_config=self._workflow.graph_dict)
|
|
|
|
|
|
|
|
|
|
|
|
db.session.close()
|
|
|
|
db.session.close()
|
|
|
|
|
|
|
|
|
|
|
|
# RUN WORKFLOW
|
|
|
|
# RUN WORKFLOW
|
|
|
|
workflow_entry = WorkflowEntry(
|
|
|
|
workflow_entry = WorkflowEntry(
|
|
|
|
tenant_id=workflow.tenant_id,
|
|
|
|
tenant_id=self._workflow.tenant_id,
|
|
|
|
app_id=workflow.app_id,
|
|
|
|
app_id=self._workflow.app_id,
|
|
|
|
workflow_id=workflow.id,
|
|
|
|
workflow_id=self._workflow.id,
|
|
|
|
workflow_type=WorkflowType.value_of(workflow.type),
|
|
|
|
workflow_type=WorkflowType.value_of(self._workflow.type),
|
|
|
|
graph=graph,
|
|
|
|
graph=graph,
|
|
|
|
graph_config=workflow.graph_dict,
|
|
|
|
graph_config=self._workflow.graph_dict,
|
|
|
|
user_id=self.application_generate_entity.user_id,
|
|
|
|
user_id=self.application_generate_entity.user_id,
|
|
|
|
user_from=(
|
|
|
|
user_from=(
|
|
|
|
UserFrom.ACCOUNT
|
|
|
|
UserFrom.ACCOUNT
|
|
|
|
|