Skip to content

Commit

Permalink
healthcare initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ttscoff committed Jul 24, 2024
1 parent ffd06f6 commit 3ea57a8
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 0 deletions.
7 changes: 7 additions & 0 deletions healthcare/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 1. Preferably use python 3.8
# 2. Install the requirmements in your venv
# 3. upload the healthcare documents into the vector store in heatwave. Sample documents can be found in the folder healthcare_docs
# 4. install the hw_healthcare.sql file in the DB system.
### 4.1. Please make sure you are using the correct vector store DB and table names in the procedure hw_healthcare
# 5. Add the DB system user and pass in the mysql_helper file.
# 6. Run the code: `streamlit run heatwave_heathcare.py`
Binary file not shown.
Empty file added healthcare/genai_autopilot.log
Empty file.
Binary file not shown.
Binary file not shown.
33 changes: 33 additions & 0 deletions healthcare/heatwave_heathcare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from mysql_helper import run_mysql_queries
import streamlit as st


if __name__ == "__main__":

gif_path= "/home/opc/hw_hc/hw.png"
_left, mid, _right = st.columns(3)
with mid:
st.image(gif_path, width=250)

engine = st.radio("", ["HeatWave GenAI with vector store (containing medicine articles)", "HeatWave GenAI without vector store (base LLM, Mistral only)"])
question = st.text_area("Enter your question:", "", key="big-textbox", height=100, help="Ask questions about healthcare: Allergies.")
if st.button("Answer"):
if engine=="HeatWave GenAI with vector store (containing medicine articles)":
query=f"CALL hw_healthcare('{question}');"
answer= run_mysql_queries(query)
st.write(f'<h5 style="color:green;">Answer of the question using Heatwave with vector store and RAG:</h5>', unsafe_allow_html=True)
st.write(answer[0])
st.write("\n\n")
with st.container():
st.write(f'<h5 style="color:black; margin-bottom: 0; padding-bottom: 0;">References:</h5>', unsafe_allow_html=True)
st.text_area('',answer[1], height=120)

elif engine == "HeatWave GenAI without vector store (base LLM, Mistral only)":
query=f"CALL ml_generate('{question}');"
answer= run_mysql_queries(query)
st.write(f'<h5 style="color:brown;">Answer of the question using Heatwave without vector store:</h5>', unsafe_allow_html=True)
st.write(answer[0])




Binary file added healthcare/hw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions healthcare/hw_healthcare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
DELIMITER //

CREATE PROCEDURE get_unique_document_names()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE n INT;
DECLARE doc_name VARCHAR(5000);
DECLARE unique_doc_names TEXT DEFAULT '';
DECLARE counter INT DEFAULT 1;

SET n = JSON_LENGTH(@output, '$.citations');
SET unique_doc_names = "";

WHILE i < n DO
SET doc_name = JSON_UNQUOTE(JSON_EXTRACT(@output, CONCAT('$.citations[', i, '].document_name')));
IF POSITION(doc_name IN unique_doc_names) = 0 THEN
SET unique_doc_names = CONCAT(unique_doc_names, counter, '. ', doc_name, '\n\n');
SET counter = counter + 1;
END IF;
SET i = i + 1;
END WHILE;
SELECT TRIM(BOTH '\n' FROM unique_doc_names) AS llm_output;
END //

DELIMITER ;


DELIMITER //

CREATE PROCEDURE hw_healthcare(IN prompt VARCHAR(2048))
BEGIN

CALL sys.ML_MODEL_LOAD('mistral-7b-instruct-v1', NULL);

CALL sys.ML_RAG(prompt, @output,
'{"vector_store": ["`genai_health_db`.`health_vs3`"],
"n_citations": 5, "distance_metric": "COSINE",
"model_options": {"temperature": 0, "repeat_penalty": 1,
"top_p": 0.2, "max_tokens": 400 , "model_id": "mistral-7b-instruct-v1"}}');

SELECT JSON_UNQUOTE(JSON_EXTRACT(@output, '$.text'));
CALL get_unique_document_names();
END //

DELIMITER ;


DELIMITER //

CREATE PROCEDURE ml_generate(IN prompt VARCHAR(2048))
BEGIN

CALL sys.ML_MODEL_LOAD('mistral-7b-instruct-v1', NULL);
SELECT sys.ML_GENERATE(prompt, '{"task": "generation", "temperature": 0, "repeat_penalty": 1, "top_p": 0.2, "max_tokens": 400, "model_id": "mistral-7b-instruct-v1"}')
INTO @llm_output;
SELECT JSON_UNQUOTE(JSON_EXTRACT(@llm_output, '$.text'));
END //

DELIMITER ;
64 changes: 64 additions & 0 deletions healthcare/mysql_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# coding: utf-8
# Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
import mysql.connector
import os
import logging
import sys
import streamlit as st
USERNAME=""
PASSWORD=""
DEFAULT_HOST=""
PORT="3306"
DATABASE=""
def setup_logging():
logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger(__name__)
if not logger.hasHandlers():
file = logging.FileHandler('genai_autopilot.log')
file.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
file.setLevel(logging.INFO)
logger.addHandler(file)
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.WARNING)
logger.addHandler(console)
return logger

logger = setup_logging()

# Connection to the MySQL server
def mysql_connect(username=USERNAME, password=PASSWORD, host=DEFAULT_HOST, database=DATABASE, port=PORT, connection_timeout=1, repeat=5):
for i in range(repeat):
try:
return mysql.connector.connect(
user=username,
password=password,
host=host,
port=port,
autocommit=True,
database=database,
ssl_disabled=False,
use_pure=True,
connection_timeout=connection_timeout
)
except mysql.connector.Error as err:
logger.warning(f"Can't connect to the backend MySQL instance ({i}): {err}")


@st.cache_data
def run_mysql_queries(query):
output = []
try:
connection = mysql_connect()
cursor = connection.cursor()
for cursor_result in cursor.execute(query, multi=True):
for row in cursor_result:
if len(row) > 0:
output.append(row[0] if len(row) == 1 else row)
cursor.close()
connection.close()
return output
except Exception as e:
logger.info(f"Problem with query: {query}")
logger.warning(f"Problem with MySQL connection during execution: {str(e)}")


2 changes: 2 additions & 0 deletions healthcare/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mysql-connector-python
streamlit

0 comments on commit 3ea57a8

Please sign in to comment.