DebabrataHalder commited on
Commit
d57dbd4
·
verified ·
1 Parent(s): 39be19e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -51
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 (No OpenAI)")
38
- st.write("Interact with an intelligent assistant powered by Groq and LangChain memory!")
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
- # Initialize or load session memory
45
- if "chat_history" not in st.session_state:
46
- st.session_state.chat_history = []
 
 
47
 
48
- # Display chat history
49
- for chat in st.session_state.chat_history:
50
- if chat["role"] == "user":
51
- st.markdown(f"**You:** {chat['content']}")
52
- elif chat["role"] == "assistant":
53
- st.markdown(f"**Assistant:** {chat['content']}")
54
 
55
  # Input box for user query
56
- user_input = st.text_input("Your Message", key="user_input")
57
 
58
- # Send button
59
  if st.button("Send") and user_input.strip():
60
- # Add user message to memory and session
61
- st.session_state.chat_history.append({"role": "user", "content": user_input})
62
 
63
- # Construct messages from memory
64
- messages = memory.load_memory_variables({}).get("chat_history", [])
65
- messages.append({"role": "user", "content": user_input})
 
 
 
 
66
 
67
- # Get response from Groq
68
- response = get_groq_response(messages, model=model_name)
69
 
70
- # Add response to memory and session
71
- st.session_state.chat_history.append({"role": "assistant", "content": response})
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 the input box
78
- st.session_state.user_input = ""
79
 
80
- # Clear chat history
81
- if st.sidebar.button("Clear Chat"):
82
- st.session_state.chat_history = []
83
- memory.clear()
 
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.")