Update app.py
Browse files
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=
|
19 |
-
model="llama-3.3-70b-versatile"
|
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 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|