| import streamlit as st
|
| from langchain_community.chat_models import ChatOllama
|
| from langchain.schema import HumanMessage, AIMessage
|
| from langchain.memory import ConversationBufferMemory
|
| from langchain.chains import LLMChain
|
| from langchain.prompts import PromptTemplate
|
|
|
|
|
|
|
| st.set_page_config(layout="wide")
|
| st.markdown(
|
| "<h1 style='text-align: center; color: inherit;'>⎚-⎚ \nMikeyBot</h1>",
|
|
|
| unsafe_allow_html=True
|
| )
|
| st.markdown(
|
|
|
| """
|
| <style>
|
| .st-emotion-cache-4zpzjl {
|
| background-color: #bf498f;
|
| color: #ffffff;
|
| }
|
|
|
| .st-emotion-cache-jmw8un{
|
| background-color: #ffd1ec;
|
| color: #000000 ;
|
| }
|
|
|
| .st-emotion-cache-1k8897g{
|
| background-color: #d98dba;
|
| }
|
|
|
| </style>
|
| """, unsafe_allow_html=True)
|
|
|
|
|
| st.sidebar.header("⚙️ Settings")
|
|
|
|
|
| model_options = ["llama3.2", "deepseek-r1:1.5b"]
|
| MODEL = st.sidebar.selectbox("Choose a Model", model_options, index=0)
|
|
|
|
|
| with st.sidebar.expander("Advanced Settings"):
|
| temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
| top_p = st.slider("Top-P", 0.0, 1.0, 0.9)
|
| top_k = st.slider("Top-K", 1, 100, 40)
|
| max_tokens = st.slider("Max Tokens", 64, 2048, 512)
|
|
|
|
|
|
|
|
|
| MAX_HISTORY = 2
|
| CONTEXT_SIZE = 8192
|
|
|
|
|
| def clear_memory():
|
| st.session_state.chat_history = []
|
| st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
|
|
|
|
| if "prev_context_size" not in st.session_state or st.session_state.prev_context_size != CONTEXT_SIZE:
|
| clear_memory()
|
| st.session_state.prev_context_size = CONTEXT_SIZE
|
|
|
|
|
| if "chat_history" not in st.session_state:
|
| st.session_state.chat_history = []
|
|
|
| if "memory" not in st.session_state:
|
| st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
|
|
|
|
| llm = ChatOllama(
|
| model=MODEL,
|
| streaming=True,
|
| temperature=temperature,
|
| top_p=top_p,
|
| top_k=top_k,
|
| num_predict=max_tokens,
|
| num_ctx=CONTEXT_SIZE,
|
| )
|
|
|
|
|
| if st.sidebar.button("Summarize Chat"):
|
| with st.spinner("Summarizing..."):
|
|
|
| history_text = "\n".join(
|
| [f"{m['role']}: {m['content']}" for m in st.session_state.chat_history]
|
| )
|
|
|
|
|
| summary_prompt = [
|
| {"role": "system", "content": "You are a helpful assistant that summarizes conversations."},
|
| {"role": "user", "content": f"Please summarize this conversation:\n\n{history_text}"}
|
| ]
|
|
|
|
|
| summary_result = llm.invoke(summary_prompt[1]["content"])
|
| summary = summary_result.content if hasattr(summary_result, 'content') else str(summary_result)
|
|
|
|
|
| st.session_state.chat_history.append(
|
| {"role": "assistant", "content": summary}
|
| )
|
|
|
|
|
| with st.chat_message("assistant"):
|
| st.markdown(summary)
|
|
|
|
|
| prompt_template = PromptTemplate(
|
| input_variables=["history", "human_input"],
|
| template="{history}\nUser: {human_input}\nAssistant:"
|
| )
|
|
|
| chain = LLMChain(llm=llm, prompt=prompt_template, memory=st.session_state.memory)
|
|
|
|
|
| for msg in st.session_state.chat_history:
|
| with st.chat_message(msg["role"]):
|
| st.markdown(msg["content"])
|
|
|
|
|
| def trim_memory():
|
| while len(st.session_state.chat_history) > MAX_HISTORY * 2:
|
| st.session_state.chat_history.pop(0)
|
| if st.session_state.chat_history:
|
| st.session_state.chat_history.pop(0)
|
|
|
|
|
| if prompt := st.chat_input("Say something"):
|
|
|
| with st.chat_message("user"):
|
| st.markdown(prompt)
|
|
|
| st.session_state.chat_history.append({"role": "user", "content": prompt})
|
|
|
|
|
| trim_memory()
|
|
|
|
|
| with st.chat_message("assistant"):
|
| response_container = st.empty()
|
| full_response = ""
|
|
|
| for chunk in chain.stream({"human_input": prompt}):
|
| if isinstance(chunk, dict) and "text" in chunk:
|
| text_chunk = chunk["text"]
|
| full_response += text_chunk
|
| response_container.markdown(full_response)
|
|
|
|
|
| st.session_state.chat_history.append({"role": "assistant", "content": full_response})
|
|
|
|
|
| trim_memory()
|
|
|
|
|