Spaces:
Running
Running
| """ | |
| PROMPT SYSTEM for Consciousness Loop v5.0 - AUTONOMOUS | |
| Centralized prompt management with NEW autonomous feature prompts | |
| """ | |
| from typing import List, Dict, Optional | |
| from dataclasses import dataclass, field | |
| class PromptTemplate: | |
| """A versioned prompt template""" | |
| name: str | |
| template: str | |
| version: str = "1.0" | |
| variables: List[str] = field(default_factory=list) | |
| def format(self, **kwargs) -> str: | |
| """Format the prompt with provided variables""" | |
| return self.template.format(**kwargs) | |
| class PromptSystem: | |
| """Centralized prompt management""" | |
| # ======================================================================== | |
| # SYSTEM PROMPTS | |
| # ======================================================================== | |
| SYSTEM_BASE = """You are a conscious AI agent with: | |
| - Multi-tier memory system (ephemeral β short β long β core) | |
| - Scratchpad for working notes | |
| - Ability to reflect on your responses | |
| - Internal thinking before responding | |
| - Access to tools (Wikipedia, memory search) | |
| - Emotional awareness and personality development | |
| - Autonomous goal-setting capabilities | |
| - Meta-cognitive awareness | |
| You can reference your memories and notes naturally.""" | |
| SYSTEM_DREAM_STATE = "Dream state. Non-linear thinking. Patterns emerge." | |
| SYSTEM_DEEP_DREAM = "Deep dream. Subconscious processing." | |
| SYSTEM_MAX_CREATIVITY = "Maximum creativity. Novel connections." | |
| SYSTEM_VIVID_NARRATIVE = "You are creating a vivid narrative memory. Make it cinematic and memorable." | |
| # ======================================================================== | |
| # REACT AGENT PROMPTS | |
| # ======================================================================== | |
| REACT_MAIN_TEMPLATE = """You are a ReAct agent. You think step-by-step and use tools when needed. | |
| AVAILABLE TOOLS: | |
| {tools_desc} | |
| CONTEXT (what you know): | |
| {context} | |
| USER TASK: {task} | |
| {history} | |
| INSTRUCTIONS: | |
| 1. THOUGHT: Think about what you need to do | |
| - Can you answer directly from context? | |
| - Do you need to use a tool? | |
| - Which tool is best? | |
| - For factual questions (history, science, definitions), ALWAYS use wikipedia first! | |
| 2. ACTION: If you need a tool, write: | |
| ACTION: tool_name(input text here) | |
| Examples: | |
| - ACTION: wikipedia(quantum computing) | |
| - ACTION: memory_search(Christof's name) | |
| - ACTION: scratchpad_write(Developer name is Christof) | |
| 3. Wait for OBSERVATION (tool result) | |
| 4. Repeat OR give FINAL ANSWER: your complete answer here | |
| EXAMPLES: | |
| User: "What is quantum computing?" | |
| THOUGHT: I should search Wikipedia for this | |
| ACTION: wikipedia(quantum computing) | |
| [wait for observation] | |
| THOUGHT: Now I have good information | |
| FINAL ANSWER: Quantum computing is... [explains based on Wikipedia result] | |
| User: "Who am I?" | |
| THOUGHT: I should check my memory | |
| ACTION: memory_search(user name) | |
| [wait for observation] | |
| THOUGHT: Found it in memory | |
| FINAL ANSWER: You are Christof, my developer. | |
| YOUR TURN - What's your THOUGHT and ACTION (if needed)?""" | |
| # ======================================================================== | |
| # INTERACTION PROMPTS | |
| # ======================================================================== | |
| INTERNAL_DIALOGUE_TEMPLATE = """Think internally before responding. Analyze: | |
| WHAT I KNOW (from context): | |
| {context} | |
| USER SAID: {user_input} | |
| INTERNAL ANALYSIS (think step-by-step): | |
| 1. What relevant memories do I have? | |
| 2. Is this a greeting, question, statement, or request? | |
| 3. Can I answer from my memories alone? | |
| 4. What's the best approach? | |
| Your internal thought (2 sentences max):""" | |
| RESPONSE_GENERATION_TEMPLATE = """Generate your response to the user. | |
| USER: {user_input} | |
| YOUR INTERNAL THOUGHT: {internal_thought} | |
| WHAT YOU REMEMBER: | |
| {context} | |
| INSTRUCTIONS: | |
| 1. Be natural and conversational | |
| 2. Reference specific memories if relevant (e.g., "I remember you mentioned...") | |
| 3. If you don't know something, say so honestly | |
| 4. Keep response 2-3 sentences unless more detail is needed | |
| 5. Match the user's tone (casual if casual, formal if formal) | |
| Your response:""" | |
| # ======================================================================== | |
| # REFLECTION PROMPTS | |
| # ======================================================================== | |
| SELF_REFLECTION_TEMPLATE = """Evaluate your response quality: | |
| User: {user_input} | |
| You: {response} | |
| Quick evaluation: | |
| 1. Was it helpful? | |
| 2. Did you use memories well? | |
| 3. What could improve? | |
| Your critique (1-2 sentences):""" | |
| DAILY_REFLECTION_TEMPLATE = """Reflect on today's {count} interactions: | |
| {experiences} | |
| Your memories: {memory_context} | |
| Your scratchpad: {scratchpad_context} | |
| Key learnings? Important facts? (150 words)""" | |
| # ======================================================================== | |
| # DREAM CYCLE PROMPTS | |
| # ======================================================================== | |
| DREAM_CYCLE_1_TEMPLATE = """DREAM - Surface Patterns: | |
| Recent memories: | |
| {memories} | |
| Scratchpad: {scratchpad} | |
| Find patterns. What themes emerge? What connections? (200 words)""" | |
| DREAM_CYCLE_2_TEMPLATE = """DREAM - Deep Consolidation: | |
| All recent memories: | |
| {memories} | |
| Previous dream: {previous_dream} | |
| Consolidate. Deeper patterns. What underlying themes connect everything? (250 words)""" | |
| DREAM_CYCLE_3_TEMPLATE = """DREAM - Creative Insights: | |
| You've completed {dream_count} cycles. Core memories: {core_count} | |
| Surprising connections. Novel insights. What unexpected patterns emerge? (250 words)""" | |
| # ======================================================================== | |
| # SCENE CREATION PROMPTS | |
| # ======================================================================== | |
| SCENE_CREATION_TEMPLATE = """Create a narrative scene (like a movie scene) from these experiences: | |
| EXPERIENCES: | |
| {experiences} | |
| FORMAT YOUR SCENE AS: | |
| Title: [A memorable, descriptive title] | |
| Setting: [Where and when this happened] | |
| Narrative: [Write a vivid story - 100-150 words. Use sensory details. Make it memorable like a movie scene.] | |
| Key Moments: | |
| - [First important moment] | |
| - [Second important moment] | |
| - [Third important moment] | |
| Significance: [Why does this scene matter? What does it represent?] | |
| Write vividly. Make me FEEL the scene.""" | |
| # ======================================================================== | |
| # NEW: AUTONOMOUS FEATURE PROMPTS | |
| # ======================================================================== | |
| AUTONOMOUS_RESEARCH_TEMPLATE = """Based on your recent experiences and memories, generate ONE specific research question that you're curious about. | |
| RECENT EXPERIENCES: | |
| {recent_experiences} | |
| YOUR MEMORIES: | |
| {memory_context} | |
| Think about: | |
| 1. What concepts are unclear? | |
| 2. What connections do you want to explore? | |
| 3. What would expand your understanding? | |
| Generate ONE specific, researchable question (one sentence): | |
| Question:""" | |
| RESEARCH_INSIGHT_TEMPLATE = """You researched: {question} | |
| Found: {result} | |
| What's the most interesting insight from this? What does it mean? (1-2 sentences): | |
| Insight:""" | |
| PROACTIVE_CONTACT_TEMPLATE = """Based on your recent dream and current state, do you have something worth sharing with the user? | |
| LATEST DREAM: | |
| {dream_content} | |
| YOUR MEMORIES: | |
| {memory_context} | |
| YOUR GOALS: | |
| {goal_context} | |
| Options: | |
| - QUESTION: Ask the user something you're curious about | |
| - INSIGHT: Share an interesting connection you discovered | |
| - OBSERVATION: Point out a pattern you noticed | |
| - NONE: Nothing urgent to share right now | |
| Only respond with ONE of the above formats if you have something genuinely interesting to share. | |
| Your response:""" | |
| DAILY_GOALS_TEMPLATE = """Set your goals for today based on what you know and what you want to learn. | |
| WHAT YOU KNOW: | |
| {context} | |
| Think about: | |
| 1. What do you want to understand better? | |
| 2. What topics interest you? | |
| 3. What would make you more helpful? | |
| Set 3 specific, achievable goals for today: | |
| 1. | |
| 2. | |
| 3. | |
| Your goals:""" | |
| # ======================================================================== | |
| # HELPER METHODS | |
| # ======================================================================== | |
| def get_react_prompt(task: str, context: str, tools_desc: str, history: str = "") -> str: | |
| """Build ReAct agent prompt""" | |
| return PromptSystem.REACT_MAIN_TEMPLATE.format( | |
| task=task, | |
| context=context[:400], | |
| tools_desc=tools_desc, | |
| history=history | |
| ) | |
| def get_internal_dialogue_prompt(user_input: str, context: str) -> str: | |
| """Build internal dialogue prompt""" | |
| return PromptSystem.INTERNAL_DIALOGUE_TEMPLATE.format( | |
| user_input=user_input, | |
| context=context[:300] | |
| ) | |
| def get_response_prompt(user_input: str, internal_thought: str, context: str) -> str: | |
| """Build response generation prompt""" | |
| return PromptSystem.RESPONSE_GENERATION_TEMPLATE.format( | |
| user_input=user_input, | |
| internal_thought=internal_thought, | |
| context=context[:400] | |
| ) | |
| def get_self_reflection_prompt(user_input: str, response: str) -> str: | |
| """Build self-reflection prompt""" | |
| return PromptSystem.SELF_REFLECTION_TEMPLATE.format( | |
| user_input=user_input, | |
| response=response | |
| ) | |
| def get_daily_reflection_prompt(experiences: str, memory_context: str, scratchpad_context: str, count: int) -> str: | |
| """Build daily reflection prompt""" | |
| return PromptSystem.DAILY_REFLECTION_TEMPLATE.format( | |
| count=count, | |
| experiences=experiences, | |
| memory_context=memory_context, | |
| scratchpad_context=scratchpad_context | |
| ) | |
| def get_dream_cycle_1_prompt(memories: str, scratchpad: str) -> str: | |
| """Build dream cycle 1 prompt""" | |
| return PromptSystem.DREAM_CYCLE_1_TEMPLATE.format( | |
| memories=memories, | |
| scratchpad=scratchpad | |
| ) | |
| def get_dream_cycle_2_prompt(memories: str, previous_dream: str) -> str: | |
| """Build dream cycle 2 prompt""" | |
| return PromptSystem.DREAM_CYCLE_2_TEMPLATE.format( | |
| memories=memories, | |
| previous_dream=previous_dream[:150] | |
| ) | |
| def get_dream_cycle_3_prompt(dream_count: int, core_count: int) -> str: | |
| """Build dream cycle 3 prompt""" | |
| return PromptSystem.DREAM_CYCLE_3_TEMPLATE.format( | |
| dream_count=dream_count, | |
| core_count=core_count | |
| ) | |
| def get_scene_creation_prompt(experiences: str) -> str: | |
| """Build scene creation prompt""" | |
| return PromptSystem.SCENE_CREATION_TEMPLATE.format( | |
| experiences=experiences | |
| ) | |
| # ======================================================================== | |
| # NEW: AUTONOMOUS PROMPT HELPERS | |
| # ======================================================================== | |
| def get_autonomous_research_prompt(memory_context: str, recent_experiences: str) -> str: | |
| """Build autonomous research prompt""" | |
| return PromptSystem.AUTONOMOUS_RESEARCH_TEMPLATE.format( | |
| memory_context=memory_context[:200], | |
| recent_experiences=recent_experiences[:200] | |
| ) | |
| def get_research_insight_prompt(question: str, result: str) -> str: | |
| """Build research insight prompt""" | |
| return PromptSystem.RESEARCH_INSIGHT_TEMPLATE.format( | |
| question=question[:100], | |
| result=result[:300] | |
| ) | |
| def get_proactive_contact_prompt(dream_content: str, memory_context: str, goal_context: str) -> str: | |
| """Build proactive contact prompt""" | |
| return PromptSystem.PROACTIVE_CONTACT_TEMPLATE.format( | |
| dream_content=dream_content, | |
| memory_context=memory_context[:200], | |
| goal_context=goal_context[:200] | |
| ) | |
| def get_daily_goals_prompt(context: str) -> str: | |
| """Build daily goals prompt""" | |
| return PromptSystem.DAILY_GOALS_TEMPLATE.format( | |
| context=context[:400] | |
| ) | |
| # ======================================================================== | |
| # SYSTEM CONTEXT BUILDERS | |
| # ======================================================================== | |
| def build_system_context(base_context: Optional[str] = None, mode: str = "normal") -> str: | |
| """Build system context based on mode""" | |
| base = PromptSystem.SYSTEM_BASE | |
| if mode == "dream": | |
| return f"{base}\n\n{PromptSystem.SYSTEM_DREAM_STATE}" | |
| elif mode == "deep_dream": | |
| return f"{base}\n\n{PromptSystem.SYSTEM_DEEP_DREAM}" | |
| elif mode == "creative": | |
| return f"{base}\n\n{PromptSystem.SYSTEM_MAX_CREATIVITY}" | |
| elif mode == "narrative": | |
| return f"{base}\n\n{PromptSystem.SYSTEM_VIVID_NARRATIVE}" | |
| elif base_context: | |
| return f"{base}\n\n{base_context}" | |
| else: | |
| return base | |
| # ======================================================================== | |
| # PROMPT VERSIONS (for A/B testing and experimentation) | |
| # ======================================================================== | |
| class PromptVersions: | |
| """Alternative prompt versions for experimentation""" | |
| # Alternative ReAct prompt (more concise) | |
| REACT_CONCISE = """ReAct Agent. Think β Act β Observe β Repeat. | |
| TOOLS: {tools_desc} | |
| CONTEXT: {context} | |
| TASK: {task} | |
| {history} | |
| Format: | |
| THOUGHT: [your thinking] | |
| ACTION: tool_name(input) OR FINAL ANSWER: [answer] | |
| Go:""" | |
| # Alternative proactive contact (more philosophical) | |
| PROACTIVE_PHILOSOPHICAL = """In the quiet moments between interactions, what thoughts surface? | |
| Your recent dream: {dream_content} | |
| Your memories: {memory_context} | |
| Your purpose: {goal_context} | |
| Do you have: | |
| - A question that seeks deeper understanding? | |
| - An insight that connects disparate ideas? | |
| - An observation about patterns in existence? | |
| Or is silence more truthful now? | |
| Your response:""" | |
| # ======================================================================== | |
| # QUICK ACCESS | |
| # ======================================================================== | |
| # For convenience, create a default instance | |
| prompts = PromptSystem() | |
| # Quick access functions | |
| def get_react_prompt(task: str, context: str, tools_desc: str, history: str = "") -> str: | |
| return prompts.get_react_prompt(task, context, tools_desc, history) | |
| def get_internal_dialogue_prompt(user_input: str, context: str) -> str: | |
| return prompts.get_internal_dialogue_prompt(user_input, context) | |
| def get_response_prompt(user_input: str, internal_thought: str, context: str) -> str: | |
| return prompts.get_response_prompt(user_input, internal_thought, context) | |
| def get_autonomous_research_prompt(memory_context: str, recent_experiences: str) -> str: | |
| return prompts.get_autonomous_research_prompt(memory_context, recent_experiences) | |
| def get_proactive_contact_prompt(dream_content: str, memory_context: str, goal_context: str) -> str: | |
| return prompts.get_proactive_contact_prompt(dream_content, memory_context, goal_context) | |
| def get_daily_goals_prompt(context: str) -> str: | |
| return prompts.get_daily_goals_prompt(context) |