gllm-pipeline-binary 0.4.21__cp312-cp312-macosx_13_0_arm64.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.
- gllm_pipeline/__init__.pyi +0 -0
- gllm_pipeline/alias.pyi +7 -0
- gllm_pipeline/exclusions/__init__.pyi +4 -0
- gllm_pipeline/exclusions/exclusion_manager.pyi +74 -0
- gllm_pipeline/exclusions/exclusion_set.pyi +46 -0
- gllm_pipeline/pipeline/__init__.pyi +4 -0
- gllm_pipeline/pipeline/composer/__init__.pyi +8 -0
- gllm_pipeline/pipeline/composer/composer.pyi +350 -0
- gllm_pipeline/pipeline/composer/guard_composer.pyi +58 -0
- gllm_pipeline/pipeline/composer/if_else_composer.pyi +57 -0
- gllm_pipeline/pipeline/composer/parallel_composer.pyi +47 -0
- gllm_pipeline/pipeline/composer/switch_composer.pyi +57 -0
- gllm_pipeline/pipeline/composer/toggle_composer.pyi +48 -0
- gllm_pipeline/pipeline/pipeline.pyi +280 -0
- gllm_pipeline/pipeline/states.pyi +139 -0
- gllm_pipeline/router/__init__.pyi +6 -0
- gllm_pipeline/router/aurelio_semantic_router/__init__.pyi +3 -0
- gllm_pipeline/router/aurelio_semantic_router/aurelio_semantic_router.pyi +86 -0
- gllm_pipeline/router/aurelio_semantic_router/bytes_compat_route.pyi +40 -0
- gllm_pipeline/router/aurelio_semantic_router/encoders/__init__.pyi +5 -0
- gllm_pipeline/router/aurelio_semantic_router/encoders/em_invoker_encoder.pyi +46 -0
- gllm_pipeline/router/aurelio_semantic_router/encoders/langchain_encoder.pyi +50 -0
- gllm_pipeline/router/aurelio_semantic_router/encoders/tei_encoder.pyi +49 -0
- gllm_pipeline/router/aurelio_semantic_router/index/__init__.pyi +4 -0
- gllm_pipeline/router/aurelio_semantic_router/index/aurelio_index.pyi +65 -0
- gllm_pipeline/router/aurelio_semantic_router/index/azure_ai_search_aurelio_index.pyi +71 -0
- gllm_pipeline/router/aurelio_semantic_router/index/vector_store_adapter_index.pyi +119 -0
- gllm_pipeline/router/lm_based_router.pyi +60 -0
- gllm_pipeline/router/preset/__init__.pyi +0 -0
- gllm_pipeline/router/preset/aurelio/__init__.pyi +0 -0
- gllm_pipeline/router/preset/aurelio/router_image_domain_specific.pyi +21 -0
- gllm_pipeline/router/preset/lm_based/__init__.pyi +0 -0
- gllm_pipeline/router/preset/lm_based/router_image_domain_specific.pyi +14 -0
- gllm_pipeline/router/preset/preset_loader.pyi +24 -0
- gllm_pipeline/router/router.pyi +46 -0
- gllm_pipeline/router/rule_based_router.pyi +80 -0
- gllm_pipeline/router/similarity_based_router.pyi +72 -0
- gllm_pipeline/router/utils.pyi +26 -0
- gllm_pipeline/steps/__init__.pyi +17 -0
- gllm_pipeline/steps/_func.pyi +958 -0
- gllm_pipeline/steps/branching_step.pyi +24 -0
- gllm_pipeline/steps/component_step.pyi +82 -0
- gllm_pipeline/steps/composite_step.pyi +65 -0
- gllm_pipeline/steps/conditional_step.pyi +161 -0
- gllm_pipeline/steps/guard_step.pyi +71 -0
- gllm_pipeline/steps/log_step.pyi +53 -0
- gllm_pipeline/steps/map_reduce_step.pyi +92 -0
- gllm_pipeline/steps/no_op_step.pyi +40 -0
- gllm_pipeline/steps/parallel_step.pyi +128 -0
- gllm_pipeline/steps/pipeline_step.pyi +231 -0
- gllm_pipeline/steps/state_operator_step.pyi +75 -0
- gllm_pipeline/steps/step_error_handler/__init__.pyi +6 -0
- gllm_pipeline/steps/step_error_handler/empty_step_error_handler.pyi +20 -0
- gllm_pipeline/steps/step_error_handler/fallback_step_error_handler.pyi +24 -0
- gllm_pipeline/steps/step_error_handler/keep_step_error_handler.pyi +9 -0
- gllm_pipeline/steps/step_error_handler/raise_step_error_handler.pyi +9 -0
- gllm_pipeline/steps/step_error_handler/step_error_handler.pyi +46 -0
- gllm_pipeline/steps/subgraph_step.pyi +90 -0
- gllm_pipeline/steps/terminator_step.pyi +57 -0
- gllm_pipeline/types.pyi +10 -0
- gllm_pipeline/utils/__init__.pyi +9 -0
- gllm_pipeline/utils/async_utils.pyi +21 -0
- gllm_pipeline/utils/copy.pyi +11 -0
- gllm_pipeline/utils/error_handling.pyi +61 -0
- gllm_pipeline/utils/graph.pyi +16 -0
- gllm_pipeline/utils/has_inputs_mixin.pyi +50 -0
- gllm_pipeline/utils/input_map.pyi +12 -0
- gllm_pipeline/utils/mermaid.pyi +29 -0
- gllm_pipeline/utils/retry_converter.pyi +25 -0
- gllm_pipeline/utils/step_execution.pyi +19 -0
- gllm_pipeline.build/.gitignore +1 -0
- gllm_pipeline.cpython-312-darwin.so +0 -0
- gllm_pipeline.pyi +86 -0
- gllm_pipeline_binary-0.4.21.dist-info/METADATA +105 -0
- gllm_pipeline_binary-0.4.21.dist-info/RECORD +77 -0
- gllm_pipeline_binary-0.4.21.dist-info/WHEEL +5 -0
- gllm_pipeline_binary-0.4.21.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_pipeline.router.aurelio_semantic_router.bytes_compat_route import BytesCompatRoute as BytesCompatRoute
|
|
3
|
+
from gllm_pipeline.router.aurelio_semantic_router.index.aurelio_index import BaseAurelioIndex as BaseAurelioIndex
|
|
4
|
+
from gllm_pipeline.router.preset.preset_loader import get_preset as get_preset
|
|
5
|
+
from gllm_pipeline.router.router import BaseRouter as BaseRouter
|
|
6
|
+
from semantic_router import Route
|
|
7
|
+
from semantic_router.encoders.base import DenseEncoder as DenseEncoder
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
manager: Incomplete
|
|
11
|
+
logger: Incomplete
|
|
12
|
+
semantic_router_logger: Incomplete
|
|
13
|
+
|
|
14
|
+
class AurelioSemanticRouter(BaseRouter):
|
|
15
|
+
"""A router that utilizes the Aurelio Labs library to route the input source to the appropriate path.
|
|
16
|
+
|
|
17
|
+
The `AurelioSemanticRouter` utilizes the Aurelio Labs library to route a given input source to an appropriate path
|
|
18
|
+
based on the similarity with existing samples. If the determined route is not valid, it defaults to a predefined
|
|
19
|
+
route.
|
|
20
|
+
|
|
21
|
+
Attributes:
|
|
22
|
+
route_layer (RouteLayer): The Aurelio Labs route layer that handles the routing logic.
|
|
23
|
+
default_route (str): The default route to be used if the input source is not similar to any of the routes.
|
|
24
|
+
valid_routes (set[str]): A set of valid routes for the router.
|
|
25
|
+
|
|
26
|
+
Notes:
|
|
27
|
+
For more information about the Aurelio Labs library, please refer to
|
|
28
|
+
https://github.com/aurelio-labs/semantic-router
|
|
29
|
+
"""
|
|
30
|
+
route_layer: Incomplete
|
|
31
|
+
def __init__(self, default_route: str, valid_routes: set[str], encoder: DenseEncoder, routes: list[Route] | dict[str, list[str | bytes]] | None = None, index: BaseAurelioIndex | None = None, auto_sync: str = ..., **kwargs: Any) -> None:
|
|
32
|
+
'''Initializes a new instance of the AurelioSemanticRouter class.
|
|
33
|
+
|
|
34
|
+
To define the routes, at least one of the `routes` or `index` parameters must be provided.
|
|
35
|
+
When both parameters are provided, the `routes` parameter is ignored.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
default_route (str): The default route to be used if the input source is not similar to any of the routes.
|
|
39
|
+
valid_routes (set[str]): A set of valid routes for the router.
|
|
40
|
+
encoder (DenseEncoder): An Aurelio Labs dense encoder to encode the input source and the samples.
|
|
41
|
+
The encoded vectors are used to calculate the similarity between the input source and the samples.
|
|
42
|
+
routes (list[Route] | dict[str, list[str | bytes]] | None, optional): A list of Aurelio Labs Routes
|
|
43
|
+
or a dictionary mapping route names to the list of samples. Ignored if `index` is provided.
|
|
44
|
+
Defaults to None.
|
|
45
|
+
index (BaseAurelioIndex | None, optional): A router index to retrieve the routes.
|
|
46
|
+
If provided, it is prioritized over `routes`. Defaults to None.
|
|
47
|
+
auto_sync (str, optional): The auto-sync mode for the router. Defaults to "local".
|
|
48
|
+
kwargs (Any): Additional keyword arguments to be passed to the Aurelio Labs Route Layer.
|
|
49
|
+
|
|
50
|
+
Raises:
|
|
51
|
+
ValueError:
|
|
52
|
+
1. If neither `routes` nor `index` is provided.
|
|
53
|
+
2. If the parsed routes contains routes that are not in the set of valid routes.
|
|
54
|
+
3. If the provided default route is not in the set of valid routes.
|
|
55
|
+
'''
|
|
56
|
+
@classmethod
|
|
57
|
+
def from_preset(cls, modality: str, preset_name: str, preset_kwargs: dict | None = None, **kwargs) -> AurelioSemanticRouter:
|
|
58
|
+
"""Initialize the Aurelio semantic based router component using preset model configurations.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
modality (str): type of modality input.
|
|
62
|
+
preset_name (str): Name of the preset to use.
|
|
63
|
+
preset_kwargs (dict | None): placeholder for preset additional arguments.
|
|
64
|
+
**kwargs (Any): Additional arguments to pass for this class.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
AurelioSemanticRouter: Initialized aurelio semantic based router component using preset model.
|
|
68
|
+
"""
|
|
69
|
+
@classmethod
|
|
70
|
+
def from_file(cls, default_route: str, valid_routes: set[str], file_path: str) -> AurelioSemanticRouter:
|
|
71
|
+
'''Creates a new instance of the AurelioSemanticRouter class from a file.
|
|
72
|
+
|
|
73
|
+
This method creates a new instance of the AurelioSemanticRouter class from a file. It supports JSON and YAML
|
|
74
|
+
file extensions.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
default_route (str): The default route to be used if the input source is not similar to any of the routes.
|
|
78
|
+
valid_routes (set[str]): A set of valid routes for the router.
|
|
79
|
+
file_path (str): The path to the file containing the routes. The file extension must be either JSON or YAML.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
AurelioSemanticRouter: A new instance of the AurelioSemanticRouter class.
|
|
83
|
+
|
|
84
|
+
Raises:
|
|
85
|
+
ValueError: If the file extension is not ".json" or ".yaml".
|
|
86
|
+
'''
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from gllm_pipeline.router.utils import encode_bytes as encode_bytes
|
|
2
|
+
from semantic_router.route import Route
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
class BytesCompatRoute(Route):
|
|
6
|
+
"""A subclass of `Route` that provides JSON-serializable support for `bytes` in utterances.
|
|
7
|
+
|
|
8
|
+
The primary motivation for this override is to prevent errors when `bytes`-based utterances are hashed
|
|
9
|
+
during `semantic_router.routers.base.BaseRouter._write_hash`.
|
|
10
|
+
|
|
11
|
+
`ByteCompatRoute` extends the standard `Route` class from the `semantic-router` library by ensuring that
|
|
12
|
+
all `bytes` values in the `utterances` field are safely encoded using base64 when converting to a dictionary.
|
|
13
|
+
This is essential for serializing routes containing binary data to formats like JSON, which does not
|
|
14
|
+
support raw bytes.
|
|
15
|
+
|
|
16
|
+
Use this class as a drop-in replacement when dealing with routes that include `bytes`-based utterances but still
|
|
17
|
+
need to be serialized (e.g., for configuration exports or caching).
|
|
18
|
+
"""
|
|
19
|
+
def to_dict(self) -> dict[str, Any]:
|
|
20
|
+
'''Convert the route instance to a dictionary with all `bytes` in the `utterances`.
|
|
21
|
+
|
|
22
|
+
This overrides the default `Route.to_dict()` to handle nested `bytes` inside lists or dictionaries.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
dict[str, Any]: A dictionary representation of the route, with all `bytes` values in `utterances`
|
|
26
|
+
converted to base64-encoded UTF-8 strings.
|
|
27
|
+
|
|
28
|
+
Example:
|
|
29
|
+
>>> route = ByteCompatRoute(name="example", utterances=[b"binary1", "text"])
|
|
30
|
+
>>> route.to_dict()
|
|
31
|
+
{
|
|
32
|
+
"name": "example",
|
|
33
|
+
"utterances": ["YmluYXJ5MQ==", "text"],
|
|
34
|
+
...
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Notes:
|
|
38
|
+
- Only the `utterances` field is altered for byte safety; other fields remain untouched.
|
|
39
|
+
- If the utterances contain nested lists or dicts with bytes, they will be recursively encoded.
|
|
40
|
+
'''
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
from gllm_pipeline.router.aurelio_semantic_router.encoders.em_invoker_encoder import EMInvokerEncoder as EMInvokerEncoder
|
|
2
|
+
from gllm_pipeline.router.aurelio_semantic_router.encoders.langchain_encoder import LangchainEmbeddingsEncoder as LangchainEmbeddingsEncoder
|
|
3
|
+
from gllm_pipeline.router.aurelio_semantic_router.encoders.tei_encoder import TEIEncoder as TEIEncoder
|
|
4
|
+
|
|
5
|
+
__all__ = ['TEIEncoder', 'LangchainEmbeddingsEncoder', 'EMInvokerEncoder']
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from gllm_inference.em_invoker.em_invoker import BaseEMInvoker as BaseEMInvoker
|
|
2
|
+
from semantic_router.encoders.base import DenseEncoder
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
class EMInvokerEncoder(DenseEncoder):
|
|
6
|
+
"""The gllm-inference EM Invoker-compatible Encoder.
|
|
7
|
+
|
|
8
|
+
This encoder is for use with gllm-inference's EM Invokers.
|
|
9
|
+
Includes handling of synchronous cases, since gllm-inference's EM Invokers are asynchronous.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
name (str): The name of the encoder.
|
|
13
|
+
score_threshold (float): The score threshold for the encoder.
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, em_invoker: BaseEMInvoker, name: str = 'em-invoker-encoder', score_threshold: float = 0.0) -> None:
|
|
16
|
+
'''Initialize the EM Invoker Encoder.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
em_invoker (BaseEMInvoker): The EM Invoker to use.
|
|
20
|
+
name (str, optional): The name of the encoder. Defaults to "em-invoker-encoder".
|
|
21
|
+
score_threshold (float, optional): The score threshold for the encoder. Defaults to 0.0.
|
|
22
|
+
'''
|
|
23
|
+
def __call__(self, docs: list[Any]) -> list[list[float]]:
|
|
24
|
+
"""Call the EM Invoker.
|
|
25
|
+
|
|
26
|
+
Handles both async and sync contexts by checking for existing event loop.
|
|
27
|
+
If an event loop is not found, we run the invoke method in the current thread.
|
|
28
|
+
If an event loop is found, we run the invoke method in a thread pool.
|
|
29
|
+
In the case of a RuntimeError, we run the invoke method in the current thread.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
docs (list[Any]): List of documents to be embedded.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
list[list[float]]: List of embeddings for each document.
|
|
36
|
+
"""
|
|
37
|
+
async def acall(self, docs: list[Any], **kwargs: Any) -> list[list[float]]:
|
|
38
|
+
"""Call the EM Invoker, which is already an async function.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
docs (list[Any]): List of documents to be embedded.
|
|
42
|
+
**kwargs (Any): Additional keyword arguments. Not used, but required by the DenseEncoder interface.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
list[list[float]]: List of embeddings for each document.
|
|
46
|
+
"""
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from langchain_core.embeddings import Embeddings as Embeddings
|
|
2
|
+
from semantic_router.encoders.base import DenseEncoder
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
class LangchainEmbeddingsEncoder(DenseEncoder):
|
|
6
|
+
"""A wrapper encoder for LangChain-compatible embedding models.
|
|
7
|
+
|
|
8
|
+
This encoder adapts any LangChain-compatible Embeddings instance to the
|
|
9
|
+
Semantic Router interface by wrapping its `embed_documents` and `aembed_documents` methods.
|
|
10
|
+
|
|
11
|
+
It supports both synchronous and asynchronous embedding calls and is useful
|
|
12
|
+
when integrating LangChain embeddings with a semantic router pipeline.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
name (str): The name of the encoder.
|
|
16
|
+
score_threshold (float): Threshold for similarity scoring.
|
|
17
|
+
"""
|
|
18
|
+
def __init__(self, embeddings: Embeddings, name: str = 'langchain-embeddings-encoder', score_threshold: float = 0.5) -> None:
|
|
19
|
+
'''Initialize the LangchainEmbeddingsEncoder.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
embeddings (Embeddings): A LangChain-compatible Embeddings instance.
|
|
23
|
+
name (str, optional): Identifier for the encoder. Defaults to "langchain-embeddings-encoder".
|
|
24
|
+
score_threshold (float, optional): Minimum similarity score to consider matches. Defaults to 0.5.
|
|
25
|
+
'''
|
|
26
|
+
def __call__(self, docs: list[Any]) -> list[list[float]]:
|
|
27
|
+
"""Synchronously embed a list of documents.
|
|
28
|
+
|
|
29
|
+
Automatically handles execution context:
|
|
30
|
+
1. If called inside an active asyncio loop, it runs the embedding call in a background thread.
|
|
31
|
+
2. If no active loop is present, it runs the embedding call normally.
|
|
32
|
+
3. If an event loop cannot be retrieved, falls back to `asyncio.run`.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
docs (list[Any]): The documents to embed.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list[list[float]]: A list of vector embeddings for each document.
|
|
39
|
+
"""
|
|
40
|
+
async def acall(self, docs: list[Any]) -> list[list[float]]:
|
|
41
|
+
"""Asynchronously embed a list of documents.
|
|
42
|
+
|
|
43
|
+
Calls the `aembed_documents` method of the underlying LangChain embeddings.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
docs (list[Any]): The documents to embed.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
list[list[float]]: A list of vector embeddings for each document.
|
|
50
|
+
"""
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from semantic_router.encoders.huggingface import HFEndpointEncoder
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
class TEIEncoder(HFEndpointEncoder):
|
|
5
|
+
"""TEI Endpoint Encoder.
|
|
6
|
+
|
|
7
|
+
This encoder is used to encode documents into embeddings using the TEI endpoint.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
name (str): The name of the encoder.
|
|
11
|
+
huggingface_url (str): The base URL of the TEI endpoint, which is a HuggingFace endpoint.
|
|
12
|
+
huggingface_api_key (str): The API key for the TEI endpoint.
|
|
13
|
+
score_threshold (float): The score threshold for the encoder.
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, base_url: str, name: str = 'tei-encoder', api_key: str = '<empty>', score_threshold: float = 0.0) -> None:
|
|
16
|
+
'''Initialize the TEI Endpoint Encoder.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
base_url (str): The base URL of the TEI endpoint, which is a HuggingFace endpoint.
|
|
20
|
+
name (str, optional): The name of the encoder. Defaults to "tei-encoder".
|
|
21
|
+
api_key (str, optional): The API key for the TEI endpoint. Defaults to "<empty>".
|
|
22
|
+
Only do this if the endpoint does not require an API key.
|
|
23
|
+
score_threshold (float, optional): The score threshold for the encoder. Defaults to 0.0.
|
|
24
|
+
'''
|
|
25
|
+
async def acall(self, docs: list[str], **kwargs: Any) -> list[list[float]]:
|
|
26
|
+
"""Asynchronously encodes a list of documents into embeddings.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
docs (list[str]): A list of documents to encode.
|
|
30
|
+
**kwargs (Any): Additional keyword arguments. Not used, but required by the BaseEncoder interface.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
list[list[float]]: A list of embeddings for the given documents.
|
|
34
|
+
|
|
35
|
+
Raises:
|
|
36
|
+
ValueError: If no embeddings are returned for a document.
|
|
37
|
+
"""
|
|
38
|
+
def __call__(self, docs: list[str]) -> list[list[float]]:
|
|
39
|
+
"""Encodes a list of documents into embeddings using the Hugging Face API.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
docs (list[str]): A list of documents to encode.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
list[list[float]]: A list of embeddings for the given documents.
|
|
46
|
+
|
|
47
|
+
Raises:
|
|
48
|
+
ValueError: If no embeddings are returned for a document.
|
|
49
|
+
"""
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
from gllm_pipeline.router.aurelio_semantic_router.index.azure_ai_search_aurelio_index import AzureAISearchAurelioIndex as AzureAISearchAurelioIndex
|
|
2
|
+
from gllm_pipeline.router.aurelio_semantic_router.index.vector_store_adapter_index import VectorStoreAdapterIndex as VectorStoreAdapterIndex
|
|
3
|
+
|
|
4
|
+
__all__ = ['AzureAISearchAurelioIndex', 'VectorStoreAdapterIndex']
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
import numpy as np
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from semantic_router.index import BaseIndex
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
class BaseAurelioIndex(BaseIndex, ABC, metaclass=abc.ABCMeta):
|
|
8
|
+
"""An abstract base class for the router index to be loaded by the AurelioSemanticRouter.
|
|
9
|
+
|
|
10
|
+
The `BaseAurelioIndex` extends the `BaseIndex` class from the semantic router library.
|
|
11
|
+
It can be used as a base class for a router index implementation that can be used with the assumption that
|
|
12
|
+
the routes are already in the index. Therefore, the index will solely be used for retrieval purposes.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
sync (bool): Flag to indicate that the index should be synchronized with the routes mapping.
|
|
16
|
+
In this implementation, it's set to `True` by default to make sure that the RouteLayer object initialized
|
|
17
|
+
with this index will perform the syncing process instead of blindly adding the routes to the index.
|
|
18
|
+
|
|
19
|
+
Notes:
|
|
20
|
+
To use this class, you need to implement the `get_routes` and `query` methods:
|
|
21
|
+
1. `get_routes`: Retrieve a list of routes and their associated utterances from the index.
|
|
22
|
+
2. `query`: Search the index for the query_vector and return top_k results.
|
|
23
|
+
"""
|
|
24
|
+
sync: bool
|
|
25
|
+
@abstractmethod
|
|
26
|
+
def get_routes(self) -> dict[str, list[str]]:
|
|
27
|
+
"""Retrieves a dictionary of routes and their associated utterances from the index.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
dict[str, list[str]]: A dictionary where the key is the route name and the value is a list of utterances.
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
NotImplementedError: If the method is not implemented by the subclass.
|
|
34
|
+
"""
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def query(self, vector: np.ndarray, top_k: int = 5, route_filter: list[str] | None = None) -> tuple[np.ndarray, list[str]]:
|
|
37
|
+
"""Search the index with the input query vector and return top_k results.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
vector (np.ndarray): The input vector to query.
|
|
41
|
+
top_k (int, optional): The number of results to return. Defaults to 5.
|
|
42
|
+
route_filter (list[str] | None, optional): The list of routes to filter the results by. Defaults to None.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
tuple[np.ndarray, list[str]]: A tuple containing the query vector and the list of results.
|
|
46
|
+
|
|
47
|
+
Raises:
|
|
48
|
+
NotImplementedError: If the method is not implemented by the subclass.
|
|
49
|
+
"""
|
|
50
|
+
def add(self, embeddings: list[list[float]], routes: list[str], utterances: list[Any], function_schemas: list[dict[str, Any]] | None = None, metadata_list: list[dict[str, Any]] | None = None) -> None:
|
|
51
|
+
"""Add embeddings to the index.
|
|
52
|
+
|
|
53
|
+
This method doesn't need to add any routes to the index since it's assumed that the routes are already in
|
|
54
|
+
the index. Therefore, this method is left empty intentionally.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
embeddings (list[list[float]]): A list of embedded vectors for the documents.
|
|
58
|
+
routes (list[str]): A list of route names for the documents.
|
|
59
|
+
utterances (list[Any]): A list of utterances for the documents.
|
|
60
|
+
function_schemas (list[dict[str, Any]]): List of function schemas to add to the index.
|
|
61
|
+
metadata_list (list[dict[str, Any]]): List of metadata to add to the index.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
None
|
|
65
|
+
"""
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from azure.search.documents import SearchClient
|
|
3
|
+
from gllm_pipeline.router.aurelio_semantic_router.index.aurelio_index import BaseAurelioIndex as BaseAurelioIndex
|
|
4
|
+
|
|
5
|
+
class AzureAISearchAurelioIndexConstants:
|
|
6
|
+
"""Defines Azure AI Search Aurelio index related constants."""
|
|
7
|
+
CONTENT_FIELD: str
|
|
8
|
+
SCORE_FIELD: str
|
|
9
|
+
VECTOR_FIELD: str
|
|
10
|
+
VECTOR_SEARCH_TYPE: str
|
|
11
|
+
|
|
12
|
+
class AzureAISearchAurelioIndexDefaults:
|
|
13
|
+
"""Defines default values for the AzureAISearchAurelioIndex class."""
|
|
14
|
+
ROUTE_FIELD_NAME: str
|
|
15
|
+
MAX_TOP_K: int
|
|
16
|
+
MAX_SEARCH_ITERATIONS: int
|
|
17
|
+
|
|
18
|
+
class AzureAISearchAurelioIndex(BaseAurelioIndex):
|
|
19
|
+
"""A router index implementation for Azure AI Search to be used by the AurelioSemanticRouter.
|
|
20
|
+
|
|
21
|
+
The `AzureAISearchAurelioIndex` class extends the `BaseAurelioIndex` class. It allows an Azure AI Search index to be
|
|
22
|
+
used as a router index by the `AurelioSemanticRouter` class. Just like the `BaseAurelioIndex` class, this class also
|
|
23
|
+
assumes that the routes are already in the index. Therefore, the index will solely be used for retrieval purposes.
|
|
24
|
+
|
|
25
|
+
Attributes:
|
|
26
|
+
client (SearchClient | None): The client to interact with the Azure AI Search index.
|
|
27
|
+
route_field_name (str): The name of the field that contains the route name.
|
|
28
|
+
max_top_k (int): The maximum number of results to return.
|
|
29
|
+
max_search_iterations (int): The maximum number of search iterations to perform.
|
|
30
|
+
sync (bool): Flag to indicate that the index should be synchronized with the routes mapping.
|
|
31
|
+
In this implementation, it's set to `True` by default to make sure that the RouteLayer object initialized
|
|
32
|
+
with this index will perform the syncing process instead of blindly adding the routes to the index.
|
|
33
|
+
"""
|
|
34
|
+
client: SearchClient | None
|
|
35
|
+
route_field_name: str
|
|
36
|
+
max_top_k: int
|
|
37
|
+
max_search_iterations: int
|
|
38
|
+
def __init__(self, endpoint: str, index_name: str, api_key: str, route_field_name: str = ..., max_top_k: int = ..., max_search_iterations: int = ...) -> None:
|
|
39
|
+
"""Initialize the AzureAISearchIndex with the given service endpoint, index name, and API key.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
endpoint (str): The endpoint of the Azure AI Search service.
|
|
43
|
+
index_name (str): The name of the Azure AI Search index.
|
|
44
|
+
api_key (str): The API key for the Azure AI Search service.
|
|
45
|
+
route_field_name (str, optional): The name of the field that contains the route name.
|
|
46
|
+
Defaults to AzureAISearchAurelioIndexDefaults.ROUTE_FIELD_NAME.
|
|
47
|
+
max_top_k (int, optional): The maximum number of results to return.
|
|
48
|
+
Defaults to AzureAISearchAurelioIndexDefaults.MAX_TOP_K.
|
|
49
|
+
max_search_iterations (int, optional): The maximum number of search iterations to perform.
|
|
50
|
+
Defaults to AzureAISearchAurelioIndexDefaults.MAX_SEARCH_ITERATIONS.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
None
|
|
54
|
+
"""
|
|
55
|
+
def get_routes(self) -> dict[str, list[str]]:
|
|
56
|
+
"""Retrieves a dictionary of routes and their associated utterances from the Azure AI Search index.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
dict[str, list[str]]: A dictionary where the key is the route name and the value is a list of utterances.
|
|
60
|
+
"""
|
|
61
|
+
def query(self, vector: np.ndarray, top_k: int = 5, route_filter: list[str] | None = None) -> tuple[np.ndarray, list[str]]:
|
|
62
|
+
"""Search the Azure AI Search index with the input query vector and return top_k results.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
vector (np.ndarray): The input vector to query.
|
|
66
|
+
top_k (int, optional): The number of results to return. Defaults to 5.
|
|
67
|
+
route_filter (list[str] | None, optional): The list of routes to filter the results by. Defaults to None.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
tuple[np.ndarray, list[str]]: A tuple containing the query vector and the list of results.
|
|
71
|
+
"""
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from gllm_datastore.vector_data_store.vector_data_store import BaseVectorDataStore as BaseVectorDataStore
|
|
3
|
+
from gllm_pipeline.router.aurelio_semantic_router.index.aurelio_index import BaseAurelioIndex as BaseAurelioIndex
|
|
4
|
+
from gllm_pipeline.router.utils import encode_bytes as encode_bytes
|
|
5
|
+
from semantic_router import Route
|
|
6
|
+
from typing import Any, Callable, TypeVar
|
|
7
|
+
|
|
8
|
+
ROUTE_PAYLOAD_KEY: str
|
|
9
|
+
UTTERANCE_PAYLOAD_KEY: str
|
|
10
|
+
T = TypeVar('T')
|
|
11
|
+
|
|
12
|
+
class VectorStoreAdapterIndex(BaseAurelioIndex):
|
|
13
|
+
"""A vector store-backed implementation of BaseAurelioIndex for use with AurelioSemanticRouter.
|
|
14
|
+
|
|
15
|
+
This index performs similarity search over a backend vector store to retrieve relevant
|
|
16
|
+
route payloads. The vector store must implement the BaseVectorDataStore interface and support
|
|
17
|
+
async methods for querying by vector and filtering by metadata.
|
|
18
|
+
"""
|
|
19
|
+
index: BaseVectorDataStore
|
|
20
|
+
def __init__(self, index: BaseVectorDataStore, **kwargs: Any) -> None:
|
|
21
|
+
"""Initialize the VectorStoreAdapterIndex.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
index (BaseVectorDataStore): The vector store instance used for retrieval.
|
|
25
|
+
**kwargs: Additional keyword arguments forwarded to the BaseAurelioIndex.
|
|
26
|
+
"""
|
|
27
|
+
def __len__(self) -> int:
|
|
28
|
+
"""Returns the total number of vectors in the index.
|
|
29
|
+
|
|
30
|
+
If the index is not initialized returns 0.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
int: The total number of vectors.
|
|
34
|
+
"""
|
|
35
|
+
def add(self, routes: list[str], utterances: list[str | bytes], **_):
|
|
36
|
+
"""Add route-utterance pairs into the vector store.
|
|
37
|
+
|
|
38
|
+
Each utterance is associated with its route and encoded as a Chunk. Binary strings
|
|
39
|
+
are automatically base64-encoded for safe storage.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
routes (list[str]): List of route identifiers.
|
|
43
|
+
utterances (list[str | bytes]): List of utterance strings or bytes.
|
|
44
|
+
**_: Ignored. Included for interface compatibility.
|
|
45
|
+
|
|
46
|
+
Raises:
|
|
47
|
+
AssertionError: If `routes` and `utterances` have different lengths.
|
|
48
|
+
"""
|
|
49
|
+
def get_routes(self) -> dict[str, list]:
|
|
50
|
+
"""Retrieve all routes and their corresponding utterances from the vector store.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
dict[str, list]: A dictionary where each key is a route and the value is a
|
|
54
|
+
list of associated utterance strings.
|
|
55
|
+
"""
|
|
56
|
+
def query(self, vector: np.ndarray, top_k: int = 5, route_filter: list[str] | None = None, retrieval_params: dict | None = None) -> tuple[np.ndarray, list[str]]:
|
|
57
|
+
"""Perform a similarity query using the provided vector, optionally filtering by route.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
vector (np.ndarray): Query vector to search against stored vectors.
|
|
61
|
+
top_k (int, optional): Maximum number of top matching results to return. Defaults to 5.
|
|
62
|
+
route_filter (list[str] | None, optional): Optional list of route names to filter
|
|
63
|
+
the search results by. If None, all routes are considered.
|
|
64
|
+
retrieval_params (dict | None, optional): Filter parameters to narrow the search. Defaults to None.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
tuple[np.ndarray, list[str]]: A tuple containing:
|
|
68
|
+
- A NumPy array of similarity scores.
|
|
69
|
+
- A list of corresponding route names for each match.
|
|
70
|
+
"""
|
|
71
|
+
def is_ready(self):
|
|
72
|
+
"""Checks if the index is ready to be used.
|
|
73
|
+
|
|
74
|
+
This is a mandatory method to be implemented from `BaseIndex`.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
bool: True if the index has one or more vectors; False otherwise.
|
|
78
|
+
"""
|
|
79
|
+
async def ais_ready(self) -> None:
|
|
80
|
+
"""Checks if the index is ready to be used in async.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
bool: True if the index has one or more vectors; False otherwise.
|
|
84
|
+
"""
|
|
85
|
+
def load_routes_from_json(self, file_path: str, transform_utterance: Callable[[str], str | bytes] | None = None):
|
|
86
|
+
"""Load route-utterance pairs from a JSON file and insert them into the vector store.
|
|
87
|
+
|
|
88
|
+
The JSON file must contain a dictionary mapping route names to lists of utterances.
|
|
89
|
+
Each utterance can optionally be transformed before storage.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
file_path (str): Path to a `.json` file containing routes and utterances.
|
|
93
|
+
transform_utterance (Callable[[str], str | bytes] | None): Optional function to
|
|
94
|
+
preprocess each utterance before storage. Must return `str` or `bytes`.
|
|
95
|
+
|
|
96
|
+
Raises:
|
|
97
|
+
ValueError: If the provided file is not a JSON file.
|
|
98
|
+
"""
|
|
99
|
+
def load_routes_from_dict(self, routes: list[Route] | dict[str, list[str | bytes]], transform_utterance: Callable[[str], str | bytes] | None = None):
|
|
100
|
+
"""Load route-utterance pairs from a list of `Route` or a dictionary and insert into the vector store.
|
|
101
|
+
|
|
102
|
+
This method supports two input formats:
|
|
103
|
+
1. A list of `Route` objects from `semantic_router`, where each route has a `name` and `utterances`.
|
|
104
|
+
2. A dictionary mapping route names (str) to lists of utterances (list[str]).
|
|
105
|
+
|
|
106
|
+
Optionally, a `transform_utterance` function can be provided to process each utterance before storage.
|
|
107
|
+
Utterances can be any type; use `transform_utterance` to ensure the final result is str or bytes.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
routes (list[Route] | dict[str, list]): Route data in either list or dictionary format.
|
|
111
|
+
transform_utterance (Callable[[str], str | bytes] | None): Optional function to transform
|
|
112
|
+
each utterance before storage. Must return `str` or `bytes`.
|
|
113
|
+
|
|
114
|
+
Raises:
|
|
115
|
+
ValueError: If `routes` is not a list of `Route` objects or a dictionary,
|
|
116
|
+
if route names are invalid,
|
|
117
|
+
if utterances are not lists of non-empty strings,
|
|
118
|
+
or if transformed utterances are not `str` or `bytes`.
|
|
119
|
+
"""
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_inference.request_processor import LMRequestProcessor as LMRequestProcessor, UsesLM
|
|
3
|
+
from gllm_pipeline.router.preset.preset_loader import get_preset as get_preset
|
|
4
|
+
from gllm_pipeline.router.router import BaseRouter as BaseRouter
|
|
5
|
+
|
|
6
|
+
DEFAULT_LM_OUTPUT_KEY: str
|
|
7
|
+
|
|
8
|
+
class LMBasedRouter(BaseRouter, UsesLM):
|
|
9
|
+
'''A router that utilizes a language model to determine the appropriate route for an input source.
|
|
10
|
+
|
|
11
|
+
This class routes a given input source to an appropriate path based on the output of a language model.
|
|
12
|
+
If the determined route is not valid, it defaults to a predefined route.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
lm_request_processor (LMRequestProcessor): The request processor that handles requests to the language model.
|
|
16
|
+
default_route (str): The default route to be used if the language model\'s output is invalid.
|
|
17
|
+
valid_routes (set[str]): A set of valid routes for the router.
|
|
18
|
+
lm_output_key (str, optional): The key in the language model\'s output that contains the route.
|
|
19
|
+
|
|
20
|
+
Notes:
|
|
21
|
+
The `lm_request_processor` must be configured to:
|
|
22
|
+
1. Take a "source" key as input. The input source of the router should be passed as the value of
|
|
23
|
+
this "source" key.
|
|
24
|
+
2. Return a JSON object which contains the selected route as a string. The key of the route is specified by the
|
|
25
|
+
`lm_output_key` attribute. Furthermore, the selected route must be present in the `valid_routes` set.
|
|
26
|
+
|
|
27
|
+
Output example, assuming the `lm_output_key` is "route":
|
|
28
|
+
{
|
|
29
|
+
"route": "<route_string>"
|
|
30
|
+
}
|
|
31
|
+
'''
|
|
32
|
+
lm_request_processor: Incomplete
|
|
33
|
+
lm_output_key: Incomplete
|
|
34
|
+
def __init__(self, lm_request_processor: LMRequestProcessor, default_route: str, valid_routes: set[str], lm_output_key: str = ...) -> None:
|
|
35
|
+
"""Initializes a new instance of the LMBasedRouter class.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
lm_request_processor (LMRequestProcessor): The request processor that handles requests to the
|
|
39
|
+
language model.
|
|
40
|
+
default_route (str): The default route to be used if the language model's output is invalid.
|
|
41
|
+
valid_routes (set[str]): A set of valid routes for the router.
|
|
42
|
+
lm_output_key (str): The key in the language model's output that contains the route.
|
|
43
|
+
Defaults to DEFAULT_LM_OUTPUT_KEY.
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
ValueError: If the provided default route is not in the set of valid routes.
|
|
47
|
+
"""
|
|
48
|
+
@classmethod
|
|
49
|
+
def from_preset(cls, modality: str, preset_name: str, preset_kwargs: dict | None = None, **kwargs) -> LMBasedRouter:
|
|
50
|
+
"""Initialize the LM based router component using preset model configurations.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
modality (str): type of modality input.
|
|
54
|
+
preset_name (str): Name of the preset to use.
|
|
55
|
+
preset_kwargs (dict | None): placeholder for preset additional arguments.
|
|
56
|
+
**kwargs (Any): Additional arguments to pass for this class.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
LMBasedRouter: Initialized lm based router component using preset model.
|
|
60
|
+
"""
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
|
|
3
|
+
ROUTES: Incomplete
|
|
4
|
+
|
|
5
|
+
def get_router_image_domain_specific(**_: dict) -> dict:
|
|
6
|
+
'''Builds a domain-specific router preset for image-based input using CLIP embeddings.
|
|
7
|
+
|
|
8
|
+
This preset is designed to classify images into domain-specific categories, such as
|
|
9
|
+
engineering diagrams, healthcare scans, financial statements, etc. Each route is defined
|
|
10
|
+
using representative natural language utterances (captions), which are embedded using
|
|
11
|
+
OpenAI\'s CLIP model (ViT-B/32). These embeddings are indexed using a LocalIndex for fast
|
|
12
|
+
similarity-based routing.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
dict: A dictionary containing:
|
|
16
|
+
1. "default_route" (str): The fallback route if no strong match is found.
|
|
17
|
+
2. "valid_routes" (set): Set of all valid route names.
|
|
18
|
+
3. "encoder" (DenseEncoder): The CLIP encoder used to embed utterances and queries.
|
|
19
|
+
4. "index" (BaseIndex): The index containing all route utterance embeddings.
|
|
20
|
+
5. "auto_sync" (str): Value used for auto-sync mode ("local").
|
|
21
|
+
'''
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
2
|
+
from gllm_inference.output_parser import JSONOutputParser
|
|
3
|
+
from gllm_inference.prompt_builder import PromptBuilder
|
|
4
|
+
|
|
5
|
+
SYSTEM_PROMPT: str
|
|
6
|
+
USER_PROMPT: str
|
|
7
|
+
|
|
8
|
+
def get_router_image_domain_specific(lm_invoker_kwargs: dict | None = None, prompt_builder_kwargs: dict | None = None) -> dict[str, BaseLMInvoker | PromptBuilder | JSONOutputParser]:
|
|
9
|
+
"""Returns the domain-specific preset components for image routing.
|
|
10
|
+
|
|
11
|
+
Returns:
|
|
12
|
+
dict[str, BaseLMInvoker | PromptBuilder | JSONOutputParser]: The LM invoker,
|
|
13
|
+
prompt builder, and output parser.
|
|
14
|
+
"""
|