SmokeyBandit commited on
Commit
7c56c28
·
verified ·
1 Parent(s): fead20a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -41
app.py CHANGED
@@ -155,59 +155,48 @@ def respond(
155
  if client is None:
156
  client = create_client()
157
  if client is None:
158
- yield "I apologize, but I'm having trouble connecting to the language model. Please try again in a moment."
159
- return
160
 
161
- # Load content
162
- content = load_site_content()
163
- if not content:
164
- yield "I apologize, but I'm having trouble accessing the company information. Please try again in a moment."
165
- return
166
-
167
- # Get relevant context
168
- context = get_relevant_context(message, content)
169
-
170
- # Enhanced system message with conversation guidance
171
- enhanced_system_message = f"""{system_message}
172
- IMPORTANT CONTEXT - USE THIS INFORMATION ONLY:
173
  {context}
174
- CONVERSATION GUIDELINES:
175
- 1. Be warm, friendly, and conversational - feel free to use emoji occasionally 🇿🇦
176
- 2. ONLY use information from the context provided above
177
- 3. If information isn't in the context, politely say you don't have that specific information
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
- if user_msg:
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
- # Stream the response
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
- token = msg.choices[0].delta.content
204
- response += token
205
- yield response
 
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
- yield "I apologize, but I encountered an error. Please try your question again."
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[-1][1] = "" # Initialize bot response
259
-
260
- for chunk in respond(user_message, chat_history[:-1], system_msg, max_tok, temp, top_probability):
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