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 288e66d7b38..6aa899bf286 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 @@ -479,7 +479,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 4bc000f7236..e6995d356fe 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 @@ -250,6 +250,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 {} @@ -258,8 +259,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 ToolRecord( id=task_record_id, workspace_id=tool_lib.workspace_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_assign_node/impl/base_variable_assign_node.py b/apps/application/flow/step_node/variable_assign_node/impl/base_variable_assign_node.py index f1711009a47..152bda93abc 100644 --- a/apps/application/flow/step_node/variable_assign_node/impl/base_variable_assign_node.py +++ b/apps/application/flow/step_node/variable_assign_node/impl/base_variable_assign_node.py @@ -71,21 +71,23 @@ def execute(self, variable_list, **kwargs) -> NodeResult: result_list = [] is_chat = False for variable in variable_list: - if 'fields' not in variable: + if not variable.get('fields'): continue + if 'global' == variable['fields'][0]: result = self.handle(variable, self.global_evaluation) result_list.append(result) - if 'chat' == variable['fields'][0]: + elif 'chat' == variable['fields'][0]: result = self.handle(variable, self.chat_evaluation) result_list.append(result) is_chat = True - if 'loop' == variable['fields'][0]: + elif 'loop' == variable['fields'][0]: result = self.handle(variable, self.loop_evaluation) result_list.append(result) - if 'output' == variable['fields'][0]: + elif 'output' == variable['fields'][0]: result = self.handle(variable, self.out_evaluation) result_list.append(result) + if is_chat: from application.flow.loop_workflow_manage import LoopWorkflowManage if isinstance(self.workflow_manage, LoopWorkflowManage): 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 076d7634178..79c3cd22379 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/chat/serializers/chat_embed_serializers.py b/apps/chat/serializers/chat_embed_serializers.py index 7f330f93e94..5594e5b0abe 100644 --- a/apps/chat/serializers/chat_embed_serializers.py +++ b/apps/chat/serializers/chat_embed_serializers.py @@ -32,9 +32,8 @@ def get_embed(self, with_valid=True, params=None): if with_valid: self.is_valid(raise_exception=True) index_path = os.path.join(PROJECT_DIR, 'apps', "chat", 'template', 'embed.js') - file = open(index_path, "r", encoding='utf-8') - content = file.read() - file.close() + with open(index_path, "r", encoding='utf-8') as file: + content = file.read() application_access_token = QuerySet(ApplicationAccessToken).filter( access_token=self.data.get('token')).first() is_draggable = 'false' diff --git a/apps/chat/serializers/chat_record.py b/apps/chat/serializers/chat_record.py index d094216f53f..ee6d446dd57 100644 --- a/apps/chat/serializers/chat_record.py +++ b/apps/chat/serializers/chat_record.py @@ -75,8 +75,7 @@ def vote(self, instance: Dict, with_valid=True): chat_record_details_model.vote_status = VoteChoices.STAR chat_record_details_model.vote_reason = vote_reason chat_record_details_model.vote_other_content = vote_other_content - - if vote_status == VoteChoices.TRAMPLE: + elif vote_status == VoteChoices.TRAMPLE: # 点踩 chat_record_details_model.vote_status = VoteChoices.TRAMPLE chat_record_details_model.vote_reason = vote_reason diff --git a/apps/common/handle/impl/common_handle.py b/apps/common/handle/impl/common_handle.py index 16c647a9626..98b7537926f 100644 --- a/apps/common/handle/impl/common_handle.py +++ b/apps/common/handle/impl/common_handle.py @@ -86,46 +86,45 @@ def handle_images(deps, archive: ZipFile) -> []: def xlsx_embed_cells_images(buffer) -> {}: - archive = ZipFile(buffer) - # 解析cellImage.xml文件 - deps = get_dependents(archive, get_rels_path("xl/cellimages.xml")) - image_rel = handle_images(deps=deps, archive=archive) - # 工作表及其中图片ID - sheet_list = {} - for item in archive.namelist(): - if not item.startswith('xl/worksheets/sheet'): - continue - key = item.split('/')[-1].split('.')[0].split('sheet')[-1] - sheet_list[key] = parse_element_sheet_xml(fromstring(archive.read(item))) - cell_images_xml = parse_element(fromstring(archive.read("xl/cellimages.xml"))) - cell_images_rel = {} - for image in image_rel: - cell_images_rel[image.embed] = image - for cnv, embed in cell_images_xml.items(): - cell_images_xml[cnv] = cell_images_rel.get(embed) - result = {} - for key, img in cell_images_xml.items(): - all_cells = [ - cell - for _sheet_id, sheet in sheet_list.items() - if sheet is not None - for cell in sheet or [] - ] + with ZipFile(buffer) as archive: + # 解析cellImage.xml文件 + deps = get_dependents(archive, get_rels_path("xl/cellimages.xml")) + image_rel = handle_images(deps=deps, archive=archive) + # 工作表及其中图片ID + sheet_list = {} + for item in archive.namelist(): + if not item.startswith('xl/worksheets/sheet'): + continue + key = item.split('/')[-1].split('.')[0].split('sheet')[-1] + sheet_list[key] = parse_element_sheet_xml(fromstring(archive.read(item))) + cell_images_xml = parse_element(fromstring(archive.read("xl/cellimages.xml"))) + cell_images_rel = {} + for image in image_rel: + cell_images_rel[image.embed] = image + for cnv, embed in cell_images_xml.items(): + cell_images_xml[cnv] = cell_images_rel.get(embed) + result = {} + for key, img in cell_images_xml.items(): + all_cells = [ + cell + for _sheet_id, sheet in sheet_list.items() + if sheet is not None + for cell in sheet or [] + ] - image_excel_id_list = [ - cell for cell in all_cells - if isinstance(cell, str) and key in cell - ] - # print(key, img) - if img is None: - continue - if len(image_excel_id_list) > 0: - image_excel_id = image_excel_id_list[-1] - f = archive.open(img.target) - img_byte = io.BytesIO() - im = PILImage.open(f).convert('RGB') - im.save(img_byte, format='JPEG') - image = File(id=uuid.uuid7(), file_name=img.path, meta={'debug': False, 'content': img_byte.getvalue()}) - result['=' + image_excel_id] = image - archive.close() + image_excel_id_list = [ + cell for cell in all_cells + if isinstance(cell, str) and key in cell + ] + # print(key, img) + if img is None: + continue + if len(image_excel_id_list) > 0: + image_excel_id = image_excel_id_list[-1] + with archive.open(img.target) as f: + img_byte = io.BytesIO() + im = PILImage.open(f).convert('RGB') + im.save(img_byte, format='JPEG') + image = File(id=uuid.uuid7(), file_name=img.path, meta={'debug': False, 'content': img_byte.getvalue()}) + result['=' + image_excel_id] = image return result 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/common.py b/apps/knowledge/serializers/common.py index 50d8b16275d..1ef66386cce 100644 --- a/apps/knowledge/serializers/common.py +++ b/apps/knowledge/serializers/common.py @@ -162,12 +162,11 @@ def get_embedding_model_id_by_knowledge_id_list(knowledge_id_list: List): def zip_dir(zip_path, output=None): output = output or os.path.basename(zip_path) + '.zip' - zip = zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) - for root, dirs, files in os.walk(zip_path): - relative_root = '' if root == zip_path else root.replace(zip_path, '') + os.sep - for filename in files: - zip.write(os.path.join(root, filename), relative_root + filename) - zip.close() + with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip: + for root, dirs, files in os.walk(zip_path): + relative_root = '' if root == zip_path else root.replace(zip_path, '') + os.sep + for filename in files: + zip.write(os.path.join(root, filename), relative_root + filename) def is_valid_uuid(s): diff --git a/apps/knowledge/serializers/document.py b/apps/knowledge/serializers/document.py index d8466663464..007ac8489d9 100644 --- a/apps/knowledge/serializers/document.py +++ b/apps/knowledge/serializers/document.py @@ -228,19 +228,17 @@ def export(self, with_valid=True): self.is_valid(raise_exception=True) language = get_language() if self.data.get('type') == 'csv': - file = open( + with open( os.path.join(PROJECT_DIR, "apps", "knowledge", 'template', f'csv_template_{to_locale(language)}.csv'), - "rb") - content = file.read() - file.close() + "rb") as file: + content = file.read() 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', - f'excel_template_{to_locale(language)}.xlsx'), "rb") - content = file.read() - file.close() + with open(os.path.join(PROJECT_DIR, "apps", "knowledge", 'template', + f'excel_template_{to_locale(language)}.xlsx'), "rb") as file: + content = file.read() return HttpResponse(content, status=200, headers={'Content-Type': 'application/vnd.ms-excel', 'Content-Disposition': 'attachment; filename="excel_template.xlsx"'}) else: @@ -251,20 +249,18 @@ def table_export(self, with_valid=True): self.is_valid(raise_exception=True) language = get_language() if self.data.get('type') == 'csv': - file = open( + with open( os.path.join(PROJECT_DIR, "apps", "knowledge", 'template', f'table_template_{to_locale(language)}.csv'), - "rb") - content = file.read() - file.close() - return HttpResponse(content, status=200, headers={'Content-Type': 'text/cxv', + "rb") as file: + content = file.read() + 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', - f'table_template_{to_locale(language)}.xlsx'), - "rb") - content = file.read() - file.close() + with open(os.path.join(PROJECT_DIR, "apps", "knowledge", 'template', + f'table_template_{to_locale(language)}.xlsx'), + "rb") as file: + content = file.read() return HttpResponse(content, status=200, headers={'Content-Type': 'application/vnd.ms-excel', 'Content-Disposition': 'attachment; filename="excel_template.xlsx"'}) else: 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/maxkb/urls/web.py b/apps/maxkb/urls/web.py index fb043d56a64..1fee100f073 100644 --- a/apps/maxkb/urls/web.py +++ b/apps/maxkb/urls/web.py @@ -80,9 +80,8 @@ def pro(): def get_index_html(index_path): - file = open(index_path, "r", encoding='utf-8') - content = file.read() - file.close() + with open(index_path, "r", encoding='utf-8') as file: + content = file.read() return content 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: diff --git a/apps/users/serializers/user.py b/apps/users/serializers/user.py index 45e55528c30..05cf370c16d 100644 --- a/apps/users/serializers/user.py +++ b/apps/users/serializers/user.py @@ -1071,11 +1071,10 @@ def send(self): ]), range(6)))) # 获取邮件模板 language = get_language() - file = open( - os.path.join(PROJECT_DIR, "apps", "common", 'template', f'email_template_{to_locale(language)}.html'), "r", - encoding='utf-8') - content = file.read() - file.close() + with open( + os.path.join(PROJECT_DIR, "apps", "common", 'template', f'email_template_{to_locale(language)}.html'), + "r", encoding='utf-8') as file: + content = file.read() code_cache_key = email + ":" + state code_cache_key_lock = code_cache_key + "_lock" # 设置缓存 diff --git a/ui/src/locales/lang/en-US/workflow.ts b/ui/src/locales/lang/en-US/workflow.ts index 74fa49f04b4..3b1ba0d740c 100644 --- a/ui/src/locales/lang/en-US/workflow.ts +++ b/ui/src/locales/lang/en-US/workflow.ts @@ -188,7 +188,7 @@ export default { result: 'Search Results', searchParam: 'Search Parameters', select_variable: 'Select Variable', - valueMessage: `Value or name `, + valueMessage: 'Value or name', searchQuestion: { label: 'Search Question',