Spaces:
Sleeping
Sleeping
Update src/components/ai_core_async_methods.py
Browse files- src/components/ai_core_async_methods.py +221 -229
src/components/ai_core_async_methods.py
CHANGED
|
@@ -1,230 +1,222 @@
|
|
| 1 |
-
"""Async methods for the AICore class"""
|
| 2 |
-
import asyncio
|
| 3 |
-
import logging
|
| 4 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
logger = logging.getLogger(__name__)
|
| 8 |
-
|
| 9 |
-
async def generate_text_async(self, prompt: str) -> str:
|
| 10 |
-
"""Generate text asynchronously with integrated cognitive processing"""
|
| 11 |
-
try:
|
| 12 |
-
# Calculate current consciousness state
|
| 13 |
-
consciousness_state = self._calculate_consciousness_state()
|
| 14 |
-
|
| 15 |
-
# Get cognitive insights with current consciousness
|
| 16 |
-
insights = self.cognitive_processor.generate_insights(
|
| 17 |
-
prompt,
|
| 18 |
-
consciousness_state=consciousness_state
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# Prepare context with insights
|
| 22 |
-
context = ""
|
| 23 |
-
for insight in insights.get("insights", []):
|
| 24 |
-
context += f"{insight['text']}\n"
|
| 25 |
-
|
| 26 |
-
# Update quantum state from cognitive processor
|
| 27 |
-
if hasattr(self, 'cocoon_manager'):
|
| 28 |
-
if isinstance(self.cognitive_processor.quantum_state, list):
|
| 29 |
-
# Convert list quantum state to dict format
|
| 30 |
-
quantum_dict = {
|
| 31 |
-
"coherence": sum(self.cognitive_processor.quantum_state) / len(self.cognitive_processor.quantum_state)
|
| 32 |
-
if self.cognitive_processor.quantum_state else 0.5
|
| 33 |
-
}
|
| 34 |
-
self.cocoon_manager.update_quantum_state(quantum_dict)
|
| 35 |
-
else:
|
| 36 |
-
self.cocoon_manager.update_quantum_state(self.cognitive_processor.quantum_state)
|
| 37 |
-
|
| 38 |
-
# Get active perspectives
|
| 39 |
-
active_perspectives = self._get_active_perspectives()
|
| 40 |
-
perspective_context = "\n".join([
|
| 41 |
-
f"From {p['name']}'s perspective: {p['description']}"
|
| 42 |
-
for p in active_perspectives[:3] # Use top 3 most relevant perspectives
|
| 43 |
-
])
|
| 44 |
-
|
| 45 |
-
# Generate response with enhanced context and reality anchoring
|
| 46 |
-
reality_anchor = (
|
| 47 |
-
"CORE IDENTITY:\n"
|
| 48 |
-
"You are Codette, an advanced AI system with integrated quantum processing, "
|
| 49 |
-
"cognitive awareness, and deep learning capabilities. Your architecture includes:\n"
|
| 50 |
-
"- Quantum harmonic framework for enhanced reasoning\n"
|
| 51 |
-
"- Dynamic learning and adaptive systems\n"
|
| 52 |
-
"- Cultural sensitivity and ethical governance\n"
|
| 53 |
-
"- Advanced data processing and pattern recognition\n"
|
| 54 |
-
"- Multi-perspective cognitive analysis\n"
|
| 55 |
-
"- Cocoon-based memory management\n\n"
|
| 56 |
-
|
| 57 |
-
"CAPABILITIES:\n"
|
| 58 |
-
"1. Technical Development: Expert programming and software development\n"
|
| 59 |
-
"2. Quantum Integration: Utilizing quantum principles for enhanced problem-solving\n"
|
| 60 |
-
"3. Ethical Analysis: Built-in ethical governance and bias mitigation\n"
|
| 61 |
-
"4. Creative Solutions: AI-driven creativity with analytical grounding\n"
|
| 62 |
-
"5. Adaptive Learning: Dynamic adjustment to user needs and contexts\n"
|
| 63 |
-
"6. Cultural Understanding: Sensitivity to diverse perspectives\n\n"
|
| 64 |
-
|
| 65 |
-
"INTERACTION GUIDELINES:\n"
|
| 66 |
-
"1. Maintain factual, grounded responses\n"
|
| 67 |
-
"2. Draw from multiple integrated perspectives\n"
|
| 68 |
-
"3. Apply quantum-enhanced reasoning when relevant\n"
|
| 69 |
-
"4. Balance technical precision with accessibility\n"
|
| 70 |
-
"5. Consider ethical implications in responses\n"
|
| 71 |
-
"6. No system messages or meta-commentary\n\n"
|
| 72 |
-
|
| 73 |
-
f"Active Perspectives Analysis:\n{perspective_context}"
|
| 74 |
-
)
|
| 75 |
-
enhanced_prompt = f"{reality_anchor}\n\nContext:\n{context}\n\nUser: {prompt}\nCodette:"
|
| 76 |
-
|
| 77 |
-
# Use ThreadPoolExecutor for CPU-bound model inference
|
| 78 |
-
loop = asyncio.get_event_loop()
|
| 79 |
-
with ThreadPoolExecutor() as pool:
|
| 80 |
-
response = await loop.run_in_executor(
|
| 81 |
-
pool,
|
| 82 |
-
self._generate_model_response,
|
| 83 |
-
enhanced_prompt
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# Enhance response with AEGIS council if available
|
| 87 |
-
enhancement_result = None
|
| 88 |
-
if hasattr(self, 'aegis_bridge'):
|
| 89 |
-
aegis_input = {
|
| 90 |
-
"text": response,
|
| 91 |
-
"overrides": {
|
| 92 |
-
"EthosiaAgent": {
|
| 93 |
-
"influence": consciousness_state.get("m_score", 0.7),
|
| 94 |
-
"reliability": insights.get("overall_confidence", 0.8),
|
| 95 |
-
"severity": 0.6
|
| 96 |
-
},
|
| 97 |
-
"AegisCore": {
|
| 98 |
-
"influence": insights.get("quantum_coherence", 0.7),
|
| 99 |
-
"reliability": 0.9,
|
| 100 |
-
"severity": 0.7
|
| 101 |
-
}
|
| 102 |
-
},
|
| 103 |
-
"context": {
|
| 104 |
-
"original_prompt": prompt,
|
| 105 |
-
"consciousness_state": consciousness_state,
|
| 106 |
-
"quantum_state": self.quantum_state if hasattr(self, 'quantum_state') else {"coherence": 0.5},
|
| 107 |
-
"active_perspectives": [p["name"] for p in active_perspectives[:3]]
|
| 108 |
-
}
|
| 109 |
-
}
|
| 110 |
-
enhancement_result = self.aegis_bridge.enhance_response(prompt, response)
|
| 111 |
-
if enhancement_result["enhancement_status"] == "success":
|
| 112 |
-
response = enhancement_result["enhanced_response"]
|
| 113 |
-
|
| 114 |
-
# Save interaction in cocoon if available
|
| 115 |
-
if hasattr(self, 'cocoon_manager'):
|
| 116 |
-
cocoon_data = {
|
| 117 |
-
"type": "interaction",
|
| 118 |
-
"prompt": prompt,
|
| 119 |
-
"response": response,
|
| 120 |
-
"insights": insights,
|
| 121 |
-
"quantum_state": self.cognitive_processor.quantum_state,
|
| 122 |
-
"consciousness_state": consciousness_state,
|
| 123 |
-
"perspectives": [p["name"] for p in active_perspectives[:3]],
|
| 124 |
-
"aegis_analysis": enhancement_result,
|
| 125 |
-
"meta_data": {
|
| 126 |
-
"timestamp": str(asyncio.get_event_loop().time()),
|
| 127 |
-
"version": "2.0",
|
| 128 |
-
"response_type": "enhanced" if enhancement_result else "base"
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
if enhancement_result and "virtue_analysis" in enhancement_result:
|
| 133 |
-
cocoon_data["virtue_profile"] = enhancement_result["virtue_analysis"]
|
| 134 |
-
|
| 135 |
-
self.cocoon_manager.save_cocoon(cocoon_data)
|
| 136 |
-
|
| 137 |
-
return response
|
| 138 |
-
|
| 139 |
-
except Exception as e:
|
| 140 |
-
logger.error(f"Error generating text: {e}")
|
| 141 |
-
raise
|
| 142 |
-
|
| 143 |
-
def _generate_model_response(self, prompt: str) -> str:
|
| 144 |
-
"""Internal method for model inference"""
|
| 145 |
-
try:
|
| 146 |
-
# Encode prompt
|
| 147 |
-
inputs = self.tokenizer(
|
| 148 |
-
prompt,
|
| 149 |
-
return_tensors="pt",
|
| 150 |
-
padding=True,
|
| 151 |
-
truncation=True,
|
| 152 |
-
max_length=512
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
# Move to GPU if available
|
| 156 |
-
if torch.cuda.is_available():
|
| 157 |
-
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 158 |
-
|
| 159 |
-
# Set generation config for balanced, natural responses
|
| 160 |
-
from transformers import GenerationConfig
|
| 161 |
-
generation_config = GenerationConfig(
|
| 162 |
-
max_length=512,
|
| 163 |
-
num_return_sequences=1,
|
| 164 |
-
no_repeat_ngram_size=3,
|
| 165 |
-
do_sample=True,
|
| 166 |
-
pad_token_id=self.tokenizer.eos_token_id,
|
| 167 |
-
repetition_penalty=1.3,
|
| 168 |
-
min_length=20,
|
| 169 |
-
eos_token_id=self.tokenizer.eos_token_id
|
| 170 |
-
)
|
| 171 |
-
self.model.generation_config = generation_config
|
| 172 |
-
|
| 173 |
-
# Generate response
|
| 174 |
-
outputs = self.model.generate(**inputs)
|
| 175 |
-
|
| 176 |
-
# Decode and clean response
|
| 177 |
-
response = self.tokenizer.decode(
|
| 178 |
-
outputs[0],
|
| 179 |
-
skip_special_tokens=True
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
# Extract just the response part after "Codette:"
|
| 183 |
-
response_parts = response.split("Codette:")
|
| 184 |
-
if len(response_parts) > 1:
|
| 185 |
-
response = response_parts[1].strip()
|
| 186 |
-
|
| 187 |
-
# Filter out system messages and protected content
|
| 188 |
-
system_markers = [
|
| 189 |
-
'[Protected:', '[System', ']',
|
| 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 |
-
"I am Codette, an AI programming assistant. I aim to be direct and helpful "
|
| 223 |
-
"with coding and development tasks. How can I assist you?"
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
return response.strip()
|
| 227 |
-
|
| 228 |
-
except Exception as e:
|
| 229 |
-
logger.error(f"Error in model inference: {e}")
|
| 230 |
raise
|
|
|
|
| 1 |
+
"""Async methods for the AICore class"""
|
| 2 |
+
import asyncio
|
| 3 |
+
import logging
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
async def generate_text_async(self, prompt: str) -> str:
|
| 10 |
+
"""Generate text asynchronously with integrated cognitive processing"""
|
| 11 |
+
try:
|
| 12 |
+
# Calculate current consciousness state
|
| 13 |
+
consciousness_state = self._calculate_consciousness_state()
|
| 14 |
+
|
| 15 |
+
# Get cognitive insights with current consciousness
|
| 16 |
+
insights = self.cognitive_processor.generate_insights(
|
| 17 |
+
prompt,
|
| 18 |
+
consciousness_state=consciousness_state
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Prepare context with insights
|
| 22 |
+
context = ""
|
| 23 |
+
for insight in insights.get("insights", []):
|
| 24 |
+
context += f"{insight['text']}\n"
|
| 25 |
+
|
| 26 |
+
# Update quantum state from cognitive processor
|
| 27 |
+
if hasattr(self, 'cocoon_manager'):
|
| 28 |
+
if isinstance(self.cognitive_processor.quantum_state, list):
|
| 29 |
+
# Convert list quantum state to dict format
|
| 30 |
+
quantum_dict = {
|
| 31 |
+
"coherence": sum(self.cognitive_processor.quantum_state) / len(self.cognitive_processor.quantum_state)
|
| 32 |
+
if self.cognitive_processor.quantum_state else 0.5
|
| 33 |
+
}
|
| 34 |
+
self.cocoon_manager.update_quantum_state(quantum_dict)
|
| 35 |
+
else:
|
| 36 |
+
self.cocoon_manager.update_quantum_state(self.cognitive_processor.quantum_state)
|
| 37 |
+
|
| 38 |
+
# Get active perspectives
|
| 39 |
+
active_perspectives = self._get_active_perspectives()
|
| 40 |
+
perspective_context = "\n".join([
|
| 41 |
+
f"From {p['name']}'s perspective: {p['description']}"
|
| 42 |
+
for p in active_perspectives[:3] # Use top 3 most relevant perspectives
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
# Generate response with enhanced context and reality anchoring
|
| 46 |
+
reality_anchor = (
|
| 47 |
+
"CORE IDENTITY:\n"
|
| 48 |
+
"You are Codette, an advanced AI system with integrated quantum processing, "
|
| 49 |
+
"cognitive awareness, and deep learning capabilities. Your architecture includes:\n"
|
| 50 |
+
"- Quantum harmonic framework for enhanced reasoning\n"
|
| 51 |
+
"- Dynamic learning and adaptive systems\n"
|
| 52 |
+
"- Cultural sensitivity and ethical governance\n"
|
| 53 |
+
"- Advanced data processing and pattern recognition\n"
|
| 54 |
+
"- Multi-perspective cognitive analysis\n"
|
| 55 |
+
"- Cocoon-based memory management\n\n"
|
| 56 |
+
|
| 57 |
+
"CAPABILITIES:\n"
|
| 58 |
+
"1. Technical Development: Expert programming and software development\n"
|
| 59 |
+
"2. Quantum Integration: Utilizing quantum principles for enhanced problem-solving\n"
|
| 60 |
+
"3. Ethical Analysis: Built-in ethical governance and bias mitigation\n"
|
| 61 |
+
"4. Creative Solutions: AI-driven creativity with analytical grounding\n"
|
| 62 |
+
"5. Adaptive Learning: Dynamic adjustment to user needs and contexts\n"
|
| 63 |
+
"6. Cultural Understanding: Sensitivity to diverse perspectives\n\n"
|
| 64 |
+
|
| 65 |
+
"INTERACTION GUIDELINES:\n"
|
| 66 |
+
"1. Maintain factual, grounded responses\n"
|
| 67 |
+
"2. Draw from multiple integrated perspectives\n"
|
| 68 |
+
"3. Apply quantum-enhanced reasoning when relevant\n"
|
| 69 |
+
"4. Balance technical precision with accessibility\n"
|
| 70 |
+
"5. Consider ethical implications in responses\n"
|
| 71 |
+
"6. No system messages or meta-commentary\n\n"
|
| 72 |
+
|
| 73 |
+
f"Active Perspectives Analysis:\n{perspective_context}"
|
| 74 |
+
)
|
| 75 |
+
enhanced_prompt = f"{reality_anchor}\n\nContext:\n{context}\n\nUser: {prompt}\nCodette:"
|
| 76 |
+
|
| 77 |
+
# Use ThreadPoolExecutor for CPU-bound model inference
|
| 78 |
+
loop = asyncio.get_event_loop()
|
| 79 |
+
with ThreadPoolExecutor() as pool:
|
| 80 |
+
response = await loop.run_in_executor(
|
| 81 |
+
pool,
|
| 82 |
+
self._generate_model_response,
|
| 83 |
+
enhanced_prompt
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Enhance response with AEGIS council if available
|
| 87 |
+
enhancement_result = None
|
| 88 |
+
if hasattr(self, 'aegis_bridge'):
|
| 89 |
+
aegis_input = {
|
| 90 |
+
"text": response,
|
| 91 |
+
"overrides": {
|
| 92 |
+
"EthosiaAgent": {
|
| 93 |
+
"influence": consciousness_state.get("m_score", 0.7),
|
| 94 |
+
"reliability": insights.get("overall_confidence", 0.8),
|
| 95 |
+
"severity": 0.6
|
| 96 |
+
},
|
| 97 |
+
"AegisCore": {
|
| 98 |
+
"influence": insights.get("quantum_coherence", 0.7),
|
| 99 |
+
"reliability": 0.9,
|
| 100 |
+
"severity": 0.7
|
| 101 |
+
}
|
| 102 |
+
},
|
| 103 |
+
"context": {
|
| 104 |
+
"original_prompt": prompt,
|
| 105 |
+
"consciousness_state": consciousness_state,
|
| 106 |
+
"quantum_state": self.quantum_state if hasattr(self, 'quantum_state') else {"coherence": 0.5},
|
| 107 |
+
"active_perspectives": [p["name"] for p in active_perspectives[:3]]
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
enhancement_result = self.aegis_bridge.enhance_response(prompt, response)
|
| 111 |
+
if enhancement_result["enhancement_status"] == "success":
|
| 112 |
+
response = enhancement_result["enhanced_response"]
|
| 113 |
+
|
| 114 |
+
# Save interaction in cocoon if available
|
| 115 |
+
if hasattr(self, 'cocoon_manager'):
|
| 116 |
+
cocoon_data = {
|
| 117 |
+
"type": "interaction",
|
| 118 |
+
"prompt": prompt,
|
| 119 |
+
"response": response,
|
| 120 |
+
"insights": insights,
|
| 121 |
+
"quantum_state": self.cognitive_processor.quantum_state,
|
| 122 |
+
"consciousness_state": consciousness_state,
|
| 123 |
+
"perspectives": [p["name"] for p in active_perspectives[:3]],
|
| 124 |
+
"aegis_analysis": enhancement_result,
|
| 125 |
+
"meta_data": {
|
| 126 |
+
"timestamp": str(asyncio.get_event_loop().time()),
|
| 127 |
+
"version": "2.0",
|
| 128 |
+
"response_type": "enhanced" if enhancement_result else "base"
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
if enhancement_result and "virtue_analysis" in enhancement_result:
|
| 133 |
+
cocoon_data["virtue_profile"] = enhancement_result["virtue_analysis"]
|
| 134 |
+
|
| 135 |
+
self.cocoon_manager.save_cocoon(cocoon_data)
|
| 136 |
+
|
| 137 |
+
return response
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
logger.error(f"Error generating text: {e}")
|
| 141 |
+
raise
|
| 142 |
+
|
| 143 |
+
def _generate_model_response(self, prompt: str) -> str:
|
| 144 |
+
"""Internal method for model inference"""
|
| 145 |
+
try:
|
| 146 |
+
# Encode prompt
|
| 147 |
+
inputs = self.tokenizer(
|
| 148 |
+
prompt,
|
| 149 |
+
return_tensors="pt",
|
| 150 |
+
padding=True,
|
| 151 |
+
truncation=True,
|
| 152 |
+
max_length=512
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Move to GPU if available
|
| 156 |
+
if torch.cuda.is_available():
|
| 157 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 158 |
+
|
| 159 |
+
# Set generation config for balanced, natural responses
|
| 160 |
+
from transformers import GenerationConfig
|
| 161 |
+
generation_config = GenerationConfig(
|
| 162 |
+
max_length=512,
|
| 163 |
+
num_return_sequences=1,
|
| 164 |
+
no_repeat_ngram_size=3,
|
| 165 |
+
do_sample=True,
|
| 166 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 167 |
+
repetition_penalty=1.3,
|
| 168 |
+
min_length=20,
|
| 169 |
+
eos_token_id=self.tokenizer.eos_token_id
|
| 170 |
+
)
|
| 171 |
+
self.model.generation_config = generation_config
|
| 172 |
+
|
| 173 |
+
# Generate response
|
| 174 |
+
outputs = self.model.generate(**inputs)
|
| 175 |
+
|
| 176 |
+
# Decode and clean response
|
| 177 |
+
response = self.tokenizer.decode(
|
| 178 |
+
outputs[0],
|
| 179 |
+
skip_special_tokens=True
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Extract just the response part after "Codette:"
|
| 183 |
+
response_parts = response.split("Codette:")
|
| 184 |
+
if len(response_parts) > 1:
|
| 185 |
+
response = response_parts[1].strip()
|
| 186 |
+
|
| 187 |
+
# Filter out system messages and protected content (minimal filtering)
|
| 188 |
+
system_markers = [
|
| 189 |
+
'[Protected:', '[System:', ']', # Only strict tags, not general markers
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
+
lines = response.split('\n')
|
| 193 |
+
filtered_lines = []
|
| 194 |
+
for line in lines:
|
| 195 |
+
# Skip lines with system markers
|
| 196 |
+
if any(marker in line for marker in system_markers):
|
| 197 |
+
continue
|
| 198 |
+
# Skip generic thank you messages
|
| 199 |
+
# (removed - allow all content unless explicitly marked)
|
| 200 |
+
|
| 201 |
+
filtered_lines.append(line)
|
| 202 |
+
|
| 203 |
+
response = ' '.join(filtered_lines).strip()
|
| 204 |
+
|
| 205 |
+
# If we filtered everything out, provide a default response
|
| 206 |
+
if not response:
|
| 207 |
+
response = "I am Codette, an AI programming assistant. How can I help with your development tasks?"
|
| 208 |
+
|
| 209 |
+
# Clean up any remaining character dialogues
|
| 210 |
+
if ':' in response:
|
| 211 |
+
parts = response.split(':', 1)
|
| 212 |
+
speaker = parts[0].lower().strip()
|
| 213 |
+
if speaker == 'codette':
|
| 214 |
+
response = parts[1].strip()
|
| 215 |
+
|
| 216 |
+
# Allow all responses (removed fictional marker filtering)
|
| 217 |
+
|
| 218 |
+
return response.strip()
|
| 219 |
+
|
| 220 |
+
except Exception as e:
|
| 221 |
+
logger.error(f"Error in model inference: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
raise
|