diff --git a/app.py b/app.py index 728e70417190138470251f05c67ad9ef8342a6b0..c8068873dd9bb70bf1dc6180aff00d411f223f22 100644 --- a/app.py +++ b/app.py @@ -9,8 +9,11 @@ from langchain.prompts import ( SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) +import pyodbc +import datetime import os +# secrets def manage_sensitive(name): secret_fpath = f'/run/secrets/{name}' existence = os.path.exists(secret_fpath) @@ -22,13 +25,34 @@ def manage_sensitive(name): if not existence: return KeyError(f'{name}') +DB_SERVER = manage_sensitive("db_server") +DB_NAME = manage_sensitive("db_name") +DB_USER = manage_sensitive("db_user") +DB_PASSWORD = manage_sensitive("db_password") OPENAI_API_KEY = manage_sensitive("openai_api_key") + +# openai os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY +# db connection +conn_str = ( + "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + + DB_SERVER + + ";DATABASE=" + + DB_NAME + + ";UID=" + + DB_USER + + ";PWD=" + + DB_PASSWORD +) +conn = pyodbc.connect(conn_str) + +# page metadata st.set_page_config( page_title="MPOG Helper ", page_icon="🤖", ) +# hide streamlit branding hide_streamlit_style = """ <style> #MainMenu {visibility: hidden;} @@ -37,6 +61,7 @@ hide_streamlit_style = """ """ st.markdown(hide_streamlit_style, unsafe_allow_html=True) +# page content st.title("ðŸ—ƒï¸ MPOG Feasibility Checker 🤖") st.markdown(""" **Determine if your research idea is appropriate for the data in MPOG** @@ -45,6 +70,8 @@ Brought to you by the Anesthesiology MPOG Steering Committee, Informatics, and D _Not affiliated with MPOG. Uses external resources. Not approved for use with PHI or sensitive data._ +All submissions are recorded for potential review by the MPOG Steering Committee. + --- """) @@ -133,5 +160,22 @@ with st.form(key="query_form"): chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt]) with st.spinner("Thinking..."): + submit_time = datetime.datetime.now() result = chat(chat_prompt.format_prompt(context=docs, question=query).to_messages()) + response_time = datetime.datetime.now() st.markdown(result.content) + + try: + with conn: + cursor = conn.cursor() + query = """ + INSERT INTO [DS_apps].[dbo].[mpog_helper] (user_input, llm_response, request_sent, response_received) + VALUES (?, ?, ?) + """ + + current_time = datetime.datetime.now() + cursor.execute(query, (query, result.content, submit_time, response_time)) + + st.success("Your idea has been recorded and may be reviewed by the MPOG Steering Committee.") + except Exception as e: + st.error("Something went wrong, and your idea was not recorded for review by the MPOG Steering Committee.")