File size: 1,348 Bytes
8403da4
df364e0
24d45f4
65857e5
24d45f4
 
65857e5
 
 
289e3e0
24d45f4
 
65857e5
24d45f4
65857e5
24d45f4
65857e5
24d45f4
65857e5
24d45f4
289e3e0
24d45f4
65857e5
24d45f4
65857e5
 
24d45f4
65857e5
24d45f4
65857e5
 
 
 
24d45f4
d8d0e05
24d45f4
d8d0e05
24d45f4
 
 
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
import os
import streamlit as st
from langchain_groq import ChatGroq

# Retrieve API key from environment variable
api_key = os.getenv('pack1st')
if not api_key:
    st.error("GROQ_API_KEY is not set. Please add it to your Hugging Face Space Secrets.")
    st.stop()

# Initialize ChatGroq model
llm = ChatGroq(model_name="gemma2-9b-it", api_key=api_key)

# Set up Streamlit page
st.set_page_config(page_title="Basic Langchain Model", page_icon=":robot:")
st.header("My  Chatbot")

# Initialize session state for chat history
if "messages" not in st.session_state:
    st.session_state.messages = []  # Stores chat history

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):  # "user" or "assistant"
        st.write(message["content"])

# Create new input box dynamically
if prompt := st.chat_input("Type your message..."):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.write(prompt)

    # Generate AI response
    with st.chat_message("assistant"):
        response = llm.invoke(prompt).content  # Extract only the text response
        st.write(response)

    # Add AI response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})