engrharis commited on
Commit
2ee0a75
·
verified ·
1 Parent(s): 1542d75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -12,11 +12,14 @@ except Exception as e:
12
  st.error(f"Error initializing Groq client: {e}")
13
  st.stop() # Stop execution if client initialization fails
14
 
15
- def get_response(prompt):
16
  try:
 
 
 
17
  chat_completion = client.chat.completions.create(
18
- messages=[{"role": "user", "content": prompt}],
19
- model="llama-3.3-70b-versatile" # Or any other available model
20
  )
21
  return chat_completion.choices[0].message.content
22
  except Exception as e:
@@ -26,16 +29,29 @@ def get_response(prompt):
26
  # Streamlit app layout
27
  st.title("Interactive Chatbot")
28
 
 
 
 
 
 
29
  user_input = st.text_input("You: ", key="user_input")
30
 
31
  if user_input:
32
- bot_response = get_response(user_input)
33
- st.write("Bot: ", bot_response)
34
-
35
- if __name__ == "__main__":
36
- st.sidebar.title("Settings")
37
- st.sidebar.write("API Key Source: " + ("Environment Variable" if os.environ.get("GROQ_API_KEY") else "Directly in Code (Not Recommended)"))
38
- if os.environ.get("GROQ_API_KEY"):
39
- st.sidebar.success("Groq Client Initialized Successfully!")
40
- else:
41
- st.sidebar.warning("Using API Key Directly in Code (Not Recommended). Set GROQ_API_KEY Environment Variable for Security.")
 
 
 
 
 
 
 
 
 
12
  st.error(f"Error initializing Groq client: {e}")
13
  st.stop() # Stop execution if client initialization fails
14
 
15
+ def get_response(prompt, chat_history):
16
  try:
17
+ # Include chat history in the request
18
+ messages = [{"role": "user", "content": msg["user"]} if "user" in msg else {"role": "assistant", "content": msg["bot"]} for msg in chat_history]
19
+ messages.append({"role": "user", "content": prompt})
20
  chat_completion = client.chat.completions.create(
21
+ messages=messages,
22
+ model="llama-3.3-70b-versatile" # Or any other available model
23
  )
24
  return chat_completion.choices[0].message.content
25
  except Exception as e:
 
29
  # Streamlit app layout
30
  st.title("Interactive Chatbot")
31
 
32
+ # Initialize session state for chat history
33
+ if "chat_history" not in st.session_state:
34
+ st.session_state.chat_history = []
35
+
36
+ # Input box for user message
37
  user_input = st.text_input("You: ", key="user_input")
38
 
39
  if user_input:
40
+ # Get bot response
41
+ bot_response = get_response(user_input, st.session_state.chat_history)
42
+ # Append to chat history
43
+ st.session_state.chat_history.append({"user": user_input, "bot": bot_response})
44
+
45
+ # Display chat history
46
+ st.subheader("Conversation")
47
+ for chat in st.session_state.chat_history:
48
+ st.write(f"You: {chat['user']}")
49
+ st.write(f"Bot: {chat['bot']}")
50
+
51
+ # Sidebar information
52
+ st.sidebar.title("Settings")
53
+ st.sidebar.write("API Key Source: " + ("Environment Variable" if os.environ.get("GROQ_API_KEY") else "Directly in Code (Not Recommended)"))
54
+ if os.environ.get("GROQ_API_KEY"):
55
+ st.sidebar.success("Groq Client Initialized Successfully!")
56
+ else:
57
+ st.sidebar.warning("Using API Key Directly in Code (Not Recommended). Set GROQ_API_KEY Environment Variable for Security.")