Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mpog-helper-openai
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anes_AI
mpog-helper-openai
Commits
ce9148ab
Commit
ce9148ab
authored
2 years ago
by
Ryan Melvin
Browse files
Options
Downloads
Patches
Plain Diff
db writing
parent
7e5b97d2
No related branches found
No related tags found
1 merge request
!2
Docker db secrets
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app.py
+44
-0
44 additions, 0 deletions
app.py
with
44 additions
and
0 deletions
app.py
+
44
−
0
View file @
ce9148ab
...
@@ -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.
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment