Query Transformer
Modules concerning the query transformers used in Gen AI applications.
ManyToOneQueryTransformer(lm_request_processor, extract_func=None, combine_func=None, on_error=ErrorHandling.RAISE)
Bases: BaseQueryTransformer
A query transformer that takes multiple queries and outputs a single transformed query.
Attributes:
Name | Type | Description |
---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance for transforming queries. The input type determines the prompt builder's structure: 1. For strings or lists, it must only have the "query" key; 2. For dictionaries, the keys must match the input. |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], str | list[str]]
|
A function to extract the transformed query from the LM output. The transformer ensures a single string is effectively returned. |
combine_func |
Callable[[list[str]], str]
|
A function to combine multiple input queries into a single string
before sending to the LM. Defaults to |
on_error |
ErrorHandling
|
The error handling strategy. Defaults to ErrorHandling.RAISE. |
Initialize the ManyToOneTransformer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance. |
required |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], str | list[str]]
|
Function to extract the transformed query. Defaults to base class's default. |
None
|
combine_func |
Callable[[list[str]], str]
|
Function to combine input queries.
Defaults to |
None
|
on_error |
ErrorHandling
|
Error handling strategy. Defaults to ErrorHandling.RAISE. |
RAISE
|
NoOpQueryTransformer(on_error=ErrorHandling.RAISE)
Bases: BaseQueryTransformer
A query transformer that returns the input query without modification.
Attributes:
Name | Type | Description |
---|---|---|
on_error |
ErrorHandling
|
The error handling strategy. Since this transformer performs no actual transformation that can fail, this primarily exists for API consistency. Defaults to ErrorHandling.RAISE. |
Initialize the NoOpQueryTransformer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
on_error |
ErrorHandling
|
The error handling strategy. Defaults to ErrorHandling.RAISE. |
RAISE
|
from_lm_components(*args, **kwargs)
classmethod
Create a NoOpQueryTransformer instance.
Returns:
Name | Type | Description |
---|---|---|
NoOpQueryTransformer |
NoOpQueryTransformer
|
A new instance of NoOpQueryTransformer. |
OneToManyQueryTransformer(lm_request_processor, extract_func=None, on_error=ErrorHandling.RAISE)
Bases: BaseQueryTransformer
A query transformer that takes one query and outputs one or more transformed queries.
When processing multiple input queries (a list of queries), the results from each query's transformation (which itself can be multiple queries) are flattened into a single list of transformed queries.
Attributes:
Name | Type | Description |
---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance for transforming queries. The input type determines the prompt builder's structure: 1. For strings or lists, it must only have the "query" key; 2. For dictionaries, the keys must match the input. |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], list[str]]
|
A function to extract
the list of transformed queries from the LM output. If None, |
on_error |
ErrorHandling
|
The error handling strategy. Defaults to ErrorHandling.RAISE. |
Initialize the OneToManyQueryTransformer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance. |
required |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], list[str]]
|
Function to extract transformed queries. Defaults to |
None
|
on_error |
ErrorHandling
|
Error handling strategy. Defaults to ErrorHandling.RAISE. |
RAISE
|
OneToOneQueryTransformer(lm_request_processor, extract_func=None, on_error=ErrorHandling.RAISE)
Bases: BaseQueryTransformer
A query transformer that processes each input query to produce one corresponding transformed query.
If a list of queries is provided as input, each query is processed independently, resulting in a list of transformed queries (maintaining the "one-to-one" mapping for each item).
Attributes:
Name | Type | Description |
---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance to be used for transforming queries. The input type determines the prompt builder's structure: 1. For strings or lists, it must only have the "query" key; 2. For dictionaries, the keys must match the input. |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], str | list[str]]
|
A function to extract the transformed query from the LM output. This transformer's logic will ensure that effectively one string is obtained per input query. |
on_error |
ErrorHandling
|
The error handling strategy to use when an exception occurs during query transformation. Defaults to ErrorHandling.RAISE. |
Initialize the OneToOneQueryTransformer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm_request_processor |
LMRequestProcessor
|
The LMRequestProcessor instance to be used for transforming queries. |
required |
extract_func |
Callable[[str | list[str] | dict[str, str | list[str]]], str | list[str]]
|
A function to extract the transformed query from the LM output. If None, the base class's default extractor is used. The logic within this transformer ensures that a single string query is derived for each input query. |
None
|
on_error |
ErrorHandling
|
The error handling strategy to use. Defaults to ErrorHandling.RAISE. |
RAISE
|