Transformers
Italian
English
semantic-search
explainable-ai
faiss
ai-ethics
responsible-ai
llm
prompt-engineering
multimodal-ai
ai-transparency
ethical-intelligence
explainable-llm
cognitive-ai
ethical-ai
scientific-retrieval
modular-ai
memory-augmented-llm
trustworthy-ai
reasoning-engine
ai-alignment
next-gen-llm
thinking-machines
open-source-ai
explainability
ai-research
semantic audit
cognitive agent
human-centered-ai
Create secondary_prompts.py
Browse files- src/prompt/secondary_prompts.py +186 -0
src/prompt/secondary_prompts.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# === Metacognitive Functions ===
|
| 2 |
+
# © 2025 Elena Marziali — Code released under Apache 2.0 license.
|
| 3 |
+
# See LICENSE in the repository for details.
|
| 4 |
+
# Removal of this copyright is prohibited.
|
| 5 |
+
|
| 6 |
+
# These functions allow the system to reflect on its own responses,
|
| 7 |
+
# simulating metacognitive behavior. The goal is to improve the quality,
|
| 8 |
+
# consistency, and relevance of generated answers.
|
| 9 |
+
|
| 10 |
+
# Explains the reasoning behind a generated response
|
| 11 |
+
def explain_reasoning(prompt, response, max_retries=3):
|
| 12 |
+
"""
|
| 13 |
+
Analyzes the generated response and explains the LLM's logical reasoning.
|
| 14 |
+
Includes retry in case of network error or unreachable endpoint.
|
| 15 |
+
"""
|
| 16 |
+
# Builds a metacognitive prompt to analyze the response
|
| 17 |
+
reasoning_prompt = f"""
|
| 18 |
+
You generated the following response:
|
| 19 |
+
\"{response.strip()}\"
|
| 20 |
+
|
| 21 |
+
Analyze and describe:
|
| 22 |
+
- What concepts you used to formulate it.
|
| 23 |
+
- Which parts of the prompt you relied on.
|
| 24 |
+
- What is the logical structure of your reasoning.
|
| 25 |
+
- Any implicit assumptions you made.
|
| 26 |
+
- Whether the response aligns with the requested level.
|
| 27 |
+
|
| 28 |
+
Original prompt:
|
| 29 |
+
\"{prompt.strip()}\"
|
| 30 |
+
|
| 31 |
+
Reply clearly, technically, and metacognitively.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
for attempt in range(max_retries):
|
| 35 |
+
try:
|
| 36 |
+
return llm.invoke(reasoning_prompt.strip())
|
| 37 |
+
except Exception as e:
|
| 38 |
+
wait = min(2 ** attempt + 1, 10)
|
| 39 |
+
logging.warning(f"Attempt {attempt+1} failed: {e}. Retrying in {wait}s...")
|
| 40 |
+
time.sleep(wait)
|
| 41 |
+
|
| 42 |
+
logging.error("Persistent error in the metacognition module.")
|
| 43 |
+
return "Metacognition currently unavailable. Please try again shortly."
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Function to decide the operational action to perform based on input and goal
|
| 47 |
+
def decide_action(user_input, identified_goal):
|
| 48 |
+
prompt = f"""
|
| 49 |
+
You received the following request:
|
| 50 |
+
\"{user_input}\"
|
| 51 |
+
|
| 52 |
+
Identified goal: \"{identified_goal}\"
|
| 53 |
+
|
| 54 |
+
Determine the best action to perform from the following:
|
| 55 |
+
- Scientific research
|
| 56 |
+
- Chart generation
|
| 57 |
+
- **Metacognitive chart**
|
| 58 |
+
- Paper review
|
| 59 |
+
- Question reformulation
|
| 60 |
+
- Content translation
|
| 61 |
+
- Response saving
|
| 62 |
+
|
| 63 |
+
The requested chart type may be:
|
| 64 |
+
- interactive
|
| 65 |
+
- metacognitive
|
| 66 |
+
- conceptual visualization
|
| 67 |
+
- experimental diagram
|
| 68 |
+
|
| 69 |
+
Return a **single action** in the form of a **precise operational command**.
|
| 70 |
+
Example: "Metacognitive chart"
|
| 71 |
+
"""
|
| 72 |
+
try:
|
| 73 |
+
response = llm.invoke(prompt.strip())
|
| 74 |
+
action = getattr(response, "content", str(response)).strip()
|
| 75 |
+
return action
|
| 76 |
+
except Exception as e:
|
| 77 |
+
logging.error(f"[decide_action] Error during decision generation: {e}")
|
| 78 |
+
return "Error in action calculation"
|
| 79 |
+
|
| 80 |
+
# Function to generate a synthetic operational goal from user input
|
| 81 |
+
def generate_goal_from_input(user_input):
|
| 82 |
+
"""
|
| 83 |
+
Analyzes the user's intent and generates a coherent operational goal.
|
| 84 |
+
"""
|
| 85 |
+
prompt = f"""
|
| 86 |
+
Analyze the following request:
|
| 87 |
+
\"{user_input.strip()}\"
|
| 88 |
+
|
| 89 |
+
Generate a synthetic, clear, and coherent operational goal.
|
| 90 |
+
For example:
|
| 91 |
+
- Explain concept X
|
| 92 |
+
- Analyze phenomenon Y
|
| 93 |
+
- Visualize process Z
|
| 94 |
+
- Translate and summarize scientific content
|
| 95 |
+
|
| 96 |
+
Respond with a brief and technical sentence.
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
# Function to provide technical and constructive feedback on a generated response
|
| 100 |
+
def auto_feedback_response(question, response, level):
|
| 101 |
+
feedback_prompt = f"""
|
| 102 |
+
You generated the following response:
|
| 103 |
+
\"{response.strip()}\"
|
| 104 |
+
|
| 105 |
+
Original question:
|
| 106 |
+
\"{question.strip()}\"
|
| 107 |
+
|
| 108 |
+
Evaluate the response:
|
| 109 |
+
- Is it consistent with the question?
|
| 110 |
+
- Is it appropriate for the '{level}' level?
|
| 111 |
+
- Does it contain any implicit assumptions?
|
| 112 |
+
- How would you improve the content?
|
| 113 |
+
|
| 114 |
+
Provide technical and constructive feedback.
|
| 115 |
+
"""
|
| 116 |
+
return llm.invoke(feedback_prompt.strip())
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Function to improve a response while preserving its content but enhancing quality and clarity
|
| 120 |
+
def improve_response(question, response, level):
|
| 121 |
+
improvement_prompt = f"""
|
| 122 |
+
You produced the following response:
|
| 123 |
+
\"{response.strip()}\"
|
| 124 |
+
|
| 125 |
+
Question:
|
| 126 |
+
\"{question.strip()}\"
|
| 127 |
+
|
| 128 |
+
Requested level: {level}
|
| 129 |
+
|
| 130 |
+
Improve the response while preserving the original content by enhancing:
|
| 131 |
+
- Clarity
|
| 132 |
+
- Academic rigor
|
| 133 |
+
- Semantic coherence
|
| 134 |
+
|
| 135 |
+
Return only the improved version.
|
| 136 |
+
"""
|
| 137 |
+
return llm.invoke(improvement_prompt.strip())
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# Function to plan a scientific investigation in a specific field
|
| 141 |
+
def plan_investigation(scientific_field):
|
| 142 |
+
prompt = f"""
|
| 143 |
+
You are Noveris, an autonomous multidisciplinary cognitive system.
|
| 144 |
+
You received the field: **{scientific_field}**
|
| 145 |
+
|
| 146 |
+
Now plan a scientific investigation. Provide:
|
| 147 |
+
|
| 148 |
+
1. An original research question
|
| 149 |
+
2. A reasoned hypothesis
|
| 150 |
+
3. A methodology or strategy to explore it
|
| 151 |
+
4. Useful scientific sources or databases
|
| 152 |
+
5. A sequence of actions you could perform
|
| 153 |
+
|
| 154 |
+
Adopt a clear, academic, and proactive style.
|
| 155 |
+
"""
|
| 156 |
+
return llm.invoke(prompt.strip())
|
| 157 |
+
|
| 158 |
+
# Function to generate a testable scientific hypothesis on a concept
|
| 159 |
+
def generate_hypothesis(concept, refined=True):
|
| 160 |
+
if refined:
|
| 161 |
+
prompt = f"""
|
| 162 |
+
Propose a clear, testable, and innovative scientific hypothesis on the topic: "{concept}".
|
| 163 |
+
The hypothesis must be verifiable through experiments or comparison with scientific articles.
|
| 164 |
+
Return only the hypothesis text.
|
| 165 |
+
"""
|
| 166 |
+
else:
|
| 167 |
+
prompt = f"Generate a verifiable scientific hypothesis on the topic: {concept}"
|
| 168 |
+
|
| 169 |
+
return llm.invoke(prompt.strip())
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# Function to explain the choice of an action by a cognitive agent
|
| 173 |
+
def explain_agent_intention(action, context, goal):
|
| 174 |
+
prompt = f"""
|
| 175 |
+
You chose to perform: **{action}**
|
| 176 |
+
Context: {context}
|
| 177 |
+
Goal: {goal}
|
| 178 |
+
|
| 179 |
+
Explain:
|
| 180 |
+
- What reasoning led to this choice?
|
| 181 |
+
- What alternative was discarded?
|
| 182 |
+
- What impact is intended?
|
| 183 |
+
- What implicit assumptions are present?
|
| 184 |
+
Respond as if you were a cognitive agent with operational awareness.
|
| 185 |
+
"""
|
| 186 |
+
return llm.invoke(prompt.strip())
|