Skip to content

Commit

Permalink
fix: Allow injection of ExpandChunks-task into `MultipleChunkRetrie…
Browse files Browse the repository at this point in the history
…verQa`
  • Loading branch information
NickyHavoc committed Apr 24, 2024
1 parent 52a68f7 commit 006b79f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from intelligence_layer.use_cases.search.expand_chunks import (
ExpandChunks,
ExpandChunksInput,
ExpandChunksOutput,
)
from intelligence_layer.use_cases.search.search import Search, SearchInput

Expand Down Expand Up @@ -49,6 +50,8 @@ class MultipleChunkRetrieverQa(
In contrast to the regular `RetrieverBasedQa`, this tasks injects multiple chunks into one
`SingleChunkQa` task run.
We recommend using this task instead of `RetrieverBasedQa`.
Note:
`model` provided should be a control-type model.
Expand All @@ -62,17 +65,17 @@ class MultipleChunkRetrieverQa(
def __init__(
self,
retriever: BaseRetriever[ID],
model: ControlModel | None = None,
insert_chunk_number: int = 5,
insert_chunk_size: int = 256,
model: ControlModel | None = None,
expand_chunks: Task[ExpandChunksInput[ID], ExpandChunksOutput] | None = None,
single_chunk_qa: Task[SingleChunkQaInput, SingleChunkQaOutput] | None = None,
):
super().__init__()
self._model = model or LuminousControlModel("luminous-supreme-control")
self._search = Search(retriever)
self._expand_chunks = ExpandChunks(retriever, self._model, insert_chunk_size)
self._single_chunk_qa = single_chunk_qa or SingleChunkQa(self._model)
self._insert_chunk_number = insert_chunk_number
self._model = model or LuminousControlModel("luminous-supreme-control")
self._expand_chunks = expand_chunks or ExpandChunks(retriever, self._model)
self._single_chunk_qa = single_chunk_qa or SingleChunkQa(self._model)

@staticmethod
def _combine_input_texts(chunks: Sequence[str]) -> tuple[TextChunk, Sequence[int]]:
Expand Down
6 changes: 6 additions & 0 deletions src/intelligence_layer/use_cases/qa/retriever_based_qa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Generic, Optional, Sequence

from pydantic import BaseModel
Expand Down Expand Up @@ -97,6 +98,11 @@ def __init__(
def do_run(
self, input: RetrieverBasedQaInput, task_span: TaskSpan
) -> RetrieverBasedQaOutput[ID]:
warnings.warn(
"`RetrieverBasedQa` is deprecated and will be removed in future versions.",
DeprecationWarning,
)

search_output = self._search.run(
SearchInput(query=input.question), task_span
).results
Expand Down
17 changes: 14 additions & 3 deletions tests/use_cases/qa/test_multiple_chunk_retriever_qa.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
from pytest import fixture

from intelligence_layer.connectors import QdrantInMemoryRetriever
from intelligence_layer.core import NoOpTracer
from intelligence_layer.core import LuminousControlModel, NoOpTracer
from intelligence_layer.core.tracer.in_memory_tracer import InMemoryTracer
from intelligence_layer.use_cases import MultipleChunkRetrieverQa, RetrieverBasedQaInput
from intelligence_layer.use_cases import (
ExpandChunks,
MultipleChunkRetrieverQa,
RetrieverBasedQaInput,
)


@fixture
def multiple_chunk_retriever_qa(
luminous_control_model: LuminousControlModel,
asymmetric_in_memory_retriever: QdrantInMemoryRetriever,
) -> MultipleChunkRetrieverQa[int]:
return MultipleChunkRetrieverQa(retriever=asymmetric_in_memory_retriever)
return MultipleChunkRetrieverQa(
retriever=asymmetric_in_memory_retriever,
model=luminous_control_model,
expand_chunks=ExpandChunks(
asymmetric_in_memory_retriever, luminous_control_model, 256
),
)


def test_multiple_chunk_retriever_qa_using_in_memory_retriever(
Expand Down

0 comments on commit 006b79f

Please sign in to comment.