medical_chatbot / app.py
jonathanjordan21's picture
Update app.py
0f238a0 verified
raw
history blame
3.58 kB
import streamlit as st
from langchain_community.llms import HuggingFaceTextGenInference
import os
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import StrOutputParser
# from datetime import datetime
from datetime import datetime, timezone, timedelta
from custom_llm import CustomLLM, custom_chain_with_history
from typing import Optional
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.chat_history import BaseChatMessageHistory
from langchain.memory import ConversationBufferMemory#, PostgresChatMessageHistory
import psycopg2
import urllib.parse as up
# os.environ['LANGCHAIN_TRACING_V2'] = "true"
API_TOKEN = os.getenv('HF_INFER_API')
@st.cache_resource
def get_llm_chain():
return custom_chain_with_history(
llm=CustomLLM(repo_id="AdaptLLM/medicine-LLM", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], temperature=0.001),
# llm=CustomLLM(repo_id="google/gemma-7b", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], temperature=0.001),
# memory=st.session_state.memory.chat_memory,
memory=st.session_state.memory
)
if 'memory' not in st.session_state:
st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
# st.session_state.memory = PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
# st.session_state.memory = get_memory()
st.session_state.memory.chat_memory.add_ai_message("Hello, I'm AI medical consultant. How can I help you today?")
# st.session_state.memory.add_ai_message("Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?")
if 'chain' not in st.session_state:
# st.session_state['chain'] = custom_chain_with_history(
# llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], temperature=0.001),
# memory=st.session_state.memory.chat_memory,
# # memory=st.session_state.memory
# )
st.session_state['chain'] = get_llm_chain()
st.title("AI Medical Consultation")
st.subheader("")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = [{"role":"assistant", "content":"Hello, I'm AI medical consultant. How can I help you today?"}]
# 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"])
# React to user input
if prompt := st.chat_input("Ask me anything.."):
# Display user message in chat message container
st.chat_message("User").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "User", "content": prompt})
response = st.session_state.chain.invoke({"question":prompt, "memory":st.session_state.memory}).split("\n<|")[0]
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# st.session_state.memory.add_user_message(prompt)
# st.session_state.memory.add_ai_message(response)
st.session_state.memory.save_context({"question":prompt}, {"output":response})
st.session_state.memory.chat_memory.messages = st.session_state.memory.chat_memory.messages[-15:]
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})