Skip to content
Snippets Groups Projects
Commit ce9148ab authored by Ryan Melvin's avatar Ryan Melvin
Browse files

db writing

parent 7e5b97d2
No related branches found
No related tags found
1 merge request!2Docker db secrets
...@@ -9,8 +9,11 @@ from langchain.prompts import ( ...@@ -9,8 +9,11 @@ from langchain.prompts import (
SystemMessagePromptTemplate, SystemMessagePromptTemplate,
HumanMessagePromptTemplate, HumanMessagePromptTemplate,
) )
import pyodbc
import datetime
import os import os
# secrets
def manage_sensitive(name): def manage_sensitive(name):
secret_fpath = f'/run/secrets/{name}' secret_fpath = f'/run/secrets/{name}'
existence = os.path.exists(secret_fpath) existence = os.path.exists(secret_fpath)
...@@ -22,13 +25,34 @@ def manage_sensitive(name): ...@@ -22,13 +25,34 @@ def manage_sensitive(name):
if not existence: if not existence:
return KeyError(f'{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_API_KEY = manage_sensitive("openai_api_key")
# openai
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY 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( st.set_page_config(
page_title="MPOG Helper ", page_title="MPOG Helper ",
page_icon="🤖", page_icon="🤖",
) )
# hide streamlit branding
hide_streamlit_style = """ hide_streamlit_style = """
<style> <style>
#MainMenu {visibility: hidden;} #MainMenu {visibility: hidden;}
...@@ -37,6 +61,7 @@ hide_streamlit_style = """ ...@@ -37,6 +61,7 @@ hide_streamlit_style = """
""" """
st.markdown(hide_streamlit_style, unsafe_allow_html=True) st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# page content
st.title("🗃️ MPOG Feasibility Checker 🤖") st.title("🗃️ MPOG Feasibility Checker 🤖")
st.markdown(""" st.markdown("""
**Determine if your research idea is appropriate for the data in MPOG** **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 ...@@ -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._ _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"): ...@@ -133,5 +160,22 @@ with st.form(key="query_form"):
chat_prompt = ChatPromptTemplate.from_messages( chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]) [system_message_prompt, human_message_prompt])
with st.spinner("Thinking..."): with st.spinner("Thinking..."):
submit_time = datetime.datetime.now()
result = chat(chat_prompt.format_prompt(context=docs, question=query).to_messages()) result = chat(chat_prompt.format_prompt(context=docs, question=query).to_messages())
response_time = datetime.datetime.now()
st.markdown(result.content) 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.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment