Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,8 +17,11 @@ from langchain_core.messages import HumanMessage
|
|
| 17 |
from langchain_openai import ChatOpenAI
|
| 18 |
import base64
|
| 19 |
from utils.mongoDB import MongoDBConnector
|
|
|
|
| 20 |
|
| 21 |
mongo = MongoDBConnector(os.getenv("mongo_db_conn"))
|
|
|
|
|
|
|
| 22 |
|
| 23 |
os.environ["AZURE_OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 24 |
os.environ["AZURE_OPENAI_API_VERSION"] = "2023-06-01-preview"
|
|
@@ -35,63 +38,65 @@ model = AzureChatOpenAI(
|
|
| 35 |
|
| 36 |
def choose_table(question):
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
-
model = AzureChatOpenAI(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
table_format = """
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
prompt = ChatPromptTemplate.from_template("Base on the question:{question},"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
chain = (
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
)
|
| 75 |
-
# query = 'select * from cdp_membership_summary;'
|
| 76 |
-
query = chain.invoke({"question": question, "table_format": table_format})
|
| 77 |
-
query = query.replace(f"`", '')
|
| 78 |
-
query = query.replace(f"sql", '')
|
| 79 |
-
query = query.split(';')[0] + f' where capsuite_ref = \'{capsuite_ref}\';'
|
| 80 |
-
df_data = pd.read_sql(query, engine)
|
| 81 |
-
print(f'*'*50)
|
| 82 |
-
print(f"Query: {query}")
|
| 83 |
-
|
| 84 |
-
if 'cdp_sale_order_line' in query:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print(f"Error while: {e}")
|
| 92 |
|
| 93 |
finally:
|
| 94 |
-
engine.dispose()
|
| 95 |
return df_data
|
| 96 |
|
| 97 |
|
|
|
|
| 17 |
from langchain_openai import ChatOpenAI
|
| 18 |
import base64
|
| 19 |
from utils.mongoDB import MongoDBConnector
|
| 20 |
+
from dags.utils.azure_blob import AzureBlob
|
| 21 |
|
| 22 |
mongo = MongoDBConnector(os.getenv("mongo_db_conn"))
|
| 23 |
+
azure_blob = AzureBlob(Variable.get("azure_blob_conn"))
|
| 24 |
+
ab = azure_blob
|
| 25 |
|
| 26 |
os.environ["AZURE_OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 27 |
os.environ["AZURE_OPENAI_API_VERSION"] = "2023-06-01-preview"
|
|
|
|
| 38 |
|
| 39 |
def choose_table(question):
|
| 40 |
try:
|
| 41 |
+
str_client_name = 'foodBeverageSample1'
|
| 42 |
+
df_data = pd.read_parquet(ab.get_latest_parquet('landing', str_client_name, 'sale', 'sol_'))
|
| 43 |
+
# connection_string = f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}/{db_name}'
|
| 44 |
+
# engine = create_engine(connection_string)
|
| 45 |
+
# capsuite_ref = 'foodBeverageSample1'
|
| 46 |
|
| 47 |
|
| 48 |
+
# model = AzureChatOpenAI(
|
| 49 |
+
# deployment_name="CapSuiteGPT4omini",
|
| 50 |
+
# openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
| 51 |
+
# )
|
| 52 |
|
| 53 |
+
# table_format = """
|
| 54 |
+
# 1.table name:cdp_sale_order,
|
| 55 |
+
# its columns:trxn_id,member_id,staff_id,subsidiary_name,staff_name,team_name,trxn_ref,trxn_channel,trxn_date,trxn_year,trxn_month,trxn_day,trxn_week,remark.
|
| 56 |
|
| 57 |
+
# 2.table name:cdp_sale_order_line,
|
| 58 |
+
# its columns:trxn_item_id,trxn_id,trxn_item_target_curr_unit_price,
|
| 59 |
+
# trxn_item_qty,trxn_item_discount_amt,trxn_original_net_currency,trxn_date,trxn_channel,staff_name,staff_id,member_id,display_name,pord_sku,prod_category,prod_type,prod_name,
|
| 60 |
+
# capsuite_ref.
|
| 61 |
|
| 62 |
+
# 3.table name:cdp_stock_quant,
|
| 63 |
+
# its columns:stock_quant_id,prod_id,location_id,stock_quantity,stock_quantity_reserved,stock_quant_create_date,capsuite_ref.
|
| 64 |
+
# """
|
| 65 |
+
|
| 66 |
+
# prompt = ChatPromptTemplate.from_template("Base on the question:{question},"
|
| 67 |
+
# "And the following table format:{table_format},"
|
| 68 |
+
# "Dont write a complex query. Only select statement like 'select * from table_name'."
|
| 69 |
+
# "Dont add any condition or filter to the query. The query should be generic and should return all the data from the table."
|
| 70 |
+
# "Select all the columns from the table. "
|
| 71 |
+
# "Only output one SQL Query without any other information even the '''sql''' prefix. ")
|
| 72 |
+
|
| 73 |
+
# chain = (
|
| 74 |
+
# {"question": RunnablePassthrough(), "table_format": RunnablePassthrough()}
|
| 75 |
+
# # {"table_format": RunnablePassthrough()}
|
| 76 |
+
# | prompt
|
| 77 |
+
# | model
|
| 78 |
+
# | StrOutputParser()
|
| 79 |
+
# )
|
| 80 |
+
# # query = 'select * from cdp_membership_summary;'
|
| 81 |
+
# query = chain.invoke({"question": question, "table_format": table_format})
|
| 82 |
+
# query = query.replace(f"`", '')
|
| 83 |
+
# query = query.replace(f"sql", '')
|
| 84 |
+
# query = query.split(';')[0] + f' where capsuite_ref = \'{capsuite_ref}\';'
|
| 85 |
+
# df_data = pd.read_sql(query, engine)
|
| 86 |
+
# print(f'*'*50)
|
| 87 |
+
# print(f"Query: {query}")
|
| 88 |
+
|
| 89 |
+
# if 'cdp_sale_order_line' in query:
|
| 90 |
+
df_data['sales_amount'] = df_data['trxn_item_target_curr_unit_price'].astype(float) * df_data['trxn_item_qty'].astype(float)
|
| 91 |
+
df_data.rename(columns={'trxn_item_target_curr_unit_price':'unit_price'}, inplace=True)
|
| 92 |
+
df_data.rename(columns={'display_name':'customer_name'}, inplace=True)
|
| 93 |
+
df_data = df_data[['trxn_item_id','trxn_id','sales_amount','unit_price','trxn_item_qty','trxn_item_discount_amt','trxn_date','trxn_channel','staff_name','customer_name','prod_category','prod_type','prod_name','capsuite_ref']]
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error while: {e}")
|
| 97 |
|
| 98 |
finally:
|
| 99 |
+
# engine.dispose()
|
| 100 |
return df_data
|
| 101 |
|
| 102 |
|