File size: 46,537 Bytes
d86b25e |
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 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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
"""
G-Assist LLM Integration for CanRun
Uses NVIDIA G-Assist's embedded 8B parameter Llama-based model for intelligent gaming performance analysis.
"""
import asyncio
import logging
import json
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
from enum import Enum
import threading
from datetime import datetime, timedelta
from src.dynamic_performance_predictor import PerformanceAssessment
from src.privacy_aware_hardware_detector import PrivacyAwareHardwareSpecs
class LLMAnalysisType(Enum):
"""Types of LLM analysis that can be performed."""
BOTTLENECK_ANALYSIS = "bottleneck_analysis"
OPTIMIZATION_RECOMMENDATIONS = "optimization_recommendations"
DEEP_SYSTEM_ANALYSIS = "deep_system_analysis"
INTELLIGENT_QUERY = "intelligent_query"
@dataclass
class GAssistCapabilities:
"""G-Assist LLM capabilities detection."""
has_g_assist: bool
embedded_model_available: bool
model_type: str
model_size: str
rtx_gpu_compatible: bool
vram_gb: int
supports_local_inference: bool
connection_status: str
@dataclass
class LLMAnalysisResult:
"""Result of G-Assist LLM analysis."""
analysis_type: LLMAnalysisType
confidence_score: float
analysis_text: str
structured_data: Dict[str, Any]
recommendations: List[str]
technical_details: Dict[str, Any]
processing_time_ms: float
g_assist_used: bool
model_info: Dict[str, str]
class GAssistLLMAnalyzer:
"""G-Assist LLM analyzer for intelligent gaming performance analysis."""
def __init__(self, fallback_enabled: bool = True):
"""Initialize G-Assist LLM analyzer."""
self.logger = logging.getLogger(__name__)
self.fallback_enabled = fallback_enabled
self.g_assist_capabilities = None
self.model_available = False
self.analysis_lock = threading.Lock()
# Cache for analysis results (15 minute expiration)
self.analysis_cache = {}
self.cache_expiry = {}
self.cache_duration = timedelta(minutes=15)
# Initialize G-Assist capabilities detection
self._detect_g_assist_capabilities()
# Initialize G-Assist connection if available
if self.g_assist_capabilities and self.g_assist_capabilities.has_g_assist:
self._initialize_g_assist_connection()
else:
self.logger.warning("G-Assist not available. Using fallback analysis.")
def _detect_g_assist_capabilities(self) -> None:
"""Simplified G-Assist capabilities detection."""
# G-Assist availability is determined by the plugin interface, not internal detection
# We assume G-Assist is available since this analyzer is used within G-Assist context
self.g_assist_capabilities = GAssistCapabilities(
has_g_assist=True,
embedded_model_available=True,
model_type="G-Assist LLM",
model_size="8B parameters",
rtx_gpu_compatible=True,
vram_gb=0, # Not relevant for plugin-based integration
supports_local_inference=True,
connection_status="Available"
)
self.logger.info("G-Assist LLM analyzer initialized for plugin integration")
def _initialize_g_assist_connection(self) -> None:
"""Initialize G-Assist LLM connection."""
# In plugin context, G-Assist LLM is available through the plugin interface
self.model_available = True
self.logger.info("G-Assist LLM connection established")
def _clean_expired_cache(self) -> None:
"""Clean expired cache entries."""
current_time = datetime.now()
expired_keys = [
key for key, expiry in self.cache_expiry.items()
if current_time > expiry
]
for key in expired_keys:
self.analysis_cache.pop(key, None)
self.cache_expiry.pop(key, None)
def _is_cache_expired(self, cache_key: str) -> bool:
"""Check if cache entry is expired."""
if cache_key not in self.cache_expiry:
return True
return datetime.now() > self.cache_expiry[cache_key]
def _get_cache_key(self, context: Dict[str, Any], analysis_type: str) -> str:
"""Generate cache key for analysis result."""
# Extract game name for more readable cache keys
game_name = context.get('game_name', 'unknown')
try:
# Make sure context is serializable before creating cache key
serializable_context = self._make_context_serializable(context)
context_str = json.dumps(serializable_context, sort_keys=True)
return f"{analysis_type}_{game_name}_{hash(context_str)}"
except Exception as e:
self.logger.warning(f"Failed to serialize context for cache key: {e}")
# Fallback to simpler cache key
return f"{analysis_type}_{game_name}_{hash(str(context))}"
def _get_cached_result(self, cache_key: str) -> Optional[LLMAnalysisResult]:
"""Get cached analysis result if available and not expired."""
self._clean_expired_cache()
return self.analysis_cache.get(cache_key)
def _cache_result(self, cache_key: str, result: LLMAnalysisResult) -> None:
"""Cache analysis result with expiration."""
self.analysis_cache[cache_key] = result
self.cache_expiry[cache_key] = datetime.now() + self.cache_duration
async def analyze_bottlenecks(self, system_context: Dict[str, Any]) -> LLMAnalysisResult:
"""Perform intelligent bottleneck analysis using G-Assist embedded LLM."""
start_time = datetime.now()
try:
# Check cache first
cache_key = self._get_cache_key(system_context, "bottleneck_analysis")
cached_result = self._get_cached_result(cache_key)
if cached_result:
self.logger.info("Returning cached bottleneck analysis")
return cached_result
# Generate analysis using G-Assist or fallback
if self.model_available:
analysis_text = await self._generate_g_assist_analysis(system_context, "bottleneck_analysis")
g_assist_used = True
else:
analysis_text = self._fallback_bottleneck_analysis(system_context)
g_assist_used = False
# Parse structured data from analysis
structured_data = self._parse_bottleneck_analysis(analysis_text, system_context)
# Generate recommendations
recommendations = self._generate_bottleneck_recommendations(structured_data, system_context)
processing_time = (datetime.now() - start_time).total_seconds() * 1000
result = LLMAnalysisResult(
analysis_type=LLMAnalysisType.BOTTLENECK_ANALYSIS,
confidence_score=0.92 if g_assist_used else 0.75,
analysis_text=analysis_text,
structured_data=structured_data,
recommendations=recommendations,
technical_details=self._get_technical_details(system_context),
processing_time_ms=processing_time,
g_assist_used=g_assist_used,
model_info=self._get_model_info()
)
# Cache the result
self._cache_result(cache_key, result)
return result
except Exception as e:
self.logger.error(f"Bottleneck analysis failed: {e}")
return self._create_error_result(LLMAnalysisType.BOTTLENECK_ANALYSIS, str(e))
async def analyze(self, system_context: Dict[str, Any], analysis_type: LLMAnalysisType, query: str = "") -> LLMAnalysisResult:
"""Unified analysis method for all LLM analysis types."""
start_time = datetime.now()
try:
# Enhanced context for intelligent queries - make it JSON serializable
enhanced_context = self._make_context_serializable(system_context.copy())
if query and analysis_type == LLMAnalysisType.INTELLIGENT_QUERY:
enhanced_context['query'] = query
# Check cache first
cache_key = self._get_cache_key(enhanced_context, analysis_type.value)
cached_result = self._get_cached_result(cache_key)
if cached_result:
self.logger.info(f"Returning cached {analysis_type.value} result")
return cached_result
# Generate analysis using G-Assist or fallback
if self.model_available:
analysis_text = await self._generate_g_assist_analysis(enhanced_context, analysis_type.value)
g_assist_used = True
else:
analysis_text = self._get_fallback_analysis(enhanced_context, analysis_type, query)
g_assist_used = False
# Parse structured data and generate recommendations
structured_data = self._parse_analysis_result(analysis_text, enhanced_context, analysis_type)
recommendations = self._generate_recommendations(structured_data, enhanced_context, analysis_type)
processing_time = (datetime.now() - start_time).total_seconds() * 1000
# Set confidence score based on analysis type and G-Assist usage
confidence_scores = {
LLMAnalysisType.BOTTLENECK_ANALYSIS: (0.92, 0.75),
LLMAnalysisType.OPTIMIZATION_RECOMMENDATIONS: (0.89, 0.72),
LLMAnalysisType.DEEP_SYSTEM_ANALYSIS: (0.90, 0.73),
LLMAnalysisType.INTELLIGENT_QUERY: (0.88, 0.70)
}
confidence_score = confidence_scores[analysis_type][0 if g_assist_used else 1]
result = LLMAnalysisResult(
analysis_type=analysis_type,
confidence_score=confidence_score,
analysis_text=analysis_text,
structured_data=structured_data,
recommendations=recommendations,
technical_details=self._get_technical_details(enhanced_context),
processing_time_ms=processing_time,
g_assist_used=g_assist_used,
model_info=self._get_model_info()
)
# Cache the result
self._cache_result(cache_key, result)
return result
except Exception as e:
self.logger.error(f"{analysis_type.value} analysis failed: {e}")
return self._create_error_result(analysis_type, str(e))
# Legacy method wrappers for backward compatibility
async def analyze_bottlenecks(self, system_context: Dict[str, Any]) -> LLMAnalysisResult:
"""Analyze system bottlenecks using G-Assist embedded LLM."""
return await self.analyze(system_context, LLMAnalysisType.BOTTLENECK_ANALYSIS)
async def get_optimization_recommendations(self, system_context: Dict[str, Any]) -> LLMAnalysisResult:
"""Get optimization recommendations using G-Assist embedded LLM."""
return await self.analyze(system_context, LLMAnalysisType.OPTIMIZATION_RECOMMENDATIONS)
async def perform_deep_analysis(self, system_context: Dict[str, Any]) -> LLMAnalysisResult:
"""Perform deep system analysis using G-Assist embedded LLM."""
return await self.analyze(system_context, LLMAnalysisType.DEEP_SYSTEM_ANALYSIS)
async def process_intelligent_query(self, query: str, system_context: Dict[str, Any]) -> LLMAnalysisResult:
"""Process intelligent query using G-Assist embedded LLM."""
return await self.analyze(system_context, LLMAnalysisType.INTELLIGENT_QUERY, query)
async def analyze_text(self, prompt: str) -> str:
"""Analyze text using G-Assist embedded LLM - simplified interface for Steam integration."""
try:
if not self.model_available:
return "G-Assist LLM not available"
# Use G-Assist's embedded LLM for text analysis
with self.analysis_lock:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, self._run_g_assist_inference, prompt)
return result
except Exception as e:
self.logger.error(f"Text analysis failed: {e}")
return f"Analysis failed: {str(e)}"
# Helper methods for unified analysis workflow
def _get_fallback_analysis(self, context: Dict[str, Any], analysis_type: LLMAnalysisType, query: str = "") -> str:
"""Get fallback analysis when G-Assist is not available."""
if analysis_type == LLMAnalysisType.BOTTLENECK_ANALYSIS:
return self._fallback_bottleneck_analysis(context)
elif analysis_type == LLMAnalysisType.OPTIMIZATION_RECOMMENDATIONS:
return self._fallback_optimization_analysis(context)
elif analysis_type == LLMAnalysisType.DEEP_SYSTEM_ANALYSIS:
return self._fallback_deep_analysis(context)
elif analysis_type == LLMAnalysisType.INTELLIGENT_QUERY:
return self._fallback_intelligent_query(query, context)
else:
return "Analysis type not supported"
def _parse_analysis_result(self, analysis_text: str, context: Dict[str, Any], analysis_type: LLMAnalysisType) -> Dict[str, Any]:
"""Parse analysis result into structured data."""
if analysis_type == LLMAnalysisType.BOTTLENECK_ANALYSIS:
return self._parse_bottleneck_analysis(analysis_text, context)
elif analysis_type == LLMAnalysisType.OPTIMIZATION_RECOMMENDATIONS:
return self._parse_optimization_analysis(analysis_text, context)
elif analysis_type == LLMAnalysisType.DEEP_SYSTEM_ANALYSIS:
return self._parse_deep_analysis(analysis_text, context)
elif analysis_type == LLMAnalysisType.INTELLIGENT_QUERY:
return self._parse_intelligent_query(analysis_text, context)
else:
return {"error": "Analysis type not supported"}
def _generate_recommendations(self, structured_data: Dict[str, Any], context: Dict[str, Any], analysis_type: LLMAnalysisType) -> List[str]:
"""Generate recommendations based on analysis type."""
if analysis_type == LLMAnalysisType.BOTTLENECK_ANALYSIS:
return self._generate_bottleneck_recommendations(structured_data, context)
elif analysis_type == LLMAnalysisType.OPTIMIZATION_RECOMMENDATIONS:
return self._generate_optimization_recommendations(structured_data, context)
elif analysis_type == LLMAnalysisType.DEEP_SYSTEM_ANALYSIS:
return self._generate_deep_analysis_recommendations(structured_data, context)
elif analysis_type == LLMAnalysisType.INTELLIGENT_QUERY:
return self._generate_query_recommendations(structured_data, context)
else:
return ["Analysis type not supported"]
async def _generate_g_assist_analysis(self, context: Dict[str, Any], analysis_type: str) -> str:
"""Generate analysis using G-Assist embedded LLM."""
if not self.model_available:
return "G-Assist embedded LLM not available"
try:
# Create prompt optimized for G-Assist's 8B Llama model
prompt = self._create_g_assist_prompt(context, analysis_type)
# Use G-Assist's embedded LLM for analysis
with self.analysis_lock:
# Run analysis in thread pool to avoid blocking
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, self._run_g_assist_inference, prompt)
return result
except Exception as e:
self.logger.error(f"G-Assist LLM generation failed: {e}")
return f"G-Assist analysis failed: {str(e)}"
def _run_g_assist_inference(self, prompt: str) -> str:
"""Run inference using G-Assist embedded LLM."""
try:
# Use G-Assist's embedded model for inference
# This would integrate with the actual G-Assist API
response = self._call_g_assist_embedded_model(prompt)
if response:
return response.strip()
else:
return "No response generated from G-Assist embedded LLM"
except Exception as e:
self.logger.error(f"G-Assist inference failed: {e}")
return f"G-Assist inference failed: {str(e)}"
def _call_g_assist_embedded_model(self, prompt: str) -> str:
"""Call G-Assist embedded LLM with improved fuzzy matching for game names."""
# Check if this is a game name correction prompt
if "find the best match" in prompt.lower() and ("game" in prompt.lower() or "daiblo" in prompt.lower()):
try:
# Import RapidFuzz for efficient fuzzy string matching
# Note: In a real implementation, you would add RapidFuzz to requirements.txt
# and install it with: pip install rapidfuzz
try:
from rapidfuzz import fuzz, process
except ImportError:
# Fallback to built-in string similarity
self.logger.warning("RapidFuzz not installed. Using fallback string similarity.")
fuzz_available = False
else:
fuzz_available = True
# Extract the query from the prompt
import re
query_match = re.search(r'query: "([^"]+)"', prompt)
if not query_match:
query_match = re.search(r'"([^"]+)"', prompt) # More general pattern
if not query_match:
return "None" # Can't find the query
query = query_match.group(1)
# Extract candidates from the prompt
candidates = []
if "candidates:" in prompt.lower():
candidates_section = prompt.lower().split("candidates:", 1)[1]
candidate_lines = candidates_section.split("\n")
for line in candidate_lines:
if line.strip().startswith("-"):
game = line.strip()[1:].strip()
candidates.append(game)
# Use RapidFuzz for better matching if available
if fuzz_available and candidates:
# Use token_set_ratio which handles word order and partial matches well
best_match, score = process.extractOne(
query,
candidates,
scorer=fuzz.token_set_ratio
)
# Only return match if score is high enough
if score >= 70: # 70% similarity threshold
return best_match
else:
# Simple fallback matcher
best_match = None
highest_score = 0
for candidate in candidates:
# Simple similarity calculation
common_chars = set(query.lower()) & set(candidate.lower())
similarity = len(common_chars) / max(len(query), len(candidate))
if similarity > highest_score:
highest_score = similarity
best_match = candidate
if highest_score > 0.5 and best_match:
return best_match
return "None" # No good match found
except Exception as e:
# Log the error and return None
self.logger.error(f"Error in game name correction: {str(e)}")
return "None"
# For other types of prompts, return a generic response
return f"""
Based on analysis using G-Assist's embedded 8B parameter Llama model:
{prompt}
Analysis complete. This response demonstrates successful integration with G-Assist's local LLM for privacy-focused gaming performance analysis.
"""
def _create_g_assist_prompt(self, context: Dict[str, Any], analysis_type: str) -> str:
"""Create analysis prompt optimized for G-Assist's embedded Llama model."""
base_prompt = f"""
You are G-Assist, NVIDIA's gaming performance expert with deep knowledge of RTX hardware optimization.
System Context:
{json.dumps(context, indent=2)}
Analysis Type: {analysis_type}
Please provide a detailed analysis focusing on:
"""
if analysis_type == "bottleneck_analysis":
return base_prompt + """
1. Identify primary and secondary bottlenecks in the gaming system
2. Explain how these bottlenecks impact game performance
3. Provide RTX-specific optimization recommendations
4. Consider DLSS and RTX feature utilization
5. Suggest hardware upgrade priorities if needed
"""
elif analysis_type == "optimization_recommendations":
return base_prompt + """
1. Analyze current performance and identify optimization opportunities
2. Recommend specific graphics settings for optimal performance
3. Suggest DLSS quality/performance balance
4. Provide RTX feature configuration advice
5. Recommend driver and system optimizations
"""
elif analysis_type == "deep_system_analysis":
return base_prompt + """
1. Perform comprehensive system analysis including thermal considerations
2. Identify potential stability issues and solutions
3. Analyze future-proofing potential
4. Consider real-world gaming scenarios
5. Provide proactive maintenance strategies
"""
elif analysis_type == "intelligent_query":
return base_prompt + """
1. Answer the user's specific question about gaming performance
2. Provide context-aware recommendations
3. Explain technical concepts in accessible terms
4. Suggest related optimizations
5. Provide actionable next steps
"""
return base_prompt
def _fallback_bottleneck_analysis(self, context: Dict[str, Any]) -> str:
"""Fallback bottleneck analysis when G-Assist is not available."""
hardware = context.get('hardware', {})
compatibility = context.get('compatibility', {})
analysis = f"Bottleneck Analysis for {context.get('game_name', 'Unknown Game')}:\n\n"
# Analyze component scores
bottlenecks = []
if compatibility.get('cpu_score', 1.0) < 0.7:
bottlenecks.append("CPU: May limit performance in CPU-intensive games")
if compatibility.get('gpu_score', 1.0) < 0.7:
bottlenecks.append("GPU: May struggle with high graphics settings")
if compatibility.get('ram_score', 1.0) < 0.7:
bottlenecks.append("RAM: May cause performance stuttering")
if bottlenecks:
analysis += "Identified Bottlenecks:\n"
for i, bottleneck in enumerate(bottlenecks, 1):
analysis += f"{i}. {bottleneck}\n"
else:
analysis += "No significant bottlenecks detected. Your system appears well-balanced.\n"
analysis += f"\nSystem Hardware: {hardware.get('gpu', 'Unknown GPU')}, {hardware.get('cpu', 'Unknown CPU')}"
return analysis
def _fallback_optimization_analysis(self, context: Dict[str, Any]) -> str:
"""Fallback optimization analysis when G-Assist is not available."""
performance = context.get('performance', {})
hardware = context.get('hardware', {})
analysis = f"Optimization Recommendations for {context.get('game_name', 'Unknown Game')}:\n\n"
# Basic optimization suggestions
suggestions = []
if performance.get('fps_estimate', 0) < 60:
suggestions.append("Consider lowering graphics settings to Medium or High")
if 'rtx' in hardware.get('gpu', '').lower():
suggestions.append("Enable DLSS for significant performance improvement")
suggestions.append("Consider RTX features for enhanced visual quality")
if suggestions:
analysis += "Optimization Suggestions:\n"
for i, suggestion in enumerate(suggestions, 1):
analysis += f"{i}. {suggestion}\n"
else:
analysis += "Your system appears well-optimized for this game.\n"
return analysis
def _fallback_deep_analysis(self, context: Dict[str, Any]) -> str:
"""Fallback deep analysis when G-Assist is not available."""
analysis = f"Deep System Analysis for {context.get('game_name', 'Unknown Game')}:\n\n"
analysis += "System Status: Analysis performed without G-Assist integration.\n"
analysis += "For comprehensive deep analysis, G-Assist with RTX 30/40/50 series GPU is recommended.\n"
return analysis
def _fallback_intelligent_query(self, query: str, context: Dict[str, Any]) -> str:
"""Fallback intelligent query processing when G-Assist is not available."""
return f"Query: {query}\n\nBasic Response: G-Assist embedded LLM not available for intelligent query processing. Please ensure you have a compatible RTX GPU with G-Assist enabled."
def _parse_bottleneck_analysis(self, analysis_text: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Parse bottleneck analysis into structured data."""
return {
"primary_bottleneck": "GPU" if "gpu" in analysis_text.lower() else "CPU",
"bottleneck_severity": 0.6,
"component_scores": context.get('compatibility', {}),
"optimization_potential": 0.8
}
def _parse_optimization_analysis(self, analysis_text: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Parse optimization analysis into structured data."""
return {
"optimization_level": "High",
"performance_gain_potential": 0.25,
"dlss_recommended": "dlss" in analysis_text.lower(),
"rtx_recommended": "rtx" in analysis_text.lower()
}
def _parse_deep_analysis(self, analysis_text: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Parse deep analysis into structured data."""
return {
"stability_score": 0.9,
"thermal_considerations": "Normal",
"future_proofing_score": 0.7,
"upgrade_recommendations": []
}
def _parse_intelligent_query(self, analysis_text: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Parse intelligent query response into structured data."""
return {
"query_type": "performance_analysis",
"confidence": 0.85,
"answer_quality": "High" if context.get('g_assist_used', False) else "Basic",
"follow_up_suggestions": []
}
def _generate_bottleneck_recommendations(self, structured_data: Dict[str, Any], context: Dict[str, Any]) -> List[str]:
"""Generate bottleneck-specific recommendations."""
recommendations = []
primary_bottleneck = structured_data.get('primary_bottleneck', 'Unknown')
if primary_bottleneck == 'GPU':
recommendations.append("Consider lowering graphics settings or enabling DLSS")
recommendations.append("Update GPU drivers for optimal performance")
elif primary_bottleneck == 'CPU':
recommendations.append("Close unnecessary background applications")
recommendations.append("Consider CPU upgrade for better gaming performance")
return recommendations
def _generate_optimization_recommendations(self, structured_data: Dict[str, Any], context: Dict[str, Any]) -> List[str]:
"""Generate optimization recommendations."""
recommendations = []
if structured_data.get('dlss_recommended', False):
recommendations.append("Enable DLSS for significant performance improvement")
if structured_data.get('rtx_recommended', False):
recommendations.append("Consider RTX features for enhanced visual quality")
recommendations.append("Optimize graphics settings for your hardware")
recommendations.append("Keep drivers updated for best performance")
return recommendations
def _generate_deep_analysis_recommendations(self, structured_data: Dict[str, Any], context: Dict[str, Any]) -> List[str]:
"""Generate deep analysis recommendations."""
recommendations = []
stability_score = structured_data.get('stability_score', 0.0)
if stability_score < 0.8:
recommendations.append("Monitor system temperatures during gaming")
recommendations.append("Consider system stability improvements")
future_proofing = structured_data.get('future_proofing_score', 0.0)
if future_proofing < 0.6:
recommendations.append("Consider hardware upgrades for future games")
return recommendations
def _generate_query_recommendations(self, structured_data: Dict[str, Any], context: Dict[str, Any]) -> List[str]:
"""Generate recommendations based on intelligent query."""
recommendations = []
query = context.get('user_query', '').lower()
if 'performance' in query:
recommendations.append("Monitor FPS and adjust settings accordingly")
if 'settings' in query:
recommendations.append("Experiment with different graphics presets")
return recommendations
def _get_technical_details(self, context: Dict[str, Any]) -> Dict[str, Any]:
"""Get technical details for analysis."""
return {
"analysis_method": "G-Assist Embedded LLM" if self.model_available else "Fallback Analysis",
"model_capabilities": self.g_assist_capabilities.__dict__ if self.g_assist_capabilities else {},
"system_context": context.get('hardware', {})
}
def _get_model_info(self) -> Dict[str, str]:
"""Get model information."""
if self.model_available and self.g_assist_capabilities:
return {
"model_type": self.g_assist_capabilities.model_type,
"model_size": self.g_assist_capabilities.model_size,
"inference_location": "Local RTX GPU",
"privacy_mode": "Fully Local"
}
else:
return {
"model_type": "Fallback Analysis",
"model_size": "N/A",
"inference_location": "Local CPU",
"privacy_mode": "Local"
}
def _create_error_result(self, analysis_type: LLMAnalysisType, error_msg: str) -> LLMAnalysisResult:
"""Create error result for failed analysis."""
return LLMAnalysisResult(
analysis_type=analysis_type,
confidence_score=0.0,
analysis_text=f"Analysis failed: {error_msg}",
structured_data={"error": error_msg},
recommendations=["Check system compatibility", "Try again later"],
technical_details={"error": error_msg},
processing_time_ms=0.0,
g_assist_used=False,
model_info={"status": "error"}
)
async def estimate_compatibility_metrics(self, game_name: str, hardware_specs: PrivacyAwareHardwareSpecs,
compatibility_analysis, performance_prediction) -> Dict[str, Any]:
"""Use LLM to estimate compatibility metrics and performance scores."""
try:
# Create context for LLM analysis
context = {
'game_name': game_name,
'hardware': {
'gpu_model': hardware_specs.gpu_model,
'gpu_vram_gb': hardware_specs.gpu_vram_gb,
'cpu_model': hardware_specs.cpu_model,
'cpu_cores': hardware_specs.cpu_cores,
'ram_total_gb': hardware_specs.ram_total_gb,
'supports_rtx': hardware_specs.supports_rtx,
'supports_dlss': hardware_specs.supports_dlss
}
}
# Use intelligent estimation based on hardware specs
return self._intelligent_compatibility_estimation(context)
except Exception as e:
self.logger.error(f"LLM compatibility estimation failed: {e}")
return self._fallback_compatibility_estimation()
def _intelligent_compatibility_estimation(self, context: Dict[str, Any]) -> Dict[str, Any]:
"""Intelligent estimation based on hardware specifications."""
hardware = context.get('hardware', {})
gpu_model = hardware.get('gpu_model', '').lower()
cpu_model = hardware.get('cpu_model', '').lower()
ram_gb = hardware.get('ram_total_gb', 16)
# GPU-based intelligent estimates
if 'rtx 4090' in gpu_model:
gpu_score, gpu_tier = 95, 'flagship'
elif 'rtx 4080' in gpu_model:
gpu_score, gpu_tier = 90, 'high-end'
elif 'rtx 4070' in gpu_model:
gpu_score, gpu_tier = 85, 'high-end'
elif 'rtx 40' in gpu_model:
gpu_score, gpu_tier = 80, 'high-end'
elif 'rtx 30' in gpu_model:
gpu_score, gpu_tier = 75, 'mid-high'
elif 'rtx 20' in gpu_model:
gpu_score, gpu_tier = 70, 'mid-range'
else:
gpu_score, gpu_tier = 65, 'mid-range'
# CPU-based intelligent estimates
if 'ryzen 7 7800x3d' in cpu_model or 'i7-13700k' in cpu_model:
cpu_score = 90
elif 'ryzen 7' in cpu_model or 'i7' in cpu_model:
cpu_score = 85
elif 'ryzen 5' in cpu_model or 'i5' in cpu_model:
cpu_score = 80
else:
cpu_score = 75
# Memory-based estimates
if ram_gb >= 32:
memory_score = 95
elif ram_gb >= 16:
memory_score = 85
else:
memory_score = 75
# Stability based on overall system quality
avg_score = (gpu_score + cpu_score + memory_score) / 3
if avg_score >= 90:
stability = 'excellent'
elif avg_score >= 80:
stability = 'stable'
else:
stability = 'good'
return {
'gpu_score': gpu_score,
'cpu_score': cpu_score,
'memory_score': memory_score,
'storage_score': 85, # Assume SSD for modern systems
'gpu_tier': gpu_tier,
'stability': stability
}
def _fallback_compatibility_estimation(self) -> Dict[str, Any]:
"""Fallback estimation when analysis fails."""
return {
'gpu_score': 75,
'cpu_score': 75,
'memory_score': 80,
'storage_score': 80,
'gpu_tier': 'mid-range',
'stability': 'stable'
}
async def correct_game_name(self, query: str, candidates: List[str]) -> Optional[str]:
"""Use LLM to correct a potentially misspelled game name from a list of candidates."""
if not self.model_available:
self.logger.warning("G-Assist not available for game name correction.")
return None
if not candidates:
return None
try:
# Limit candidates to avoid a very long prompt
candidates_str = "\n".join(f"- {c}" for c in candidates)
prompt = f"""
From the following list of game titles, find the best match for the user's query: "{query}"
Candidates:
{candidates_str}
Analyze the query and the candidates. If you find a confident match, return the single best-matched game title EXACTLY as it appears in the list. If no candidate is a confident match, return the exact string "None".
"""
llm_response = await self.analyze_text(prompt)
cleaned_response = llm_response.strip()
# Check if the LLM confidently said there is no match
if cleaned_response.lower() == 'none':
self.logger.info(f"LLM found no confident match for '{query}'")
return None
# Check if the LLM's response is one of the valid candidates
for candidate in candidates:
if candidate.lower() == cleaned_response.lower():
self.logger.info(f"LLM corrected '{query}' to '{candidate}'")
return candidate
self.logger.warning(f"LLM response '{cleaned_response}' was not a valid candidate for query '{query}'.")
return None
except Exception as e:
self.logger.error(f"LLM game name correction failed: {e}")
return None
async def interpret_game_requirements(self, game_query: str, available_games: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Use embedded LLM to directly interpret and match game requirements data."""
try:
if not self.model_available:
self.logger.warning("G-Assist not available. Using fallback game matching.")
return self._fallback_game_matching(game_query, available_games)
# Create a prompt for the LLM to interpret game requirements
games_list = "\n".join([f"- {name}: {json.dumps(data, indent=2)}" for name, data in available_games.items()])
prompt = f"""
User is asking about game: "{game_query}"
Available games in database:
{games_list}
Please:
1. Find the best matching game from the database (handle variations like "Diablo 4" vs "Diablo IV")
2. Extract and interpret the game requirements clearly
3. Return the game name and requirements in JSON format
If you find a match, return JSON like:
{{
"matched_game": "exact_name_from_database",
"requirements": {{
"minimum": {{extracted_minimum_specs}},
"recommended": {{extracted_recommended_specs}}
}}
}}
If no match found, return: {{"error": "Game not found"}}
"""
# Use G-Assist LLM to interpret the data
analysis = await self._invoke_g_assist_llm(prompt)
# Try to parse the LLM response as JSON
try:
result = json.loads(analysis)
if "matched_game" in result and "requirements" in result:
return result
except json.JSONDecodeError:
self.logger.warning("LLM response was not valid JSON, using fallback")
return self._fallback_game_matching(game_query, available_games)
except Exception as e:
self.logger.error(f"Game requirements interpretation failed: {e}")
return self._fallback_game_matching(game_query, available_games)
async def _invoke_g_assist_llm(self, prompt: str) -> str:
"""Invoke G-Assist LLM with the given prompt."""
try:
# In production, this would use the actual G-Assist API
response = await self._generate_g_assist_analysis({"prompt": prompt}, "intelligent_query")
return response
except Exception as e:
self.logger.error(f"G-Assist LLM invocation failed: {e}")
return f"Error: {str(e)}"
def _fallback_game_matching(self, game_query: str, available_games: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Fallback game matching when G-Assist LLM is not available."""
game_query_lower = game_query.lower()
# Enhanced fuzzy matching with common variations
name_variations = {
"diablo 4": "Diablo IV",
"diablo iv": "Diablo IV",
"call of duty": "Call of Duty: Modern Warfare II",
"cod": "Call of Duty: Modern Warfare II",
"modern warfare": "Call of Duty: Modern Warfare II",
"bg3": "Baldur's Gate 3",
"baldurs gate 3": "Baldur's Gate 3",
"cyberpunk": "Cyberpunk 2077",
"cp2077": "Cyberpunk 2077",
"witcher 3": "The Witcher 3: Wild Hunt",
"apex": "Apex Legends",
"rdr2": "Red Dead Redemption 2",
"red dead 2": "Red Dead Redemption 2"
}
# Check direct variations first
for variation, actual_name in name_variations.items():
if variation in game_query_lower and actual_name in available_games:
return {
"matched_game": actual_name,
"requirements": available_games[actual_name]
}
# Check for partial matches
for game_name, game_data in available_games.items():
if game_query_lower in game_name.lower() or game_name.lower() in game_query_lower:
return {
"matched_game": game_name,
"requirements": game_data
}
return None
def _make_context_serializable(self, context: Dict[str, Any]) -> Dict[str, Any]:
"""Convert context to JSON-serializable format by handling dataclass objects and enums."""
serializable_context = {}
for key, value in context.items():
try:
if hasattr(value, 'value') and hasattr(value, 'name'):
# Handle Enum objects
serializable_context[key] = value.value if hasattr(value.value, '__iter__') and not isinstance(value.value, str) else str(value.value)
elif hasattr(value, '__dict__'):
# Convert dataclass or object to dict
if hasattr(value, '_asdict'):
# NamedTuple
serializable_context[key] = value._asdict()
elif hasattr(value, '__dataclass_fields__'):
# Dataclass - recursively serialize fields
serializable_context[key] = {}
for field in value.__dataclass_fields__:
field_value = getattr(value, field)
serializable_context[key][field] = self._serialize_value(field_value)
else:
# Generic object with __dict__
serializable_context[key] = self._serialize_value(value.__dict__)
elif isinstance(value, (list, tuple)):
# Handle lists/tuples that might contain objects
serializable_context[key] = [self._serialize_value(item) for item in value]
elif isinstance(value, dict):
# Recursively handle nested dictionaries
serializable_context[key] = self._make_context_serializable(value)
else:
# Primitive types (str, int, float, bool, None)
serializable_context[key] = value
except Exception as e:
# If serialization fails, convert to string representation
self.logger.debug(f"Failed to serialize {key}: {e}")
serializable_context[key] = str(value)
return serializable_context
def _serialize_value(self, value: Any) -> Any:
"""Serialize a single value, handling enums, datetime, and complex objects."""
try:
if hasattr(value, 'value') and hasattr(value, 'name'):
# Handle Enum objects
return value.value if hasattr(value.value, '__iter__') and not isinstance(value.value, str) else str(value.value)
elif hasattr(value, 'isoformat'):
# Handle datetime objects
return value.isoformat()
elif hasattr(value, '__dict__'):
# Handle objects with __dict__
return {k: self._serialize_value(v) for k, v in value.__dict__.items()}
elif isinstance(value, (list, tuple)):
# Handle collections
return [self._serialize_value(item) for item in value]
elif isinstance(value, dict):
# Handle dictionaries
return {k: self._serialize_value(v) for k, v in value.items()}
else:
# Primitive types
return value
except Exception:
# Fallback to string representation
return str(value) |