-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathquery.py
107 lines (97 loc) · 3.03 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from superlinked import framework as sl
from superlinked_app import constants, index
from superlinked_app.config import settings
assert (
settings.OPENAI_API_KEY
), "OPENAI_API_KEY must be set in environment variables to use natural language queries"
openai_config = sl.OpenAIClientConfig(
api_key=settings.OPENAI_API_KEY.get_secret_value(), model=settings.OPENAI_MODEL_ID
)
title_similar_param = sl.Param(
"query_title",
description=(
"The text in the user's query that is used to search in the products' title."
"Extract info that does not apply to other spaces or params."
),
)
text_similar_param = sl.Param(
"query_description",
description=(
"The text in the user's query that is used to search in the products' description."
" Extract info that does not apply to other spaces or params."
),
)
base_query = (
sl.Query(
index.product_index,
weights={
index.title_space: sl.Param("title_weight"),
index.description_space: sl.Param("description_weight"),
index.review_rating_maximizer_space: sl.Param(
"review_rating_maximizer_weight"
),
index.price_minimizer_space: sl.Param("price_minimizer_weights"),
},
)
.find(index.product)
.limit(sl.Param("limit"))
.with_natural_query(sl.Param("natural_query"), openai_config)
.filter(
index.product.type
== sl.Param(
"filter_by_type",
description="Used to only present items that have a specific type",
options=constants.TYPES,
)
)
)
filter_query = (
base_query.similar(
index.description_space,
text_similar_param,
sl.Param("description_similar_clause_weight"),
)
.filter(
index.product.category
== sl.Param(
"filter_by_cateogry",
description="Used to only present items that have a specific cateogry",
options=constants.CATEGORIES,
)
)
.filter(
index.product.review_rating
>= sl.Param(
"review_rating_bigger_than",
description="Used to find items with a review rating bigger than the provided number.",
)
)
.filter(
index.product.price
<= sl.Param(
"price_smaller_than",
description="Used to find items with a price smaller than the provided number.",
)
)
)
semantic_query = (
base_query.similar(
index.description_space,
text_similar_param,
sl.Param("description_similar_clause_weight"),
)
.similar(
index.title_space,
title_similar_param,
sl.Param("title_similar_clause_weight"),
)
.filter(
index.product.category
== sl.Param(
"filter_by_cateogry",
description="Used to only present items that have a specific cateogry",
options=constants.CATEGORIES,
)
)
)
similar_items_query = semantic_query.with_vector(index.product, sl.Param("product_id"))