CCockrum commited on
Commit
2b522b4
Β·
verified Β·
1 Parent(s): 83e0d11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -56,19 +56,17 @@ def ensure_english(text):
56
  def get_response(system_message, chat_history, user_text, max_new_tokens=800):
57
  # βœ… Ensure conversation history is included correctly
58
  filtered_history = "\n".join(
59
- f"{msg['role'].capitalize()}: {msg['content']}"
60
- for msg in chat_history
61
- )
62
-
63
  prompt = PromptTemplate.from_template(
64
- "[INST] You are a knowledgeable and formal AI assistant. Please provide detailed, structured answers "
65
- "without repetition, unnecessary enthusiasm or emojis.\n\n"
66
- "Ensure responses are structured and non-repetitive."
67
- "\nPrevious Conversation:\n{chat_history}\n\n"
68
- "User: {user_text}.\n [/INST]\n"
69
- "AI: Provide a structured and informative response while maintaining a neutral and professional tone."
70
- "Ensure your response is engaging yet clear."
71
- "\nHAL:"
72
  )
73
 
74
  # βœ… Invoke Hugging Face Model
@@ -86,7 +84,9 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=800):
86
  # βœ… Preserve conversation history
87
  chat_history.append({'role': 'user', 'content': user_text})
88
  chat_history.append({'role': 'assistant', 'content': response})
89
- st.session_state.chat_history = chat_history # βœ… Update session state history
 
 
90
 
91
  return response, st.session_state.chat_history
92
 
 
56
  def get_response(system_message, chat_history, user_text, max_new_tokens=800):
57
  # βœ… Ensure conversation history is included correctly
58
  filtered_history = "\n".join(
59
+ f"{msg['role'].capitalize()}: {msg['content']}"
60
+ for msg in chat_history[-5:] # βœ… Only keep the last 5 exchanges to prevent overflow
61
+ )
62
+
63
  prompt = PromptTemplate.from_template(
64
+ "[INST] You are a highly knowledgeable AI assistant. Answer concisely, avoid repetition, and structure responses well."
65
+ "\n\nCONTEXT:\n{chat_history}\n"
66
+ "\nLATEST USER INPUT:\nUser: {user_text}\n"
67
+ "\n[END CONTEXT]\n"
68
+ "Assistant:"
69
+
 
 
70
  )
71
 
72
  # βœ… Invoke Hugging Face Model
 
84
  # βœ… Preserve conversation history
85
  chat_history.append({'role': 'user', 'content': user_text})
86
  chat_history.append({'role': 'assistant', 'content': response})
87
+
88
+ # βœ… Keep only last 10 exchanges to prevent unnecessary repetition
89
+ st.session_state.chat_history = chat_history[-10:]
90
 
91
  return response, st.session_state.chat_history
92