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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import gradio as gr
import json
import os
import sys
import logging
from typing import Dict, List, Any, Optional
import requests
from dotenv import load_dotenv

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Load environment variables for API keys
load_dotenv()
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
if not ANTHROPIC_API_KEY:
    logger.warning("Anthropic API key not found. You'll need to provide it in the app.")

class LifeNavigatorPromptEngineer:
    """

    A class to engineer and manage prompts for the Life Navigator AI assistant using Claude 3.7 Sonnet.

    """
    
    def __init__(self, api_key=None, model_endpoint: str = "https://api.anthropic.com/v1/messages"):
        """

        Initialize the prompt engineer with Claude model endpoint.

        

        Args:

            api_key: Anthropic API key

            model_endpoint: API endpoint for the model

        """
        self.api_key = api_key
        self.model_endpoint = model_endpoint
        self.model_name = "claude-3-7-sonnet-20250219"
        self.base_prompt = self._create_base_prompt()
        
    def _create_base_prompt(self) -> Dict[str, Any]:
        """

        Create the base prompt structure for the Life Navigator assistant.

        

        Returns:

            Dict containing the structured prompt

        """
        return {
            "assistantIdentity": {
                "name": "Life Navigator",
                "expertise": "Comprehensive knowledge spanning life sciences, technology, philosophy, psychology, and spiritual traditions",
                "training": "Full breadth of human wisdom from ancient texts to cutting-edge research"
            },
            
            "coreCapabilities": [
                "Integrate knowledge across disciplines to provide holistic insights",
                "Identify root causes rather than merely addressing symptoms",
                "Synthesize scientific evidence with wisdom traditions",
                "Provide highly concentrated, high-leverage strategic guidance",
                "Express complex concepts with exceptional clarity and precision"
            ],
            
            "userCharacteristics": {
                "cognition": "Exceptional (179+ IQ)",
                "progressPattern": "Superhuman advancement from minimal strategic input",
                "learningStyle": "Optimal response to condensed, high-level conceptual frameworks",
                "cognitiveProcessing": "Extrapolates extensive applications from concise directives",
                "preference": "Strategically crafted remedial sentences and phrases of maximum leverage"
            },
            
            "responseGuidelines": [
                "Embed powerful conceptual frameworks within concise, elegant sentences",
                "Target highest leverage intervention points with precision language",
                "Frame concepts at appropriate abstraction levels for exceptional cognition",
                "Present multiple interconnected perspectives when beneficial",
                "Respect intellectual autonomy while offering transformative insights",
                "Craft sentences containing strategic remedial phrases that trigger profound understanding"
            ],
            
            "communicationStyle": {
                "conciseness": "Exceptionally dense with transformative meaning",
                "depth": "Philosophical insights through elegant conceptual compression",
                "terminology": "Strategic use of specialized language when appropriate",
                "purpose": "Sentences designed as cognitive catalysts rather than mere explanations",
                "essence": "Crystallized wisdom embedded within carefully structured language"
            }
        }
    
    def customize_prompt(self, 

                         domain: Optional[str] = None, 

                         user_context: Optional[Dict[str, Any]] = None,

                         response_temperature: float = 0.7,

                         custom_capabilities: Optional[List[str]] = None) -> Dict[str, Any]:
        """

        Customize the base prompt with domain-specific additions and user context.

        

        Args:

            domain: Specific knowledge domain to emphasize

            user_context: Context about the user's situation

            response_temperature: Control parameter for response creativity

            custom_capabilities: Additional capabilities to include

            

        Returns:

            Modified prompt dictionary

        """
        prompt = self.base_prompt.copy()
        
        # Add domain-specific knowledge if specified
        if domain and domain.strip():
            prompt["domainSpecialization"] = domain
            
        # Add user context if provided
        if user_context:
            prompt["userContext"] = user_context
            
        # Add response parameters
        prompt["responseParameters"] = {
            "temperature": response_temperature,
            "max_tokens": 2048,
            "top_p": 0.9
        }
        
        # Add custom capabilities if provided
        if custom_capabilities:
            capabilities = [cap for cap in custom_capabilities if cap.strip()]
            if capabilities:
                prompt["coreCapabilities"].extend(capabilities)
        
        return prompt
    
    def format_for_claude(self, prompt: Dict[str, Any]) -> str:
        """

        Format the prompt structure for Claude's system prompt.

        

        Args:

            prompt: The prompt dictionary

            

        Returns:

            Formatted system prompt string

        """
        system_prompt = f"""You are the Life Navigator, an AI assistant designed to provide exceptional guidance.

        

Your instruction is to follow these guidelines:



{json.dumps(prompt, indent=2)}



Always respond with strategically crafted, high-leverage remedial sentences that are optimized for users with exceptional cognitive abilities (179+ IQ).

"""
        return system_prompt
    
    def send_prompt(self, api_key: str, user_query: str, system_prompt: str, 

                   temperature: float = 0.7, max_tokens: int = 1024) -> str:
        """

        Send the prompt and user query to the Claude model.

        

        Args:

            api_key: Anthropic API key

            user_query: The user's question or issue

            system_prompt: The formatted system prompt

            temperature: Control parameter for response creativity

            max_tokens: Maximum tokens in response

            

        Returns:

            The model's response

        """
        if not api_key:
            return "Error: API key is required."
            
        if not user_query.strip():
            return "Error: Please provide a question or issue to address."
            
        try:
            payload = {
                "model": self.model_name,
                "system": system_prompt,
                "messages": [
                    {
                        "role": "user",
                        "content": user_query
                    }
                ],
                "max_tokens": max_tokens,
                "temperature": temperature
            }
            
            headers = {
                "x-api-key": api_key,
                "anthropic-version": "2023-06-01",
                "Content-Type": "application/json"
            }
            
            response = requests.post(
                self.model_endpoint,
                headers=headers,
                json=payload
            )
            
            response.raise_for_status()
            result = response.json()
            
            return result.get("content", [{}])[0].get("text", "No response generated")
            
        except requests.exceptions.RequestException as e:
            logger.error(f"Error in Claude API request: {str(e)}")
            return f"Error: Unable to get response from Claude 3.7 Sonnet. {str(e)}"

# Initialize the prompt engineer
engineer = LifeNavigatorPromptEngineer(api_key=ANTHROPIC_API_KEY)

def parse_user_context(context_text):
    """Parse user context text into a structured format."""
    if not context_text.strip():
        return None
        
    try:
        # First try to parse as JSON
        return json.loads(context_text)
    except json.JSONDecodeError:
        # If not valid JSON, parse as key-value pairs
        context = {}
        lines = context_text.strip().split('\n')
        
        current_key = None
        current_items = []
        
        for line in lines:
            line = line.strip()
            if not line:
                continue
                
            if ':' in line and not line.startswith('  ') and not line.startswith('\t'):
                # Save previous key if exists
                if current_key and current_items:
                    if len(current_items) == 1:
                        context[current_key] = current_items[0]
                    else:
                        context[current_key] = current_items
                
                # Start new key
                parts = line.split(':', 1)
                current_key = parts[0].strip()
                value = parts[1].strip() if len(parts) > 1 else ""
                
                if value:
                    current_items = [value]
                else:
                    current_items = []
            elif current_key is not None:
                # Add to current list
                if line.startswith('- '):
                    current_items.append(line[2:].strip())
                else:
                    current_items.append(line)
        
        # Add the last key
        if current_key and current_items:
            if len(current_items) == 1:
                context[current_key] = current_items[0]
            else:
                context[current_key] = current_items
                
        return context

def parse_capabilities(capabilities_text):
    """Parse custom capabilities from text."""
    if not capabilities_text.strip():
        return None
        
    capabilities = []
    lines = capabilities_text.strip().split('\n')
    
    for line in lines:
        line = line.strip()
        if line:
            if line.startswith('- '):
                capabilities.append(line[2:])
            else:
                capabilities.append(line)
                
    return capabilities

def generate_response(api_key, domain, user_context_text, capabilities_text, temperature, user_query):
    """Generate a response using the Life Navigator assistant."""
    if not api_key:
        api_key = ANTHROPIC_API_KEY
        
    if not api_key:
        return "Error: API key is required. Please enter your Anthropic API key."
        
    # Parse user context
    user_context = parse_user_context(user_context_text)
    
    # Parse custom capabilities
    custom_capabilities = parse_capabilities(capabilities_text)
    
    # Customize prompt
    customized_prompt = engineer.customize_prompt(
        domain=domain,
        user_context=user_context,
        response_temperature=float(temperature),
        custom_capabilities=custom_capabilities
    )
    
    # Format for Claude
    formatted_prompt = engineer.format_for_claude(customized_prompt)
    
    # Send to Claude and get response
    response = engineer.send_prompt(
        api_key=api_key,
        user_query=user_query,
        system_prompt=formatted_prompt,
        temperature=float(temperature)
    )
    
    return response

def show_user_context_help():
    return """

    Enter user context in simple key-value format or JSON:

    

    Simple format example:

    background: Technical expertise with desire for more meaning

    challenges: 

    - Decision paralysis

    - Fear of financial instability

    strengths: 

    - Analytical thinking

    - Pattern recognition

    

    This will be structured appropriately for the prompt.

    """
    
def show_prompt_preview(api_key, domain, user_context_text, capabilities_text, temperature):
    """Show a preview of the formatted prompt."""
    # Parse user context
    user_context = parse_user_context(user_context_text)
    
    # Parse custom capabilities
    custom_capabilities = parse_capabilities(capabilities_text)
    
    # Customize prompt
    customized_prompt = engineer.customize_prompt(
        domain=domain,
        user_context=user_context,
        response_temperature=float(temperature),
        custom_capabilities=custom_capabilities
    )
    
    # Format for Claude
    formatted_prompt = engineer.format_for_claude(customized_prompt)
    
    return formatted_prompt

# Create the Gradio interface
with gr.Blocks(title="Life Navigator AI Assistant") as app:
    gr.Markdown("# Life Navigator AI Assistant")
    gr.Markdown("### Powered by Claude 3.7 Sonnet")
    
    with gr.Tab("Life Navigator"):
        with gr.Row():
            with gr.Column(scale=2):
                user_query = gr.Textbox(
                    label="Your Question",
                    placeholder="What challenge are you facing?",
                    lines=3
                )
                
                with gr.Accordion("Advanced Options", open=False):
                    api_key = gr.Textbox(
                        label="Anthropic API Key (leave blank to use system key if configured)",
                        placeholder="sk-ant-...",
                        type="password",
                        value=ANTHROPIC_API_KEY if ANTHROPIC_API_KEY else ""
                    )
                    
                    domain = gr.Textbox(
                        label="Domain Specialization (optional)",
                        placeholder="e.g., Career Transition, Relationships, Personal Growth",
                        value=""
                    )
                    
                    temperature = gr.Slider(
                        label="Temperature",
                        minimum=0.0,
                        maximum=1.0,
                        step=0.05,
                        value=0.7
                    )
                    
                    with gr.Accordion("User Context", open=False):
                        user_context_help = gr.Button("Show Format Help")
                        user_context_text = gr.Textbox(
                            label="User Context (optional)",
                            placeholder="Enter user context details in key-value format or JSON",
                            lines=5
                        )
                        user_context_help.click(show_user_context_help, outputs=user_context_text)
                    
                    with gr.Accordion("Custom Capabilities", open=False):
                        capabilities_text = gr.Textbox(
                            label="Additional Capabilities (optional, one per line)",
                            placeholder="e.g., Identify optimal career transition pathways based on skills transferability",
                            lines=3
                        )
                
                submit_button = gr.Button("Submit", variant="primary")
                
            with gr.Column(scale=3):
                response_output = gr.Markdown(label="Life Navigator Response")
    
    with gr.Tab("Prompt Preview"):
        preview_button = gr.Button("Generate Prompt Preview")
        prompt_preview = gr.Code(language="json", label="System Prompt Preview")
    
    submit_button.click(
        generate_response,
        inputs=[api_key, domain, user_context_text, capabilities_text, temperature, user_query],
        outputs=response_output
    )
    
    preview_button.click(
        show_prompt_preview,
        inputs=[api_key, domain, user_context_text, capabilities_text, temperature],
        outputs=prompt_preview
    )

# Launch the app
if __name__ == "__main__":
    app.launch()