File size: 14,434 Bytes
2e2dda5
 
 
 
 
 
 
f725bfe
 
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475a4b0
 
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48ccdfb
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
48ccdfb
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475a4b0
2e2dda5
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import json
import os
import sys
import boto3
import amazon_rekognition
from botocore.config import Config
import getpass
#from nltk.stem import PorterStemmer
#from nltk.tokenize import word_tokenize
import os
import streamlit as st
from langchain.schema import Document
#from langchain_community.vectorstores import OpenSearchVectorSearch,ElasticsearchStore
from requests_aws4auth import AWS4Auth
from requests.auth import HTTPBasicAuth
from langchain.chains.query_constructor.base import (
    StructuredQueryOutputParser,
    get_query_constructor_prompt,
)
from langchain.retrievers.self_query.opensearch import OpenSearchTranslator
from langchain.chains import ConversationChain
from langchain.llms.bedrock import Bedrock
from langchain.memory import ConversationBufferMemory
from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts.prompt import PromptTemplate
from langchain.embeddings import BedrockEmbeddings
#from langchain.vectorstores import OpenSearchVectorSearch
from opensearchpy import OpenSearch, RequestsHttpConnection
import utilities.invoke_models as invoke_models



bedrock_params = {
    "max_tokens_to_sample":2048,
    "temperature":0.0001,
    "top_k":250,
    "top_p":1,
    "stop_sequences":["\\n\\nHuman:"]
}
bedrock_region="us-east-1"

#boto3_bedrock = boto3.client(service_name="bedrock-runtime", endpoint_url=f"https://bedrock-runtime.{bedrock_region}.amazonaws.com")
boto3_bedrock = boto3.client(service_name="bedrock-runtime", config=Config(region_name=bedrock_region))

bedrock_titan_llm = Bedrock(model_id="anthropic.claude-instant-v1", client=boto3_bedrock)
bedrock_titan_llm.model_kwargs = bedrock_params
bedrock_embeddings = BedrockEmbeddings(model_id='amazon.titan-embed-text-v1',client=boto3_bedrock)


schema = """{{
    "content": "Brief summary of a retail product",
    "attributes": {{
    "category": {{
        "description": "The category of the product, the available categories are apparel, footwear, outdoors, electronics, beauty, jewelry, accessories, housewares, homedecor, furniture, seasonal, floral, books, groceries, instruments, tools, hot dispensed, cold dispensed, food service and salty snacks",
        "type": "string"
    }},
    "gender_affinity": {{
        "description": "The gender that the product relates to, the choices are Male and Female", 
        "type": "string"
    }},
    "price": {{
        "description": "Cost of the product",
        "type": "double"
    }},
    "description": {{
        "description": "The detailed description of the product",
        "type": "string"
    }},
    "color": {{
        "description": "The color of the product",
        "type": "string"
    }},
     "caption": {{
        "description": "The short description of the product",
        "type": "string"
    }},
     "current_stock": {{
        "description": "The available quantity of the product in stock for sale",
        "type": "integer"
    }},
     "style": {{
        "description": "The style of the product",
        "type": "string"
    }}
}}
}}"""
metadata_field_info_ = [
    AttributeInfo(
        name="price",
        description="Cost of the product",
        type="string",
    ),
    AttributeInfo(
        name="style",
        description="The style of the product",
        type="string",
    ),
    AttributeInfo(
        name="category",
        description="The category of the product, the available categories are apparel, footwear, outdoors, electronics, beauty, jewelry, accessories, housewares, homedecor, furniture, seasonal, floral, books, groceries, instruments, tools, hot dispensed, cold dispensed, food service and salty snacks",
        type="string",
    ),
    AttributeInfo(
        name="current_stock",
        description="The available quantity of the product",
        type="string",
    ),
    AttributeInfo(
        name="gender_affinity", 
        description="The gender that the product relates to, the choices are Male and Female", 
        type="string"
    ),
    AttributeInfo(
        name="caption", 
        description="The short description of the product", 
        type="string"
    ),
    AttributeInfo(
        name="description", 
        description="The detailed description of the product", 
        type="string"
    ),
    AttributeInfo(
        name="color", 
        description="The color of the product", 
        type="string"
    )
]
document_content_description_ = "Brief summary of a retail product"

# open_search_vector_store = OpenSearchVectorSearch(
#                                     index_name="retail-ml-search-index",#"self-query-rewrite-retail",
#                                     embedding_function=bedrock_embeddings,
#                                     opensearch_url=os_domain_ep,
#                                     http_auth=auth
#                                     )  

examples = [
    {  "i":1,
        "data_source": schema,
        "user_query": "black shoes for men",
        "structured_request": """{{
    "query": "shoes",
    "filter": "and(eq(\"color\", \"black\"),  eq(\"category\", \"footwear\")), eq(\"gender_affinity\", \"Male\")"
            }}""",
    },
    
        {  "i":2,
        "data_source": schema,
        "user_query": "black or brown jackets for men under 50 dollars",
        "structured_request": """{{
    "query": "jackets",
    "filter": "and(eq(\"style\", \"jacket\"), or(eq(\"color\", \"brown\"),eq(\"color\", \"black\")),eq(\"category\", \"apparel\"),eq(\"gender_affinity\", \"male\"),lt(\"price\", \"50\"))"
            }}""",
    },
          {  "i":2,
        "data_source": schema,
        "user_query": "trendy handbags for women",
        "structured_request": """{{
    "query": "handbag",
    "filter": "and(eq(\"style\", \"bag\") ,eq(\"category\", \"accessories\"),eq(\"gender_affinity\", \"female\"))"
            }}""",
    }
]


example_prompt = PromptTemplate(
    input_variables=["question", "answer"], template="Question: {question}\n{answer}"
)
example_prompt=PromptTemplate(input_variables=['data_source', 'i', 'structured_request', 'user_query'],
template='<< Example {i}. >>\nData Source:\n{data_source}\n\nUser Query:\n{user_query}\n\nStructured Request:\n{structured_request}\n')
#print(example_prompt.format(**examples[0]))

prefix_ = """
Your goal is to structure the user's query to match the request schema provided below.

<< Structured Request Schema >>
When responding use a markdown code snippet with a JSON object formatted in the following schema:

```json
{{
    "query": string \ text string to compare to document contents
    "filter": string \ logical condition statement for filtering documents
}}
```

The query string should contain only text that is expected to match the contents of documents. Any conditions in the filter should not be mentioned in the query as well.

A logical condition statement is composed of one or more comparison and logical operation statements.

A comparison statement takes the form: `comp(attr, val)`:
- `comp` (eq | ne | gt | gte | lt | lte | contain | like | in | nin): comparator
- `attr` (string):  name of attribute to apply the comparison to
- `val` (string): is the comparison value

A logical operation statement takes the form `op(statement1, statement2, ...)`:
- `op` (and | or | not): logical operator
- `statement1`, `statement2`, ... (comparison statements or logical operation statements): one or more statements to apply the operation to

Make sure that you only use the comparators and logical operators listed above and no others.
Make sure that filters only refer to attributes that exist in the data source.
Make sure that filters only use the attributed names with its function names if there are functions applied on them.
Make sure that filters only use format `YYYY-MM-DD` when handling date data typed values.
Make sure that filters take into account the descriptions of attributes and only make comparisons that are feasible given the type of data being stored.
Make sure that filters are only used as needed. If there are no filters that should be applied return "NO_FILTER" for the filter value.
"""

suffix_ = """<< Example 3. >>
Data Source:
{schema}

User Query:
{query}

Structured Request:
"""

prompt_ = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix=suffix_,
    prefix=prefix_,
    input_variables=["query","schema"],
)





# retriever = SelfQueryRetriever.from_llm(
#     bedrock_titan_llm, open_search_vector_store, document_content_description_, metadata_field_info_, verbose=True
# )

# res = retriever.get_relevant_documents("bagpack for men")

# st.write(res)

######### use this for self query retriever ########
# prompt = get_query_constructor_prompt(
#     document_content_description_,
#     metadata_field_info_,
# )
# output_parser = StructuredQueryOutputParser.from_components()
# query_constructor = prompt | bedrock_titan_llm | output_parser

def get_new_query_res(query):
    field_map = {'Price':'price','Gender':'gender_affinity','Category':'category','Style':'style','Color':'color'}
    field_map_filter = {key: field_map[key] for key in st.session_state.input_must}
    if(query == ""):
        query = st.session_state.input_rekog_label
    if(st.session_state.input_is_rewrite_query == 'enabled'):

        res = invoke_models.invoke_llm_model( prompt_.format(query=query,schema = schema)  ,False)
        inter_query = res[7:-3].replace('\\"',"'").replace("\n","")
        print("inter_query")
        print(inter_query)
        query_struct = StructuredQueryOutputParser.from_components().parse(inter_query)  
        print("query_struct")
        print(query_struct)
        opts = OpenSearchTranslator()
        result_query_llm = opts.visit_structured_query(query_struct)[1]['filter']
        print(result_query_llm)
        draft_new_query = {'bool':{'should':[],'must':[]}}
        if('bool' in result_query_llm and ('must' in result_query_llm['bool'] or 'should' in result_query_llm['bool'])):
            #draft_new_query['bool']['should'] = []
            if('must' in result_query_llm['bool']):
                for q in result_query_llm['bool']['must']:
                    old_clause = list(q.keys())[0]
                    if(old_clause == 'term'):
                        new_clause = 'match'
                    else:
                        new_clause = old_clause
                    q_dash = {}
                    q_dash[new_clause] = {}
                    long_field = list(q[old_clause].keys())[0]
                    #print(long_field)
                    get_attr = long_field.split(".")[1]
                    #print(get_attr)
                    q_dash[new_clause][get_attr] = q[old_clause][long_field]
                    #print(q_dash)
                    if(get_attr in list(field_map_filter.values())):
                        draft_new_query['bool']['must'].append(q_dash)
                    else:
                        draft_new_query['bool']['should'].append(q_dash)

        query_ = draft_new_query
        
        
        ###### find the main subject of the query
        #imp_item = ""
        # if("bool" in query_ and  'should' in query_['bool']):
        #     for i in query_['bool']['should']:
        #         if("term" in i.keys()):
        #             if("metadata.category.keyword" in i["term"]):
        #                 imp_item = imp_item + i["term"]["metadata.category.keyword"]+ " "
        #             if("metadata.style.keyword" in i["term"]):
        #                 imp_item = imp_item + i["term"]["metadata.style.keyword"]+ " "
        #         if("match" in i.keys()):
        #             if("metadata.category.keyword" in i["match"]):
        #                 imp_item = imp_item + i["match"]["metadata.category.keyword"]+ " "
        #             if("metadata.style.keyword" in i["match"]):
        #                 imp_item = imp_item + i["match"]["metadata.style.keyword"]+ " "
        # else:
        #     if("term" in query_):
        #             if("metadata.category.keyword" in query_):
        #                 imp_item = imp_item + query_["metadata.category.keyword"] + " "
        #             if("metadata.style.keyword" in query_):
        #                 imp_item = imp_item + query_["metadata.style.keyword"]+ " "
        #     if("match" in query_):
        #             if("metadata.category.keyword" in query_):
        #                 imp_item = imp_item + query_["metadata.category.keyword"]+ " "
        #             if("metadata.style.keyword" in query_):
        #                 imp_item = imp_item + query_["metadata.style.keyword"]+ " "
        ###### find the main subject of the query
        imp_item = (opts.visit_structured_query(query_struct)[0]).replace(",","")
            
                    
        if(imp_item == ""):
            imp_item = query
            
        #ps = PorterStemmer()        
        # def stem_(sentence):
        #     words = word_tokenize(sentence)
            
        #     words_stem = ""

        #     for w in words:
        #         words_stem = words_stem +" "+ps.stem(w)
        #     return words_stem.strip()
        
        #imp_item = stem_(imp_item)
        print("imp_item---------------")
        print(imp_item)
        print(query_)
        if('must' in query_['bool']):
            query_['bool']['must'].append({
                    "simple_query_string": {
                    
                        "query": imp_item.strip(),
                      "fields":['description',"style","caption"]#'rekog_all^3'
                    
                    }
                    #"match":{"description":imp_item.strip()}
                })
        else:
            query_['bool']['must']={
                    "multi_match": {
                    
                        "query": imp_item.strip(),
                      "fields":['description',"style"]#'rekog_all^3'
                    
                    }
                    #"match":{"description":imp_item.strip()}
                }
       
            
        #query_['bool']["minimum_should_match"] = 1
            
        st.session_state.input_rewritten_query = {"query":query_}
        print(st.session_state.input_rewritten_query)