Skip to content

Commit

Permalink
Merge branch 'feat/workflow-backend' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
takatost committed Mar 17, 2024
2 parents 94b0654 + 80f1fbb commit e46381a
Show file tree
Hide file tree
Showing 40 changed files with 365 additions and 166 deletions.
2 changes: 1 addition & 1 deletion api/controllers/files/tool_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get(self, file_id, extension):
raise Forbidden('Invalid request.')

try:
result = ToolFileManager.get_file_generator_by_message_file_id(
result = ToolFileManager.get_file_generator_by_tool_file_id(
file_id,
)

Expand Down
2 changes: 1 addition & 1 deletion api/controllers/service_api/app/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MessageListApi(Resource):
'conversation_id': fields.String,
'inputs': fields.Raw,
'query': fields.String,
'answer': fields.String,
'answer': fields.String(attribute='re_sign_file_url_answer'),
'message_files': fields.List(fields.Nested(message_file_fields), attribute='files'),
'feedback': fields.Nested(feedback_fields, attribute='user_feedback', allow_null=True),
'retriever_resources': fields.List(fields.Nested(retriever_resource_fields)),
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/web/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MessageListApi(WebApiResource):
'conversation_id': fields.String,
'inputs': fields.Raw,
'query': fields.String,
'answer': fields.String,
'answer': fields.String(attribute='re_sign_file_url_answer'),
'message_files': fields.List(fields.Nested(message_file_fields), attribute='files'),
'feedback': fields.Nested(feedback_fields, attribute='user_feedback', allow_null=True),
'retriever_resources': fields.List(fields.Nested(retriever_resource_fields)),
Expand Down
4 changes: 2 additions & 2 deletions api/core/app/app_config/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ class TextToSpeechEntity(BaseModel):
language: Optional[str] = None


class FileUploadEntity(BaseModel):
class FileExtraConfig(BaseModel):
"""
File Upload Entity.
"""
image_config: Optional[dict[str, Any]] = None


class AppAdditionalFeatures(BaseModel):
file_upload: Optional[FileUploadEntity] = None
file_upload: Optional[FileExtraConfig] = None
opening_statement: Optional[str] = None
suggested_questions: list[str] = []
suggested_questions_after_answer: bool = False
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/app_config/features/file_upload/manager.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Optional

from core.app.app_config.entities import FileUploadEntity
from core.app.app_config.entities import FileExtraConfig


class FileUploadConfigManager:
@classmethod
def convert(cls, config: dict) -> Optional[FileUploadEntity]:
def convert(cls, config: dict) -> Optional[FileExtraConfig]:
"""
Convert model config to model config
Expand All @@ -15,7 +15,7 @@ def convert(cls, config: dict) -> Optional[FileUploadEntity]:
if file_upload_dict:
if 'image' in file_upload_dict and file_upload_dict['image']:
if 'enabled' in file_upload_dict['image'] and file_upload_dict['image']['enabled']:
return FileUploadEntity(
return FileExtraConfig(
image_config={
'number_limits': file_upload_dict['image']['number_limits'],
'detail': file_upload_dict['image']['detail'],
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/apps/advanced_chat/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def generate(self, app_model: App,
# parse files
files = args['files'] if 'files' in args and args['files'] else []
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(workflow.features_dict)
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(workflow.features_dict)
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down
31 changes: 22 additions & 9 deletions api/core/app/apps/advanced_chat/generate_task_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import time
from collections.abc import Generator
Expand All @@ -11,7 +12,6 @@
QueueAdvancedChatMessageEndEvent,
QueueAnnotationReplyEvent,
QueueErrorEvent,
QueueMessageFileEvent,
QueueMessageReplaceEvent,
QueueNodeFailedEvent,
QueueNodeStartedEvent,
Expand All @@ -34,6 +34,7 @@
from core.app.task_pipeline.based_generate_task_pipeline import BasedGenerateTaskPipeline
from core.app.task_pipeline.message_cycle_manage import MessageCycleManage
from core.app.task_pipeline.workflow_cycle_manage import WorkflowCycleManage
from core.file.file_obj import FileVar
from core.model_runtime.entities.llm_entities import LLMUsage
from core.workflow.entities.node_entities import NodeType, SystemVariable
from core.workflow.nodes.answer.answer_node import AnswerNode
Expand Down Expand Up @@ -260,10 +261,10 @@ def _process_stream_response(self) -> Generator:
annotation = self._handle_annotation_reply(event)
if annotation:
self._task_state.answer = annotation.content
elif isinstance(event, QueueMessageFileEvent):
response = self._message_file_to_stream_response(event)
if response:
yield response
# elif isinstance(event, QueueMessageFileEvent):
# response = self._message_file_to_stream_response(event)
# if response:
# yield response
elif isinstance(event, QueueTextChunkEvent):
delta_text = event.text
if delta_text is None:
Expand Down Expand Up @@ -464,10 +465,22 @@ def _generate_stream_outputs_when_node_finished(self) -> None:
text = None
if isinstance(value, str | int | float):
text = str(value)
elif isinstance(value, object): # TODO FILE
# convert file to markdown
text = f'![]({value.get("url")})'
pass
elif isinstance(value, dict | list):
# handle files
file_vars = self._fetch_files_from_variable_value(value)
for file_var in file_vars:
try:
file_var_obj = FileVar(**file_var)
except Exception as e:
logger.error(f'Error creating file var: {e}')
continue

# convert file to markdown
text = file_var_obj.to_markdown()

if not text:
# other types
text = json.dumps(value, ensure_ascii=False)

if text:
for token in text:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def on_workflow_node_execute_failed(self, node_id: str,
node_data: BaseNodeData,
error: str,
inputs: Optional[dict] = None,
outputs: Optional[dict] = None,
process_data: Optional[dict] = None) -> None:
"""
Workflow node execute failed
Expand All @@ -107,6 +108,7 @@ def on_workflow_node_execute_failed(self, node_id: str,
node_type=node_type,
node_data=node_data,
inputs=inputs,
outputs=outputs,
process_data=process_data,
error=error
),
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/apps/agent_chat/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def generate(self, app_model: App,
# parse files
files = args['files'] if 'files' in args and args['files'] else []
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/apps/base_app_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from core.app.features.annotation_reply.annotation_reply import AnnotationReplyFeature
from core.app.features.hosting_moderation.hosting_moderation import HostingModerationFeature
from core.external_data_tool.external_data_fetch import ExternalDataFetch
from core.file.file_obj import FileObj
from core.file.file_obj import FileVar
from core.memory.token_buffer_memory import TokenBufferMemory
from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta, LLMUsage
from core.model_runtime.entities.message_entities import AssistantPromptMessage, PromptMessage
Expand All @@ -33,7 +33,7 @@ def get_pre_calculate_rest_tokens(self, app_record: App,
model_config: ModelConfigWithCredentialsEntity,
prompt_template_entity: PromptTemplateEntity,
inputs: dict[str, str],
files: list[FileObj],
files: list[FileVar],
query: Optional[str] = None) -> int:
"""
Get pre calculate rest tokens
Expand Down Expand Up @@ -125,7 +125,7 @@ def organize_prompt_messages(self, app_record: App,
model_config: ModelConfigWithCredentialsEntity,
prompt_template_entity: PromptTemplateEntity,
inputs: dict[str, str],
files: list[FileObj],
files: list[FileVar],
query: Optional[str] = None,
context: Optional[str] = None,
memory: Optional[TokenBufferMemory] = None) \
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/apps/chat/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def generate(self, app_model: App,
# parse files
files = args['files'] if 'files' in args and args['files'] else []
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down
12 changes: 6 additions & 6 deletions api/core/app/apps/completion/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def generate(self, app_model: App,
# parse files
files = args['files'] if 'files' in args and args['files'] else []
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down Expand Up @@ -233,11 +233,11 @@ def generate_more_like_this(self, app_model: App,

# parse files
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(override_model_config_dict or app_model_config.to_dict())
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
message.files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down
2 changes: 1 addition & 1 deletion api/core/app/apps/message_based_app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _init_generate_records(self,
transfer_method=file.transfer_method.value,
belongs_to='user',
url=file.url,
upload_file_id=file.upload_file_id,
upload_file_id=file.related_id,
created_by_role=('account' if account_id else 'end_user'),
created_by=account_id or end_user_id,
)
Expand Down
6 changes: 3 additions & 3 deletions api/core/app/apps/workflow/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def generate(self, app_model: App,
# parse files
files = args['files'] if 'files' in args and args['files'] else []
message_file_parser = MessageFileParser(tenant_id=app_model.tenant_id, app_id=app_model.id)
file_upload_entity = FileUploadConfigManager.convert(workflow.features_dict)
if file_upload_entity:
file_extra_config = FileUploadConfigManager.convert(workflow.features_dict)
if file_extra_config:
file_objs = message_file_parser.validate_and_transform_files_arg(
files,
file_upload_entity,
file_extra_config,
user
)
else:
Expand Down
2 changes: 2 additions & 0 deletions api/core/app/apps/workflow/workflow_event_trigger_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def on_workflow_node_execute_failed(self, node_id: str,
node_data: BaseNodeData,
error: str,
inputs: Optional[dict] = None,
outputs: Optional[dict] = None,
process_data: Optional[dict] = None) -> None:
"""
Workflow node execute failed
Expand All @@ -106,6 +107,7 @@ def on_workflow_node_execute_failed(self, node_id: str,
node_type=node_type,
node_data=node_data,
inputs=inputs,
outputs=outputs,
process_data=process_data,
error=error
),
Expand Down
4 changes: 2 additions & 2 deletions api/core/app/entities/app_invoke_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from core.app.app_config.entities import AppConfig, EasyUIBasedAppConfig, WorkflowUIBasedAppConfig
from core.entities.provider_configuration import ProviderModelBundle
from core.file.file_obj import FileObj
from core.file.file_obj import FileVar
from core.model_runtime.entities.model_entities import AIModelEntity


Expand Down Expand Up @@ -73,7 +73,7 @@ class AppGenerateEntity(BaseModel):
app_config: AppConfig

inputs: dict[str, str]
files: list[FileObj] = []
files: list[FileVar] = []
user_id: str

# extras
Expand Down
1 change: 1 addition & 0 deletions api/core/app/entities/queue_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class QueueNodeFailedEvent(AppQueueEvent):
node_data: BaseNodeData

inputs: Optional[dict] = None
outputs: Optional[dict] = None
process_data: Optional[dict] = None

error: str
Expand Down
2 changes: 2 additions & 0 deletions api/core/app/entities/task_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class Data(BaseModel):
total_steps: int
created_at: int
finished_at: int
files: Optional[list[dict]] = []

event: StreamEvent = StreamEvent.WORKFLOW_FINISHED
workflow_run_id: str
Expand Down Expand Up @@ -253,6 +254,7 @@ class Data(BaseModel):
execution_metadata: Optional[dict] = None
created_at: int
finished_at: int
files: Optional[list[dict]] = []

event: StreamEvent = StreamEvent.NODE_FINISHED
workflow_run_id: str
Expand Down
7 changes: 6 additions & 1 deletion api/core/app/task_pipeline/message_cycle_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def _message_file_to_stream_response(self, event: QueueMessageFileEvent) -> Opti
)

if message_file:
# get tool file id
tool_file_id = message_file.url.split('/')[-1]
# trim extension
tool_file_id = tool_file_id.split('.')[0]

# get extension
if '.' in message_file.url:
extension = f'.{message_file.url.split(".")[-1]}'
Expand All @@ -105,7 +110,7 @@ def _message_file_to_stream_response(self, event: QueueMessageFileEvent) -> Opti
else:
extension = '.bin'
# add sign url
url = ToolFileManager.sign_file(file_id=message_file.id, extension=extension)
url = ToolFileManager.sign_file(tool_file_id=tool_file_id, extension=extension)

return MessageFileStreamResponse(
task_id=self._application_generate_entity.task_id,
Expand Down
Loading

0 comments on commit e46381a

Please sign in to comment.