diff --git a/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py b/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py index 1b3020bca0b..dd557306305 100644 --- a/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py +++ b/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py @@ -461,7 +461,7 @@ def handle_variables(self, tool_params): tool_params[k] = self.workflow_manage.generate_prompt(tool_params[k]) if type(v) == dict: self.handle_variables(v) - if (type(v) == list) and (type(v[0]) == str): + if (type(v) == list) and len(v) > 0 and (type(v[0]) == str): tool_params[k] = self.get_reference_content(v) return tool_params @@ -487,7 +487,7 @@ def generate_prompt_question(self, prompt): def generate_message_list(self, system: str, prompt: str, history_message): if system is not None and len(system) > 0: - return [SystemMessage(self.workflow_manage.generate_prompt(system)), *history_message, + return [SystemMessage(system), *history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))] else: return [*history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))] diff --git a/apps/application/flow/step_node/question_node/impl/base_question_node.py b/apps/application/flow/step_node/question_node/impl/base_question_node.py index 2a83493a534..07ba8cd7622 100644 --- a/apps/application/flow/step_node/question_node/impl/base_question_node.py +++ b/apps/application/flow/step_node/question_node/impl/base_question_node.py @@ -136,7 +136,7 @@ def generate_prompt_question(self, prompt): def generate_message_list(self, system: str, prompt: str, history_message): if system is not None and len(system) > 0: - return [SystemMessage(self.workflow_manage.generate_prompt(system)), *history_message, + return [SystemMessage(system), *history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))] else: return [*history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))] diff --git a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py index 23d18efe2f6..421b94dee45 100644 --- a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py +++ b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py @@ -251,6 +251,7 @@ def execute(self, tool_lib_id, input_field_list, **kwargs) -> NodeResult: def tool_exec_record(self, tool_lib, all_params): task_record_id = uuid.uuid7() start_time = time.time() + filtered_args = all_params try: # 过滤掉 tool_init_params 中的参数 tool_init_params = json.loads(rsa_long_decrypt(tool_lib.init_params)) if tool_lib.init_params else {} @@ -259,8 +260,6 @@ def tool_exec_record(self, tool_lib, all_params): k: v for k, v in all_params.items() if k not in tool_init_params } - else: - filtered_args = all_params if [WorkflowMode.KNOWLEDGE, WorkflowMode.KNOWLEDGE_LOOP].__contains__( self.workflow_manage.flow.workflow_mode): source_id = self.workflow_manage.params.get('knowledge_id') diff --git a/apps/application/flow/step_node/variable_aggregation_node/impl/base_variable_aggregation_node.py b/apps/application/flow/step_node/variable_aggregation_node/impl/base_variable_aggregation_node.py index fa1d6479e5c..341f2e0eab9 100644 --- a/apps/application/flow/step_node/variable_aggregation_node/impl/base_variable_aggregation_node.py +++ b/apps/application/flow/step_node/variable_aggregation_node/impl/base_variable_aggregation_node.py @@ -24,7 +24,7 @@ class BaseVariableAggregationNode(IVariableAggregation): def save_context(self, details, workflow_manage): for key, value in details.get('result').items(): - self.context['key'] = value + self.context[key] = value self.context['result'] = details.get('result') self.context['strategy'] = details.get('strategy') self.context['group_list'] = details.get('group_list') diff --git a/apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py b/apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py index 90a47613a46..274604e2328 100644 --- a/apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py +++ b/apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py @@ -16,7 +16,7 @@ jsonpath_expr_cache = MemCache('parse_path', { 'TIMEOUT': 3600, # 缓存有效期为 1 小时 'OPTIONS': { - 'MAX_ENTRIES': 1000, # 最多缓存 500 个条目 + 'MAX_ENTRIES': 1000, # 最多缓存 1000 个条目 'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存 }, }) diff --git a/apps/application/serializers/application_chat_record.py b/apps/application/serializers/application_chat_record.py index 540d9310dd9..92946bbdc01 100644 --- a/apps/application/serializers/application_chat_record.py +++ b/apps/application/serializers/application_chat_record.py @@ -104,7 +104,6 @@ def is_valid(self, *, raise_exception=False): def list(self, with_valid=True): if with_valid: self.is_valid(raise_exception=True) - QuerySet(ChatRecord).filter(chat_id=self.data.get('chat_id')) order_by = 'create_time' if self.data.get('order_asc') is None or self.data.get( 'order_asc') else '-create_time' return [ChatRecordSerializerModel(chat_record).data for chat_record in @@ -169,7 +168,7 @@ def reset_chat_record(chat_record, show_source, show_exec): 'padding_problem_text': chat_record.details.get('problem_padding').get( 'padding_problem_text') if 'problem_padding' in chat_record.details else None, **(show_source_dict if show_source else {}), - **(show_exec_dict if show_exec else show_exec_dict) + **(show_exec_dict if show_exec else {}) } def page(self, current_page: int, page_size: int, with_valid=True, show_source=None, show_exec=None): diff --git a/apps/chat/serializers/chat.py b/apps/chat/serializers/chat.py index b474e567853..9cc22810953 100644 --- a/apps/chat/serializers/chat.py +++ b/apps/chat/serializers/chat.py @@ -382,10 +382,10 @@ def get_chat_record(chat_info, chat_record_id): str(chat_record.id) == str(chat_record_id)] if chat_record_list is not None and len(chat_record_list): return chat_record_list[-1] - chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_info.chat_id).first() - if chat_record is None: - if not is_valid_uuid(chat_record_id): - raise ChatException(500, _("Conversation record does not exist")) + chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_info.chat_id).first() + if chat_record is None: + if not is_valid_uuid(chat_record_id): + raise ChatException(500, _("Conversation record does not exist")) chat_record = QuerySet(ChatRecord).filter(id=chat_record_id).first() return chat_record diff --git a/apps/common/handle/impl/text/pdf_split_handle.py b/apps/common/handle/impl/text/pdf_split_handle.py index f3172e29884..0ffafff61c0 100644 --- a/apps/common/handle/impl/text/pdf_split_handle.py +++ b/apps/common/handle/impl/text/pdf_split_handle.py @@ -49,8 +49,9 @@ def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_bu # 获取临时文件的路径 temp_file_path = temp_file.name - pdf_document = fitz.open(temp_file_path) + pdf_document = None try: + pdf_document = fitz.open(temp_file_path) if type(limit) is str: limit = int(limit) if type(with_filter) is str: @@ -79,7 +80,8 @@ def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_bu 'content': [] } finally: - pdf_document.close() + if pdf_document is not None: + pdf_document.close() # 处理完后可以删除临时文件 os.remove(temp_file_path) @@ -331,9 +333,15 @@ def get_content(self, file, save_image): # 获取临时文件的路径 temp_file_path = temp_file.name - pdf_document = fitz.open(temp_file_path) + pdf_document = None try: + pdf_document = fitz.open(temp_file_path) return self.handle_pdf_content(file, pdf_document) except BaseException as e: traceback.print_exception(e) return f'{e}' + finally: + if pdf_document is not None: + pdf_document.close() + # 处理完后可以删除临时文件 + os.remove(temp_file_path) diff --git a/apps/common/handle/impl/text/zip_split_handle.py b/apps/common/handle/impl/text/zip_split_handle.py index d8365d5c285..498afbea848 100644 --- a/apps/common/handle/impl/text/zip_split_handle.py +++ b/apps/common/handle/impl/text/zip_split_handle.py @@ -83,7 +83,7 @@ def get_image_list(result_list: list, zip_files: List[str]): if not zip_files.__contains__(image_path): continue if image_path.startswith('oss/file/') or image_path.startswith('oss/image/'): - image_id = image_path.replace('oss/file/', '').replace('oss/file/', '') + image_id = image_path.replace('oss/file/', '').replace('oss/image/', '') if is_valid_uuid(image_id): image_file_list.append({'source_file': image_path, 'image_id': image_id}) @@ -115,7 +115,7 @@ def get_image_list_by_content(name: str, content: str, zip_files: List[str]): if not zip_files.__contains__(image_path): continue if image_path.startswith('oss/file/') or image_path.startswith('oss/image/'): - image_id = image_path.replace('oss/file/', '').replace('oss/file/', '') + image_id = image_path.replace('oss/file/', '').replace('oss/image/', '') if is_valid_uuid(image_id): image_file_list.append({'source_file': image_path, 'image_id': image_id}) diff --git a/apps/knowledge/serializers/document.py b/apps/knowledge/serializers/document.py index d8466663464..e52cf23bbd8 100644 --- a/apps/knowledge/serializers/document.py +++ b/apps/knowledge/serializers/document.py @@ -257,7 +257,7 @@ def table_export(self, with_valid=True): "rb") content = file.read() file.close() - return HttpResponse(content, status=200, headers={'Content-Type': 'text/cxv', + return HttpResponse(content, status=200, headers={'Content-Type': 'text/csv', 'Content-Disposition': 'attachment; filename="csv_template.csv"'}) elif self.data.get('type') == 'excel': file = open(os.path.join(PROJECT_DIR, "apps", "knowledge", 'template', diff --git a/apps/knowledge/serializers/paragraph.py b/apps/knowledge/serializers/paragraph.py index a1df1bd8100..c3a36c2b2ba 100644 --- a/apps/knowledge/serializers/paragraph.py +++ b/apps/knowledge/serializers/paragraph.py @@ -226,7 +226,6 @@ def edit(self, instance: Dict): return self.one(), instance, self.data.get('knowledge_id') def get_problem_list(self): - ProblemParagraphMapping(ProblemParagraphMapping) problem_paragraph_mapping = QuerySet(ProblemParagraphMapping).filter( paragraph_id=self.data.get("paragraph_id")) if len(problem_paragraph_mapping) > 0: diff --git a/apps/models_provider/impl/aws_bedrock_model_provider/model/llm.py b/apps/models_provider/impl/aws_bedrock_model_provider/model/llm.py index 50ee4abfe65..e5d32ab1c9d 100644 --- a/apps/models_provider/impl/aws_bedrock_model_provider/model/llm.py +++ b/apps/models_provider/impl/aws_bedrock_model_provider/model/llm.py @@ -92,7 +92,11 @@ def _update_aws_credentials(profile_name, access_key_id, secret_access_key): credentials_path = os.path.join(os.path.expanduser("~"), ".aws", "credentials") os.makedirs(os.path.dirname(credentials_path), exist_ok=True) - content = open(credentials_path, 'r').read() if os.path.exists(credentials_path) else '' + if os.path.exists(credentials_path): + with open(credentials_path, 'r') as f: + content = f.read() + else: + content = '' pattern = rf'\n*\[{profile_name}\]\n*(aws_access_key_id = .*)\n*(aws_secret_access_key = .*)\n*' content = re.sub(pattern, '', content, flags=re.DOTALL) diff --git a/apps/models_provider/impl/ollama_model_provider/ollama_model_provider.py b/apps/models_provider/impl/ollama_model_provider/ollama_model_provider.py index 2ad2107e1a2..5bb8a54049f 100644 --- a/apps/models_provider/impl/ollama_model_provider/ollama_model_provider.py +++ b/apps/models_provider/impl/ollama_model_provider/ollama_model_provider.py @@ -224,7 +224,7 @@ def convert_to_down_model_chunk(row_str: str, chunk_index: int): if row.get('status').__contains__("pulling"): progress = 0 status = DownModelChunkStatus.pulling - if 'total' in row and 'completed' in row: + if 'total' in row and 'completed' in row and row.get('total'): progress = (row.get('completed') / row.get('total') * 100) elif 'error' in row: status = DownModelChunkStatus.error diff --git a/apps/trigger/handler/impl/trigger/event_trigger.py b/apps/trigger/handler/impl/trigger/event_trigger.py index 6368cf58b13..cb3df4c26f4 100644 --- a/apps/trigger/handler/impl/trigger/event_trigger.py +++ b/apps/trigger/handler/impl/trigger/event_trigger.py @@ -119,7 +119,7 @@ def execute(trigger, request=None, **kwargs): trigger_setting = trigger.get('trigger_setting') if trigger_setting.get('token'): token = request.META.get('HTTP_AUTHORIZATION') - if trigger_setting.get('token') != token.replace('Bearer ', ''): + if not token or trigger_setting.get('token') != token.replace('Bearer ', ''): raise AppAuthenticationFailed(1002, _('Authentication information is incorrect')) is_active = trigger.get('is_active') if not is_active: