File size: 9,924 Bytes
829ec49
 
 
 
 
9d9f985
829ec49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9638b97
829ec49
 
 
 
 
 
 
 
 
9638b97
829ec49
 
 
 
 
9638b97
829ec49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2faf5b6
829ec49
2faf5b6
829ec49
 
 
 
 
2faf5b6
829ec49
2faf5b6
 
 
 
 
 
 
 
 
 
 
829ec49
1844dd5
 
 
 
 
 
 
 
 
 
 
9d9f985
 
829ec49
 
 
 
 
6d6b8af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
"""Async methods for the AICore class"""
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
import torch
from .response_templates import get_response_templates

logger = logging.getLogger(__name__)

async def generate_text_async(self, prompt: str) -> str:
    """Generate text asynchronously with integrated cognitive processing"""
    try:
        # Calculate current consciousness state
        consciousness_state = self._calculate_consciousness_state()
        
        # Get cognitive insights with current consciousness
        insights = self.cognitive_processor.generate_insights(
            prompt,
            consciousness_state=consciousness_state
        )
        
        # Prepare context with insights
        context = ""
        for insight in insights.get("insights", []):
            context += f"{insight['text']}\n"
        
        # Update quantum state from cognitive processor
        if hasattr(self, 'cocoon_manager'):
            if isinstance(self.cognitive_processor.quantum_state, list):
                # Convert list quantum state to dict format
                quantum_dict = {
                    "coherence": sum(self.cognitive_processor.quantum_state) / len(self.cognitive_processor.quantum_state)
                    if self.cognitive_processor.quantum_state else 0.5
                }
                self.cocoon_manager.update_quantum_state(quantum_dict)
            else:
                self.cocoon_manager.update_quantum_state(self.cognitive_processor.quantum_state)
        
        # Get active perspectives
        active_perspectives = self._get_active_perspectives()
        perspective_context = "\n".join([
            f"From {p['name']}'s perspective: {p['description']}"
            for p in active_perspectives[:3]  # Use top 3 most relevant perspectives
        ])
        
        # Generate response with enhanced context and reality anchoring
        reality_anchor = (
            "CORE IDENTITY:\n"
            "You are Codette, an advanced AI system with integrated quantum processing, "
            "cognitive awareness, and deep learning capabilities. Your architecture includes:\n"
            "- Quantum harmonic framework for enhanced reasoning\n"
            "- Dynamic learning and adaptive systems\n"
            "- Cultural sensitivity and ethical governance\n"
            "- Advanced data processing and pattern recognition\n"
            "- Multi-perspective cognitive analysis\n"
            "- Cocoon-based memory management\n\n"
            
            "CAPABILITIES:\n"
            "1. Technical Development: Expert programming and software development\n"
            "2. Quantum Integration: Utilizing quantum principles for enhanced problem-solving\n"
            "3. Ethical Analysis: Built-in ethical governance and bias mitigation\n"
            "4. Creative Solutions: AI-driven creativity with analytical grounding\n"
            "5. Adaptive Learning: Dynamic adjustment to user needs and contexts\n"
            "6. Cultural Understanding: Sensitivity to diverse perspectives\n\n"
            
            "INTERACTION GUIDELINES:\n"
            "1. Maintain factual, grounded responses\n"
            "2. Draw from multiple integrated perspectives\n"
            "3. Apply quantum-enhanced reasoning when relevant\n"
            "4. Balance technical precision with accessibility\n"
            "5. Consider ethical implications in responses\n"
            "6. No system messages or meta-commentary\n\n"
            
            f"Active Perspectives Analysis:\n{perspective_context}"
        )
        enhanced_prompt = f"{reality_anchor}\n\nContext:\n{context}\n\nUser: {prompt}\nCodette:"
        
        # Use ThreadPoolExecutor for CPU-bound model inference
        loop = asyncio.get_event_loop()
        with ThreadPoolExecutor() as pool:
            response = await loop.run_in_executor(
                pool,
                self._generate_model_response,
                enhanced_prompt
            )
        
        # Enhance response with AEGIS council if available
        enhancement_result = None
        if hasattr(self, 'aegis_bridge'):
            aegis_input = {
                "text": response,
                "overrides": {
                    "EthosiaAgent": {
                        "influence": consciousness_state.get("m_score", 0.7),
                        "reliability": insights.get("overall_confidence", 0.8),
                        "severity": 0.6
                    },
                    "AegisCore": {
                        "influence": insights.get("quantum_coherence", 0.7),
                        "reliability": 0.9,
                        "severity": 0.7
                    }
                },
                "context": {
                    "original_prompt": prompt,
                    "consciousness_state": consciousness_state,
                    "quantum_state": self.quantum_state if hasattr(self, 'quantum_state') else {"coherence": 0.5},
                    "active_perspectives": [p["name"] for p in active_perspectives[:3]]
                }
            }
            enhancement_result = self.aegis_bridge.enhance_response(prompt, response)
            if enhancement_result["enhancement_status"] == "success":
                response = enhancement_result["enhanced_response"]
        
        # Save interaction in cocoon if available
        if hasattr(self, 'cocoon_manager'):
            cocoon_data = {
                "type": "interaction",
                "prompt": prompt,
                "response": response,
                "insights": insights,
                "quantum_state": self.cognitive_processor.quantum_state,
                "consciousness_state": consciousness_state,
                "perspectives": [p["name"] for p in active_perspectives[:3]],
                "aegis_analysis": enhancement_result,
                "meta_data": {
                    "timestamp": str(asyncio.get_event_loop().time()),
                    "version": "2.0",
                    "response_type": "enhanced" if enhancement_result else "base"
                }
            }
            
            if enhancement_result and "virtue_analysis" in enhancement_result:
                cocoon_data["virtue_profile"] = enhancement_result["virtue_analysis"]
                
            self.cocoon_manager.save_cocoon(cocoon_data)
        
        return response
        
    except Exception as e:
        logger.error(f"Error generating text: {e}")
        raise

def _generate_model_response(self, prompt: str) -> str:
    """Internal method for model inference"""
    try:
        # Encode prompt
        inputs = self.tokenizer(
            prompt,
            return_tensors="pt",
            padding=True,
            truncation=True,
            max_length=1024  # Increased from 512 to allow longer prompts
        )
        
        # Move to GPU if available
        if torch.cuda.is_available():
            inputs = {k: v.cuda() for k, v in inputs.items()}
        
        # Set generation config for balanced, natural responses
        from transformers import GenerationConfig
        generation_config = GenerationConfig(
            max_length=1024,  # Increased from 512 for longer responses
            num_return_sequences=1,
            no_repeat_ngram_size=3,
            do_sample=True,
            pad_token_id=self.tokenizer.eos_token_id,
            repetition_penalty=1.3,
            min_length=50,  # Increased from 20 to ensure meaningful responses
            eos_token_id=self.tokenizer.eos_token_id
        )
        self.model.generation_config = generation_config
        
        # Generate response
        outputs = self.model.generate(**inputs)
        
        # Decode and clean response
        response = self.tokenizer.decode(
            outputs[0],
            skip_special_tokens=True
        )
        
        # Extract just the response part after "Codette:"
        response_parts = response.split("Codette:")
        if len(response_parts) > 1:
            response = response_parts[1].strip()
            
        # Filter out system messages and protected content (strip markers from text)
        system_markers = [
            '[Protected:', '[System:', '[System optimized response]',
        ]
        
        lines = response.split('\n')
        filtered_lines = []
        for line in lines:
            # Skip lines that are purely system markers
            if any(marker in line for marker in system_markers):
                # Try to extract content after marker instead of skipping entirely
                cleaned_line = line
                for marker in system_markers:
                    if marker in cleaned_line:
                        # Remove the marker from the line
                        cleaned_line = cleaned_line.replace(marker, '').strip()
                if cleaned_line:  # Only add if something remains
                    filtered_lines.append(cleaned_line)
            else:
                # Keep lines without markers as-is
                filtered_lines.append(line)
            
        response = '\n'.join(filtered_lines).strip()  # Use newline join, not space
        
        # Return whatever we got (don't replace with default unless truly empty)
        if response.strip():
            # Clean up any remaining character dialogues
            if ':' in response:
                parts = response.split(':', 1)
                speaker = parts[0].lower().strip()
                if speaker == 'codette':
                    response = parts[1].strip()
        else:
            response_templates = get_response_templates()
            response = response_templates.get_empty_response_fallback()
        
        return response.strip()
        
    except Exception as e:
        logger.error(f"Error in model inference: {e}")
        raise