|
| 1 | +# coding=utf-8 |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import jieba |
| 5 | +from django.db.models import Q |
| 6 | +from django.db.models import QuerySet |
| 7 | + |
| 8 | +from application.flow.i_step_node import NodeResult |
| 9 | +from application.flow.step_node.search_document_node.i_search_document_node import ISearchDocumentStepNode |
| 10 | +from knowledge.models import Document, DocumentTag, Knowledge |
| 11 | + |
| 12 | + |
| 13 | +class BaseSearchDocumentNode(ISearchDocumentStepNode): |
| 14 | + def save_context(self, details, workflow_manage): |
| 15 | + self.context['document_list'] = details.get('document_list') |
| 16 | + self.context['knowledge_list'] = details.get('knowledge_list') |
| 17 | + self.context['document_items'] = details.get('document_items') |
| 18 | + self.context['knowledge_items'] = details.get('knowledge_items') |
| 19 | + self.context['question'] = details.get('question') |
| 20 | + self.context['run_time'] = details.get('run_time') |
| 21 | + |
| 22 | + def get_reference_content(self, fields: List[str]): |
| 23 | + return self.workflow_manage.get_reference_field(fields[0], fields[1:]) |
| 24 | + |
| 25 | + def execute(self, knowledge_id_list: List, search_mode: str, search_scope_type: str, search_scope_source: str, |
| 26 | + search_scope_reference: List, question_reference: List, search_condition_type: str, |
| 27 | + search_condition_list: List, |
| 28 | + **kwargs) -> NodeResult: |
| 29 | + |
| 30 | + if search_scope_type == 'custom': # 手动选择知识库 |
| 31 | + document_id_list = QuerySet(Document).filter( |
| 32 | + knowledge_id__in=knowledge_id_list |
| 33 | + ).values_list('id', flat=True) |
| 34 | + else: # 引用上一步知识库/文档 |
| 35 | + if search_scope_source == 'document': # 文档 |
| 36 | + document_id_list = self.get_reference_content(search_scope_reference) |
| 37 | + else: # 知识库 |
| 38 | + document_id_list = QuerySet(Document).filter( |
| 39 | + knowledge_id__in=self.get_reference_content(search_scope_reference) |
| 40 | + ).values_list('id', flat=True) |
| 41 | + |
| 42 | + if search_mode == 'auto': # 通过问题自动检索 |
| 43 | + matched_doc_ids = self.handle_auto_tags(document_id_list, question_reference) |
| 44 | + |
| 45 | + final_document_ids = list(matched_doc_ids) |
| 46 | + else: # 自定义检索条件 |
| 47 | + matched_document_ids = self.handle_custom_tags( |
| 48 | + document_id_list, search_condition_list, search_condition_type |
| 49 | + ) |
| 50 | + |
| 51 | + final_document_ids = list(matched_document_ids) |
| 52 | + |
| 53 | + # UUID to str |
| 54 | + final_document_ids = [str(doc_id) for doc_id in final_document_ids] |
| 55 | + document_items = QuerySet(Document).filter(id__in=final_document_ids).values() |
| 56 | + final_knowledge_ids = list(set(str(doc['knowledge_id']) for doc in document_items)) |
| 57 | + knowledge_items = QuerySet(Knowledge).filter(id__in=final_knowledge_ids).values() |
| 58 | + |
| 59 | + return NodeResult({ |
| 60 | + 'document_list': final_document_ids, |
| 61 | + 'document_items': list(document_items), |
| 62 | + 'knowledge_list': final_knowledge_ids, |
| 63 | + 'knowledge_items': list(knowledge_items) |
| 64 | + }, {}) |
| 65 | + |
| 66 | + def handle_auto_tags(self, document_id_list: list, question_reference: list): |
| 67 | + question = self.get_reference_content(question_reference) |
| 68 | + |
| 69 | + # 使用jieba分词 |
| 70 | + keywords = jieba.lcut(question) |
| 71 | + if not keywords: |
| 72 | + return set() |
| 73 | + |
| 74 | + # 构建OR查询,一次性获取所有匹配的文档 |
| 75 | + q_objects = Q() |
| 76 | + for keyword in keywords: |
| 77 | + q_objects |= Q(tag__value__icontains=keyword) |
| 78 | + |
| 79 | + # 单次数据库查询 |
| 80 | + matched_doc_ids = set( |
| 81 | + QuerySet(DocumentTag) |
| 82 | + .filter(document_id__in=document_id_list) |
| 83 | + .filter(q_objects) |
| 84 | + .values_list('document_id', flat=True) |
| 85 | + .distinct() |
| 86 | + ) |
| 87 | + |
| 88 | + return matched_doc_ids |
| 89 | + |
| 90 | + def handle_custom_tags(self, document_id_list: List, search_condition_list: list, search_condition_type: str): |
| 91 | + |
| 92 | + if not search_condition_list: |
| 93 | + return set(document_id_list) |
| 94 | + |
| 95 | + if search_condition_type == 'AND': |
| 96 | + # AND逻辑:使用子查询和聚合 |
| 97 | + matched_doc_ids = set(document_id_list) |
| 98 | + |
| 99 | + for condition in search_condition_list: |
| 100 | + tag_key = condition['key'] |
| 101 | + field_value = self.workflow_manage.generate_prompt(condition['value']) |
| 102 | + compare_type = condition['compare'] |
| 103 | + |
| 104 | + # 构建查询条件 |
| 105 | + if compare_type == 'contain': |
| 106 | + q_filter = Q(tag__key=tag_key, tag__value__icontains=field_value) |
| 107 | + elif compare_type == 'eq': |
| 108 | + q_filter = Q(tag__key=tag_key, tag__value=field_value) |
| 109 | + elif compare_type == 'not_contain': |
| 110 | + q_filter = ~Q(tag__key=tag_key, tag__value__icontains=field_value) |
| 111 | + else: |
| 112 | + continue |
| 113 | + |
| 114 | + # 单次查询获取符合条件的文档 |
| 115 | + tag_docs = set(QuerySet(DocumentTag).filter( |
| 116 | + document_id__in=matched_doc_ids |
| 117 | + ).filter(q_filter).values_list('document_id', flat=True).distinct()) |
| 118 | + |
| 119 | + matched_doc_ids = matched_doc_ids.intersection(tag_docs) |
| 120 | + |
| 121 | + return matched_doc_ids |
| 122 | + |
| 123 | + else: |
| 124 | + # OR逻辑:使用一次查询完成 |
| 125 | + q_objects = Q() |
| 126 | + |
| 127 | + for condition in search_condition_list: |
| 128 | + tag_key = condition['key'] |
| 129 | + field_value = self.workflow_manage.generate_prompt(condition['value']) |
| 130 | + compare_type = condition['compare'] |
| 131 | + |
| 132 | + if compare_type == 'contain': |
| 133 | + q_objects |= Q(tag__key=tag_key, tag__value__icontains=field_value) |
| 134 | + elif compare_type == 'eq': |
| 135 | + q_objects |= Q(tag__key=tag_key, tag__value=field_value) |
| 136 | + elif compare_type == 'not_contain': |
| 137 | + q_objects |= ~Q(tag__key=tag_key, tag__value__icontains=field_value) |
| 138 | + |
| 139 | + # 一次查询获取所有匹配的文档 |
| 140 | + matched_docs = set(QuerySet(DocumentTag).filter( |
| 141 | + document_id__in=document_id_list |
| 142 | + ).filter(q_objects).values_list('document_id', flat=True).distinct()) |
| 143 | + |
| 144 | + return matched_docs |
| 145 | + |
| 146 | + def get_details(self, index: int, **kwargs): |
| 147 | + return { |
| 148 | + 'name': self.node.properties.get('stepName'), |
| 149 | + 'question': self.context.get('question'), |
| 150 | + "index": index, |
| 151 | + 'run_time': self.context.get('run_time'), |
| 152 | + 'document_list': self.context.get('document_list'), |
| 153 | + 'knowledge_list': self.context.get('knowledge_list'), |
| 154 | + 'document_items': self.context.get('document_items'), |
| 155 | + 'knowledge_items': self.context.get('knowledge_items'), |
| 156 | + 'type': self.node.type, |
| 157 | + 'status': self.status, |
| 158 | + 'err_message': self.err_message |
| 159 | + } |
0 commit comments