Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
# Run Streamlit app
|
| 2 |
-
import os
|
| 3 |
import streamlit as st
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from groq import Groq
|
| 6 |
-
from langchain.memory import ConversationBufferMemory
|
| 7 |
-
from langchain.memory.chat_message_histories import ChatMessageHistory
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
@@ -17,68 +15,54 @@ if not api_key:
|
|
| 17 |
|
| 18 |
client = Groq(api_key=api_key)
|
| 19 |
|
| 20 |
-
# Initialize LangChain memory with updated structure
|
| 21 |
-
chat_history = ChatMessageHistory()
|
| 22 |
-
memory = ConversationBufferMemory(
|
| 23 |
-
chat_memory=chat_history,
|
| 24 |
-
return_messages=True
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Function to call the Groq API
|
| 28 |
-
def get_groq_response(messages, model="llama3-8b-8192"):
|
| 29 |
-
try:
|
| 30 |
-
chat_completion = client.chat.completions.create(messages=messages, model=model)
|
| 31 |
-
return chat_completion.choices[0].message.content
|
| 32 |
-
except Exception as e:
|
| 33 |
-
st.error(f"Error communicating with Groq API: {e}")
|
| 34 |
-
return "I'm sorry, there was an error processing your request."
|
| 35 |
-
|
| 36 |
# Streamlit app layout
|
| 37 |
-
st.title("Chat with Groq
|
| 38 |
-
st.write("
|
| 39 |
|
| 40 |
# Sidebar for settings
|
| 41 |
st.sidebar.title("Settings")
|
| 42 |
model_name = st.sidebar.text_input("Model Name", value="llama3-8b-8192")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
if "
|
| 46 |
-
st.session_state.
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.markdown(f"**Assistant:** {chat['content']}")
|
| 54 |
|
| 55 |
# Input box for user query
|
| 56 |
-
user_input = st.text_input("Your Message", key="
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if st.button("Send") and user_input.strip():
|
| 60 |
-
# Add user message to
|
| 61 |
-
st.session_state.
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
memory.save_context(
|
| 73 |
-
{"input": user_input}, # Pass user input as "input"
|
| 74 |
-
{"output": response} # Pass assistant response as "output"
|
| 75 |
-
)
|
| 76 |
|
| 77 |
-
# Clear
|
| 78 |
-
st.session_state.
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
if st.sidebar.button("Clear Chat"):
|
| 82 |
-
st.session_state.
|
| 83 |
-
|
|
|
|
| 84 |
st.success("Chat history cleared.")
|
|
|
|
| 1 |
# Run Streamlit app
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from groq import Groq
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load environment variables
|
| 8 |
load_dotenv()
|
|
|
|
| 15 |
|
| 16 |
client = Groq(api_key=api_key)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Streamlit app layout
|
| 19 |
+
st.title("Chat with Groq")
|
| 20 |
+
st.write("Ask anything, and the Groq model will respond!")
|
| 21 |
|
| 22 |
# Sidebar for settings
|
| 23 |
st.sidebar.title("Settings")
|
| 24 |
model_name = st.sidebar.text_input("Model Name", value="llama3-8b-8192")
|
| 25 |
|
| 26 |
+
# Main chat interface
|
| 27 |
+
if "messages" not in st.session_state:
|
| 28 |
+
st.session_state.messages = [
|
| 29 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
| 30 |
+
]
|
| 31 |
|
| 32 |
+
for message in st.session_state.messages:
|
| 33 |
+
if message["role"] == "user":
|
| 34 |
+
st.markdown(f"**User:** {message['content']}")
|
| 35 |
+
elif message["role"] == "assistant":
|
| 36 |
+
st.markdown(f"**Assistant:** {message['content']}")
|
|
|
|
| 37 |
|
| 38 |
# Input box for user query
|
| 39 |
+
user_input = st.text_input("Your Message", key="input_box")
|
| 40 |
|
| 41 |
+
# Handle user input and get response
|
| 42 |
if st.button("Send") and user_input.strip():
|
| 43 |
+
# Add user message to session
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 45 |
|
| 46 |
+
# Call Groq API
|
| 47 |
+
try:
|
| 48 |
+
chat_completion = client.chat.completions.create(
|
| 49 |
+
messages=st.session_state.messages,
|
| 50 |
+
model=model_name,
|
| 51 |
+
)
|
| 52 |
+
assistant_response = chat_completion.choices[0].message.content
|
| 53 |
|
| 54 |
+
# Add assistant message to session
|
| 55 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 56 |
|
| 57 |
+
except Exception as e:
|
| 58 |
+
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Clear input box
|
| 61 |
+
st.session_state.input_box = ""
|
| 62 |
|
| 63 |
+
# Button to clear chat history
|
| 64 |
+
if st.sidebar.button("Clear Chat History"):
|
| 65 |
+
st.session_state.messages = [
|
| 66 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
| 67 |
+
]
|
| 68 |
st.success("Chat history cleared.")
|