Skip to content
Merged
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 @@ -85,11 +85,11 @@ def execute(self, knowledge_id_list, knowledge_setting, question, show_knowledge
knowledge_id_list = self.get_reference_content(search_scope_reference)
else: # 文档
document_id_list = self.get_reference_content(search_scope_reference)
knowledge_id_list = list(QuerySet(Document).filter(
knowledge_id_list = [str(k) for k in QuerySet(Document).filter(
id__in=document_id_list
).values_list(
'knowledge_id', flat=True
).distinct())
).distinct()]

get_knowledge_list_of_authorized = DatabaseModelManage.get_model('get_knowledge_list_of_authorized')
chat_user_type = self.workflow_manage.get_body().get('chat_user_type')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code snippet seems to be part of a Django application for querying knowledge data based on specified parameters. However, there are some improvements and corrections that can be made:

  1. List Comprehension: When converting document_id_list from a queryset into a list of strings using [str(k) for k in ...], it might lead to unnecessary memory usage if document_id_list is already a list of ids.

  2. Distinct Method: The .distinct() method should not typically include any arguments when used with values lists, so you can remove (flat=True) since the default behavior is to return unique elements.

  3. Consistency: Ensure consistency between variable names and their types within the function to improve readability and prevent typos.

Here's the corrected version:

def execute(self, knowledge_id_list, knowledge_setting, question, show_knowledge):
    search_scope_reference = workflow_manage.build_search_scope(knowledge_id_list, knowledge_setting)
    
    document_id_list = []
    if isinstance(search_scope_reference, Document):  # 文案库中的文档id数组
        document_id_list.append(search_scope_reference.id)
    elif isinstance(search_scope_reference, ReferenceContentBase):  # 虚拟知识集(参考内容)
        reference_content_base = search_scope_reference
        reference_document_ids = query_set(ReferenceDocumentInfo).filter(reference_content=reference_content_base.reference_id).values_list('info_id', flat=True)
        
        if len(reference_document_ids) == 0:
            knowledge_id_list = [
                str(doc.knowledge_id)
                for doc in database_model.manage.get_model(get_knowledge_list_of_authorized, {}).order_by('-createdate').all()
            ]
        else:
            knowledge_id_list.extend([doc.knowledge_id for doc in query_set(Document).filter(id__in=reference_document_ids).values_list('knowledge_id', flat=True)])
    else:  # 搜索范围为其他类型的数据结构需要进一步处理...

# Note: Ensure proper formatting and adherence to Python best practices throughout the project.

These changes ensure better performance and maintainability of the code while aligning with Django ORM conventions.

Expand Down
Loading