import streamlit as st import os from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder from langchain_google_genai import ChatGoogleGenerativeAI from langchain.memory import ConversationBufferWindowMemory from operator import itemgetter from langchain_core.runnables import RunnableLambda, RunnablePassthrough os.environ['GOOGLE_API_KEY'] = "AIzaSyClJ9WDy55XBfYGhlXXDjZUdd9H_moqi4c" prompt = ChatPromptTemplate.from_messages( [ ('system', 'you are a good assistant.'), MessagesPlaceholder(variable_name='history'), ("human", "{input}") ] ) if 'memory' not in st.session_state: st.session_state.memory = ConversationBufferWindowMemory(k=10, return_messages=True) chain = (RunnablePassthrough.assign(history=RunnableLambda(st.session_state.memory.load_memory_variables) | itemgetter("history")) | prompt | ChatGoogleGenerativeAI(model='gemini-pro', temperature=0, max_output_tokens=500, convert_system_message_to_human=True)) def home(): st.header('Interactive Chatbot') st.write('''An interactive chatbot is designed to engage in dynamic, back-and-forth conversations with users. These chatbots can understand and retain context from previous interactions, making their responses more relevant and coherent as the conversation progresses. Interactive chatbots often use advanced natural language processing (NLP) techniques and memory management to provide a more human-like experience. They are commonly used in applications where ongoing interaction and context awareness are crucial, such as customer support, virtual assistants, and personalized recommendations.''') st.header('Non-Interactive Chatbot') st.write('''A non-interactive chatbot, on the other hand, is designed for more straightforward, single-turn interactions. These chatbots do not retain context from previous interactions, meaning each user query is treated independently. Non-interactive chatbots are typically used for simple, transactional tasks where context is not required. They are easier to develop and deploy and are suitable for scenarios where the interaction is brief and to the point.''') def page1(): st.title("Interactive Chatbot") if 'user_input' not in st.session_state: st.session_state.user_input = "" user_input = st.text_area("User: ", st.session_state.user_input, height=100) if st.button("Submit"): response = chain.invoke({"input": user_input}) st.write(f"Assistant: {response.content}") st.session_state.memory.save_context({"input": user_input}, {"output": response.content}) st.session_state.user_input = "" # Clear the input box if st.checkbox("Show Chat History"): chat_history = st.session_state.memory.load_memory_variables({}) st.write(chat_history) def page2(): st.title("Non Interactive Chatbot") if 'user_input' not in st.session_state: st.session_state.user_input = "" user_input = st.text_area("User: ", st.session_state.user_input, height=100) if st.button("Submit"): response = chain.invoke({"input": user_input}) st.write(f"Assistant: {response.content}") st.session_state.memory.save_context({"input": user_input}, {"output": response.content}) st.session_state.user_input = "" # Clear the input box if st.checkbox("Show Chat History"): chat_history = st.session_state.memory.load_memory_variables({}) st.write(chat_history) # Sidebar navigation st.sidebar.title("Navigation") page = st.sidebar.radio("Go to", ("Home", "Page 1", "Page 2")) # Page rendering if page == "Home": home() elif page == "Page 1": page1() elif page == "Page 2": page2()