Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -100,9 +100,44 @@ def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
|
100 |
if chunk.choices[0].delta.content:
|
101 |
yield chunk.choices[0].delta.content
|
102 |
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
107 |
|
108 |
with st.chat_message("user", avatar="❓"):
|
|
|
100 |
if chunk.choices[0].delta.content:
|
101 |
yield chunk.choices[0].delta.content
|
102 |
|
103 |
+
if prompt := st.chat_input("Enter your prompt here..."):
|
104 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
105 |
+
|
106 |
+
with st.chat_message("user", avatar="❓"):
|
107 |
+
st.markdown(prompt)
|
108 |
+
|
109 |
+
# Fetch response from Groq API
|
110 |
+
try:
|
111 |
+
chat_completion = client.chat.completions.create(
|
112 |
+
model=model_option,
|
113 |
+
messages=[
|
114 |
+
{"role": m["role"], "content": m["content"]}
|
115 |
+
for m in st.session_state.messages
|
116 |
+
],
|
117 |
+
max_tokens=max_tokens,
|
118 |
+
stream=True,
|
119 |
+
)
|
120 |
+
|
121 |
+
# Use the generator function with st.write_stream
|
122 |
+
with st.chat_message("assistant", avatar="🧠"):
|
123 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
124 |
+
full_response = st.write_stream(chat_responses_generator)
|
125 |
+
except Exception as e:
|
126 |
+
st.error(e, icon="🚨")
|
127 |
|
128 |
+
# Append the full response to session_state.messages
|
129 |
+
if isinstance(full_response, str):
|
130 |
+
st.session_state.messages.append(
|
131 |
+
{"role": "assistant", "content": full_response}
|
132 |
+
)
|
133 |
+
else:
|
134 |
+
# Handle the case where full_response is not a string
|
135 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
136 |
+
st.session_state.messages.append(
|
137 |
+
{"role": "assistant", "content": combined_response}
|
138 |
+
)
|
139 |
+
|
140 |
+
if prompt := prompts.get(prompt_selection):
|
141 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
142 |
|
143 |
with st.chat_message("user", avatar="❓"):
|