Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -155,59 +155,48 @@ def respond(
|
|
155 |
if client is None:
|
156 |
client = create_client()
|
157 |
if client is None:
|
158 |
-
|
159 |
-
return
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
IMPORTANT CONTEXT - USE THIS INFORMATION ONLY:
|
173 |
{context}
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
4. We are a proudly South African company - incorporate this identity naturally
|
179 |
-
5. Avoid technical jargon unless directly relevant to the question
|
180 |
-
6. Keep responses concise and focused on helping the user
|
181 |
-
7. For product inquiries, mention relevant features and pricing
|
182 |
-
8. Acknowledge Wayne Sletcher as CEO and Founder when contextually appropriate"""
|
183 |
|
184 |
-
try:
|
185 |
# Format conversation history
|
186 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
187 |
for user_msg, assistant_msg in history:
|
188 |
-
|
189 |
-
messages.append({"role": "user", "content": user_msg})
|
190 |
if assistant_msg:
|
191 |
messages.append({"role": "assistant", "content": assistant_msg})
|
192 |
messages.append({"role": "user", "content": message})
|
193 |
|
194 |
-
#
|
195 |
-
response =
|
196 |
-
for msg in client.chat_completion(
|
197 |
messages,
|
198 |
max_tokens=max_tokens,
|
199 |
-
stream=True,
|
200 |
temperature=temperature,
|
201 |
top_p=top_p,
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
206 |
except Exception as e:
|
207 |
print(f"Error in chat completion: {e}")
|
208 |
-
# Try to recreate client on error
|
209 |
client = create_client()
|
210 |
-
|
211 |
|
212 |
def create_chat_interface():
|
213 |
content = load_site_content()
|
@@ -255,11 +244,9 @@ def create_chat_interface():
|
|
255 |
def bot_response(chat_history, system_msg, max_tok, temp, top_probability):
|
256 |
if chat_history and chat_history[-1][1] is None:
|
257 |
user_message = chat_history[-1][0]
|
258 |
-
chat_history[
|
259 |
-
|
260 |
-
|
261 |
-
chat_history[-1][1] = chunk
|
262 |
-
yield chat_history
|
263 |
else:
|
264 |
yield chat_history
|
265 |
|
|
|
155 |
if client is None:
|
156 |
client = create_client()
|
157 |
if client is None:
|
158 |
+
return "I apologize, but I'm having trouble connecting to the language model."
|
|
|
159 |
|
160 |
+
try:
|
161 |
+
# Load content
|
162 |
+
content = load_site_content()
|
163 |
+
if not content:
|
164 |
+
return "I apologize, but I'm having trouble accessing the company information."
|
165 |
+
|
166 |
+
# Get relevant context
|
167 |
+
context = get_relevant_context(message, content)
|
168 |
+
|
169 |
+
# Enhanced system message
|
170 |
+
enhanced_system_message = f"""You are the official AI assistant for SletcherSystems.
|
|
|
171 |
{context}
|
172 |
+
GUIDELINES:
|
173 |
+
- Be friendly and professional
|
174 |
+
- Only use information from the context above
|
175 |
+
- Keep responses concise and helpful"""
|
|
|
|
|
|
|
|
|
|
|
176 |
|
|
|
177 |
# Format conversation history
|
178 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
179 |
for user_msg, assistant_msg in history:
|
180 |
+
messages.append({"role": "user", "content": user_msg})
|
|
|
181 |
if assistant_msg:
|
182 |
messages.append({"role": "assistant", "content": assistant_msg})
|
183 |
messages.append({"role": "user", "content": message})
|
184 |
|
185 |
+
# Get response without streaming
|
186 |
+
response = client.chat_completion(
|
|
|
187 |
messages,
|
188 |
max_tokens=max_tokens,
|
|
|
189 |
temperature=temperature,
|
190 |
top_p=top_p,
|
191 |
+
stream=False
|
192 |
+
)
|
193 |
+
|
194 |
+
return response.choices[0].message.content
|
195 |
+
|
196 |
except Exception as e:
|
197 |
print(f"Error in chat completion: {e}")
|
|
|
198 |
client = create_client()
|
199 |
+
return "I apologize, but I encountered an error. Please try again."
|
200 |
|
201 |
def create_chat_interface():
|
202 |
content = load_site_content()
|
|
|
244 |
def bot_response(chat_history, system_msg, max_tok, temp, top_probability):
|
245 |
if chat_history and chat_history[-1][1] is None:
|
246 |
user_message = chat_history[-1][0]
|
247 |
+
response = respond(user_message, chat_history[:-1], system_msg, max_tok, temp, top_probability)
|
248 |
+
chat_history[-1][1] = response
|
249 |
+
yield chat_history
|
|
|
|
|
250 |
else:
|
251 |
yield chat_history
|
252 |
|