Spaces:
Sleeping
Sleeping
pragneshbarik
commited on
Commit
•
ebbf860
1
Parent(s):
fb6ef21
removed web retrieval
Browse files- components/generate_chat_stream.py +75 -1
- middlewares/utils.py +0 -69
components/generate_chat_stream.py
CHANGED
@@ -2,6 +2,78 @@ import streamlit as st
|
|
2 |
from middlewares.utils import gen_augmented_prompt_via_websearch
|
3 |
from middlewares.chat_client import chat
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def generate_chat_stream(session_state, query, config):
|
7 |
# 1. augments prompt according to the template
|
@@ -23,8 +95,10 @@ def generate_chat_stream(session_state, query, config):
|
|
23 |
pass_prev=session_state.pass_prev,
|
24 |
prev_output=session_state.history[-1][1],
|
25 |
)
|
|
|
|
|
26 |
|
27 |
with st.spinner("Generating response..."):
|
28 |
-
chat_stream = chat(session_state, query, config)
|
29 |
|
30 |
return chat_stream, links
|
|
|
2 |
from middlewares.utils import gen_augmented_prompt_via_websearch
|
3 |
from middlewares.chat_client import chat
|
4 |
|
5 |
+
from pprint import pformat
|
6 |
+
from notion_client import Client
|
7 |
+
|
8 |
+
def safe_get(data, dot_chained_keys):
|
9 |
+
'''
|
10 |
+
{'a': {'b': [{'c': 1}]}}
|
11 |
+
safe_get(data, 'a.b.0.c') -> 1
|
12 |
+
'''
|
13 |
+
keys = dot_chained_keys.split('.')
|
14 |
+
for key in keys:
|
15 |
+
try:
|
16 |
+
if isinstance(data, list):
|
17 |
+
data = data[int(key)]
|
18 |
+
else:
|
19 |
+
data = data[key]
|
20 |
+
except (KeyError, TypeError, IndexError):
|
21 |
+
return None
|
22 |
+
return data
|
23 |
+
|
24 |
+
def get_notion_data() :
|
25 |
+
integration_token = "secret_lTOe0q9dqqKQLRRb2KJwi7QFSl0vqoztroRFHW6MeQE"
|
26 |
+
notion_database_id = "6c0d877b823a4e3699016fa7083f3006"
|
27 |
+
|
28 |
+
client = Client(auth=integration_token)
|
29 |
+
|
30 |
+
first_db_rows = client.databases.query(notion_database_id)
|
31 |
+
rows = []
|
32 |
+
|
33 |
+
|
34 |
+
for row in first_db_rows['results']:
|
35 |
+
price = safe_get(row, 'properties.($) Per Unit.number')
|
36 |
+
store_link = safe_get(row, 'properties.Store Link.url')
|
37 |
+
supplier_email = safe_get(row, 'properties.Supplier Email.email')
|
38 |
+
exp_del = safe_get(row, 'properties.Expected Delivery.date')
|
39 |
+
|
40 |
+
collections = safe_get(row, 'properties.Collection.multi_select')
|
41 |
+
collection_names = []
|
42 |
+
for collection in collections :
|
43 |
+
collection_names.append(collection['name'])
|
44 |
+
|
45 |
+
status = safe_get(row, 'properties.Status.select.name')
|
46 |
+
sup_phone = safe_get(row, 'properties.Supplier Phone.phone_number')
|
47 |
+
stock_alert = safe_get(row, 'properties.Stock Alert.status.name')
|
48 |
+
prod_name = safe_get(row, 'properties.Product .title.0.text.content')
|
49 |
+
sku = safe_get(row, 'properties.SKU.number')
|
50 |
+
shipped_date = safe_get(row, 'properties.Shipped On.date')
|
51 |
+
on_order = safe_get(row, 'properties.On Order.number')
|
52 |
+
on_hand = safe_get(row, 'properties.On Hand.number')
|
53 |
+
size_names = []
|
54 |
+
sizes = safe_get(row, 'properties.Size.multi_select')
|
55 |
+
for size in sizes :
|
56 |
+
size_names.append(size['name'])
|
57 |
+
|
58 |
+
rows.append({
|
59 |
+
'Price Per unit': price,
|
60 |
+
'Store Link' : store_link,
|
61 |
+
'Supplier Email' : supplier_email,
|
62 |
+
'Expected Delivery' : exp_del,
|
63 |
+
'Collection' : collection_names,
|
64 |
+
'Status' : status,
|
65 |
+
'Supplier Phone' : sup_phone,
|
66 |
+
'Stock Alert' : stock_alert,
|
67 |
+
'Product Name' : prod_name,
|
68 |
+
'SKU' : sku,
|
69 |
+
'Sizes' : size_names,
|
70 |
+
'Shipped Date' : shipped_date,
|
71 |
+
'On Order' : on_order,
|
72 |
+
"On Hand" : on_hand,
|
73 |
+
})
|
74 |
+
|
75 |
+
notion_data_string = json.dumps(rows)
|
76 |
+
return notion_data_string
|
77 |
|
78 |
def generate_chat_stream(session_state, query, config):
|
79 |
# 1. augments prompt according to the template
|
|
|
95 |
pass_prev=session_state.pass_prev,
|
96 |
prev_output=session_state.history[-1][1],
|
97 |
)
|
98 |
+
|
99 |
+
notion_data = get_notion_data()
|
100 |
|
101 |
with st.spinner("Generating response..."):
|
102 |
+
chat_stream = chat(session_state, notion_data + " " + query , config)
|
103 |
|
104 |
return chat_stream, links
|
middlewares/utils.py
CHANGED
@@ -23,75 +23,6 @@ googleSearchClient = SearchClient(
|
|
23 |
)
|
24 |
bingSearchClient = SearchClient("bing", api_key=BING_SEARCH_API_KEY, engine_id=None)
|
25 |
|
26 |
-
def safe_get(data, dot_chained_keys):
|
27 |
-
'''
|
28 |
-
{'a': {'b': [{'c': 1}]}}
|
29 |
-
safe_get(data, 'a.b.0.c') -> 1
|
30 |
-
'''
|
31 |
-
keys = dot_chained_keys.split('.')
|
32 |
-
for key in keys:
|
33 |
-
try:
|
34 |
-
if isinstance(data, list):
|
35 |
-
data = data[int(key)]
|
36 |
-
else:
|
37 |
-
data = data[key]
|
38 |
-
except (KeyError, TypeError, IndexError):
|
39 |
-
return None
|
40 |
-
return data
|
41 |
-
|
42 |
-
def get_notion_data() :
|
43 |
-
integration_token = "secret_lTOe0q9dqqKQLRRb2KJwi7QFSl0vqoztroRFHW6MeQE"
|
44 |
-
notion_database_id = "6c0d877b823a4e3699016fa7083f3006"
|
45 |
-
|
46 |
-
client = Client(auth=integration_token)
|
47 |
-
|
48 |
-
first_db_rows = client.databases.query(notion_database_id)
|
49 |
-
rows = []
|
50 |
-
|
51 |
-
|
52 |
-
for row in first_db_rows['results']:
|
53 |
-
price = safe_get(row, 'properties.($) Per Unit.number')
|
54 |
-
store_link = safe_get(row, 'properties.Store Link.url')
|
55 |
-
supplier_email = safe_get(row, 'properties.Supplier Email.email')
|
56 |
-
exp_del = safe_get(row, 'properties.Expected Delivery.date')
|
57 |
-
|
58 |
-
collections = safe_get(row, 'properties.Collection.multi_select')
|
59 |
-
collection_names = []
|
60 |
-
for collection in collections :
|
61 |
-
collection_names.append(collection['name'])
|
62 |
-
|
63 |
-
status = safe_get(row, 'properties.Status.select.name')
|
64 |
-
sup_phone = safe_get(row, 'properties.Supplier Phone.phone_number')
|
65 |
-
stock_alert = safe_get(row, 'properties.Stock Alert.status.name')
|
66 |
-
prod_name = safe_get(row, 'properties.Product .title.0.text.content')
|
67 |
-
sku = safe_get(row, 'properties.SKU.number')
|
68 |
-
shipped_date = safe_get(row, 'properties.Shipped On.date')
|
69 |
-
on_order = safe_get(row, 'properties.On Order.number')
|
70 |
-
on_hand = safe_get(row, 'properties.On Hand.number')
|
71 |
-
size_names = []
|
72 |
-
sizes = safe_get(row, 'properties.Size.multi_select')
|
73 |
-
for size in sizes :
|
74 |
-
size_names.append(size['name'])
|
75 |
-
|
76 |
-
rows.append({
|
77 |
-
'Price Per unit': price,
|
78 |
-
'Store Link' : store_link,
|
79 |
-
'Supplier Email' : supplier_email,
|
80 |
-
'Expected Delivery' : exp_del,
|
81 |
-
'Collection' : collection_names,
|
82 |
-
'Status' : status,
|
83 |
-
'Supplier Phone' : sup_phone,
|
84 |
-
'Stock Alert' : stock_alert,
|
85 |
-
'Product Name' : prod_name,
|
86 |
-
'SKU' : sku,
|
87 |
-
'Sizes' : size_names,
|
88 |
-
'Shipped Date' : shipped_date,
|
89 |
-
'On Order' : on_order,
|
90 |
-
"On Hand" : on_hand,
|
91 |
-
})
|
92 |
-
|
93 |
-
notion_data_string = json.dumps(rows)
|
94 |
-
return notion_data_string
|
95 |
|
96 |
|
97 |
|
|
|
23 |
)
|
24 |
bingSearchClient = SearchClient("bing", api_key=BING_SEARCH_API_KEY, engine_id=None)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
|