diff --git a/W7LJpoO-_400x400.png b/W7LJpoO-_400x400.png deleted file mode 100644 index 2f6f56b62a42c87f6a73ca0f078229bb893bb7bd..0000000000000000000000000000000000000000 Binary files a/W7LJpoO-_400x400.png and /dev/null differ diff --git a/app.py b/app.py index 728e70417190138470251f05c67ad9ef8342a6b0..ac0544a96c16d683ed6d503d371cadf07195ef50 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) @@ -20,15 +23,38 @@ def manage_sensitive(name): return v2 if not existence: + print(name) 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 +def get_db_connection(): + conn_str = ( + "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + + DB_SERVER + + ";DATABASE=" + + DB_NAME + + ";UID=" + + DB_USER + + ";PWD=" + + DB_PASSWORD + ) + return 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 +63,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 +72,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. + --- """) @@ -65,11 +94,11 @@ vectordb = FAISS.load_local("faiss_index", embedding) docsearch = vectordb.as_retriever() with st.form(key="query_form"): - query = st.text_input("Enter your research question:") + user_question = st.text_input("Enter your research question:") submit_button = st.form_submit_button("Submit") if submit_button: - docs = docsearch.get_relevant_documents(query) + docs = docsearch.get_relevant_documents(user_question) template="""You are a helpful assistant that tells a researcher whether their idea is appropriate given the data in the MPOG database. The MPOG database contains the following data elements that may be relevant to the researcher's ideas. @@ -133,5 +162,22 @@ with st.form(key="query_form"): chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt]) with st.spinner("Thinking..."): - result = chat(chat_prompt.format_prompt(context=docs, question=query).to_messages()) + submit_time = datetime.datetime.now() + result = chat(chat_prompt.format_prompt(context=docs, question=user_question).to_messages()) + response_time = datetime.datetime.now() st.markdown(result.content) + + try: + with get_db_connection() as conn: + cursor = conn.cursor() + query = """ + INSERT INTO [dbo].[mpog_helper] (user_input, llm_response, request_sent, response_received) + VALUES (?, ?, ?, ?) + """ + + cursor.execute(query, (user_question, result.content, submit_time, response_time)) + + st.success("Your idea has also 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. Give the following message when asking for help.") + st.error(e) diff --git a/db_test.py b/db_test.py deleted file mode 100644 index 35b2855ac8adc4905b632b0f98878ad79c260341..0000000000000000000000000000000000000000 --- a/db_test.py +++ /dev/null @@ -1,78 +0,0 @@ -import streamlit as st -import pyodbc -import os - -def manage_sensitive(name): - secret_fpath = f'/run/secrets/{name}' - existence = os.path.exists(secret_fpath) - - if existence: - v2 = open(secret_fpath).read().rstrip('\n') - return v2 - - 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") - -os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY - -# Initialize db connection. -# Uses st.cache_resource to only run once. -@st.cache_resource -def init_connection(): - return pyodbc.connect( - "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" - + DB_SERVER - + ";DATABASE=" - + DB_NAME - + ";UID=" - + DB_USER - + ";PWD=" - + DB_PASSWORD - ) - -if st.button("test"): - conn = init_connection() - - # Perform query. - # Uses st.cache_data to only rerun when the query changes or after 10 min. - @st.cache_data(ttl=600) - def run_query(query): - with conn.cursor() as cur: - cur.execute(query) - return cur.fetchall() - - rows = run_query("SELECT TOP 2 * from DS_apps.dbo.test_table;") - run_query("""INSERT INTO DS_apps.dbo.test_table (name, age, city) - VALUES ('Emma Brown', 40, 'San Francisco'), - ('Oliver Davis', 25, 'Seattle'), - ('Sophia Wilson', 32, 'Austin'), - ('James Taylor', 45, 'Boston');""") - # Print results. - for row in rows: - st.write(f"{row[1]}") - - conn.close() - # write - # Insert more example rows into DS_apps.dbo.test_table - - # cursor = conn.cursor() - - # # Insert a single example row into DS_apps.dbo.test_table - # insert_query = """ - # INSERT INTO DS_apps.dbo.test_table (name, age, city) - # VALUES ('Emily White', 31, 'San Diego'); - # """ - # cursor.execute(insert_query) - # conn.commit() - - # # Close the cursor and connection - # cursor.close() - # conn.close() - - # print("Data inserted successfully.") \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d98a77070b519ee06fc2ac60df3bad40ff2521cb..c60279fa0f816bc17ea678299f408862a169d6e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,19 @@ services: environment: - PYTHONUNBUFFERED=1 secrets: - - openai_api_key + - db_server + - db_name + - db_user + - db_password + - openai_api_key secrets: + db_server: + file: secrets/db_server.txt + db_name: + file: secrets/db_name.txt + db_user: + file: secrets/db_user.txt + db_password: + file: secrets/db_password.txt openai_api_key: file: secrets/openai_api_key.txt