Articles → LANGCHAIN → Maintain The Chat History In LLM

Maintain The Chat History In LLM






Code


import streamlit as st
import os
from google import genai


# Configure Gemini
client = genai.Client(api_key="your_api_key")


# Function to get Gemini response
def get_gemini_response(question):
    response = client.models.generate_content(
        model="gemini-3.6-flash",
        contents=question
    )
    return response.text


# Initialize Streamlit app
st.set_page_config(page_title="Q&A Demo")

st.header("Gemini LLM Application")


# Initialize session state
if "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 the output of maintaining chat history using llm





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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250