File size: 1,826 Bytes
2ad184d be5cae8 3c67bbb 4adbe51 2ad184d 4adbe51 be5cae8 3c67bbb 2ad184d dedf405 4adbe51 3c67bbb 2ad184d 3c67bbb 4adbe51 3c67bbb 4adbe51 3c67bbb 4adbe51 3c67bbb 4adbe51 3c67bbb 4adbe51 3c67bbb 4adbe51 3c67bbb 4adbe51 435e461 4adbe51 |
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 |
import streamlit as st
from langchain.llms import LlamaCpp
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
# Streamlit page configuration
st.set_page_config(page_title="Simple AI Chatbot")
st.header("Simple AI Chatbot")
# Initialize the Language Model Chain
@st.experimental_singleton
def initialize_chain():
n_gpu_layers = 20
n_batch = 1024
llm = LlamaCpp(
model_path="models/mistral-7b-instruct-v0.1.Q5_0.gguf",
n_gpu_layers=n_gpu_layers,
n_batch=n_batch,
n_ctx=2048,
temperature=0,
verbose=False,
streaming=True,
)
# Setup memory for contextual conversation
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Initialize the conversational chain
chat_chain = ConversationalChain(llm=llm, memory=memory, verbose=False)
return chat_chain
llm_chain = initialize_chain()
if "messages" not in st.session_state:
st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
# Display conversation messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Handling user input
user_input = st.chat_input("Type your message...", key="user_input")
if user_input:
# Append user message to the conversation
st.session_state.messages.append({"role": "user", "content": user_input})
# Get response from the LLM
response = llm_chain.run(user_input)
# Append LLM response to the conversation
st.session_state.messages.append({"role": "assistant", "content": response})
# Update chat window with the assistant's response
with st.chat_message("assistant"):
st.markdown(response)
|