Articles → LANGCHAIN → Maintain The Chat History In LLM

Maintain The Chat History In LLM






Code


import streamlit as stimport osfrom google import genai# Configure Geminiclient = genai.Client(api_key="your_api_key")# Function to get Gemini responsedef get_gemini_response(question):    response = client.models.generate_content(        model="gemini-3.6-flash",        contents=question    )    return response.text# Initialize Streamlit appst.set_page_config(page_title="Q&A Demo")st.header("Gemini LLM Application")# Initialize session stateif "chat_history" not in st.session_state:    st.session_state["chat_history"] = []input_text = st.text_input("Input:", key="input")submit = st.button("Ask the question")if submit and input_text:    response = get_gemini_response(input_text)    # Add user query    st.session_state["chat_history"].append(        ("You", input_text)    )    # Add bot response    st.session_state["chat_history"].append(        ("Bot", response)    )    st.subheader("The Response is")    st.write(response)st.subheader("The Chat history is")for role, text in st.session_state["chat_history"]:    st.write(f"{role}:{text}")



Output


Picture showing





Posted By  -  Karan Gupta
 
Posted On  -  Monday, August 31, 2026

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250