Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
prompt = PromptTemplate.from_template(
|
64 |
-
"[INST] You are a knowledgeable
|
65 |
-
"
|
66 |
-
"
|
67 |
-
"\
|
68 |
-
"
|
69 |
-
|
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 |
-
|
|
|
|
|
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 |
|