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)