Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此方法调用前,已经执行过,无需重复执行。

HumanMessage(self.workflow_manage.generate_prompt(prompt))]
else:
return [*history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此方法调用前,已经执行过,无需重复执行。

HumanMessage(self.workflow_manage.generate_prompt(prompt))]
else:
return [*history_message, HumanMessage(self.workflow_manage.generate_prompt(prompt))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

@wangliang181230 wangliang181230 Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except 代码段中在用。

try:
# 过滤掉 tool_init_params 中的参数
tool_init_params = json.loads(rsa_long_decrypt(tool_lib.init_params)) if tool_lib.init_params else {}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions apps/application/serializers/application_chat_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions apps/chat/serializers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chat_info 判断不为空才执行,否则肯定会报空指针异常。

chat_record = QuerySet(ChatRecord).filter(id=chat_record_id).first()
return chat_record

Expand Down
5 changes: 2 additions & 3 deletions apps/chat/serializers/chat_embed_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions apps/chat/serializers/chat_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 40 additions & 41 deletions apps/common/handle/impl/common_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 11 additions & 3 deletions apps/common/handle/impl/text/pdf_split_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions apps/common/handle/impl/text/zip_split_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand Down
11 changes: 5 additions & 6 deletions apps/knowledge/serializers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
32 changes: 14 additions & 18 deletions apps/knowledge/serializers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion apps/knowledge/serializers/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions apps/maxkb/urls/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/trigger/handler/impl/trigger/event_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading
Loading