Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return page number of pdf documents upon retrieval #7749

Merged
merged 3 commits into from
Sep 5, 2024
Merged
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
3 changes: 2 additions & 1 deletion api/core/rag/datasource/vdb/vector_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def gen_index_struct_dict(vector_type: VectorType, collection_name: str) -> dict
class Vector:
def __init__(self, dataset: Dataset, attributes: list = None):
if attributes is None:
attributes = ['doc_id', 'dataset_id', 'document_id', 'doc_hash']
attributes = ['doc_id', 'dataset_id', 'document_id', 'doc_hash', 'page']
self._dataset = dataset
self._embeddings = self._get_embeddings()
self._attributes = attributes
Expand Down Expand Up @@ -107,6 +107,7 @@ def create(self, texts: list = None, **kwargs):
def add_texts(self, documents: list[Document], **kwargs):
if kwargs.get('duplicate_check', False):
documents = self._filter_duplicate_texts(documents)

embeddings = self._embeddings.embed_documents([document.page_content for document in documents])
self._vector_processor.create(
texts=documents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ def _fetch_dataset_retriever(self, node_data: KnowledgeRetrievalNodeData, query:
context_list = []
if all_documents:
document_score_list = {}
page_number_list = {}
for item in all_documents:
if item.metadata.get('score'):
document_score_list[item.metadata['doc_id']] = item.metadata['score']
# both 'page' and 'score' are metadata fields
if item.metadata.get('page'):
page_number_list[item.metadata['doc_id']] = item.metadata['page']

index_node_ids = [document.metadata['doc_id'] for document in all_documents]
segments = DocumentSegment.query.filter(
Expand All @@ -199,9 +203,9 @@ def _fetch_dataset_retriever(self, node_data: KnowledgeRetrievalNodeData, query:
Document.enabled == True,
Document.archived == False,
).first()

resource_number = 1
if dataset and document:

source = {
'metadata': {
'_source': 'knowledge',
Expand All @@ -211,6 +215,7 @@ def _fetch_dataset_retriever(self, node_data: KnowledgeRetrievalNodeData, query:
'document_id': document.id,
'document_name': document.name,
'document_data_source_type': document.data_source_type,
'page': page_number_list.get(segment.index_node_id, None),
'segment_id': segment.id,
'retriever_from': 'workflow',
'score': document_score_list.get(segment.index_node_id, None),
Expand Down
2 changes: 2 additions & 0 deletions api/core/workflow/nodes/llm/llm_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def _convert_to_original_retriever_resource(self, context_dict: dict) -> Optiona
if ('metadata' in context_dict and '_source' in context_dict['metadata']
and context_dict['metadata']['_source'] == 'knowledge'):
metadata = context_dict.get('metadata', {})

source = {
'position': metadata.get('position'),
'dataset_id': metadata.get('dataset_id'),
Expand All @@ -415,6 +416,7 @@ def _convert_to_original_retriever_resource(self, context_dict: dict) -> Optiona
'segment_position': metadata.get('segment_position'),
'index_node_hash': metadata.get('segment_index_node_hash'),
'content': context_dict.get('content'),
'page': metadata.get('page'),
}

return source
Expand Down
Loading