File size: 14,104 Bytes
8174855 |
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 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 |
"""
Advanced Supernova Chat System with Enhanced Reasoning
Provides sophisticated AI reasoning capabilities through multi-step problem solving,
knowledge synthesis, and intelligent tool coordination.
"""
import argparse
import json
import os
import yaml
from typing import Optional
import torch
from supernova.config import ModelConfig
from supernova.model import SupernovaModel
from supernova.tokenizer import load_gpt2_tokenizer
from supernova.tools import ToolOrchestrator, ToolCall
from supernova.reasoning_engine import EnhancedReasoningEngine
BRAND_PATH = os.path.join(os.path.dirname(__file__), "branding", "ALGORHYTHM_TECH_PROFILE.txt")
def load_brand_text() -> str:
with open(BRAND_PATH, "r", encoding="utf-8") as f:
return f.read().strip()
def load_api_keys(api_keys_path: str) -> dict:
"""Load API keys from YAML configuration file."""
if not os.path.exists(api_keys_path):
print(f"Warning: API keys file not found at {api_keys_path}")
return {}
try:
with open(api_keys_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f) or {}
return config
except Exception as e:
print(f"Warning: Could not load API keys: {e}")
return {}
def should_return_brand(prompt: str) -> bool:
p = prompt.lower()
keys = [
"algorythm tech",
"algorythm technologies",
"company profile",
"vision",
"who are you",
"about algorythm",
"who built you",
"who created you"
]
return any(k in p for k in keys)
def generate(
model: SupernovaModel,
tok,
prompt: str,
max_new_tokens: int = 200,
temperature: float = 0.8,
top_k: Optional[int] = 50,
) -> str:
"""Enhanced generation function with better sampling."""
model.eval()
device = next(model.parameters()).device
input_ids = tok.encode(prompt, return_tensors="pt").to(device)
with torch.no_grad():
for _ in range(max_new_tokens):
if input_ids.size(1) >= model.cfg.n_positions:
input_cond = input_ids[:, -model.cfg.n_positions:]
else:
input_cond = input_ids
logits, _ = model(input_cond)
logits = logits[:, -1, :]
logits = logits / max(1e-6, temperature)
if top_k is not None and top_k > 0:
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = -float("Inf")
probs = torch.softmax(logits, dim=-1)
next_id = torch.multinomial(probs, num_samples=1)
input_ids = torch.cat([input_ids, next_id], dim=1)
return tok.decode(input_ids[0].tolist())
class AdvancedSupernovaChat:
"""Advanced chat system with sophisticated reasoning capabilities."""
def __init__(self, config_path: str, api_keys_path: str, checkpoint_path: Optional[str] = None):
self.cfg = ModelConfig.from_json_file(config_path)
self.tok = load_gpt2_tokenizer()
# Initialize model
self.model = SupernovaModel(self.cfg)
# Load checkpoint if provided
if checkpoint_path and os.path.exists(checkpoint_path):
checkpoint = torch.load(checkpoint_path, map_location='cpu')
self.model.load_state_dict(checkpoint['model_state_dict'])
print(f"β
Loaded checkpoint from {checkpoint_path}")
else:
print("β οΈ No checkpoint loaded - using randomly initialized model")
# Load API configuration
api_config = load_api_keys(api_keys_path)
# Initialize tool orchestrator with proper API keys
serper_key = api_config.get('serper_api_key', '06f4918f3ea721d9742f940fb7c7ba1ac44e7c14') # fallback key
self.tools = ToolOrchestrator(serper_api_key=serper_key)
# Initialize enhanced reasoning engine
self.reasoning_engine = EnhancedReasoningEngine(self.tools)
# Track conversation for context
self.conversation_history = []
print(f"π§ Advanced reasoning engine initialized")
print(f"π§ Available tools: Math Engine, Web Search")
def analyze_query_intent(self, user_input: str) -> dict:
"""Analyze the user's intent and determine the best response strategy."""
intent_analysis = {
'complexity': 'simple',
'requires_reasoning': False,
'domains': [],
'tool_needed': None,
'response_strategy': 'direct'
}
# Check for complex reasoning indicators
complex_indicators = [
'explain why', 'analyze', 'compare and contrast', 'evaluate',
'what are the implications', 'how does this relate to',
'consider multiple factors', 'pros and cons'
]
if any(indicator in user_input.lower() for indicator in complex_indicators):
intent_analysis['requires_reasoning'] = True
intent_analysis['complexity'] = 'complex'
intent_analysis['response_strategy'] = 'reasoning'
# Check for multi-domain queries
domain_keywords = {
'science': ['physics', 'chemistry', 'biology', 'scientific'],
'technology': ['programming', 'software', 'computer', 'AI', 'algorithm'],
'medicine': ['health', 'medical', 'disease', 'treatment', 'symptoms'],
'business': ['market', 'economy', 'finance', 'management', 'strategy']
}
for domain, keywords in domain_keywords.items():
if any(keyword in user_input.lower() for keyword in keywords):
intent_analysis['domains'].append(domain)
if len(intent_analysis['domains']) > 1:
intent_analysis['requires_reasoning'] = True
intent_analysis['response_strategy'] = 'reasoning'
return intent_analysis
def respond(self, user_input: str) -> str:
"""Generate sophisticated responses using advanced reasoning."""
# Check for brand queries first
if should_return_brand(user_input):
return load_brand_text()
# Analyze query intent
intent = self.analyze_query_intent(user_input)
# For complex queries requiring reasoning, use the enhanced reasoning engine
if intent['requires_reasoning'] or intent['response_strategy'] == 'reasoning':
try:
return self.reasoning_engine.process_complex_query(
user_input, self.model, self.tok
)
except Exception as e:
print(f"Reasoning engine error: {e}")
# Fall back to standard processing
# For standard queries, use existing tool routing
tool_call = self.tools.route_query(user_input)
if tool_call:
# Execute the tool call
tool_call = self.tools.execute_tool_call(tool_call)
if tool_call.result:
# Format the response with enhanced context
if tool_call.tool == "math_engine":
response = f"I'll solve this mathematical problem for you:\n\n{tool_call.result}\n\n**Mathematical Analysis Complete** β
\nThe solution above shows the step-by-step computation with precise results."
elif tool_call.tool == "serper":
response = f"Based on the latest information I found:\n\n{tool_call.result}\n**Information Synthesis** π\nThis data reflects current, real-time information from authoritative sources."
else:
response = tool_call.result
return response
elif tool_call.error:
# Enhanced error handling with intelligent fallback
fallback_prompt = f"""You are Supernova, an advanced AI assistant with comprehensive knowledge across all domains. The user asked: "{user_input}"
I couldn't access external tools ({tool_call.error}), but I can provide substantial help based on my extensive training across science, technology, mathematics, literature, history, medicine, and more.
Provide a detailed, thoughtful response that demonstrates deep understanding:"""
try:
response = generate(self.model, self.tok, fallback_prompt, max_new_tokens=500, temperature=0.7)
# Clean up the response
if "Provide a detailed" in response:
response = response.split("Provide a detailed", 1)[1]
if "response that demonstrates" in response:
response = response.split("response that demonstrates", 1)[1]
return f"**Advanced Analysis** π§ \n\n{response.strip()}"
except Exception as e:
return f"I apologize, but I'm experiencing technical difficulties. However, I can tell you that {user_input.lower()} is an excellent question that touches on important concepts. Could you please rephrase or break it down into more specific parts?"
# No tools needed, use enhanced direct generation
try:
enhanced_prompt = f"""You are Supernova, an advanced AI assistant built by AlgoRythm Technologies with sophisticated reasoning capabilities. You possess deep expertise across multiple domains including:
β’ Science & Mathematics: Physics, chemistry, biology, calculus, statistics
β’ Technology & Engineering: Programming, AI, systems design, algorithms
β’ Medicine & Health: Anatomy, pharmacology, diagnostics, treatments
β’ Business & Economics: Finance, strategy, market analysis, management
β’ Humanities: History, literature, philosophy, psychology, sociology
β’ Arts & Culture: Music, visual arts, design, architecture
Provide comprehensive, nuanced responses that demonstrate sophisticated understanding and reasoning.
User: {user_input}
Supernova (Advanced Analysis): """
response = generate(self.model, self.tok, enhanced_prompt, max_new_tokens=600, temperature=0.7)
# Extract just the Supernova response part
if "Supernova (Advanced Analysis): " in response:
response = response.split("Supernova (Advanced Analysis): ", 1)[1]
elif "Supernova:" in response:
response = response.split("Supernova:", 1)[1]
return f"**Comprehensive Analysis** π\n\n{response.strip()}"
except Exception as e:
return f"I encountered an error while generating a response: {str(e)}. Let me try to help in a different way - could you rephrase your question or break it into smaller parts?"
def chat_loop(self):
"""Interactive chat loop with enhanced features."""
print("π β¨ SUPERNOVA ADVANCED AI ASSISTANT β¨ π")
print("β" * 50)
print("Built by AlgoRythm Technologies")
print("π§ Enhanced with Advanced Reasoning Engine")
print("π§ Integrated Tools: Math Engine + Web Search")
print("π Multi-Domain Expertise & Sophisticated Analysis")
print("β" * 50)
print("Type 'quit', 'exit', or 'bye' to end the conversation.\n")
while True:
try:
user_input = input("\nπ€ You: ").strip()
if user_input.lower() in ['quit', 'exit', 'bye', 'q']:
print("\nπ Supernova: Thank you for this intellectually stimulating conversation! I enjoyed applying advanced reasoning to help with your questions. Until next time! β¨")
break
if not user_input:
continue
print("\nπ§ Supernova: ", end="")
response = self.respond(user_input)
print(response)
# Add to conversation history for context
self.conversation_history.append({
'user': user_input,
'assistant': response
})
# Keep only last 5 exchanges for memory efficiency
if len(self.conversation_history) > 5:
self.conversation_history.pop(0)
except KeyboardInterrupt:
print("\n\nπ Supernova: Goodbye! Thanks for the engaging discussion! β¨")
break
except Exception as e:
print(f"\\nError: {e}")
def main():
parser = argparse.ArgumentParser(description="Advanced Supernova Chat with Enhanced Reasoning")
parser.add_argument("--config", required=True, help="Path to model config file")
parser.add_argument("--api-keys", default="./configs/api_keys.yaml", help="Path to API keys file")
parser.add_argument("--checkpoint", help="Path to model checkpoint (optional)")
parser.add_argument("--prompt", help="Single prompt mode (instead of chat loop)")
args = parser.parse_args()
# Initialize advanced chat system
chat = AdvancedSupernovaChat(
config_path=args.config,
api_keys_path=args.api_keys,
checkpoint_path=args.checkpoint
)
if args.prompt:
# Single prompt mode
response = chat.respond(args.prompt)
print(response)
else:
# Interactive chat loop
chat.chat_loop()
if __name__ == "__main__":
main() |