unique_toolkit 2026.20.0.dev28__py3-none-any.whl → 2026.20.0.dev30__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unique_toolkit/_common/chunk_relevancy_sorter/service.py +9 -1
- unique_toolkit/_common/metadata_filter_scope.py +47 -0
- unique_toolkit/_common/pydantic_helpers.py +6 -1
- unique_toolkit/agentic/history_manager/history_manager.py +1 -5
- unique_toolkit/app/schemas.py +21 -0
- unique_toolkit/experimental/components/__init__.py +3 -0
- unique_toolkit/experimental/components/_base.py +147 -0
- unique_toolkit/experimental/components/internal_search/__init__.py +97 -0
- unique_toolkit/experimental/components/internal_search/base/__init__.py +37 -0
- unique_toolkit/experimental/components/internal_search/base/config.py +72 -0
- unique_toolkit/experimental/components/internal_search/base/schemas.py +56 -0
- unique_toolkit/experimental/components/internal_search/base/service.py +189 -0
- unique_toolkit/experimental/components/internal_search/base/utils.py +183 -0
- unique_toolkit/experimental/components/internal_search/chat/__init__.py +9 -0
- unique_toolkit/experimental/components/internal_search/chat/config.py +27 -0
- unique_toolkit/experimental/components/internal_search/chat/schemas.py +15 -0
- unique_toolkit/experimental/components/internal_search/chat/service.py +61 -0
- unique_toolkit/experimental/components/internal_search/knowledge_base/__init__.py +15 -0
- unique_toolkit/experimental/components/internal_search/knowledge_base/config.py +61 -0
- unique_toolkit/experimental/components/internal_search/knowledge_base/schemas.py +41 -0
- unique_toolkit/experimental/components/internal_search/knowledge_base/service.py +119 -0
- unique_toolkit/experimental/components/internal_search/post_processing/__init__.py +52 -0
- unique_toolkit/experimental/components/internal_search/post_processing/config.py +61 -0
- unique_toolkit/experimental/components/internal_search/post_processing/service.py +199 -0
- unique_toolkit/services/chat_service.py +67 -0
- unique_toolkit/services/factory.py +21 -5
- unique_toolkit/services/knowledge_base.py +38 -34
- {unique_toolkit-2026.20.0.dev28.dist-info → unique_toolkit-2026.20.0.dev30.dist-info}/METADATA +2 -2
- {unique_toolkit-2026.20.0.dev28.dist-info → unique_toolkit-2026.20.0.dev30.dist-info}/RECORD +30 -11
- {unique_toolkit-2026.20.0.dev28.dist-info → unique_toolkit-2026.20.0.dev30.dist-info}/WHEEL +0 -0
|
@@ -3,7 +3,7 @@ import time
|
|
|
3
3
|
from collections import Counter
|
|
4
4
|
from typing import Any, overload
|
|
5
5
|
|
|
6
|
-
from typing_extensions import deprecated
|
|
6
|
+
from typing_extensions import Self, deprecated
|
|
7
7
|
|
|
8
8
|
from unique_toolkit._common.chunk_relevancy_sorter.config import (
|
|
9
9
|
ChunkRelevancySortConfig,
|
|
@@ -39,6 +39,7 @@ from unique_toolkit.agentic.evaluation.schemas import (
|
|
|
39
39
|
)
|
|
40
40
|
from unique_toolkit.app.performance.async_tasks import run_async_tasks_parallel
|
|
41
41
|
from unique_toolkit.app.schemas import BaseEvent, ChatEvent
|
|
42
|
+
from unique_toolkit.app.unique_settings import UniqueSettings
|
|
42
43
|
from unique_toolkit.content.schemas import ContentChunk
|
|
43
44
|
from unique_toolkit.language_model.infos import LanguageModelInfo
|
|
44
45
|
|
|
@@ -84,6 +85,13 @@ class ChunkRelevancySorter:
|
|
|
84
85
|
def from_event(cls, event: ChatEvent | BaseEvent[Any]):
|
|
85
86
|
return cls(company_id=event.company_id, user_id=event.user_id)
|
|
86
87
|
|
|
88
|
+
@classmethod
|
|
89
|
+
def from_settings(cls, settings: UniqueSettings) -> Self:
|
|
90
|
+
return cls(
|
|
91
|
+
company_id=settings.authcontext.get_confidential_company_id(),
|
|
92
|
+
user_id=settings.authcontext.get_confidential_user_id(),
|
|
93
|
+
)
|
|
94
|
+
|
|
87
95
|
async def run(
|
|
88
96
|
self,
|
|
89
97
|
input_text: str,
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""UniqueQL helpers for KB ``folderId`` / ``metadata_filter`` scoping."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from unique_toolkit.content.smart_rules import (
|
|
9
|
+
Operator,
|
|
10
|
+
Statement,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
_UQL_AND = "and"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def build_folder_id_in_clause(scope_ids: list[str]) -> dict[str, Any]:
|
|
17
|
+
"""UniqueQL for selected folders using ``folderId in [scope_ids]``.
|
|
18
|
+
|
|
19
|
+
Equivalent to passing ``scope_ids`` directly to the wire-level search API.
|
|
20
|
+
"""
|
|
21
|
+
if not scope_ids:
|
|
22
|
+
raise ValueError("scope_ids must be a non-empty list")
|
|
23
|
+
return Statement(
|
|
24
|
+
operator=Operator.IN,
|
|
25
|
+
path=["folderId"],
|
|
26
|
+
value=scope_ids,
|
|
27
|
+
).model_dump(mode="json")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def merge_scope_clause_into_metadata_filter(
|
|
31
|
+
scope_clause: Mapping[str, Any],
|
|
32
|
+
metadata_filter: dict[str, Any] | None,
|
|
33
|
+
) -> dict[str, Any]:
|
|
34
|
+
"""AND-merge *scope_clause* with *metadata_filter*, flattening a top-level ``and``."""
|
|
35
|
+
scope_dict = dict(scope_clause)
|
|
36
|
+
if not metadata_filter:
|
|
37
|
+
return scope_dict
|
|
38
|
+
inner_and = metadata_filter.get(_UQL_AND)
|
|
39
|
+
if isinstance(inner_and, list):
|
|
40
|
+
return {_UQL_AND: [scope_dict, *inner_and]}
|
|
41
|
+
return {_UQL_AND: [scope_dict, dict(metadata_filter)]}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
"build_folder_id_in_clause",
|
|
46
|
+
"merge_scope_clause_into_metadata_filter",
|
|
47
|
+
]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import warnings
|
|
3
|
-
from typing import Any, TypeVar, Unpack
|
|
3
|
+
from typing import Annotated, Any, TypeVar, Unpack
|
|
4
4
|
|
|
5
5
|
import humps
|
|
6
6
|
from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, create_model
|
|
@@ -33,6 +33,11 @@ BeforeValidator to replace an incoming None with the field's declared default.
|
|
|
33
33
|
"""
|
|
34
34
|
NoneToDefault = BeforeValidator(_none_to_default)
|
|
35
35
|
|
|
36
|
+
DeactivatedNone = Annotated[
|
|
37
|
+
None,
|
|
38
|
+
Field(title="Deactivated", description="None"),
|
|
39
|
+
]
|
|
40
|
+
|
|
36
41
|
|
|
37
42
|
def get_configuration_dict(**kwargs: Unpack[ConfigDict]) -> ConfigDict:
|
|
38
43
|
base: dict[str, object] = {
|
|
@@ -8,6 +8,7 @@ from pydantic import BaseModel, Field
|
|
|
8
8
|
from unique_toolkit._common.feature_flags.schema import (
|
|
9
9
|
FeatureExtendedSourceSerialization,
|
|
10
10
|
)
|
|
11
|
+
from unique_toolkit._common.pydantic_helpers import DeactivatedNone
|
|
11
12
|
from unique_toolkit._common.validators import LMI
|
|
12
13
|
from unique_toolkit.agentic.history_manager.loop_token_reducer import LoopTokenReducer
|
|
13
14
|
from unique_toolkit.agentic.history_manager.utils import (
|
|
@@ -30,11 +31,6 @@ from unique_toolkit.language_model.schemas import (
|
|
|
30
31
|
LanguageModelToolMessage,
|
|
31
32
|
)
|
|
32
33
|
|
|
33
|
-
DeactivatedNone = Annotated[
|
|
34
|
-
None,
|
|
35
|
-
Field(title="Deactivated", description="None"),
|
|
36
|
-
]
|
|
37
|
-
|
|
38
34
|
|
|
39
35
|
class UploadedContentConfig(BaseModel):
|
|
40
36
|
model_config = get_configuration_dict()
|
unique_toolkit/app/schemas.py
CHANGED
|
@@ -215,6 +215,20 @@ class Correlation(BaseModel):
|
|
|
215
215
|
parent_assistant_id: str
|
|
216
216
|
|
|
217
217
|
|
|
218
|
+
class ChatEventSkillChoice(BaseModel):
|
|
219
|
+
model_config = model_config
|
|
220
|
+
|
|
221
|
+
name: str = Field(default="", description="Skill name.")
|
|
222
|
+
scope_id: str = Field(
|
|
223
|
+
default="",
|
|
224
|
+
description="Knowledge base scope ID that contains the skill file.",
|
|
225
|
+
)
|
|
226
|
+
content_id: str = Field(
|
|
227
|
+
default="",
|
|
228
|
+
description="Knowledge base content ID of the ``SKILL.md`` file.",
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
|
|
218
232
|
class ChatEventPayload(BaseModel):
|
|
219
233
|
model_config = model_config
|
|
220
234
|
|
|
@@ -234,6 +248,13 @@ class ChatEventPayload(BaseModel):
|
|
|
234
248
|
default_factory=list,
|
|
235
249
|
description="A list containing the tool names the user has chosen to be activated.",
|
|
236
250
|
)
|
|
251
|
+
skill_choices: list[ChatEventSkillChoice] = Field(
|
|
252
|
+
default_factory=list,
|
|
253
|
+
description=(
|
|
254
|
+
"A list containing selected skills with ``scope_id``, "
|
|
255
|
+
"``content_id``, and ``name``."
|
|
256
|
+
),
|
|
257
|
+
)
|
|
237
258
|
disabled_tools: list[str] = Field(
|
|
238
259
|
default_factory=list,
|
|
239
260
|
description="A list containing the tool names of tools that are disabled at the company level",
|
|
@@ -24,4 +24,7 @@ Planned contents (current home → new home):
|
|
|
24
24
|
(re-export: :mod:`unique_toolkit.experimental.content_tree`); a derived
|
|
25
25
|
tree/fuzzy-search view over the ``content`` resource; does not wrap an SDK
|
|
26
26
|
endpoint of its own
|
|
27
|
+
* ``internal_search`` ← :mod:`unique_toolkit.experimental.components.internal_search`
|
|
28
|
+
(UN-19039 / UN-20075); pure-retrieval KB and chat search services plus an
|
|
29
|
+
optional post-processing pipeline (:class:`InternalSearchPostProcessor`)
|
|
27
30
|
"""
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
from functools import cached_property
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Generic, TypeVar
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
from unique_toolkit._common.event_bus import TypedEventBus
|
|
13
|
+
from unique_toolkit.app.unique_settings import UniqueContext, UniqueSettings
|
|
14
|
+
|
|
15
|
+
RunResult = TypeVar("RunResult", bound=BaseModel)
|
|
16
|
+
Config = TypeVar("Config", bound=BaseModel)
|
|
17
|
+
Context = TypeVar("Context")
|
|
18
|
+
ServiceState = TypeVar("ServiceState")
|
|
19
|
+
ProgressMessage = TypeVar("ProgressMessage")
|
|
20
|
+
Deps = TypeVar("Deps")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class _RunMixin(ABC, Generic[RunResult]):
|
|
24
|
+
@abstractmethod
|
|
25
|
+
async def run(self) -> RunResult: ...
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _ConfigMixin(ABC, Generic[Config]):
|
|
29
|
+
"""Subclasses must set ``_config_model_cls`` so ``from_json`` can deserialize."""
|
|
30
|
+
|
|
31
|
+
_config_model_cls: type[Config]
|
|
32
|
+
|
|
33
|
+
@classmethod
|
|
34
|
+
@abstractmethod
|
|
35
|
+
def from_config(cls, config: Config) -> Self: ...
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
@abstractmethod
|
|
39
|
+
def config(self) -> Config: ...
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_json(cls, file_path: Path) -> Self:
|
|
43
|
+
parsed = cls._config_model_cls.model_validate_json(file_path.read_text())
|
|
44
|
+
return cls.from_config(parsed)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class _ContextMixin(ABC, Generic[Context]):
|
|
48
|
+
@property
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def context(self) -> Context: ...
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class _StateMixin(ABC, Generic[ServiceState]):
|
|
54
|
+
@property
|
|
55
|
+
@abstractmethod
|
|
56
|
+
def state(self) -> ServiceState: ...
|
|
57
|
+
|
|
58
|
+
@abstractmethod
|
|
59
|
+
def reset_state(self) -> None: ...
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _ProgressMixin(Generic[ProgressMessage]):
|
|
63
|
+
"""Concrete by default — no-op bus unless something subscribes."""
|
|
64
|
+
|
|
65
|
+
def __init__(self) -> None:
|
|
66
|
+
super().__init__()
|
|
67
|
+
self._progress_publisher: TypedEventBus[ProgressMessage] = TypedEventBus()
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def progress_publisher(self) -> TypedEventBus[ProgressMessage]:
|
|
71
|
+
return self._progress_publisher
|
|
72
|
+
|
|
73
|
+
async def post_progress_message(self, message: ProgressMessage) -> None:
|
|
74
|
+
await self.progress_publisher.publish_and_wait_async(message)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _DependenciesMixin(ABC, Generic[Deps]):
|
|
78
|
+
@property
|
|
79
|
+
@abstractmethod
|
|
80
|
+
def dependencies(self) -> Deps: ...
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class BaseService( # pyright: ignore[reportImplicitAbstractClass]
|
|
84
|
+
_RunMixin[RunResult],
|
|
85
|
+
_ConfigMixin[Config],
|
|
86
|
+
_ContextMixin[UniqueContext],
|
|
87
|
+
_StateMixin[ServiceState],
|
|
88
|
+
_ProgressMixin[ProgressMessage],
|
|
89
|
+
_DependenciesMixin[Deps],
|
|
90
|
+
Generic[RunResult, Config, ServiceState, ProgressMessage, Deps],
|
|
91
|
+
):
|
|
92
|
+
"""
|
|
93
|
+
Two-step init: from_config() at startup, bind_settings() per request.
|
|
94
|
+
|
|
95
|
+
service = MyService.from_config(config) # startup
|
|
96
|
+
service = service.bind_settings(settings) # per request
|
|
97
|
+
# or in one shot:
|
|
98
|
+
service = MyService.from_settings(settings, config=config)
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
_config: Config
|
|
102
|
+
_context: UniqueContext
|
|
103
|
+
_state: ServiceState
|
|
104
|
+
_dependencies: Deps
|
|
105
|
+
|
|
106
|
+
@abstractmethod
|
|
107
|
+
def bind_settings(self, settings: UniqueSettings) -> Self: ...
|
|
108
|
+
|
|
109
|
+
@classmethod
|
|
110
|
+
def from_settings(cls, settings: UniqueSettings, **kwargs) -> Self:
|
|
111
|
+
config = kwargs.pop("config")
|
|
112
|
+
return cls.from_config(config).bind_settings(settings)
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def config(self) -> Config:
|
|
116
|
+
return self._config # type: ignore[attr-defined]
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def context(self) -> UniqueContext:
|
|
120
|
+
return self._context # type: ignore[attr-defined]
|
|
121
|
+
|
|
122
|
+
@context.setter
|
|
123
|
+
def context(self, value: UniqueContext) -> None:
|
|
124
|
+
self._context = value
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def state(self) -> ServiceState:
|
|
128
|
+
return self._state # type: ignore[attr-defined]
|
|
129
|
+
|
|
130
|
+
@state.setter
|
|
131
|
+
def state(self, value: ServiceState) -> None:
|
|
132
|
+
self._state = value
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def dependencies(self) -> Deps:
|
|
136
|
+
return self._dependencies # type: ignore[attr-defined]
|
|
137
|
+
|
|
138
|
+
@dependencies.setter
|
|
139
|
+
def dependencies(self, value: Deps) -> None:
|
|
140
|
+
self._dependencies = value
|
|
141
|
+
|
|
142
|
+
@cached_property
|
|
143
|
+
def logger(self) -> logging.Logger:
|
|
144
|
+
return logging.getLogger(f"{type(self).__module__}.{type(self).__qualname__}")
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
__all__ = ["BaseService"]
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Internal search component — retrieval services and post-processing pipeline.
|
|
2
|
+
|
|
3
|
+
!!! warning "Experimental"
|
|
4
|
+
This subpackage lives under :mod:`unique_toolkit.experimental` and its
|
|
5
|
+
API may change between minor releases.
|
|
6
|
+
|
|
7
|
+
Provides framework-agnostic knowledge-base and chat search services, plus an
|
|
8
|
+
optional post-processing pipeline for reranking and token-budget windowing.
|
|
9
|
+
|
|
10
|
+
The search services are pure retrieval primitives: they return all matching
|
|
11
|
+
chunks up to the configured ``limit``. Post-retrieval steps (reranking, token
|
|
12
|
+
windowing, sorting) are handled separately by
|
|
13
|
+
:class:`InternalSearchPostProcessor`.
|
|
14
|
+
|
|
15
|
+
Typical usage::
|
|
16
|
+
|
|
17
|
+
from unique_toolkit.experimental.components.internal_search import (
|
|
18
|
+
KnowledgeBaseInternalSearchService,
|
|
19
|
+
KnowledgeBaseInternalSearchConfig,
|
|
20
|
+
InternalSearchFilterConfig,
|
|
21
|
+
InternalSearchPostProcessor,
|
|
22
|
+
PostProcessorConfig,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
search_config = KnowledgeBaseInternalSearchConfig(
|
|
26
|
+
filtering=InternalSearchFilterConfig(limit=200),
|
|
27
|
+
...,
|
|
28
|
+
)
|
|
29
|
+
post_config = PostProcessorConfig(max_tokens_for_sources=30_000, ...)
|
|
30
|
+
|
|
31
|
+
search_service = KnowledgeBaseInternalSearchService.from_config(search_config)
|
|
32
|
+
processor = InternalSearchPostProcessor.from_settings(settings, config=post_config)
|
|
33
|
+
|
|
34
|
+
search_service.bind_settings(settings)
|
|
35
|
+
search_service.state.search_queries = ["my query"]
|
|
36
|
+
result = await search_service.run()
|
|
37
|
+
|
|
38
|
+
chunks = await processor.process(
|
|
39
|
+
result,
|
|
40
|
+
query_text="my query",
|
|
41
|
+
model_info=model_info,
|
|
42
|
+
)
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
from unique_toolkit.experimental.components.internal_search.base import (
|
|
46
|
+
DEFAULT_LIMIT,
|
|
47
|
+
InternalSearchBaseService,
|
|
48
|
+
InternalSearchConfig,
|
|
49
|
+
InternalSearchProgressMessage,
|
|
50
|
+
InternalSearchResult,
|
|
51
|
+
InternalSearchStage,
|
|
52
|
+
InternalSearchState,
|
|
53
|
+
SearchStringResult,
|
|
54
|
+
TInternalSearchDeps,
|
|
55
|
+
)
|
|
56
|
+
from unique_toolkit.experimental.components.internal_search.chat import (
|
|
57
|
+
ChatInternalSearchConfig,
|
|
58
|
+
ChatInternalSearchDeps,
|
|
59
|
+
ChatInternalSearchService,
|
|
60
|
+
)
|
|
61
|
+
from unique_toolkit.experimental.components.internal_search.knowledge_base import (
|
|
62
|
+
UNSET,
|
|
63
|
+
KnowledgeBaseInternalSearchConfig,
|
|
64
|
+
KnowledgeBaseInternalSearchDeps,
|
|
65
|
+
KnowledgeBaseInternalSearchService,
|
|
66
|
+
KnowledgeBaseInternalSearchState,
|
|
67
|
+
)
|
|
68
|
+
from unique_toolkit.experimental.components.internal_search.post_processing import (
|
|
69
|
+
InternalSearchPostProcessor,
|
|
70
|
+
PostProcessorConfig,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
__all__ = [
|
|
74
|
+
# base
|
|
75
|
+
"DEFAULT_LIMIT",
|
|
76
|
+
"InternalSearchBaseService",
|
|
77
|
+
"InternalSearchConfig",
|
|
78
|
+
"InternalSearchProgressMessage",
|
|
79
|
+
"InternalSearchResult",
|
|
80
|
+
"InternalSearchStage",
|
|
81
|
+
"InternalSearchState",
|
|
82
|
+
"SearchStringResult",
|
|
83
|
+
"TInternalSearchDeps",
|
|
84
|
+
# chat
|
|
85
|
+
"ChatInternalSearchConfig",
|
|
86
|
+
"ChatInternalSearchDeps",
|
|
87
|
+
"ChatInternalSearchService",
|
|
88
|
+
# knowledge base
|
|
89
|
+
"KnowledgeBaseInternalSearchConfig",
|
|
90
|
+
"KnowledgeBaseInternalSearchDeps",
|
|
91
|
+
"KnowledgeBaseInternalSearchService",
|
|
92
|
+
"KnowledgeBaseInternalSearchState",
|
|
93
|
+
"UNSET",
|
|
94
|
+
# post-processing
|
|
95
|
+
"InternalSearchPostProcessor",
|
|
96
|
+
"PostProcessorConfig",
|
|
97
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from .config import (
|
|
2
|
+
DEFAULT_LIMIT,
|
|
3
|
+
InternalSearchConfig,
|
|
4
|
+
InternalSearchFilterConfig,
|
|
5
|
+
InternalSearchSearchConfig,
|
|
6
|
+
)
|
|
7
|
+
from .schemas import (
|
|
8
|
+
InternalSearchProgressMessage,
|
|
9
|
+
InternalSearchResult,
|
|
10
|
+
InternalSearchStage,
|
|
11
|
+
InternalSearchState,
|
|
12
|
+
SearchStringResult,
|
|
13
|
+
TInternalSearchDeps,
|
|
14
|
+
)
|
|
15
|
+
from .service import InternalSearchBaseService
|
|
16
|
+
from .utils import (
|
|
17
|
+
append_metadata_in_chunks,
|
|
18
|
+
clean_search_string,
|
|
19
|
+
interleave_search_results_round_robin,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"append_metadata_in_chunks",
|
|
24
|
+
"clean_search_string",
|
|
25
|
+
"DEFAULT_LIMIT",
|
|
26
|
+
"InternalSearchBaseService",
|
|
27
|
+
"InternalSearchConfig",
|
|
28
|
+
"InternalSearchFilterConfig",
|
|
29
|
+
"InternalSearchProgressMessage",
|
|
30
|
+
"InternalSearchResult",
|
|
31
|
+
"InternalSearchSearchConfig",
|
|
32
|
+
"InternalSearchStage",
|
|
33
|
+
"InternalSearchState",
|
|
34
|
+
"interleave_search_results_round_robin",
|
|
35
|
+
"SearchStringResult",
|
|
36
|
+
"TInternalSearchDeps",
|
|
37
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from pydantic import BaseModel, Field
|
|
2
|
+
|
|
3
|
+
from unique_toolkit._common.config_checker import register_config
|
|
4
|
+
from unique_toolkit.agentic.tools.config import get_configuration_dict
|
|
5
|
+
from unique_toolkit.content.schemas import ContentSearchType
|
|
6
|
+
|
|
7
|
+
# 200 matches the tool's sort-enabled default — conservative by design.
|
|
8
|
+
# Callers who need higher recall (e.g. multi-query with post-processing) should set
|
|
9
|
+
# limit explicitly in config. The service does not cap by token budget; that is the
|
|
10
|
+
# post-processor's job via pick_content_chunks_for_token_window.
|
|
11
|
+
DEFAULT_LIMIT = 200
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class InternalSearchSearchConfig(BaseModel):
|
|
15
|
+
model_config = get_configuration_dict()
|
|
16
|
+
|
|
17
|
+
search_type: ContentSearchType = Field(
|
|
18
|
+
default=ContentSearchType.COMBINED,
|
|
19
|
+
description="The type of search to perform. Two possible values: `COMBINED` or `VECTOR`.",
|
|
20
|
+
)
|
|
21
|
+
search_language: str = Field(
|
|
22
|
+
default="english",
|
|
23
|
+
description="The language to use for the search.",
|
|
24
|
+
)
|
|
25
|
+
max_search_strings: int = Field(
|
|
26
|
+
default=10,
|
|
27
|
+
ge=1,
|
|
28
|
+
description="The maximum number of search strings to perform in a single tool call.",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class InternalSearchFilterConfig(BaseModel):
|
|
33
|
+
model_config = get_configuration_dict()
|
|
34
|
+
|
|
35
|
+
score_threshold: float = Field(
|
|
36
|
+
default=0.0,
|
|
37
|
+
ge=0.0,
|
|
38
|
+
le=1.0,
|
|
39
|
+
description="Score threshold to filter chunks on relevancy.",
|
|
40
|
+
)
|
|
41
|
+
limit: int = Field(
|
|
42
|
+
default=DEFAULT_LIMIT,
|
|
43
|
+
description=(
|
|
44
|
+
"Maximum number of chunks to return per search query. "
|
|
45
|
+
"Default is 200 — the conservative baseline. Increase for higher recall "
|
|
46
|
+
"when a post-processor with token windowing follows."
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@register_config()
|
|
52
|
+
class InternalSearchConfig(BaseModel):
|
|
53
|
+
"""Retrieval-only config. Token budgeting, reranking and sorting live in PostProcessorConfig."""
|
|
54
|
+
|
|
55
|
+
model_config = get_configuration_dict()
|
|
56
|
+
|
|
57
|
+
search: InternalSearchSearchConfig = Field(
|
|
58
|
+
default_factory=InternalSearchSearchConfig,
|
|
59
|
+
description="Search-related configuration such as query mode, language, and multi-query settings.",
|
|
60
|
+
)
|
|
61
|
+
filtering: InternalSearchFilterConfig = Field(
|
|
62
|
+
default_factory=InternalSearchFilterConfig,
|
|
63
|
+
description="Result filtering configuration such as retrieval limit and score threshold.",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
__all__ = [
|
|
68
|
+
"InternalSearchConfig",
|
|
69
|
+
"DEFAULT_LIMIT",
|
|
70
|
+
"InternalSearchFilterConfig",
|
|
71
|
+
"InternalSearchSearchConfig",
|
|
72
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import StrEnum
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
from typing_extensions import TypeVar
|
|
9
|
+
|
|
10
|
+
from unique_toolkit.chat.schemas import MessageLogStatus
|
|
11
|
+
from unique_toolkit.content.schemas import ContentChunk
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SearchStringResult(BaseModel):
|
|
15
|
+
query: str
|
|
16
|
+
chunks: list[ContentChunk]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class InternalSearchStage(StrEnum):
|
|
20
|
+
RETRIEVING = "retrieving"
|
|
21
|
+
POSTPROCESSING = "postprocessing"
|
|
22
|
+
COMPLETED = "completed"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class InternalSearchProgressMessage(BaseModel):
|
|
26
|
+
stage: InternalSearchStage
|
|
27
|
+
status: MessageLogStatus
|
|
28
|
+
search_queries: list[str]
|
|
29
|
+
chunks: list[ContentChunk] | None = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
TInternalSearchDeps = TypeVar("TInternalSearchDeps")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class InternalSearchState:
|
|
37
|
+
search_queries: list[str]
|
|
38
|
+
content_ids: list[str] | None = None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class InternalSearchResult(BaseModel):
|
|
42
|
+
chunks: list[ContentChunk]
|
|
43
|
+
"""Interleaved flat chunk list — for callers that skip the post-processor."""
|
|
44
|
+
debug_info: dict[str, Any]
|
|
45
|
+
search_string_results: list[SearchStringResult] = Field(default_factory=list)
|
|
46
|
+
"""Per-query results before interleaving — for the post-processor's per-query rerank."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
"InternalSearchProgressMessage",
|
|
51
|
+
"InternalSearchResult",
|
|
52
|
+
"InternalSearchStage",
|
|
53
|
+
"InternalSearchState",
|
|
54
|
+
"SearchStringResult",
|
|
55
|
+
"TInternalSearchDeps",
|
|
56
|
+
]
|