File size: 2,815 Bytes
2b67ae3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import streamlit as st
from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    StorageContext,
    load_index_from_storage,
)
from dotenv import load_dotenv
import openai

# Load environment variables
load_dotenv()

# Set OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")

# Define the storage directory
PERSIST_DIR = "./storage"

# Check if storage already exists and load or create the index
if not os.path.exists(PERSIST_DIR):
    # Load the documents and create the index
    documents = SimpleDirectoryReader(
        "data",
        exclude_hidden=False,
    ).load_data()
    index = VectorStoreIndex.from_documents(documents)
    # Store it for later
    index.storage_context.persist(persist_dir=PERSIST_DIR)
else:
    # Load the existing index
    storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
    index = load_index_from_storage(storage_context)

# Create a QueryEngine for Retrieval & Augmentation
query_engine = index.as_query_engine()

# Streamlit app
st.title("Conversational Medical Chat Assistant")

def get_medical_llm_response(query):
    # Generate response from the specialized medical LLM
    response = openai.chat.completions.create(
        model="gpt-3.5-turbo",  # Assuming this is a more evolved model suited for medical queries
        messages=[
            {"role": "system", "content": "You are an expert in Homeopathic treatment with advanced training on medicine and diagnosis."},
            {"role": "user", "content": query}
        ]
    )
    return response.choices[0].message.content.strip()


# Initialize session state for chat history
if 'messages' not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Get user input
if user_input := st.chat_input("Describe your symptoms or ask a medical question:"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": user_input})
    with st.chat_message("user"):
        st.markdown(user_input)

    with st.spinner('Generating response...'):
        # Get the RAG-based response
        rag_response = query_engine.query(user_input).response
        # Combine RAG response with LLM response
        combined_query = f"Based on the following information, provide a comprehensive response:\n\n{rag_response}\n\nUser's query: {user_input}"
        llm_response = get_medical_llm_response(combined_query)

        # Add assistant message to chat history
        st.session_state.messages.append({"role": "assistant", "content": llm_response})
        with st.chat_message("assistant"):
            st.markdown(llm_response)