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

Merge branch 'docker_db_secrets' into 'main'

Docker db secrets

See merge request !2
parents b1ea2a79 e20a0da9
No related branches found
No related tags found
1 merge request!2Docker db secrets
W7LJpoO-_400x400.png

34.1 KiB

...@@ -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)
...@@ -20,15 +23,38 @@ def manage_sensitive(name): ...@@ -20,15 +23,38 @@ def manage_sensitive(name):
return v2 return v2
if not existence: if not existence:
print(name)
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
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( 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 +63,7 @@ hide_streamlit_style = """ ...@@ -37,6 +63,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 +72,8 @@ Brought to you by the Anesthesiology MPOG Steering Committee, Informatics, and D ...@@ -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._ _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) ...@@ -65,11 +94,11 @@ vectordb = FAISS.load_local("faiss_index", embedding)
docsearch = vectordb.as_retriever() docsearch = vectordb.as_retriever()
with st.form(key="query_form"): 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") submit_button = st.form_submit_button("Submit")
if submit_button: 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. 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. 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"): ...@@ -133,5 +162,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..."):
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) 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)
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
...@@ -10,7 +10,19 @@ services: ...@@ -10,7 +10,19 @@ services:
environment: environment:
- PYTHONUNBUFFERED=1 - PYTHONUNBUFFERED=1
secrets: secrets:
- openai_api_key - db_server
- db_name
- db_user
- db_password
- openai_api_key
secrets: 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: openai_api_key:
file: secrets/openai_api_key.txt file: secrets/openai_api_key.txt
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