diff --git a/api/constants/model_template.py b/api/constants/model_template.py index c8aaba23cb83ed..42e182236ff793 100644 --- a/api/constants/model_template.py +++ b/api/constants/model_template.py @@ -1,3 +1,5 @@ +import json + from models.model import AppMode default_app_templates = { @@ -10,6 +12,35 @@ } }, + # completion default mode + AppMode.COMPLETION: { + 'app': { + 'mode': AppMode.COMPLETION.value, + 'enable_site': True, + 'enable_api': True + }, + 'model_config': { + 'model': { + "provider": "openai", + "name": "gpt-4", + "mode": "chat", + "completion_params": {} + }, + 'user_input_form': json.dumps([ + { + "paragraph": { + "label": "Query", + "variable": "query", + "required": True, + "default": "" + } + } + ]), + 'pre_prompt': '{{query}}' + }, + + }, + # chat default mode AppMode.CHAT: { 'app': { diff --git a/api/controllers/console/app/app.py b/api/controllers/console/app/app.py index 94406030697458..9c8ebfac6c18ba 100644 --- a/api/controllers/console/app/app.py +++ b/api/controllers/console/app/app.py @@ -22,7 +22,7 @@ from core.tools.tool_manager import ToolManager -ALLOW_CREATE_APP_MODES = ['chat', 'agent-chat', 'advanced-chat', 'workflow'] +ALLOW_CREATE_APP_MODES = ['chat', 'agent-chat', 'advanced-chat', 'workflow', 'completion'] class AppListApi(Resource): diff --git a/api/core/workflow/nodes/code/code_node.py b/api/core/workflow/nodes/code/code_node.py index 0b46f86e9d22e7..01e4fc458311fd 100644 --- a/api/core/workflow/nodes/code/code_node.py +++ b/api/core/workflow/nodes/code/code_node.py @@ -15,13 +15,13 @@ MAX_STRING_ARRAY_LENGTH = 30 MAX_NUMBER_ARRAY_LENGTH = 1000 -JAVASCRIPT_DEFAULT_CODE = """function main({args1, args2}) { +JAVASCRIPT_DEFAULT_CODE = """function main({arg1, arg2}) { return { result: args1 + args2 } }""" -PYTHON_DEFAULT_CODE = """def main(args1: int, args2: int) -> dict: +PYTHON_DEFAULT_CODE = """def main(arg1: int, arg2: int) -> dict: return { "result": args1 + args2, }""" diff --git a/api/services/app_service.py b/api/services/app_service.py index 58b102f826102e..940d4eac6cf9b3 100644 --- a/api/services/app_service.py +++ b/api/services/app_service.py @@ -150,10 +150,10 @@ def import_app(self, tenant_id: str, data: str, args: dict, account: Account) -> if not workflow: raise ValueError("Missing workflow in data argument " "when app mode is advanced-chat or workflow") - elif app_mode in [AppMode.CHAT, AppMode.AGENT_CHAT]: + elif app_mode in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.COMPLETION]: if not model_config_data: raise ValueError("Missing model_config in data argument " - "when app mode is chat or agent-chat") + "when app mode is chat, agent-chat or completion") else: raise ValueError("Invalid app mode") diff --git a/api/services/workflow/workflow_converter.py b/api/services/workflow/workflow_converter.py index 953c5c5a3cdb73..b1b0b2f3159fee 100644 --- a/api/services/workflow/workflow_converter.py +++ b/api/services/workflow/workflow_converter.py @@ -131,7 +131,8 @@ def convert_app_model_config_to_workflow(self, app_model: App, if app_config.dataset: knowledge_retrieval_node = self._convert_to_knowledge_retrieval_node( new_app_mode=new_app_mode, - dataset_config=app_config.dataset + dataset_config=app_config.dataset, + model_config=app_config.model ) if knowledge_retrieval_node: @@ -359,12 +360,15 @@ def _convert_to_http_request_node(self, app_model: App, return nodes - def _convert_to_knowledge_retrieval_node(self, new_app_mode: AppMode, dataset_config: DatasetEntity) \ + def _convert_to_knowledge_retrieval_node(self, new_app_mode: AppMode, + dataset_config: DatasetEntity, + model_config: ModelConfigEntity) \ -> Optional[dict]: """ Convert datasets to Knowledge Retrieval Node :param new_app_mode: new app mode :param dataset_config: dataset + :param model_config: model config :return: """ retrieve_config = dataset_config.retrieve_config @@ -385,6 +389,19 @@ def _convert_to_knowledge_retrieval_node(self, new_app_mode: AppMode, dataset_co "query_variable_selector": query_variable_selector, "dataset_ids": dataset_config.dataset_ids, "retrieval_mode": retrieve_config.retrieve_strategy.value, + "single_retrieval_config": { + "model": { + "provider": model_config.provider, + "name": model_config.model, + "mode": model_config.mode, + "completion_params": { + **model_config.parameters, + "stop": model_config.stop, + } + } + } + if retrieve_config.retrieve_strategy == DatasetRetrieveConfigEntity.RetrieveStrategy.MULTIPLE + else None, "multiple_retrieval_config": { "top_k": retrieve_config.top_k, "score_threshold": retrieve_config.score_threshold, diff --git a/api/tests/unit_tests/services/workflow/test_workflow_converter.py b/api/tests/unit_tests/services/workflow/test_workflow_converter.py index 0ca8ae135ce8dc..b4a4d6707aeb1f 100644 --- a/api/tests/unit_tests/services/workflow/test_workflow_converter.py +++ b/api/tests/unit_tests/services/workflow/test_workflow_converter.py @@ -206,9 +206,18 @@ def test__convert_to_knowledge_retrieval_node_for_chatbot(): ) ) + model_config = ModelConfigEntity( + provider='openai', + model='gpt-4', + mode='chat', + parameters={}, + stop=[] + ) + node = WorkflowConverter()._convert_to_knowledge_retrieval_node( new_app_mode=new_app_mode, - dataset_config=dataset_config + dataset_config=dataset_config, + model_config=model_config ) assert node["data"]["type"] == "knowledge-retrieval" @@ -240,9 +249,18 @@ def test__convert_to_knowledge_retrieval_node_for_workflow_app(): ) ) + model_config = ModelConfigEntity( + provider='openai', + model='gpt-4', + mode='chat', + parameters={}, + stop=[] + ) + node = WorkflowConverter()._convert_to_knowledge_retrieval_node( new_app_mode=new_app_mode, - dataset_config=dataset_config + dataset_config=dataset_config, + model_config=model_config ) assert node["data"]["type"] == "knowledge-retrieval"