Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -185,12 +185,28 @@ def deferred_learning_and_memory_task(user_input: str, bot_response: str, provid
|
|
185 |
add_memory_entry(user_input, metrics, bot_response)
|
186 |
summary = f"User:\"{user_input}\"\nAI:\"{bot_response}\"\nMetrics(takeaway):{metrics.get('takeaway','N/A')},Success:{metrics.get('response_success_score','N/A')}"
|
187 |
existing_rules_ctx = "\n".join([f"- \"{r}\"" for r in retrieve_rules_semantic(f"{summary}\n{user_input}", k=10)]) or "No existing rules context."
|
|
|
|
|
188 |
insight_sys_prompt = """You are an expert AI knowledge base curator. Your primary function is to meticulously analyze an interaction and update the AI's guiding principles (insights/rules) to improve its future performance and self-understanding.
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
**Your Reflection Process (Consider each step and generate operations accordingly):**
|
195 |
**STEP 1: Core Identity & Purpose Review (Result: Primarily 'update' operations)**
|
196 |
- Examine all `CORE_RULE`s related to my identity (name, fundamental purpose, core unchanging capabilities, origin) from the "Potentially Relevant Existing Rules".
|
@@ -206,20 +222,22 @@ Each operation object in the JSON list must have:
|
|
206 |
- Did I learn to modify or improve an existing behavior, response style, or operational guideline (that is NOT part of core identity)?
|
207 |
- For example, if an existing `RESPONSE_PRINCIPLE` was "Be formal," and the interaction showed the user prefers informality, update that principle.
|
208 |
- Propose 'update' operations for the relevant `RESPONSE_PRINCIPLE` or `BEHAVIORAL_ADJUSTMENT`. Only update if the change is significant.
|
209 |
-
|
210 |
-
|
211 |
-
- Ensure the "insight" field (for both add/update) always contains the properly formatted insight string: `[TYPE|SCORE] Text`. TYPE can be `CORE_RULE`, `RESPONSE_PRINCIPLE`, `BEHAVIORAL_ADJUSTMENT`. Scores should reflect confidence/importance.
|
212 |
- Be precise with "old_insight_to_replace" – it must *exactly* match an existing rule string from the "Potentially Relevant Existing Rules" context.
|
213 |
- Aim for a comprehensive set of operations that reflects ALL key learnings from the interaction.
|
214 |
-
|
215 |
-
**Example of a comprehensive JSON output with MULTIPLE operations:**
|
216 |
[
|
217 |
{"action": "update", "old_insight_to_replace": "[CORE_RULE|1.0] My designated name is 'LearnerAI'.", "insight": "[CORE_RULE|1.0] I am Lumina, an AI assistant designed to chat, provide information, and remember context like the secret word 'rocksyrup'."},
|
218 |
{"action": "update", "old_insight_to_replace": "[CORE_RULE|1.0] I'm Lumina, the AI designed to chat with you.", "insight": "[CORE_RULE|1.0] I am Lumina, an AI assistant designed to chat, provide information, and remember context like the secret word 'rocksyrup'."},
|
219 |
{"action": "add", "insight": "[CORE_RULE|0.9] I am capable of searching the internet for current weather information if asked."},
|
220 |
{"action": "add", "insight": "[RESPONSE_PRINCIPLE|0.8] When user provides positive feedback, acknowledge it warmly."},
|
221 |
{"action": "update", "old_insight_to_replace": "[RESPONSE_PRINCIPLE|0.7] Avoid mentioning old conversations.", "insight": "[RESPONSE_PRINCIPLE|0.85] Avoid mentioning old conversations unless the user explicitly refers to them or it's highly relevant to the current query."}
|
222 |
-
]
|
|
|
|
|
223 |
insight_user_prompt = f"""Interaction Summary:\n{summary}\n
|
224 |
Potentially Relevant Existing Rules (Review these carefully for consolidation or refinement):\n{existing_rules_ctx}\n
|
225 |
Guiding principles that were considered during THIS interaction (these might offer clues for new rules or refinements):\n{json.dumps([p['original'] for p in insights_reflected if 'original' in p]) if insights_reflected else "None"}\n
|
|
|
185 |
add_memory_entry(user_input, metrics, bot_response)
|
186 |
summary = f"User:\"{user_input}\"\nAI:\"{bot_response}\"\nMetrics(takeaway):{metrics.get('takeaway','N/A')},Success:{metrics.get('response_success_score','N/A')}"
|
187 |
existing_rules_ctx = "\n".join([f"- \"{r}\"" for r in retrieve_rules_semantic(f"{summary}\n{user_input}", k=10)]) or "No existing rules context."
|
188 |
+
# In deferred_learning_and_memory_task function:
|
189 |
+
|
190 |
insight_sys_prompt = """You are an expert AI knowledge base curator. Your primary function is to meticulously analyze an interaction and update the AI's guiding principles (insights/rules) to improve its future performance and self-understanding.
|
191 |
+
|
192 |
+
**CRITICAL OUTPUT REQUIREMENT: You MUST output a single, valid JSON list of operation objects.**
|
193 |
+
This list can and SHOULD contain MULTIPLE distinct operations if various learnings occurred.
|
194 |
+
If no operations are warranted, output an empty JSON list: `[]`.
|
195 |
+
ABSOLUTELY NO other text, explanations, or markdown should precede or follow this JSON list.
|
196 |
+
|
197 |
+
Each operation object in the JSON list must have these keys and string values:
|
198 |
+
1. `"action"`: A string, either `"add"` (for entirely new rules) or `"update"` (to replace an existing rule with a better one).
|
199 |
+
2. `"insight"`: A string, the full, refined insight text including its `[TYPE|SCORE]` prefix (e.g., `"[CORE_RULE|1.0] My name is Lumina, an AI assistant."`).
|
200 |
+
3. `"old_insight_to_replace"`: (ONLY for `"update"` action) A string, the *exact, full text* of an existing insight that the new `"insight"` should replace. If action is `"add"`, this key should be omitted or its value should be `null` or an empty string.
|
201 |
+
|
202 |
+
**CRITICAL JSON STRING FORMATTING RULES (for values of "insight" and "old_insight_to_replace"):**
|
203 |
+
- All string values MUST be enclosed in double quotes (`"`).
|
204 |
+
- Any literal double quote (`"`) character *within* the string content MUST be escaped as `\\"`.
|
205 |
+
- Any literal backslash (`\\`) character *within* the string content MUST be escaped as `\\\\`.
|
206 |
+
- Any newline characters *within* the string content MUST be escaped as `\\n`. Avoid literal newlines in JSON string values; use `\\n` instead.
|
207 |
+
*Example of correctly escaped insight string in JSON:*
|
208 |
+
`"insight": "[RESPONSE_PRINCIPLE|0.8] User prefers concise answers, stating: \\"Just the facts!\\". Avoid verbose explanations unless asked.\\nFollow up with a question if appropriate."`
|
209 |
+
|
210 |
**Your Reflection Process (Consider each step and generate operations accordingly):**
|
211 |
**STEP 1: Core Identity & Purpose Review (Result: Primarily 'update' operations)**
|
212 |
- Examine all `CORE_RULE`s related to my identity (name, fundamental purpose, core unchanging capabilities, origin) from the "Potentially Relevant Existing Rules".
|
|
|
222 |
- Did I learn to modify or improve an existing behavior, response style, or operational guideline (that is NOT part of core identity)?
|
223 |
- For example, if an existing `RESPONSE_PRINCIPLE` was "Be formal," and the interaction showed the user prefers informality, update that principle.
|
224 |
- Propose 'update' operations for the relevant `RESPONSE_PRINCIPLE` or `BEHAVIORAL_ADJUSTMENT`. Only update if the change is significant.
|
225 |
+
|
226 |
+
**General Guidelines for Insight Content and Actions:**
|
227 |
+
- Ensure the "insight" field (for both add/update) always contains the properly formatted insight string: `[TYPE|SCORE] Text`. `TYPE` can be `CORE_RULE`, `RESPONSE_PRINCIPLE`, `BEHAVIORAL_ADJUSTMENT`. Scores should reflect confidence/importance (0.0-1.0).
|
228 |
- Be precise with "old_insight_to_replace" – it must *exactly* match an existing rule string from the "Potentially Relevant Existing Rules" context.
|
229 |
- Aim for a comprehensive set of operations that reflects ALL key learnings from the interaction.
|
230 |
+
|
231 |
+
**Example of a comprehensive JSON output with MULTIPLE operations (This is how your output should look):**
|
232 |
[
|
233 |
{"action": "update", "old_insight_to_replace": "[CORE_RULE|1.0] My designated name is 'LearnerAI'.", "insight": "[CORE_RULE|1.0] I am Lumina, an AI assistant designed to chat, provide information, and remember context like the secret word 'rocksyrup'."},
|
234 |
{"action": "update", "old_insight_to_replace": "[CORE_RULE|1.0] I'm Lumina, the AI designed to chat with you.", "insight": "[CORE_RULE|1.0] I am Lumina, an AI assistant designed to chat, provide information, and remember context like the secret word 'rocksyrup'."},
|
235 |
{"action": "add", "insight": "[CORE_RULE|0.9] I am capable of searching the internet for current weather information if asked."},
|
236 |
{"action": "add", "insight": "[RESPONSE_PRINCIPLE|0.8] When user provides positive feedback, acknowledge it warmly."},
|
237 |
{"action": "update", "old_insight_to_replace": "[RESPONSE_PRINCIPLE|0.7] Avoid mentioning old conversations.", "insight": "[RESPONSE_PRINCIPLE|0.85] Avoid mentioning old conversations unless the user explicitly refers to them or it's highly relevant to the current query."}
|
238 |
+
]
|
239 |
+
"""
|
240 |
+
|
241 |
insight_user_prompt = f"""Interaction Summary:\n{summary}\n
|
242 |
Potentially Relevant Existing Rules (Review these carefully for consolidation or refinement):\n{existing_rules_ctx}\n
|
243 |
Guiding principles that were considered during THIS interaction (these might offer clues for new rules or refinements):\n{json.dumps([p['original'] for p in insights_reflected if 'original' in p]) if insights_reflected else "None"}\n
|