shaped 2.0.0__py3-none-any.whl → 2.0.2__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.
- shaped/__init__.py +54 -4
- shaped/autogen/__init__.py +541 -66
- shaped/autogen/api/__init__.py +4 -3
- shaped/autogen/api/engine_api.py +1467 -0
- shaped/autogen/api/{dataset_api.py → query_api.py} +219 -194
- shaped/autogen/api/table_api.py +1494 -0
- shaped/autogen/api/{model_management_api.py → view_api.py} +179 -181
- shaped/autogen/api_client.py +15 -8
- shaped/autogen/configuration.py +20 -9
- shaped/autogen/exceptions.py +19 -2
- shaped/autogen/models/__init__.py +254 -52
- shaped/autogen/models/ai_enrichment_view_config.py +123 -0
- shaped/autogen/models/{path.py → algorithm.py} +19 -19
- shaped/autogen/models/amplitude_table_config.py +106 -0
- shaped/autogen/models/ascending.py +136 -0
- shaped/autogen/models/attn_dropout_prob.py +136 -0
- shaped/autogen/models/attribute_journey.py +124 -0
- shaped/autogen/models/attribute_value.py +178 -0
- shaped/autogen/models/autoscaling_config.py +95 -0
- shaped/autogen/models/aws_pinpoint_table_config.py +108 -0
- shaped/autogen/models/batch_size.py +136 -0
- shaped/autogen/models/batch_size1.py +136 -0
- shaped/autogen/models/batch_size2.py +136 -0
- shaped/autogen/models/big_query_table_config.py +147 -0
- shaped/autogen/models/bm25.py +136 -0
- shaped/autogen/models/boosted_reorder_step.py +125 -0
- shaped/autogen/models/canary_rollout.py +99 -0
- shaped/autogen/models/candidate_attributes_retrieve_step.py +126 -0
- shaped/autogen/models/candidate_ids_retrieve_step.py +113 -0
- shaped/autogen/models/candidate_retrieval_strategy.py +41 -0
- shaped/autogen/models/clickhouse_table_config.py +146 -0
- shaped/autogen/models/column_order_retrieve_step.py +123 -0
- shaped/autogen/models/column_ordering.py +91 -0
- shaped/autogen/models/create_table_response.py +87 -0
- shaped/autogen/models/create_view_response.py +87 -0
- shaped/autogen/models/custom_table_config.py +135 -0
- shaped/autogen/models/data_compute_config.py +89 -0
- shaped/autogen/models/data_config.py +145 -0
- shaped/autogen/models/data_config_interaction_table.py +146 -0
- shaped/autogen/models/data_split_config.py +88 -0
- shaped/autogen/models/data_split_strategy.py +37 -0
- shaped/autogen/models/data_tier.py +37 -0
- shaped/autogen/models/default.py +246 -0
- shaped/autogen/models/delete_engine_response.py +87 -0
- shaped/autogen/models/delete_table_response.py +87 -0
- shaped/autogen/models/delete_view_response.py +87 -0
- shaped/autogen/models/deployment_config.py +123 -0
- shaped/autogen/models/distance_function.py +38 -0
- shaped/autogen/models/diversity_reorder_step.py +137 -0
- shaped/autogen/models/dropout_rate.py +136 -0
- shaped/autogen/models/dynamo_db_table_config.py +160 -0
- shaped/autogen/models/dynamo_db_table_config_scan_kwargs_value.py +138 -0
- shaped/autogen/models/embedder_batch_size.py +136 -0
- shaped/autogen/models/embedding_config.py +93 -0
- shaped/autogen/models/embedding_dim.py +136 -0
- shaped/autogen/models/embedding_dims.py +136 -0
- shaped/autogen/models/embedding_size.py +136 -0
- shaped/autogen/models/encoder.py +140 -0
- shaped/autogen/models/encoding_pooling_strategy.py +38 -0
- shaped/autogen/models/engine.py +109 -0
- shaped/autogen/models/engine_config_v2.py +152 -0
- shaped/autogen/models/engine_details_response.py +120 -0
- shaped/autogen/models/engine_schema.py +113 -0
- shaped/autogen/models/engine_schema_user_inner.py +134 -0
- shaped/autogen/models/entity_config.py +109 -0
- shaped/autogen/models/entity_journey.py +152 -0
- shaped/autogen/models/entity_type.py +38 -0
- shaped/autogen/models/evaluation_config.py +92 -0
- shaped/autogen/models/exploration_reorder_step.py +125 -0
- shaped/autogen/models/expression_filter_step.py +106 -0
- shaped/autogen/models/factors.py +136 -0
- shaped/autogen/models/factors1.py +136 -0
- shaped/autogen/models/feature.py +90 -0
- shaped/autogen/models/feature_type.py +60 -0
- shaped/autogen/models/file_table_config.py +112 -0
- shaped/autogen/models/filter_config.py +99 -0
- shaped/autogen/models/filter_dataset.py +140 -0
- shaped/autogen/models/filter_index_type.py +36 -0
- shaped/autogen/models/filter_retrieve_step.py +113 -0
- shaped/autogen/models/global_filter.py +102 -0
- shaped/autogen/models/hidden_dropout_prob.py +136 -0
- shaped/autogen/models/hidden_size.py +136 -0
- shaped/autogen/models/hidden_size1.py +136 -0
- shaped/autogen/models/http_problem_response.py +115 -0
- shaped/autogen/models/http_validation_error.py +2 -2
- shaped/autogen/models/hugging_face_encoder.py +113 -0
- shaped/autogen/models/iceberg_table_config.py +154 -0
- shaped/autogen/models/index_config.py +101 -0
- shaped/autogen/models/inner_size.py +136 -0
- shaped/autogen/models/inner_size1.py +136 -0
- shaped/autogen/models/interaction_config.py +122 -0
- shaped/autogen/models/interaction_pooling_encoder.py +104 -0
- shaped/autogen/models/interaction_round_robin_encoder.py +104 -0
- shaped/autogen/models/item_attribute_pooling_encoder.py +124 -0
- shaped/autogen/models/journey.py +140 -0
- shaped/autogen/models/kafka_table_config.py +129 -0
- shaped/autogen/models/kinesis_table_config.py +140 -0
- shaped/autogen/models/kinesis_table_config_column_schema_value.py +136 -0
- shaped/autogen/models/label.py +90 -0
- shaped/autogen/models/label_type.py +37 -0
- shaped/autogen/models/laplace_smoothing.py +136 -0
- shaped/autogen/models/latency_scaling_policy.py +112 -0
- shaped/autogen/models/learning_rate.py +136 -0
- shaped/autogen/models/learning_rate1.py +136 -0
- shaped/autogen/models/learning_rate2.py +136 -0
- shaped/autogen/models/learning_rate3.py +136 -0
- shaped/autogen/models/lexical_search_mode.py +99 -0
- shaped/autogen/models/list_engines_response.py +95 -0
- shaped/autogen/models/list_tables_response.py +95 -0
- shaped/autogen/models/list_views_response.py +95 -0
- shaped/autogen/models/loss_types.py +37 -0
- shaped/autogen/models/lr.py +136 -0
- shaped/autogen/models/lr1.py +136 -0
- shaped/autogen/models/lr2.py +136 -0
- shaped/autogen/models/max_depth.py +136 -0
- shaped/autogen/models/max_leaves.py +136 -0
- shaped/autogen/models/max_seq_length.py +136 -0
- shaped/autogen/models/max_seq_length1.py +136 -0
- shaped/autogen/models/max_seq_length2.py +136 -0
- shaped/autogen/models/mode.py +134 -0
- shaped/autogen/models/mode1.py +134 -0
- shaped/autogen/models/mode2.py +136 -0
- shaped/autogen/models/mongo_db_table_config.py +147 -0
- shaped/autogen/models/mssql_table_config.py +155 -0
- shaped/autogen/models/{my_sql_dataset_config.py → my_sql_table_config.py} +45 -28
- shaped/autogen/models/n_epochs.py +136 -0
- shaped/autogen/models/n_epochs1.py +136 -0
- shaped/autogen/models/n_epochs2.py +136 -0
- shaped/autogen/models/n_estimators.py +136 -0
- shaped/autogen/models/n_heads.py +136 -0
- shaped/autogen/models/n_layers.py +136 -0
- shaped/autogen/models/neg_per_positive.py +136 -0
- shaped/autogen/models/negative_samples_count.py +136 -0
- shaped/autogen/models/ngram_tokenizer.py +103 -0
- shaped/autogen/models/no_op_config.py +117 -0
- shaped/autogen/models/num_blocks.py +136 -0
- shaped/autogen/models/num_heads.py +136 -0
- shaped/autogen/models/num_leaves.py +136 -0
- shaped/autogen/models/objective.py +40 -0
- shaped/autogen/models/objective1.py +134 -0
- shaped/autogen/models/online_store_config.py +89 -0
- shaped/autogen/models/pagination_config.py +87 -0
- shaped/autogen/models/parameter_definition.py +96 -0
- shaped/autogen/models/parameters_value.py +240 -0
- shaped/autogen/models/passthrough_score.py +104 -0
- shaped/autogen/models/personal_filter.py +104 -0
- shaped/autogen/models/pipeline_stage_explanation.py +118 -0
- shaped/autogen/models/policy.py +134 -0
- shaped/autogen/models/pool_fn.py +134 -0
- shaped/autogen/models/pooling_function.py +37 -0
- shaped/autogen/models/{postgres_dataset_config.py → postgres_table_config.py} +66 -28
- shaped/autogen/models/posthog_table_config.py +133 -0
- shaped/autogen/models/prebuilt_filter_step.py +113 -0
- shaped/autogen/models/precomputed_item_embedding.py +99 -0
- shaped/autogen/models/precomputed_user_embedding.py +99 -0
- shaped/autogen/models/query.py +136 -0
- shaped/autogen/models/query1.py +136 -0
- shaped/autogen/models/query_any_of.py +140 -0
- shaped/autogen/models/query_definition.py +106 -0
- shaped/autogen/models/query_encoder.py +194 -0
- shaped/autogen/models/query_explanation.py +197 -0
- shaped/autogen/models/query_request.py +121 -0
- shaped/autogen/models/query_result.py +113 -0
- shaped/autogen/models/query_table_config.py +99 -0
- shaped/autogen/models/rank_item_attribute_values_query_config.py +122 -0
- shaped/autogen/models/rank_query_config.py +167 -0
- shaped/autogen/models/rank_query_config_filter_inner.py +149 -0
- shaped/autogen/models/rank_query_config_reorder_inner.py +149 -0
- shaped/autogen/models/rank_query_config_retrieve_inner.py +196 -0
- shaped/autogen/models/recreate_rollout.py +97 -0
- shaped/autogen/models/{redshift_dataset_config.py → redshift_table_config.py} +48 -25
- shaped/autogen/models/reference_table_config.py +113 -0
- shaped/autogen/models/regularization.py +136 -0
- shaped/autogen/models/request.py +378 -0
- shaped/autogen/models/request1.py +140 -0
- shaped/autogen/models/requests_per_second_scaling_policy.py +112 -0
- shaped/autogen/models/response_get_view_details_views_view_name_get.py +134 -0
- shaped/autogen/models/result.py +145 -0
- shaped/autogen/models/result_embeddings_value.py +127 -0
- shaped/autogen/models/retriever.py +196 -0
- shaped/autogen/models/retriever1.py +196 -0
- shaped/autogen/models/rollout_config.py +91 -0
- shaped/autogen/models/rudderstack_table_config.py +106 -0
- shaped/autogen/models/sampling_strategy.py +36 -0
- shaped/autogen/models/saved_query_info_response.py +89 -0
- shaped/autogen/models/saved_query_list_response.py +87 -0
- shaped/autogen/models/saved_query_request.py +115 -0
- shaped/autogen/models/schema_config.py +117 -0
- shaped/autogen/models/score.py +134 -0
- shaped/autogen/models/score_ensemble.py +140 -0
- shaped/autogen/models/score_ensemble_policy_config.py +141 -0
- shaped/autogen/models/score_ensemble_policy_config_policies_inner.py +422 -0
- shaped/autogen/models/search_config.py +105 -0
- shaped/autogen/models/segment_table_config.py +106 -0
- shaped/autogen/models/sequence_length.py +136 -0
- shaped/autogen/models/server_config.py +87 -0
- shaped/autogen/models/setup_engine_response.py +87 -0
- shaped/autogen/models/shaped_internal_recsys_policies_als_model_policy_als_model_policy_config.py +148 -0
- shaped/autogen/models/shaped_internal_recsys_policies_beeformer_model_policy_beeformer_model_policy_beeformer_model_policy_config.py +154 -0
- shaped/autogen/models/shaped_internal_recsys_policies_bert_model_policy_bert_model_policy_bert_model_policy_config.py +209 -0
- shaped/autogen/models/shaped_internal_recsys_policies_chronological_model_policy_chronological_model_policy_config.py +137 -0
- shaped/autogen/models/shaped_internal_recsys_policies_elsa_model_policy_elsa_model_policy_elsa_model_policy_config.py +139 -0
- shaped/autogen/models/shaped_internal_recsys_policies_gsasrec_model_policy_gsasrec_model_policy_gsas_rec_model_policy_config.py +205 -0
- shaped/autogen/models/shaped_internal_recsys_policies_item2vec_model_policy_item2_vec_model_policy_config.py +140 -0
- shaped/autogen/models/shaped_internal_recsys_policies_item_content_similarity_model_policy_item_content_similarity_model_policy_config.py +152 -0
- shaped/autogen/models/shaped_internal_recsys_policies_lightgbm_model_policy_lightgbm_model_policy_light_gbm_model_policy_config.py +239 -0
- shaped/autogen/models/shaped_internal_recsys_policies_ngram_model_policy_ngram_model_policy_config.py +119 -0
- shaped/autogen/models/shaped_internal_recsys_policies_popular_model_policy_popular_model_policy_config.py +137 -0
- shaped/autogen/models/shaped_internal_recsys_policies_random_model_policy_random_model_policy_config.py +104 -0
- shaped/autogen/models/shaped_internal_recsys_policies_recently_popular_policy_recently_popular_policy_config.py +130 -0
- shaped/autogen/models/shaped_internal_recsys_policies_rising_popular_policy_rising_popular_policy_config.py +123 -0
- shaped/autogen/models/shaped_internal_recsys_policies_sasrec_model_policy_sasrec_model_policy_sas_rec_model_policy_config.py +224 -0
- shaped/autogen/models/shaped_internal_recsys_policies_svd_model_policy_svd_model_policy_config.py +119 -0
- shaped/autogen/models/shaped_internal_recsys_policies_two_tower_model_policy_two_tower_model_policy_two_tower_model_policy_config.py +159 -0
- shaped/autogen/models/shaped_internal_recsys_policies_user_item_content_similarity_model_policy_user_item_content_similarity_model_policy_config.py +131 -0
- shaped/autogen/models/shaped_internal_recsys_policies_widedeep_model_policy_wide_deep_model_policy_config.py +131 -0
- shaped/autogen/models/shaped_internal_recsys_policies_xgboost_model_policy_xg_boost_model_policy_config.py +149 -0
- shaped/autogen/models/shopify_table_config.py +156 -0
- shaped/autogen/models/similarity_retrieve_step.py +121 -0
- shaped/autogen/models/{snowflake_dataset_config.py → snowflake_table_config.py} +47 -18
- shaped/autogen/models/sql_transform_type.py +37 -0
- shaped/autogen/models/sql_view_config.py +111 -0
- shaped/autogen/models/stemmer_tokenizer.py +105 -0
- shaped/autogen/models/step_explanation.py +137 -0
- shaped/autogen/models/strategy.py +134 -0
- shaped/autogen/models/table.py +102 -0
- shaped/autogen/models/table_deployment_type.py +38 -0
- shaped/autogen/models/table_insert_arguments.py +87 -0
- shaped/autogen/models/table_insert_response.py +87 -0
- shaped/autogen/models/text_encoding.py +136 -0
- shaped/autogen/models/text_search_retrieve_step.py +121 -0
- shaped/autogen/models/time_frequency.py +136 -0
- shaped/autogen/models/time_window.py +136 -0
- shaped/autogen/models/time_window_in_days.py +142 -0
- shaped/autogen/models/tokenizer.py +149 -0
- shaped/autogen/models/trained_model_encoder.py +99 -0
- shaped/autogen/models/training_compute_config.py +99 -0
- shaped/autogen/models/training_config.py +121 -0
- shaped/autogen/models/training_config_models_inner.py +308 -0
- shaped/autogen/models/training_strategy.py +37 -0
- shaped/autogen/models/trending_mode.py +37 -0
- shaped/autogen/models/truncate_filter_step.py +106 -0
- shaped/autogen/models/tunable_bool.py +97 -0
- shaped/autogen/models/tunable_float.py +118 -0
- shaped/autogen/models/tunable_int.py +118 -0
- shaped/autogen/models/tunable_int_categorical.py +99 -0
- shaped/autogen/models/tunable_string.py +99 -0
- shaped/autogen/models/tuning_config.py +89 -0
- shaped/autogen/models/type.py +134 -0
- shaped/autogen/models/update_table_response.py +87 -0
- shaped/autogen/models/update_view_response.py +87 -0
- shaped/autogen/models/user_attribute_pooling_encoder.py +124 -0
- shaped/autogen/models/val_split.py +136 -0
- shaped/autogen/models/validation_error.py +13 -3
- shaped/autogen/models/validation_error_loc_inner.py +138 -0
- shaped/autogen/models/value_type.py +7 -5
- shaped/autogen/models/vector_search_mode.py +99 -0
- shaped/autogen/models/view.py +104 -0
- shaped/autogen/models/view_details_ai.py +140 -0
- shaped/autogen/models/view_details_ai_schema_value.py +153 -0
- shaped/autogen/models/view_details_sql.py +140 -0
- shaped/autogen/models/view_status.py +41 -0
- shaped/autogen/models/weight_decay.py +136 -0
- shaped/autogen/models/whitespace_tokenizer.py +97 -0
- shaped/autogen/models/window_size.py +136 -0
- shaped/autogen/rest.py +8 -2
- shaped/cli/shaped_cli.py +12 -7
- shaped/client.py +587 -174
- shaped/config_builders.py +695 -0
- shaped/query_builder.py +774 -0
- {shaped-2.0.0.dist-info → shaped-2.0.2.dist-info}/METADATA +119 -56
- shaped-2.0.2.dist-info/RECORD +278 -0
- shaped-2.0.2.dist-info/entry_points.txt +2 -0
- shaped/autogen/api/model_inference_api.py +0 -2825
- shaped/autogen/models/amplitude_dataset_config.py +0 -96
- shaped/autogen/models/aws_pinpoint_dataset_config.py +0 -96
- shaped/autogen/models/big_query_dataset_config.py +0 -114
- shaped/autogen/models/complement_items_request.py +0 -99
- shaped/autogen/models/complement_items_response.py +0 -89
- shaped/autogen/models/connectors_inner.py +0 -134
- shaped/autogen/models/create_dataset_arguments.py +0 -263
- shaped/autogen/models/create_embedding_response.py +0 -87
- shaped/autogen/models/create_item_embedding_request.py +0 -89
- shaped/autogen/models/create_model_arguments.py +0 -107
- shaped/autogen/models/create_model_response.py +0 -87
- shaped/autogen/models/create_user_embedding_request.py +0 -89
- shaped/autogen/models/custom_dataset_config.py +0 -115
- shaped/autogen/models/dataset_config.py +0 -101
- shaped/autogen/models/dataset_schema_type.py +0 -47
- shaped/autogen/models/datasets_inner.py +0 -91
- shaped/autogen/models/delete_model_response.py +0 -87
- shaped/autogen/models/fetch_config.py +0 -95
- shaped/autogen/models/file_config.py +0 -105
- shaped/autogen/models/file_source_config.py +0 -89
- shaped/autogen/models/inference_config.py +0 -101
- shaped/autogen/models/insert_model_response.py +0 -87
- shaped/autogen/models/interaction.py +0 -87
- shaped/autogen/models/list_datasets_response.py +0 -95
- shaped/autogen/models/list_models_response.py +0 -95
- shaped/autogen/models/model_config.py +0 -99
- shaped/autogen/models/model_response.py +0 -95
- shaped/autogen/models/mongo_db_dataset_config.py +0 -119
- shaped/autogen/models/post_rank_request.py +0 -117
- shaped/autogen/models/rank_attribute_response.py +0 -89
- shaped/autogen/models/rank_grid_attribute_request.py +0 -91
- shaped/autogen/models/rank_grid_attribute_request1.py +0 -93
- shaped/autogen/models/rank_grid_attribute_response.py +0 -91
- shaped/autogen/models/rank_response.py +0 -91
- shaped/autogen/models/retrieve_request.py +0 -101
- shaped/autogen/models/retrieve_response.py +0 -91
- shaped/autogen/models/retriever_top_k_override.py +0 -97
- shaped/autogen/models/rudder_stack_dataset_config.py +0 -96
- shaped/autogen/models/segment_dataset_config.py +0 -96
- shaped/autogen/models/similar_item_request.py +0 -101
- shaped/autogen/models/similar_response.py +0 -89
- shaped/autogen/models/similar_users_request.py +0 -99
- shaped/autogen/models/successful_response.py +0 -87
- shaped/autogen/models/view_model_response.py +0 -99
- shaped-2.0.0.dist-info/RECORD +0 -73
- shaped-2.0.0.dist-info/entry_points.txt +0 -2
- {shaped-2.0.0.dist-info → shaped-2.0.2.dist-info}/WHEEL +0 -0
- {shaped-2.0.0.dist-info → shaped-2.0.2.dist-info}/top_level.txt +0 -0
- {shaped-2.0.0.dist-info → shaped-2.0.2.dist-info}/zip-safe +0 -0
shaped/query_builder.py
ADDED
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Fluent query builder for constructing Shaped V2 declarative queries.
|
|
3
|
+
This module provides a fluent API for building RankQueryConfig objects
|
|
4
|
+
that leverage the declarative nature of Shaped's query language.
|
|
5
|
+
"""
|
|
6
|
+
from typing import Any, Dict, List, Optional, TypedDict, Union
|
|
7
|
+
|
|
8
|
+
# Import all model classes
|
|
9
|
+
from shaped.autogen.models.rank_query_config import RankQueryConfig
|
|
10
|
+
from shaped.autogen.models.column_order_retrieve_step import ColumnOrderRetrieveStep
|
|
11
|
+
from shaped.autogen.models.text_search_retrieve_step import TextSearchRetrieveStep
|
|
12
|
+
from shaped.autogen.models.similarity_retrieve_step import SimilarityRetrieveStep
|
|
13
|
+
from shaped.autogen.models.candidate_ids_retrieve_step import CandidateIdsRetrieveStep
|
|
14
|
+
from shaped.autogen.models.candidate_attributes_retrieve_step import CandidateAttributesRetrieveStep
|
|
15
|
+
from shaped.autogen.models.filter_retrieve_step import FilterRetrieveStep
|
|
16
|
+
from shaped.autogen.models.expression_filter_step import ExpressionFilterStep
|
|
17
|
+
from shaped.autogen.models.truncate_filter_step import TruncateFilterStep
|
|
18
|
+
from shaped.autogen.models.prebuilt_filter_step import PrebuiltFilterStep
|
|
19
|
+
from shaped.autogen.models.score_ensemble import ScoreEnsemble
|
|
20
|
+
from shaped.autogen.models.passthrough_score import PassthroughScore
|
|
21
|
+
from shaped.autogen.models.diversity_reorder_step import DiversityReorderStep
|
|
22
|
+
from shaped.autogen.models.boosted_reorder_step import BoostedReorderStep
|
|
23
|
+
from shaped.autogen.models.exploration_reorder_step import ExplorationReorderStep
|
|
24
|
+
|
|
25
|
+
# Type definition for column ordering
|
|
26
|
+
class ColumnOrdering(TypedDict, total=False):
|
|
27
|
+
"""Type definition for column ordering in retrieve steps."""
|
|
28
|
+
name: str
|
|
29
|
+
ascending: Optional[bool]
|
|
30
|
+
nulls_first: Optional[bool]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Factory functions for creating step objects (no "Step" suffix for cleaner names)
|
|
34
|
+
|
|
35
|
+
def Passthrough(
|
|
36
|
+
name: Optional[str] = None,
|
|
37
|
+
) -> PassthroughScore:
|
|
38
|
+
"""
|
|
39
|
+
Create a passthrough score.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
name: Optional name for this score.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
PassthroughScore instance
|
|
46
|
+
"""
|
|
47
|
+
return PassthroughScore(
|
|
48
|
+
name=name,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def ColumnOrder(
|
|
53
|
+
columns: List[Union[Dict[str, Any], ColumnOrdering]],
|
|
54
|
+
limit: int = 100,
|
|
55
|
+
where: Optional[str] = None,
|
|
56
|
+
name: Optional[str] = None,
|
|
57
|
+
) -> ColumnOrderRetrieveStep:
|
|
58
|
+
"""
|
|
59
|
+
Create a column order retrieve step.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
columns: List of column orderings (dicts with 'name' and optionally
|
|
63
|
+
'ascending', 'nulls_first').
|
|
64
|
+
limit: Maximum number of candidates to retrieve.
|
|
65
|
+
where: Optional DuckDB filter expression.
|
|
66
|
+
name: Optional name for this retrieve step.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
ColumnOrderRetrieveStep instance
|
|
70
|
+
"""
|
|
71
|
+
return ColumnOrderRetrieveStep(
|
|
72
|
+
columns=columns,
|
|
73
|
+
limit=limit,
|
|
74
|
+
where=where,
|
|
75
|
+
name=name,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def TextSearch(
|
|
80
|
+
input_text_query: str,
|
|
81
|
+
mode: Dict[str, Any],
|
|
82
|
+
limit: int = 100,
|
|
83
|
+
where: Optional[str] = None,
|
|
84
|
+
name: Optional[str] = None,
|
|
85
|
+
) -> TextSearchRetrieveStep:
|
|
86
|
+
"""
|
|
87
|
+
Create a text search retrieve step.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
input_text_query: Text query parameter or value.
|
|
91
|
+
mode: Search mode dict with 'type' ('lexical' or 'vector') and
|
|
92
|
+
mode-specific fields.
|
|
93
|
+
limit: Maximum number of candidates to retrieve.
|
|
94
|
+
where: Optional DuckDB filter expression.
|
|
95
|
+
name: Optional name for this retrieve step.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
TextSearchRetrieveStep instance
|
|
99
|
+
"""
|
|
100
|
+
return TextSearchRetrieveStep(
|
|
101
|
+
input_text_query=input_text_query,
|
|
102
|
+
mode=mode,
|
|
103
|
+
limit=limit,
|
|
104
|
+
where=where,
|
|
105
|
+
name=name,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def Similarity(
|
|
110
|
+
embedding_ref: str,
|
|
111
|
+
query_encoder: Dict[str, Any],
|
|
112
|
+
limit: int = 100,
|
|
113
|
+
where: Optional[str] = None,
|
|
114
|
+
name: Optional[str] = None,
|
|
115
|
+
) -> SimilarityRetrieveStep:
|
|
116
|
+
"""
|
|
117
|
+
Create a similarity retrieve step.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
embedding_ref: Name of the embedding to use.
|
|
121
|
+
query_encoder: Encoder configuration dict.
|
|
122
|
+
limit: Maximum number of candidates to retrieve.
|
|
123
|
+
where: Optional DuckDB filter expression.
|
|
124
|
+
name: Optional name for this retrieve step.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
SimilarityRetrieveStep instance
|
|
128
|
+
"""
|
|
129
|
+
return SimilarityRetrieveStep(
|
|
130
|
+
embedding_ref=embedding_ref,
|
|
131
|
+
query_encoder=query_encoder,
|
|
132
|
+
limit=limit,
|
|
133
|
+
where=where,
|
|
134
|
+
name=name,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def CandidateIds(
|
|
138
|
+
item_ids: List[str],
|
|
139
|
+
limit: Optional[int] = None,
|
|
140
|
+
name: Optional[str] = None,
|
|
141
|
+
) -> CandidateIdsRetrieveStep:
|
|
142
|
+
"""
|
|
143
|
+
Create a candidate IDs retrieve step.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
item_ids: List of entity IDs to retrieve.
|
|
147
|
+
limit: Maximum number of IDs to retrieve (defaults to length of item_ids).
|
|
148
|
+
name: Optional name for this retrieve step.
|
|
149
|
+
|
|
150
|
+
Returns:
|
|
151
|
+
CandidateIdsRetrieveStep instance
|
|
152
|
+
"""
|
|
153
|
+
return CandidateIdsRetrieveStep(
|
|
154
|
+
item_ids=item_ids,
|
|
155
|
+
limit=limit,
|
|
156
|
+
name=name,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def CandidateAttributes(
|
|
161
|
+
item_attributes: List[Dict[str, Any]],
|
|
162
|
+
limit: Optional[int] = None,
|
|
163
|
+
name: Optional[str] = None,
|
|
164
|
+
) -> CandidateAttributesRetrieveStep:
|
|
165
|
+
"""
|
|
166
|
+
Create a candidate attributes retrieve step.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
item_attributes: List of item attribute dictionaries.
|
|
170
|
+
limit: Maximum number of items to retrieve.
|
|
171
|
+
name: Optional name for this retrieve step.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
CandidateAttributesRetrieveStep instance
|
|
175
|
+
"""
|
|
176
|
+
return CandidateAttributesRetrieveStep(
|
|
177
|
+
item_attributes=item_attributes,
|
|
178
|
+
limit=limit,
|
|
179
|
+
name=name,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def Filter(
|
|
184
|
+
where: Optional[str] = None,
|
|
185
|
+
limit: int = 100,
|
|
186
|
+
name: Optional[str] = None,
|
|
187
|
+
) -> FilterRetrieveStep:
|
|
188
|
+
"""
|
|
189
|
+
Create a filter retrieve step (filtering without ordering).
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
where: Optional DuckDB filter expression.
|
|
193
|
+
limit: Maximum number of candidates to retrieve.
|
|
194
|
+
name: Optional name for this retrieve step.
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
FilterRetrieveStep instance
|
|
198
|
+
"""
|
|
199
|
+
return FilterRetrieveStep(
|
|
200
|
+
where=where,
|
|
201
|
+
limit=limit,
|
|
202
|
+
name=name,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def Expression(
|
|
207
|
+
expression: str,
|
|
208
|
+
name: Optional[str] = None,
|
|
209
|
+
) -> ExpressionFilterStep:
|
|
210
|
+
"""
|
|
211
|
+
Create an expression filter step.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
expression: DuckDB filter expression.
|
|
215
|
+
name: Optional name for this filter step.
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
ExpressionFilterStep instance
|
|
219
|
+
"""
|
|
220
|
+
return ExpressionFilterStep(
|
|
221
|
+
expression=expression,
|
|
222
|
+
name=name,
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def Truncate(
|
|
227
|
+
max_length: int = 500,
|
|
228
|
+
name: Optional[str] = None,
|
|
229
|
+
) -> TruncateFilterStep:
|
|
230
|
+
"""
|
|
231
|
+
Create a truncate filter step.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
max_length: Maximum number of items to keep after truncation.
|
|
235
|
+
name: Optional name for this filter step.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
TruncateFilterStep instance
|
|
239
|
+
"""
|
|
240
|
+
return TruncateFilterStep(
|
|
241
|
+
max_length=max_length,
|
|
242
|
+
name=name,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def Prebuilt(
|
|
247
|
+
filter_ref: str,
|
|
248
|
+
input_user_id: Optional[str] = None,
|
|
249
|
+
name: Optional[str] = None,
|
|
250
|
+
) -> PrebuiltFilterStep:
|
|
251
|
+
"""
|
|
252
|
+
Create a prebuilt filter step.
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
filter_ref: Reference to a prebuilt filter (e.g., ref:data.filters:name).
|
|
256
|
+
input_user_id: User ID for personal filters. Required for personal,
|
|
257
|
+
omitted for global filters.
|
|
258
|
+
name: Optional name for this filter step.
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
PrebuiltFilterStep instance
|
|
262
|
+
"""
|
|
263
|
+
return PrebuiltFilterStep(
|
|
264
|
+
filter_ref=filter_ref,
|
|
265
|
+
input_user_id=input_user_id,
|
|
266
|
+
name=name,
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def ensemble(
|
|
271
|
+
value_model: str,
|
|
272
|
+
input_user_id: Optional[str] = None,
|
|
273
|
+
input_user_features: Optional[Dict[str, Any]] = None,
|
|
274
|
+
input_interactions_item_ids: Optional[List[str]] = None,
|
|
275
|
+
name: Optional[str] = None,
|
|
276
|
+
**kwargs: Any,
|
|
277
|
+
) -> ScoreEnsemble:
|
|
278
|
+
"""
|
|
279
|
+
Create a score ensemble step.
|
|
280
|
+
|
|
281
|
+
Args:
|
|
282
|
+
value_model: Name of the value model to use for scoring.
|
|
283
|
+
input_user_id: Optional user ID for personalization.
|
|
284
|
+
input_user_features: Optional dictionary of user features.
|
|
285
|
+
input_interactions_item_ids: Optional list of item IDs from user interactions.
|
|
286
|
+
name: Optional name for this score step.
|
|
287
|
+
**kwargs: Additional keyword arguments for backward compatibility.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
ScoreEnsemble instance
|
|
291
|
+
|
|
292
|
+
Example:
|
|
293
|
+
>>> ensemble("lightgbm", input_user_id="user123")
|
|
294
|
+
>>> ensemble("xgboost", name="xgboost_scorer")
|
|
295
|
+
"""
|
|
296
|
+
# Handle the case where parameters are passed in the options dictionary
|
|
297
|
+
if not input_user_id and 'input_user_id' in kwargs:
|
|
298
|
+
input_user_id = kwargs.pop('input_user_id')
|
|
299
|
+
if not input_user_features and 'input_user_features' in kwargs:
|
|
300
|
+
input_user_features = kwargs.pop('input_user_features')
|
|
301
|
+
if not input_interactions_item_ids and 'input_interactions_item_ids' in kwargs:
|
|
302
|
+
input_interactions_item_ids = kwargs.pop('input_interactions_item_ids')
|
|
303
|
+
if not name and 'name' in kwargs:
|
|
304
|
+
name = kwargs.pop('name')
|
|
305
|
+
|
|
306
|
+
# Create the config dictionary
|
|
307
|
+
config: Dict[str, Any] = {
|
|
308
|
+
'value_model': value_model,
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
# Add optional parameters if provided
|
|
312
|
+
if input_user_id is not None:
|
|
313
|
+
config['input_user_id'] = input_user_id
|
|
314
|
+
if input_user_features is not None:
|
|
315
|
+
config['input_user_features'] = input_user_features
|
|
316
|
+
if input_interactions_item_ids is not None:
|
|
317
|
+
config['input_interactions_item_ids'] = input_interactions_item_ids
|
|
318
|
+
if name is not None:
|
|
319
|
+
config['name'] = name
|
|
320
|
+
|
|
321
|
+
# Add any additional parameters from kwargs (for backward compatibility)
|
|
322
|
+
config.update(kwargs)
|
|
323
|
+
|
|
324
|
+
return ScoreEnsemble(**config)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def passthrough(name: Optional[str] = None) -> PassthroughScore:
|
|
328
|
+
"""
|
|
329
|
+
Create a passthrough score step that doesn't modify the scores.
|
|
330
|
+
|
|
331
|
+
Args:
|
|
332
|
+
name: Optional name for this score step.
|
|
333
|
+
|
|
334
|
+
Returns:
|
|
335
|
+
PassthroughScore instance
|
|
336
|
+
|
|
337
|
+
Example:
|
|
338
|
+
>>> passthrough()
|
|
339
|
+
>>> passthrough(name="noop_scorer")
|
|
340
|
+
"""
|
|
341
|
+
config: Dict[str, Any] = {
|
|
342
|
+
'type': 'passthrough',
|
|
343
|
+
}
|
|
344
|
+
if name is not None:
|
|
345
|
+
config['name'] = name
|
|
346
|
+
return PassthroughScore(**config)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def Boosted(
|
|
350
|
+
retriever: Dict[str, Any],
|
|
351
|
+
strength: float = 0.5,
|
|
352
|
+
name: Optional[str] = None,
|
|
353
|
+
) -> BoostedReorderStep:
|
|
354
|
+
"""
|
|
355
|
+
Create a boosted reorder step.
|
|
356
|
+
|
|
357
|
+
Args:
|
|
358
|
+
retriever: Retrieve step to use as source for boosting.
|
|
359
|
+
strength: Boost strength (0.0-1.0).
|
|
360
|
+
name: Optional name for this reorder step.
|
|
361
|
+
|
|
362
|
+
Returns:
|
|
363
|
+
BoostedReorderStep instance
|
|
364
|
+
"""
|
|
365
|
+
return BoostedReorderStep(
|
|
366
|
+
retriever=retriever,
|
|
367
|
+
strength=strength,
|
|
368
|
+
name=name,
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def Exploration(
|
|
373
|
+
retriever: Dict[str, Any],
|
|
374
|
+
strength: float = 0.5,
|
|
375
|
+
name: Optional[str] = None,
|
|
376
|
+
) -> ExplorationReorderStep:
|
|
377
|
+
"""
|
|
378
|
+
Create an exploration reorder step.
|
|
379
|
+
|
|
380
|
+
Args:
|
|
381
|
+
retriever: Retrieve step to use as source for exploration.
|
|
382
|
+
strength: Exploration strength (0.0-1.0).
|
|
383
|
+
name: Optional name for this reorder step.
|
|
384
|
+
|
|
385
|
+
Returns:
|
|
386
|
+
ExplorationReorderStep instance
|
|
387
|
+
"""
|
|
388
|
+
return ExplorationReorderStep(
|
|
389
|
+
retriever=retriever,
|
|
390
|
+
strength=strength,
|
|
391
|
+
name=name,
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def Diversity(
|
|
396
|
+
diversity_attributes: Optional[Union[List[str], str]] = None,
|
|
397
|
+
limit: int = 100,
|
|
398
|
+
name: Optional[str] = None,
|
|
399
|
+
strength: float = 0.5,
|
|
400
|
+
diversity_lookback_window: int = 30,
|
|
401
|
+
diversity_lookforward_window: int = 30,
|
|
402
|
+
text_encoding_embedding_ref: Optional[str] = None,
|
|
403
|
+
**kwargs
|
|
404
|
+
) -> DiversityReorderStep:
|
|
405
|
+
"""
|
|
406
|
+
Create a diversity reorder step.
|
|
407
|
+
|
|
408
|
+
Args:
|
|
409
|
+
diversity_attributes: List of attribute names to use for diversification.
|
|
410
|
+
limit: Maximum number of items to return after diversification.
|
|
411
|
+
name: Optional name for this reorder step.
|
|
412
|
+
strength: Strength of the diversity effect (0.0 to 1.0).
|
|
413
|
+
diversity_lookback_window: Number of previous items to consider for diversity.
|
|
414
|
+
diversity_lookforward_window: Number of next items to consider for diversity.
|
|
415
|
+
text_encoding_embedding_ref: Reference to text encoding embedding.
|
|
416
|
+
**kwargs: Additional keyword arguments for backward compatibility.
|
|
417
|
+
|
|
418
|
+
Returns:
|
|
419
|
+
DiversityReorderStep instance
|
|
420
|
+
"""
|
|
421
|
+
# Handle the case where diversity_attributes is passed as a single string
|
|
422
|
+
if isinstance(diversity_attributes, str):
|
|
423
|
+
diversity_attributes = [diversity_attributes]
|
|
424
|
+
|
|
425
|
+
# Handle the case where diversity_attributes is not provided but is in kwargs
|
|
426
|
+
if diversity_attributes is None:
|
|
427
|
+
if 'diversity_attribute' in kwargs:
|
|
428
|
+
diversity_attributes = [kwargs.pop('diversity_attribute')]
|
|
429
|
+
elif 'diversityAttributes' in kwargs:
|
|
430
|
+
diversity_attributes = kwargs.pop('diversityAttributes')
|
|
431
|
+
if isinstance(diversity_attributes, str):
|
|
432
|
+
diversity_attributes = [diversity_attributes]
|
|
433
|
+
|
|
434
|
+
# Create the config dictionary
|
|
435
|
+
config: Dict[str, Any] = {
|
|
436
|
+
'diversity_attributes': diversity_attributes or [],
|
|
437
|
+
'limit': limit,
|
|
438
|
+
'strength': strength,
|
|
439
|
+
'diversity_lookback_window': diversity_lookback_window,
|
|
440
|
+
'diversity_lookforward_window': diversity_lookforward_window,
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
# Add text_encoding_embedding_ref if provided
|
|
444
|
+
if text_encoding_embedding_ref is not None:
|
|
445
|
+
config['text_encoding_embedding_ref'] = text_encoding_embedding_ref
|
|
446
|
+
|
|
447
|
+
# Add name if provided
|
|
448
|
+
if name is not None:
|
|
449
|
+
config['name'] = name
|
|
450
|
+
|
|
451
|
+
# Add any additional parameters from kwargs (for backward compatibility)
|
|
452
|
+
config.update(kwargs)
|
|
453
|
+
|
|
454
|
+
return DiversityReorderStep(**config)
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
def _get_step_type(step: Any) -> Optional[str]:
|
|
458
|
+
"""Extract step type from step object or dict."""
|
|
459
|
+
if isinstance(step, dict):
|
|
460
|
+
return step.get("type")
|
|
461
|
+
if hasattr(step, "type"):
|
|
462
|
+
return getattr(step, "type")
|
|
463
|
+
return None
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
class RankQueryBuilder:
|
|
467
|
+
"""
|
|
468
|
+
Fluent builder for constructing RankQueryConfig queries.
|
|
469
|
+
|
|
470
|
+
Example:
|
|
471
|
+
from shaped import RankQueryBuilder, ColumnOrder, TextSearch, Filter, Expression, Ensemble, Diversity
|
|
472
|
+
|
|
473
|
+
query = (RankQueryBuilder()
|
|
474
|
+
.from_entity('item')
|
|
475
|
+
.retrieve(
|
|
476
|
+
ColumnOrder([{'name': 'popularity', 'ascending': False}], limit=1000),
|
|
477
|
+
TextSearch('laptop', mode={'type': 'vector', 'text_embedding_ref': 'text_emb'}),
|
|
478
|
+
Filter(where='category == "electronics"', limit=500)
|
|
479
|
+
)
|
|
480
|
+
.filter(Expression('price < 1000'))
|
|
481
|
+
.score(Ensemble('lightgbm', input_user_id='$parameters.userId'))
|
|
482
|
+
.reorder(Diversity(diversity_attributes=['category']))
|
|
483
|
+
.limit(50)
|
|
484
|
+
.columns(['item_id', 'title', 'price'])
|
|
485
|
+
.build())
|
|
486
|
+
"""
|
|
487
|
+
|
|
488
|
+
def __init__(self):
|
|
489
|
+
self._retrieve_steps: List[Union[Dict[str, Any], Any]] = []
|
|
490
|
+
self._filter_steps: List[Union[Dict[str, Any], Any]] = []
|
|
491
|
+
self._score_config: Optional[Union[Dict[str, Any], Any]] = None
|
|
492
|
+
self._reorder_steps: List[Union[Dict[str, Any], Any]] = []
|
|
493
|
+
self._from_entity: Optional[str] = None
|
|
494
|
+
self._limit: Optional[int] = None
|
|
495
|
+
self._columns: Optional[List[str]] = None
|
|
496
|
+
self._embeddings: Optional[List[str]] = None
|
|
497
|
+
|
|
498
|
+
def from_entity(self, entity: str) -> "RankQueryBuilder":
|
|
499
|
+
"""
|
|
500
|
+
Set the entity type to rank (item or user).
|
|
501
|
+
|
|
502
|
+
Args:
|
|
503
|
+
entity: Entity type, either 'item', 'user', or 'item_attribute'.
|
|
504
|
+
|
|
505
|
+
Returns:
|
|
506
|
+
Self for method chaining.
|
|
507
|
+
"""
|
|
508
|
+
if entity not in ["item", "user", "item_attribute"]:
|
|
509
|
+
raise ValueError(
|
|
510
|
+
f"Entity must be 'item', 'user', or 'item_attribute', got '{entity}'"
|
|
511
|
+
)
|
|
512
|
+
self._from_entity = entity
|
|
513
|
+
return self
|
|
514
|
+
|
|
515
|
+
def retrieve(
|
|
516
|
+
self, *steps: Union[
|
|
517
|
+
ColumnOrderRetrieveStep,
|
|
518
|
+
TextSearchRetrieveStep,
|
|
519
|
+
SimilarityRetrieveStep,
|
|
520
|
+
FilterRetrieveStep,
|
|
521
|
+
CandidateIdsRetrieveStep,
|
|
522
|
+
CandidateAttributesRetrieveStep,
|
|
523
|
+
Dict[str, Any],
|
|
524
|
+
]
|
|
525
|
+
) -> "RankQueryBuilder":
|
|
526
|
+
"""
|
|
527
|
+
Add one or more retrieve steps.
|
|
528
|
+
|
|
529
|
+
Args:
|
|
530
|
+
*steps: Retrieve step objects (ColumnOrder, TextSearch, Similarity,
|
|
531
|
+
Filter, CandidateIds, CandidateAttributes) or dicts.
|
|
532
|
+
|
|
533
|
+
Returns:
|
|
534
|
+
Self for method chaining.
|
|
535
|
+
"""
|
|
536
|
+
valid_types = {
|
|
537
|
+
"column_order",
|
|
538
|
+
"text_search",
|
|
539
|
+
"similarity",
|
|
540
|
+
"filter",
|
|
541
|
+
"candidate_ids",
|
|
542
|
+
"candidate_attributes",
|
|
543
|
+
}
|
|
544
|
+
for step in steps:
|
|
545
|
+
step_type = _get_step_type(step)
|
|
546
|
+
if step_type is None:
|
|
547
|
+
raise ValueError(
|
|
548
|
+
f"Invalid retrieve step: missing 'type' field. "
|
|
549
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
550
|
+
)
|
|
551
|
+
if step_type not in valid_types:
|
|
552
|
+
raise ValueError(
|
|
553
|
+
f"Invalid retrieve step type: '{step_type}'. "
|
|
554
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
555
|
+
)
|
|
556
|
+
self._retrieve_steps.append(step)
|
|
557
|
+
return self
|
|
558
|
+
|
|
559
|
+
def filter(
|
|
560
|
+
self,
|
|
561
|
+
*steps: Union[
|
|
562
|
+
ExpressionFilterStep,
|
|
563
|
+
TruncateFilterStep,
|
|
564
|
+
PrebuiltFilterStep,
|
|
565
|
+
Dict[str, Any],
|
|
566
|
+
],
|
|
567
|
+
) -> "RankQueryBuilder":
|
|
568
|
+
"""
|
|
569
|
+
Add one or more filter steps.
|
|
570
|
+
|
|
571
|
+
Args:
|
|
572
|
+
*steps: Filter step objects (Expression, Truncate, Prebuilt) or dicts.
|
|
573
|
+
|
|
574
|
+
Returns:
|
|
575
|
+
Self for method chaining.
|
|
576
|
+
|
|
577
|
+
Examples:
|
|
578
|
+
.filter(Expression('price < 1000'))
|
|
579
|
+
.filter(Truncate(max_length=100))
|
|
580
|
+
.filter(Prebuilt('ref:data.filters:my_filter', input_user_id='$parameters.userId'))
|
|
581
|
+
.filter(Expression('price > 100'), Truncate(max_length=500), Prebuilt('ref:data.filters:global'))
|
|
582
|
+
"""
|
|
583
|
+
valid_types = {"expression", "truncate", "prebuilt"}
|
|
584
|
+
|
|
585
|
+
for step in steps:
|
|
586
|
+
step_type = _get_step_type(step)
|
|
587
|
+
if step_type is None:
|
|
588
|
+
raise ValueError(
|
|
589
|
+
f"Invalid filter step: missing 'type' field. "
|
|
590
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
591
|
+
)
|
|
592
|
+
if step_type not in valid_types:
|
|
593
|
+
raise ValueError(
|
|
594
|
+
f"Invalid filter step type: '{step_type}'. "
|
|
595
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
596
|
+
)
|
|
597
|
+
self._filter_steps.append(step)
|
|
598
|
+
return self
|
|
599
|
+
|
|
600
|
+
def score(
|
|
601
|
+
self,
|
|
602
|
+
config: Union[ScoreEnsemble, PassthroughScore, Dict[str, Any]],
|
|
603
|
+
) -> "RankQueryBuilder":
|
|
604
|
+
"""
|
|
605
|
+
Set the score configuration.
|
|
606
|
+
|
|
607
|
+
Args:
|
|
608
|
+
config: Score configuration (Ensemble, Passthrough) or dict.
|
|
609
|
+
|
|
610
|
+
Returns:
|
|
611
|
+
Self for method chaining.
|
|
612
|
+
"""
|
|
613
|
+
valid_types = {"score_ensemble", "passthrough"}
|
|
614
|
+
step_type = _get_step_type(config)
|
|
615
|
+
if step_type is None:
|
|
616
|
+
raise ValueError(
|
|
617
|
+
f"Invalid score config: missing 'type' field. "
|
|
618
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
619
|
+
)
|
|
620
|
+
if step_type not in valid_types:
|
|
621
|
+
raise ValueError(
|
|
622
|
+
f"Invalid score config type: '{step_type}'. "
|
|
623
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
624
|
+
)
|
|
625
|
+
self._score_config = config
|
|
626
|
+
return self
|
|
627
|
+
|
|
628
|
+
def reorder(
|
|
629
|
+
self,
|
|
630
|
+
*steps: Union[
|
|
631
|
+
DiversityReorderStep,
|
|
632
|
+
BoostedReorderStep,
|
|
633
|
+
ExplorationReorderStep,
|
|
634
|
+
Dict[str, Any],
|
|
635
|
+
],
|
|
636
|
+
) -> "RankQueryBuilder":
|
|
637
|
+
"""
|
|
638
|
+
Add one or more reorder steps.
|
|
639
|
+
|
|
640
|
+
Args:
|
|
641
|
+
*steps: Reorder step objects (Diversity, Boosted, Exploration) or dicts.
|
|
642
|
+
|
|
643
|
+
Returns:
|
|
644
|
+
Self for method chaining.
|
|
645
|
+
"""
|
|
646
|
+
valid_types = {"diversity", "boosted", "exploration"}
|
|
647
|
+
for step in steps:
|
|
648
|
+
step_type = _get_step_type(step)
|
|
649
|
+
if step_type is None:
|
|
650
|
+
raise ValueError(
|
|
651
|
+
f"Invalid reorder step: missing 'type' field. "
|
|
652
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
653
|
+
)
|
|
654
|
+
if step_type not in valid_types:
|
|
655
|
+
raise ValueError(
|
|
656
|
+
f"Invalid reorder step type: '{step_type}'. "
|
|
657
|
+
f"Expected one of: {', '.join(sorted(valid_types))}"
|
|
658
|
+
)
|
|
659
|
+
self._reorder_steps.append(step)
|
|
660
|
+
return self
|
|
661
|
+
|
|
662
|
+
def limit(self, limit: int) -> "RankQueryBuilder":
|
|
663
|
+
"""
|
|
664
|
+
Set the maximum number of entities to return.
|
|
665
|
+
|
|
666
|
+
Args:
|
|
667
|
+
limit: Maximum number of entities.
|
|
668
|
+
|
|
669
|
+
Returns:
|
|
670
|
+
Self for method chaining.
|
|
671
|
+
"""
|
|
672
|
+
self._limit = limit
|
|
673
|
+
return self
|
|
674
|
+
|
|
675
|
+
def columns(self, columns: List[str]) -> "RankQueryBuilder":
|
|
676
|
+
"""
|
|
677
|
+
Set the list of column names to include in results.
|
|
678
|
+
|
|
679
|
+
Args:
|
|
680
|
+
columns: List of column names.
|
|
681
|
+
|
|
682
|
+
Returns:
|
|
683
|
+
Self for method chaining.
|
|
684
|
+
"""
|
|
685
|
+
self._columns = columns
|
|
686
|
+
return self
|
|
687
|
+
|
|
688
|
+
def embeddings(self, embeddings: List[str]) -> "RankQueryBuilder":
|
|
689
|
+
"""
|
|
690
|
+
Set the list of embedding names to include in results.
|
|
691
|
+
|
|
692
|
+
Args:
|
|
693
|
+
embeddings: List of embedding names.
|
|
694
|
+
|
|
695
|
+
Returns:
|
|
696
|
+
Self for method chaining.
|
|
697
|
+
"""
|
|
698
|
+
self._embeddings = embeddings
|
|
699
|
+
return self
|
|
700
|
+
|
|
701
|
+
def build(self) -> Union[RankQueryConfig, Dict[str, Any]]:
|
|
702
|
+
"""
|
|
703
|
+
Build the RankQueryConfig object.
|
|
704
|
+
|
|
705
|
+
Returns:
|
|
706
|
+
RankQueryConfig instance if models are available, otherwise a dict.
|
|
707
|
+
"""
|
|
708
|
+
config: Dict[str, Any] = {"type": "rank"}
|
|
709
|
+
|
|
710
|
+
if self._from_entity is not None:
|
|
711
|
+
config["from"] = self._from_entity
|
|
712
|
+
|
|
713
|
+
if self._retrieve_steps:
|
|
714
|
+
# Convert steps to dicts if they're model instances
|
|
715
|
+
retrieve_list = []
|
|
716
|
+
for step in self._retrieve_steps:
|
|
717
|
+
if isinstance(step, dict):
|
|
718
|
+
retrieve_list.append(step)
|
|
719
|
+
elif hasattr(step, "model_dump"):
|
|
720
|
+
retrieve_list.append(step.model_dump(by_alias=True))
|
|
721
|
+
else:
|
|
722
|
+
retrieve_list.append(step)
|
|
723
|
+
config["retrieve"] = retrieve_list
|
|
724
|
+
|
|
725
|
+
if self._filter_steps:
|
|
726
|
+
filter_list = []
|
|
727
|
+
for step in self._filter_steps:
|
|
728
|
+
if isinstance(step, dict):
|
|
729
|
+
filter_list.append(step)
|
|
730
|
+
elif hasattr(step, "model_dump"):
|
|
731
|
+
filter_list.append(step.model_dump(by_alias=True))
|
|
732
|
+
else:
|
|
733
|
+
filter_list.append(step)
|
|
734
|
+
config["filter"] = filter_list
|
|
735
|
+
|
|
736
|
+
if self._score_config is not None:
|
|
737
|
+
if isinstance(self._score_config, dict):
|
|
738
|
+
config["score"] = self._score_config
|
|
739
|
+
elif hasattr(self._score_config, "model_dump"):
|
|
740
|
+
config["score"] = self._score_config.model_dump(by_alias=True)
|
|
741
|
+
else:
|
|
742
|
+
config["score"] = self._score_config
|
|
743
|
+
|
|
744
|
+
if self._reorder_steps:
|
|
745
|
+
reorder_list = []
|
|
746
|
+
for step in self._reorder_steps:
|
|
747
|
+
if isinstance(step, dict):
|
|
748
|
+
reorder_list.append(step)
|
|
749
|
+
elif hasattr(step, "model_dump"):
|
|
750
|
+
reorder_list.append(step.model_dump(by_alias=True))
|
|
751
|
+
else:
|
|
752
|
+
reorder_list.append(step)
|
|
753
|
+
config["reorder"] = reorder_list
|
|
754
|
+
|
|
755
|
+
if self._limit is not None:
|
|
756
|
+
config["limit"] = self._limit
|
|
757
|
+
|
|
758
|
+
if self._columns is not None:
|
|
759
|
+
config["columns"] = self._columns
|
|
760
|
+
|
|
761
|
+
if self._embeddings is not None:
|
|
762
|
+
config["embeddings"] = self._embeddings
|
|
763
|
+
|
|
764
|
+
# If models are available, convert to proper model instance.
|
|
765
|
+
if RankQueryConfig is not None:
|
|
766
|
+
try:
|
|
767
|
+
return RankQueryConfig(**config)
|
|
768
|
+
except Exception:
|
|
769
|
+
# Fallback to dict if conversion fails
|
|
770
|
+
return config
|
|
771
|
+
|
|
772
|
+
return config
|
|
773
|
+
|
|
774
|
+
|