Skip to content
Closed
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
18 changes: 16 additions & 2 deletions api/core/workflow/nodes/knowledge_index/knowledge_index_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .exc import (
KnowledgeIndexNodeError,
)
from .protocols import IndexProcessorProtocol, SummaryIndexServiceProtocol

if TYPE_CHECKING:
from graphon.runtime import InitParams, RuntimeState
Expand All @@ -40,15 +41,28 @@ def __init__(
*,
init_params: "InitParams",
runtime_state: "RuntimeState",
index_processor: IndexProcessorProtocol | None = None,
summary_index_service: SummaryIndexServiceProtocol | None = None,
) -> None:
"""Build the node with its indexing and summary dependencies.

``index_processor`` previews or indexes the chunks, and ``summary_index_service``
generates their summaries. ``None`` selects the production ``IndexProcessor`` and
``SummaryIndex``, as the workflow node factory does; tests pass their own
implementations of these protocols. Construction performs no I/O.
"""
super().__init__(
node_id=node_id,
data=data,
init_params=init_params,
runtime_state=runtime_state,
)
self.index_processor = IndexProcessor()
self.summary_index_service = SummaryIndex()
self.index_processor: IndexProcessorProtocol = (
index_processor if index_processor is not None else IndexProcessor()
)
self.summary_index_service: SummaryIndexServiceProtocol = (
summary_index_service if summary_index_service is not None else SummaryIndex()
)

@override
def _run(self) -> NodeRunResult:
Expand Down
19 changes: 15 additions & 4 deletions api/core/workflow/nodes/knowledge_index/protocols.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from collections.abc import Mapping
from typing import Any, Protocol, TypedDict
from typing import TYPE_CHECKING, Any, Protocol, TypedDict

from pydantic import BaseModel, Field
from sqlalchemy.orm import Session

if TYPE_CHECKING:
from core.rag.index_processor.index_processor_base import SummaryIndexSettingDict


class IndexingResultDict(TypedDict):
dataset_id: str
Expand Down Expand Up @@ -35,6 +38,8 @@ class Preview(BaseModel):


class IndexProcessorProtocol(Protocol):
"""Indexing boundary of ``KnowledgeIndexNode``; ``IndexProcessor`` is the production implementation."""

def format_preview(self, chunk_structure: str, chunks: Any) -> Preview: ...

def index_and_clean(
Expand All @@ -44,7 +49,7 @@ def index_and_clean(
original_document_id: str,
chunks: Mapping[str, Any],
batch: Any,
summary_index_setting: dict[str, Any] | None = None,
summary_index_setting: "SummaryIndexSettingDict | None" = None,
*,
session: Session,
) -> IndexingResultDict: ...
Expand All @@ -55,13 +60,19 @@ def get_preview_output(
dataset_id: str,
document_id: str,
chunk_structure: str,
summary_index_setting: dict[str, Any] | None,
summary_index_setting: "SummaryIndexSettingDict | None",
*,
session: Session,
) -> Preview: ...


class SummaryIndexServiceProtocol(Protocol):
"""Summary boundary of ``KnowledgeIndexNode``; ``SummaryIndex`` is the production implementation."""

def generate_and_vectorize_summary(
self, dataset_id: str, document_id: str, is_preview: bool, summary_index_setting: dict[str, Any] | None = None
self,
dataset_id: str,
document_id: str,
is_preview: bool,
summary_index_setting: "SummaryIndexSettingDict | None" = None,
) -> None: ...
Loading