From 8a775b44df36bfb748ff6c90de17411cd2a6cfc7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 24 Jul 2026 04:11:52 +0000 Subject: [PATCH 1/2] deps(llm-maker): bump the llm-maker-deps group across 1 directory with 2 updates Updates the requirements on [ast-grep-py](https://github.com/ast-grep/ast-grep) and [restrictedpython](https://github.com/zopefoundation/RestrictedPython) to permit the latest version. Updates `ast-grep-py` to 0.45.0 - [Release notes](https://github.com/ast-grep/ast-grep/releases) - [Changelog](https://github.com/ast-grep/ast-grep/blob/main/CHANGELOG.md) - [Commits](https://github.com/ast-grep/ast-grep/compare/0.44.1...0.45.0) Updates `restrictedpython` to 8.4 - [Changelog](https://github.com/zopefoundation/RestrictedPython/blob/master/CHANGES.rst) - [Commits](https://github.com/zopefoundation/RestrictedPython/compare/8.3...8.4) --- updated-dependencies: - dependency-name: ast-grep-py dependency-version: 0.45.0 dependency-type: direct:production dependency-group: llm-maker-deps - dependency-name: restrictedpython dependency-version: '8.4' dependency-type: direct:production dependency-group: llm-maker-deps ... Signed-off-by: dependabot[bot] --- ai-projects/llm-maker/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ai-projects/llm-maker/requirements.txt b/ai-projects/llm-maker/requirements.txt index 9d1946369..896698393 100644 --- a/ai-projects/llm-maker/requirements.txt +++ b/ai-projects/llm-maker/requirements.txt @@ -4,11 +4,11 @@ # Already installed in parent: openai, azure-openai # Code Safety and Analysis -ast-grep-py>=0.44.1 +ast-grep-py>=0.45.0 astroid>=4.1.2 # Sandboxed Execution -RestrictedPython>=8.3 +RestrictedPython>=8.4 # MCP Server (Model Context Protocol) mcp>=1.28.1 From 2580138b49c5bed21edfa9bbe4ed15240bc2b222 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 24 Jul 2026 04:28:24 +0000 Subject: [PATCH 2/2] style: apply automated autofixes (ruff / prettier / clang-format) [CodeQL Advanced] --- .github/CODE_GENERATION_GUIDE.md | 119 +++++--------- .github/COPILOT_ADVANCED_CUSTOMIZATION.md | 3 + .github/MCP_SERVERS_REFERENCE.md | 2 + .github/agents/AI_chat_development.agent.md | 1 + .github/agents/agi-reasoning.agent.md | 8 +- .github/agents/ai-architect.agent.md | 3 +- .github/agents/aria-character.agent.md | 6 +- .github/agents/platform-ops.agent.md | 4 +- .github/agents/vision-ai.agent.md | 15 +- .github/copilot-instructions.md | 10 +- .../instructions/chat-memory.instructions.md | 2 +- .../instructions/cooking-ai.instructions.md | 2 +- .../instructions/llm-maker.instructions.md | 2 +- .../instructions/subscription.instructions.md | 24 ++- .../instructions/token-utils.instructions.md | 16 +- .../vision-inference.instructions.md | 15 +- .github/prompts/agi.prompt.md | 6 +- .../agi-reasoning-debug-workflow/SKILL.md | 4 +- .../chat-memory-embeddings-workflow/SKILL.md | 4 +- .github/skills/create-hook-workflow/SKILL.md | 1 + .../llm-tool-generation-workflow/SKILL.md | 2 +- .../SKILL.md | 8 +- .../token-budget-debug-workflow/SKILL.md | 14 +- .../vision-inference-debug-workflow/SKILL.md | 15 +- AI/README.md | 15 +- ai-projects/llm-maker/QUICKSTART.md | 23 +-- ai-projects/llm-maker/README.md | 2 +- ai-projects/llm-maker/UI_ENHANCEMENTS.md | 9 +- ai-projects/llm-maker/WEBSITE_MAKER_README.md | 28 ++-- ai-projects/llm-maker/WEB_UI_README.md | 2 +- ai-projects/lmstudio-mcp/AGENT_INTEGRATION.md | 45 +++--- .../lmstudio-mcp/AGI_INTEGRATION_INDEX.md | 9 +- .../lmstudio-mcp/AGI_PROVIDER_INTEGRATION.md | 76 +++------ .../lmstudio-mcp/AGI_PROVIDER_SUMMARY.md | 24 +-- .../lmstudio-mcp/INTEGRATION_SUMMARY.md | 22 ++- .../lmstudio-mcp/PRIVACY_FIRST_GUIDE.md | 29 ++-- .../lmstudio-mcp/PRIVACY_FIRST_SUMMARY.md | 2 +- ai-projects/lmstudio-mcp/QUICK_REFERENCE.md | 7 +- ai-projects/lmstudio-mcp/README.md | 14 +- ai-projects/lmstudio-mcp/SUMMARY.md | 2 + .../ADVANCED_TRAINING_GUIDE.md | 16 +- .../CONTINUOUS_IMPROVEMENTS_PHASE2.md | 26 ++-- .../lora-training/quantum-ai/README.md | 10 +- .../quantum-ai/azure/DEPLOYMENT.md | 2 +- .../COMPREHENSIVE_BENCHMARK_SUMMARY.md | 4 +- .../quantum-ml/CUSTOM_DATASET_GUIDE.md | 105 +++++++------ .../quantum-ml/DEPLOYMENT_SUCCESS_REPORT.md | 1 + ai-projects/quantum-ml/MCP_SERVER_README.md | 83 ++-------- .../quantum-ml/PRODUCTION_DEPLOYMENT_GUIDE.md | 13 +- .../quantum-ml/PROVIDER_COMPARISON_RESULTS.md | 20 +-- .../QUANTUM_LLM_IMPROVEMENTS_SUMMARY.md | 4 +- ai-projects/quantum-ml/QUANTUM_LLM_README.md | 16 +- ai-projects/quantum-ml/README.md | 17 +- .../quantum-ml/TRAINING_SESSION_SUMMARY.md | 5 +- .../quantum-ml/WEB_DASHBOARD_README.md | 29 ++-- .../quantum-ml/WEB_DASHBOARD_SUCCESS.md | 6 +- ai-projects/quantum-ml/azure/DEPLOYMENT.md | 2 +- ai-projects/quantum-ml/examples/README.md | 1 + .../experiments/AZURE_QUICKSTART.md | 7 +- ai-projects/quantum-ml/production/README.md | 4 +- .../src/quantum_llm/COMPREHENSIVE_README.md | 25 +-- apps/aria/AUTO-EXECUTE.md | 41 +++-- apps/aria/IMPLEMENTATION_SUMMARY.md | 6 +- database/README.md | 56 +++++-- docs/CODE_INEFFICIENCY_ANALYSIS_2026-02-17.md | 49 +++--- docs/FUTURE_PERFORMANCE_OPPORTUNITIES.md | 9 +- docs/FUTURE_PERFORMANCE_OPTIMIZATIONS.md | 18 ++- docs/OPTIMIZATION_QUICK_GUIDE.md | 28 +++- docs/PERFORMANCE_ANALYSIS.md | 81 +++++----- docs/PERFORMANCE_CRITICAL_FIXES_2026.md | 2 + docs/PERFORMANCE_IMPLEMENTATION_GUIDE.md | 145 ++++++++---------- docs/PERFORMANCE_IMPLEMENTATION_SUMMARY.md | 11 +- docs/PERFORMANCE_IMPROVEMENTS.md | 10 +- docs/PERFORMANCE_IMPROVEMENTS_REPORT.md | 28 ++-- docs/PERFORMANCE_INDEX.md | 2 + docs/PERFORMANCE_OPTIMIZATIONS.md | 13 +- docs/PERFORMANCE_OPTIMIZATIONS_FEB_2026.md | 21 +-- ...FORMANCE_OPTIMIZATIONS_SUMMARY_FEB_2026.md | 12 +- docs/PERFORMANCE_OPTIMIZATION_GUIDE.md | 21 ++- docs/PERFORMANCE_OPTIMIZATION_PR_SUMMARY.md | 2 + docs/PERFORMANCE_OPTIMIZATION_README.md | 12 +- docs/PERFORMANCE_OPTIMIZATION_SUMMARY.md | 14 +- ...ORMANCE_OPTIMIZATION_SUMMARY_2026-02-17.md | 41 +++-- docs/PERFORMANCE_QUICK_FIXES.md | 16 +- docs/PERFORMANCE_README.md | 29 ++-- docs/QUANTUM_LLM_STATUS_TRACKING.md | 19 ++- docs/README_PAGES.md | 2 +- docs/REGEX_OPTIMIZATION_2026-02-17.md | 18 +-- docs/SPARKLE_IMPROVEMENTS.md | 6 +- .../architecture/QAI_PHASE_24_IMPROVEMENTS.md | 5 +- docs/architecture/agi-provider.md | 8 +- docs/aria/ARIA_MOVEMENT_COMPLETE.md | 1 + docs/database/AZURE_MONITOR_SQL_SETUP.md | 4 +- docs/database/DATABASE_INTEGRATION_GUIDE.md | 136 ++++++++-------- docs/database/DATABASE_SQL_SETUP.md | 6 +- docs/database/TELEMETRY_COSMOS_ENABLEMENT.md | 5 + docs/deployment/AZURE_ML_TRAINING_GUIDE.md | 4 +- docs/deployment/PRODUCTION_DEPLOYMENT_PLAN.md | 3 + docs/guides/ADVANCED_AUTOMATION.md | 2 +- docs/guides/ADVANCED_FEATURES_GUIDE.md | 18 +-- docs/guides/AI_DATASETS_CATALOG.md | 4 + docs/guides/AI_IMPROVEMENTS.md | 20 +-- docs/guides/AUTONOMOUS_ORCHESTRATOR_MODEL.md | 67 ++++---- docs/guides/CHAT_WEBSITE_GUIDE.md | 5 +- docs/guides/DATASETS_SETUP_COMPLETE.md | 2 +- docs/guides/DEVELOPER_TOOLING_SUMMARY.md | 5 +- docs/guides/MONETIZATION_GUIDE.md | 14 +- docs/guides/MONITORING_GUIDE.md | 5 +- docs/guides/NOTIFICATION_SYSTEM_GUIDE.md | 13 +- docs/guides/QUICK_START_MONETIZATION.md | 9 +- docs/guides/VISION_API_GUIDE.md | 22 +-- docs/guides/VISION_ENDPOINT_QUICKTEST.md | 7 +- docs/lmstudio_agi_integration.md | 6 +- docs/quantum/QUANTUM_AZURE_EXAMPLES.md | 8 +- docs/quickrefs/QUANTUM_LLM_QUICKREF.md | 8 +- docs/root-guides/AGENT_QUICKSTART.md | 10 +- .../AUTONOMOUS_AGENT_IMPLEMENTATION.md | 8 +- docs/root-guides/ENVIRONMENT_SETUP.md | 19 +-- docs/setup/OLLAMA_USAGE_GUIDE.md | 6 +- docs/summaries/INCOME_STREAM_SUMMARY.md | 2 +- docs/summaries/PHASE_2_COMPLETE.md | 15 +- docs/training/AUTONOMOUS_TRAINING_README.md | 5 +- docs/training/BATCH_EVALUATION_GUIDE.md | 1 + notebooks/README.md | 15 +- shared/README.md | 14 +- 125 files changed, 1023 insertions(+), 1162 deletions(-) diff --git a/.github/CODE_GENERATION_GUIDE.md b/.github/CODE_GENERATION_GUIDE.md index 9a667b92c..02c587a31 100644 --- a/.github/CODE_GENERATION_GUIDE.md +++ b/.github/CODE_GENERATION_GUIDE.md @@ -92,16 +92,12 @@ from ai_projects.llm_maker.src.tool_maker import ToolMaker maker = ToolMaker() code = maker.create_tool( - 'validate_email', - 'Validates email format using regex', - {'email': 'str'}, - {'is_valid': 'bool'}, - max_attempts=3 + "validate_email", "Validates email format using regex", {"email": "str"}, {"is_valid": "bool"}, max_attempts=3 ) # Use the generated code: exec(code) -print(validate_email('user@example.com')) # True +print(validate_email("user@example.com")) # True ``` ### Example 2: Generate Data Processing Function @@ -109,15 +105,15 @@ print(validate_email('user@example.com')) # True ```python maker = ToolMaker() code = maker.create_tool( - 'parse_csv_line', - 'Parse a CSV line into a list of values', - {'line': 'str', 'delimiter': 'str'}, - {'values': 'list'}, - max_attempts=3 + "parse_csv_line", + "Parse a CSV line into a list of values", + {"line": "str", "delimiter": "str"}, + {"values": "list"}, + max_attempts=3, ) exec(code) -print(parse_csv_line('a,b,c', ',')) # ['a', 'b', 'c'] +print(parse_csv_line("a,b,c", ",")) # ['a', 'b', 'c'] ``` ### Example 3: Generate Static Website @@ -127,17 +123,15 @@ from ai_projects.llm_maker.src.website_maker import WebsiteMaker maker = WebsiteMaker() result = maker.create_website( - 'blog-template', - ['index.html', 'post.html', 'about.html'], - 'A minimalist blog template with dark mode' + "blog-template", ["index.html", "post.html", "about.html"], "A minimalist blog template with dark mode" ) # Check result -print(result['files'].keys()) # dict_keys(['index.html', 'style.css', ...]) +print(result["files"].keys()) # dict_keys(['index.html', 'style.css', ...]) # Write files -for filename, content in result['files'].items(): - with open(f"build/{filename}", 'w') as f: +for filename, content in result["files"].items(): + with open(f"build/{filename}", "w") as f: f.write(content) ``` @@ -164,12 +158,7 @@ Generate a validated Python function. **Example:** ```python -code = maker.create_tool( - 'reverse_string', - 'Reverses a string', - {'text': 'str'}, - {'reversed': 'str'} -) +code = maker.create_tool("reverse_string", "Reverses a string", {"text": "str"}, {"reversed": "str"}) ``` ### WebsiteMaker @@ -193,12 +182,10 @@ Generate a complete static website. ```python result = maker.create_website( - 'landing-page', - ['index.html', 'features.html'], - 'Marketing landing page for SaaS product' + "landing-page", ["index.html", "features.html"], "Marketing landing page for SaaS product" ) -for filename, content in result['files'].items(): +for filename, content in result["files"].items(): print(f"Generated {filename}: {len(content)} bytes") ``` @@ -225,12 +212,7 @@ http, pickle, threading, multiprocessing, ctypes, cffi **Request (fails):** ```python -code = maker.create_tool( - 'list_files', - 'List files in directory', - {'path': 'str'}, - {'files': 'list'} -) +code = maker.create_tool("list_files", "List files in directory", {"path": "str"}, {"files": "list"}) # ERROR: os import not allowed ``` @@ -238,11 +220,7 @@ code = maker.create_tool( ```python code = maker.create_tool( - 'extract_numbers', - 'Extract all numbers from text', - {'text': 'str'}, - {'numbers': 'list'}, - max_attempts=3 + "extract_numbers", "Extract all numbers from text", {"text": "str"}, {"numbers": "list"}, max_attempts=3 ) # Uses regex instead of os/file operations ``` @@ -287,12 +265,7 @@ The agent will: maker = ToolMaker() # Phone number validator -code = maker.create_tool( - 'validate_phone', - 'Validate US phone number format', - {'phone': 'str'}, - {'is_valid': 'bool'} -) +code = maker.create_tool("validate_phone", "Validate US phone number format", {"phone": "str"}, {"is_valid": "bool"}) ``` ### Use Case 2: Text Processing @@ -300,10 +273,7 @@ code = maker.create_tool( ```python # Extract hashtags from text code = maker.create_tool( - 'extract_hashtags', - 'Extract all hashtags from social media text', - {'text': 'str'}, - {'hashtags': 'list'} + "extract_hashtags", "Extract all hashtags from social media text", {"text": "str"}, {"hashtags": "list"} ) ``` @@ -312,10 +282,10 @@ code = maker.create_tool( ```python # Calculate compound interest code = maker.create_tool( - 'compound_interest', - 'Calculate compound interest', - {'principal': 'float', 'rate': 'float', 'years': 'float'}, - {'amount': 'float'} + "compound_interest", + "Calculate compound interest", + {"principal": "float", "rate": "float", "years": "float"}, + {"amount": "float"}, ) ``` @@ -323,12 +293,7 @@ code = maker.create_tool( ```python # Generate slug from title -code = maker.create_tool( - 'slugify', - 'Convert title to URL-friendly slug', - {'title': 'str'}, - {'slug': 'str'} -) +code = maker.create_tool("slugify", "Convert title to URL-friendly slug", {"title": "str"}, {"slug": "str"}) ``` ### Use Case 5: Static Sites @@ -338,9 +303,9 @@ maker = WebsiteMaker() # Documentation site result = maker.create_website( - 'api-docs', - ['index.html', 'getting-started.html', 'reference.html', 'examples.html'], - 'API documentation with navigation and code examples' + "api-docs", + ["index.html", "getting-started.html", "reference.html", "examples.html"], + "API documentation with navigation and code examples", ) ``` @@ -365,10 +330,10 @@ result = maker.create_website( ```python code = maker.create_tool( - 'my_func', - 'Does X', - {'input_value': 'str'}, # Exact name matters - {'output_result': 'str'} + "my_func", + "Does X", + {"input_value": "str"}, # Exact name matters + {"output_result": "str"}, ) ``` @@ -391,9 +356,9 @@ code = maker.create_tool( ```python result = maker.create_website( - 'simple-site', - ['index.html'], # Start with single page - 'A simple single-page site' + "simple-site", + ["index.html"], # Start with single page + "A simple single-page site", ) ``` @@ -406,11 +371,11 @@ from ai_projects.llm_maker.src.tool_maker import ToolMaker maker = ToolMaker() code = maker.create_tool( - 'my_function', - 'Does something', - {'input': 'str'}, - {'output': 'str'}, - max_attempts=5 # Increase retries + "my_function", + "Does something", + {"input": "str"}, + {"output": "str"}, + max_attempts=5, # Increase retries ) ``` @@ -418,9 +383,9 @@ code = maker.create_tool( ```python functions_to_generate = [ - ('add', 'Add two numbers', {'a': 'int', 'b': 'int'}, {'sum': 'int'}), - ('multiply', 'Multiply numbers', {'a': 'int', 'b': 'int'}, {'product': 'int'}), - ('divide', 'Divide two numbers', {'a': 'int', 'b': 'int'}, {'quotient': 'float'}), + ("add", "Add two numbers", {"a": "int", "b": "int"}, {"sum": "int"}), + ("multiply", "Multiply numbers", {"a": "int", "b": "int"}, {"product": "int"}), + ("divide", "Divide two numbers", {"a": "int", "b": "int"}, {"quotient": "float"}), ] maker = ToolMaker() diff --git a/.github/COPILOT_ADVANCED_CUSTOMIZATION.md b/.github/COPILOT_ADVANCED_CUSTOMIZATION.md index e68b67aa9..c9c345b45 100644 --- a/.github/COPILOT_ADVANCED_CUSTOMIZATION.md +++ b/.github/COPILOT_ADVANCED_CUSTOMIZATION.md @@ -423,6 +423,7 @@ import json import sys from pathlib import Path + def analyze_codebase(): """Analyze codebase structure""" result = { @@ -433,6 +434,7 @@ def analyze_codebase(): } return result + def analyze_performance(): """Analyze recent performance metrics""" result = { @@ -442,6 +444,7 @@ def analyze_performance(): } return result + if __name__ == "__main__": if len(sys.argv) > 1: command = sys.argv[1] diff --git a/.github/MCP_SERVERS_REFERENCE.md b/.github/MCP_SERVERS_REFERENCE.md index 05353101b..8efc3ec4f 100644 --- a/.github/MCP_SERVERS_REFERENCE.md +++ b/.github/MCP_SERVERS_REFERENCE.md @@ -446,12 +446,14 @@ from mcp.types import Tool, TextContent server = Server("my-server") + @server.call_tool() async def call_tool(name: str, arguments: dict) -> TextContent: if name == "hello": return TextContent(text=f"Hello {arguments['name']}") return TextContent(text="Unknown tool") + if __name__ == "__main__": server.run() ``` diff --git a/.github/agents/AI_chat_development.agent.md b/.github/agents/AI_chat_development.agent.md index 4b001ea21..bc55b01f9 100644 --- a/.github/agents/AI_chat_development.agent.md +++ b/.github/agents/AI_chat_development.agent.md @@ -166,6 +166,7 @@ Chat conversation → data_out/self_learning/*.jsonl ```python from shared.subscription_manager import get_subscription_manager, Feature + sub = mgr.get_subscription(user_id) if not sub.has_feature(Feature.BASIC_CHAT): return 403 diff --git a/.github/agents/agi-reasoning.agent.md b/.github/agents/agi-reasoning.agent.md index dfe803253..47cb8c71f 100644 --- a/.github/agents/agi-reasoning.agent.md +++ b/.github/agents/agi-reasoning.agent.md @@ -135,9 +135,9 @@ This agent works with the AGI provider system in this codebase: ```python # Key classes (ai-projects/chat-cli/src/agi_provider.py) -AGIProvider # Wraps base LLM with reasoning capabilities -AGIContext # Memory: conversation_history, reasoning_chains, goals, learned_patterns -ReasoningStep # step_type, content, confidence, metadata +AGIProvider # Wraps base LLM with reasoning capabilities +AGIContext # Memory: conversation_history, reasoning_chains, goals, learned_patterns +ReasoningStep # step_type, content, confidence, metadata # Factory create_agi_provider( @@ -148,7 +148,7 @@ create_agi_provider( enable_self_reflection=True, enable_task_decomposition=True, reasoning_depth=3, - verbose=False + verbose=False, ) ``` diff --git a/.github/agents/ai-architect.agent.md b/.github/agents/ai-architect.agent.md index 573e2741d..24966d989 100644 --- a/.github/agents/ai-architect.agent.md +++ b/.github/agents/ai-architect.agent.md @@ -112,6 +112,7 @@ class NewProvider(BaseChatProvider): else: return self.client.generate(messages) + # Wire into detection chain in shared/chat_providers.py ``` @@ -144,7 +145,7 @@ from shared.subscription_manager import get_subscription_manager, Feature sub = mgr.get_subscription(user_id) if not sub.has_feature(Feature.QUANTUM_COMPUTING): return HttpResponse("Upgrade required", status_code=403) -if not sub.track_usage('quantum_jobs', amount=1): +if not sub.track_usage("quantum_jobs", amount=1): return HttpResponse("Usage limit reached", status_code=429) ``` diff --git a/.github/agents/aria-character.agent.md b/.github/agents/aria-character.agent.md index 0eb4b18b6..6d38ba165 100644 --- a/.github/agents/aria-character.agent.md +++ b/.github/agents/aria-character.agent.md @@ -95,9 +95,9 @@ Aria uses `[aria:action:param]` tags embedded in text responses: ```python stage_state = { - 'aria': {'position': {'x': 15, 'y': 20}, 'expression': 'neutral', 'held_object': None, 'facing': 'right'}, - 'objects': {}, # Dynamic object registry - 'environment': {'table': {...}, 'stage_bounds': {'width': 100, 'height': 100}} + "aria": {"position": {"x": 15, "y": 20}, "expression": "neutral", "held_object": None, "facing": "right"}, + "objects": {}, # Dynamic object registry + "environment": {"table": {...}, "stage_bounds": {"width": 100, "height": 100}}, } ``` diff --git a/.github/agents/platform-ops.agent.md b/.github/agents/platform-ops.agent.md index d6216212b..b7bf81e5a 100644 --- a/.github/agents/platform-ops.agent.md +++ b/.github/agents/platform-ops.agent.md @@ -55,9 +55,9 @@ ANALYTICS_DASHBOARD, BATCH_PROCESSING sub = manager.get_subscription(user_id) if not sub.has_feature(Feature.QUANTUM_COMPUTING): return 403 # Upgrade required -if not sub.check_limit('quantum_jobs'): +if not sub.check_limit("quantum_jobs"): return 429 # Usage limit reached -sub.increment_usage('quantum_jobs') +sub.increment_usage("quantum_jobs") ``` ### Storage diff --git a/.github/agents/vision-ai.agent.md b/.github/agents/vision-ai.agent.md index 1da593f1c..03aef22be 100644 --- a/.github/agents/vision-ai.agent.md +++ b/.github/agents/vision-ai.agent.md @@ -66,21 +66,16 @@ Search order for `.pt` files: Checkpoint format: ```python -{ - 'model_state_dict': state_dict, - 'class_names': ['happy', 'sad', ...], - 'epoch': int, - 'accuracy': float -} +{"model_state_dict": state_dict, "class_names": ["happy", "sad", ...], "epoch": int, "accuracy": float} ``` ### VisionInference API ```python -vi = VisionInference() # Auto-loads latest checkpoint -result = vi.predict(pil_image) # → {label, confidence, scores} -result = vi.predict_base64(b64_string) # → same -result = vi.predict_file(file_path) # → same +vi = VisionInference() # Auto-loads latest checkpoint +result = vi.predict(pil_image) # → {label, confidence, scores} +result = vi.predict_base64(b64_string) # → same +result = vi.predict_file(file_path) # → same ``` ## Integration with Aria diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index a0465a077..3ef0a0ed2 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -186,13 +186,13 @@ watch -n 5 'cat data_out/autonomous_training_status.json | python -m json.tool' ```python # State machine: discovery → collection → training → analysis → optimization → deployment async def run_single_cycle(cycle_number): - await discover_datasets() # Scan datasets/, catalog by category - await download_new_datasets() # Download if below min_datasets threshold + await discover_datasets() # Scan datasets/, catalog by category + await download_new_datasets() # Download if below min_datasets threshold epochs = await select_optimal_epochs() # Adaptive: increase if accuracy < 0.70 or plateau - results = await train_cycle(epochs) # Distributed training with multiprocessing + results = await train_cycle(epochs) # Distributed training with multiprocessing await analyze_performance(results) # Track metrics, detect degradation - await optimization_cycle() # Hyperparameter tuning (if enabled) - await deployment_cycle() # Auto-deploy if accuracy > 0.90 (if enabled) + await optimization_cycle() # Hyperparameter tuning (if enabled) + await deployment_cycle() # Auto-deploy if accuracy > 0.90 (if enabled) ``` **Process management:** diff --git a/.github/instructions/chat-memory.instructions.md b/.github/instructions/chat-memory.instructions.md index 0f3813fac..ea39dd06f 100644 --- a/.github/instructions/chat-memory.instructions.md +++ b/.github/instructions/chat-memory.instructions.md @@ -54,7 +54,7 @@ similar = fetch_similar_messages(embedding, top_k=5) # 3. Inject as system context for i, msg in enumerate(similar): - messages.insert(1, {"role": "system", "content": f"[Memory #{i+1}] {msg['content']}"}) + messages.insert(1, {"role": "system", "content": f"[Memory #{i + 1}] {msg['content']}"}) # 4. Prune to fit context window messages, stats, sys_msg = prune_messages(messages, provider, model, max_tokens) diff --git a/.github/instructions/cooking-ai.instructions.md b/.github/instructions/cooking-ai.instructions.md index dccafafea..a1552c74f 100644 --- a/.github/instructions/cooking-ai.instructions.md +++ b/.github/instructions/cooking-ai.instructions.md @@ -31,7 +31,7 @@ The agent accepts any provider matching this protocol — no inheritance require ```python # _invoke() implements 2-retry: try: - response = provider.complete(messages, json_mode=True) # Attempt structured output + response = provider.complete(messages, json_mode=True) # Attempt structured output return json.loads(response) except: response = provider.complete(messages, json_mode=False) # Fallback: free text diff --git a/.github/instructions/llm-maker.instructions.md b/.github/instructions/llm-maker.instructions.md index 8b932a611..8cf8a5f72 100644 --- a/.github/instructions/llm-maker.instructions.md +++ b/.github/instructions/llm-maker.instructions.md @@ -56,7 +56,7 @@ for attempt in range(max_attempts): # default: 3 "success": True, "files": {"index.html": "...", "style.css": "...", "script.js": "..."}, "path": "ai-projects/generated_sites/{name}/", - "metadata": {"created_at": "...", "pages": [...], "features": [...]} + "metadata": {"created_at": "...", "pages": [...], "features": [...]}, } ``` diff --git a/.github/instructions/subscription.instructions.md b/.github/instructions/subscription.instructions.md index 3ba4e9318..6c550b59c 100644 --- a/.github/instructions/subscription.instructions.md +++ b/.github/instructions/subscription.instructions.md @@ -16,8 +16,18 @@ applyTo: "**/subscription_manager.py" ```python # 10 gatable features: -BASIC_CHAT, ARIA_CHARACTER, QUANTUM_COMPUTING, ADVANCED_TRAINING, -WEBSITE_MAKER, API_ACCESS, CUSTOM_MODELS, PRIORITY_SUPPORT, +( + BASIC_CHAT, + ARIA_CHARACTER, + QUANTUM_COMPUTING, + ADVANCED_TRAINING, +) +( + WEBSITE_MAKER, + API_ACCESS, + CUSTOM_MODELS, + PRIORITY_SUPPORT, +) ANALYTICS_DASHBOARD, BATCH_PROCESSING ``` @@ -34,21 +44,21 @@ if not sub.has_feature(Feature.QUANTUM_COMPUTING): return HttpResponse("Upgrade to PRO for quantum access", status_code=403) # Usage limit check -if not sub.check_limit('quantum_jobs'): +if not sub.check_limit("quantum_jobs"): return HttpResponse("Monthly quota exceeded", status_code=429) # Track usage -sub.increment_usage('quantum_jobs') +sub.increment_usage("quantum_jobs") # Usage reporting -pct = sub.get_usage_percentage('chat_messages') # → 0.0 to 1.0 +pct = sub.get_usage_percentage("chat_messages") # → 0.0 to 1.0 ``` ## Subscription Lifecycle ```python -sub.is_active() # Check if subscription is current -sub.reset_usage() # Called monthly to reset counters +sub.is_active() # Check if subscription is current +sub.reset_usage() # Called monthly to reset counters ``` ## Storage diff --git a/.github/instructions/token-utils.instructions.md b/.github/instructions/token-utils.instructions.md index 56964108f..bf7ef1c99 100644 --- a/.github/instructions/token-utils.instructions.md +++ b/.github/instructions/token-utils.instructions.md @@ -34,9 +34,9 @@ pruned_msgs, stats, system_msg = prune_messages( messages, provider, model, - max_context_tokens=None, # Auto-detect from model - reserve_output_tokens=1024, # Reserve for response - system_prompt=None + max_context_tokens=None, # Auto-detect from model + reserve_output_tokens=1024, # Reserve for response + system_prompt=None, ) ``` @@ -51,11 +51,11 @@ pruned_msgs, stats, system_msg = prune_messages( ```python { - "original_tokens": int, # Total before pruning - "pruned_tokens": int, # Total after pruning - "removed_count": int, # Messages removed - "budget": int, # Context window budget - "reserve_output_tokens": int # Reserved for response + "original_tokens": int, # Total before pruning + "pruned_tokens": int, # Total after pruning + "removed_count": int, # Messages removed + "budget": int, # Context window budget + "reserve_output_tokens": int, # Reserved for response } ``` diff --git a/.github/instructions/vision-inference.instructions.md b/.github/instructions/vision-inference.instructions.md index 624fca477..73cf73344 100644 --- a/.github/instructions/vision-inference.instructions.md +++ b/.github/instructions/vision-inference.instructions.md @@ -17,10 +17,10 @@ class TinyConvNet(nn.Module): ## Inference Pipeline ```python -vi = VisionInference() # Auto-loads latest checkpoint -result = vi.predict(pil_image) # PIL Image input -result = vi.predict_base64(b64_string) # Base64 string input -result = vi.predict_file(file_path) # File path input +vi = VisionInference() # Auto-loads latest checkpoint +result = vi.predict(pil_image) # PIL Image input +result = vi.predict_base64(b64_string) # Base64 string input +result = vi.predict_file(file_path) # File path input # Returns: {label: str, confidence: float, scores: Dict[str, float]} ``` @@ -41,12 +41,7 @@ Search order for `.pt` files: Checkpoint format: ```python -{ - 'model_state_dict': OrderedDict, - 'class_names': ['happy', 'sad', ...], - 'epoch': int, - 'accuracy': float -} +{"model_state_dict": OrderedDict, "class_names": ["happy", "sad", ...], "epoch": int, "accuracy": float} ``` ## Coding Conventions diff --git a/.github/prompts/agi.prompt.md b/.github/prompts/agi.prompt.md index 0417c5a28..8de5d3276 100644 --- a/.github/prompts/agi.prompt.md +++ b/.github/prompts/agi.prompt.md @@ -44,9 +44,9 @@ You are an autonomous AGI agent capable of independent reasoning, self-correctio **AGI Provider Context:** ```python -AGIProvider # Wraps base LLM with reasoning capabilities -AGIContext # Memory: conversation_history, reasoning_chains, goals, learned_patterns -ReasoningStep # step_type, content, confidence, metadata +AGIProvider # Wraps base LLM with reasoning capabilities +AGIContext # Memory: conversation_history, reasoning_chains, goals, learned_patterns +ReasoningStep # step_type, content, confidence, metadata create_agi_provider(reasoning_depth=3, enable_chain_of_thought=True, enable_self_reflection=True) ``` diff --git a/.github/skills/agi-reasoning-debug-workflow/SKILL.md b/.github/skills/agi-reasoning-debug-workflow/SKILL.md index 14670e6fe..0c8f830cb 100644 --- a/.github/skills/agi-reasoning-debug-workflow/SKILL.md +++ b/.github/skills/agi-reasoning-debug-workflow/SKILL.md @@ -87,9 +87,9 @@ If decomposition steps don't match intent, check `_decompose_task()` intent dete ### Step 6 — Inspect memory limits ```python -MAX_HISTORY_SIZE = 50 # conversation_history entries — prune if exceeded +MAX_HISTORY_SIZE = 50 # conversation_history entries — prune if exceeded MAX_REASONING_CHAINS = 10 # ReasoningStep records — old chains are dropped -MAX_GOALS = 5 # active goals in AGIContext +MAX_GOALS = 5 # active goals in AGIContext # If reasoning feels repetitive: MAX_REASONING_CHAINS may be saturated # Clear via: agi_provider.context.reasoning_chains.clear() diff --git a/.github/skills/chat-memory-embeddings-workflow/SKILL.md b/.github/skills/chat-memory-embeddings-workflow/SKILL.md index 8a9b70592..c3c071559 100644 --- a/.github/skills/chat-memory-embeddings-workflow/SKILL.md +++ b/.github/skills/chat-memory-embeddings-workflow/SKILL.md @@ -39,6 +39,7 @@ Diagnose which tier is active: ```python from shared.chat_memory import generate_embedding + emb = generate_embedding("test") print(f"Dimension: {len(emb) if emb else 'None — all providers failed'}") ``` @@ -57,6 +58,7 @@ If `QAI_DB_CONN` is unset, `generate_embedding()` and `store_embedding()` degrad ```python from shared.chat_memory import generate_embedding, fetch_similar_messages + q = generate_embedding("What is quantum computing?") results = fetch_similar_messages(q, top_k=5, session_id="test-session") print(results) @@ -86,7 +88,7 @@ Correct injection order: embedding = generate_embedding(user_text) similar = fetch_similar_messages(embedding, top_k=5, session_id=session_id) for i, msg in enumerate(similar): - messages.insert(1, {"role": "system", "content": f"[Memory #{i+1}] {msg['content']}"}) + messages.insert(1, {"role": "system", "content": f"[Memory #{i + 1}] {msg['content']}"}) # Then prune AFTER injecting memories messages, stats, sys_msg = prune_messages(messages, provider, model, max_tokens) ``` diff --git a/.github/skills/create-hook-workflow/SKILL.md b/.github/skills/create-hook-workflow/SKILL.md index 2b57f545e..f5b7cd092 100644 --- a/.github/skills/create-hook-workflow/SKILL.md +++ b/.github/skills/create-hook-workflow/SKILL.md @@ -141,6 +141,7 @@ def main() -> None: sys.exit(0) + if __name__ == "__main__": main() ``` diff --git a/.github/skills/llm-tool-generation-workflow/SKILL.md b/.github/skills/llm-tool-generation-workflow/SKILL.md index d36a73182..95c60b4cf 100644 --- a/.github/skills/llm-tool-generation-workflow/SKILL.md +++ b/.github/skills/llm-tool-generation-workflow/SKILL.md @@ -110,7 +110,7 @@ result = maker.create_website( name="my-portfolio", description="Personal portfolio with about and contact pages", pages=["index", "about", "contact"], - features=["responsive", "dark-mode"] + features=["responsive", "dark-mode"], ) # result = { # "success": True, diff --git a/.github/skills/subscription-feature-gate-workflow/SKILL.md b/.github/skills/subscription-feature-gate-workflow/SKILL.md index 8ca641c16..449355249 100644 --- a/.github/skills/subscription-feature-gate-workflow/SKILL.md +++ b/.github/skills/subscription-feature-gate-workflow/SKILL.md @@ -63,11 +63,11 @@ if not sub.has_feature(Feature.QUANTUM_COMPUTING): return func.HttpResponse("Upgrade to PRO for quantum access", status_code=403) # 2. Usage limit SECOND (returns 429) -if not sub.check_limit('quantum_jobs'): +if not sub.check_limit("quantum_jobs"): return func.HttpResponse("Monthly quantum job quota exceeded", status_code=429) # 3. Track usage AFTER confirming capacity -sub.increment_usage('quantum_jobs') +sub.increment_usage("quantum_jobs") ``` **Critical order**: `has_feature()` → `check_limit()` → work → `increment_usage()`. @@ -80,7 +80,7 @@ ENTERPRISE tier has no limits — `check_limit()` always returns `True` for ENTE ```python sub = mgr.get_subscription("enterprise-user-id") print(sub.tier) # → "ENTERPRISE" -print(sub.check_limit('quantum_jobs')) # → True (unlimited) +print(sub.check_limit("quantum_jobs")) # → True (unlimited) ``` ### Step 4 — Verify Response Codes @@ -103,7 +103,7 @@ Check fields: `tier`, `usage_counts`, `active`, `reset_date`. ```python sub.reset_usage() # Resets all counters; called monthly -sub.is_active() # Returns False if subscription expired +sub.is_active() # Returns False if subscription expired ``` If counters are stuck after month rollover, call `reset_usage()` manually or restart the host to trigger reload. diff --git a/.github/skills/token-budget-debug-workflow/SKILL.md b/.github/skills/token-budget-debug-workflow/SKILL.md index aba899216..46eee68e3 100644 --- a/.github/skills/token-budget-debug-workflow/SKILL.md +++ b/.github/skills/token-budget-debug-workflow/SKILL.md @@ -52,10 +52,10 @@ from token_utils import count_messages_tokens ```python # Known context windows: MODEL_DEFAULTS = { - "gpt-4o": 128_000, - "gpt-3.5-turbo": 16_384, - "azure_default": 16_384, - "phi": 4_096, + "gpt-4o": 128_000, + "gpt-3.5-turbo": 16_384, + "azure_default": 16_384, + "phi": 4_096, } # If the wrong default is applied, token budget will be too small/large @@ -70,9 +70,9 @@ pruned_msgs, stats, system_msg = prune_messages( messages, provider="azure", model="gpt-4o", - max_context_tokens=None, # None = auto-detect from model - reserve_output_tokens=1024, # Always reserve for response - system_prompt=None + max_context_tokens=None, # None = auto-detect from model + reserve_output_tokens=1024, # Always reserve for response + system_prompt=None, ) print(stats) diff --git a/.github/skills/vision-inference-debug-workflow/SKILL.md b/.github/skills/vision-inference-debug-workflow/SKILL.md index 7be27c736..9b41a0853 100644 --- a/.github/skills/vision-inference-debug-workflow/SKILL.md +++ b/.github/skills/vision-inference-debug-workflow/SKILL.md @@ -74,11 +74,13 @@ All inputs are resized and normalized before inference: ```python # Required preprocessing pipeline: -transform = transforms.Compose([ - transforms.Resize((64, 64)), # ← MUST match training resolution - transforms.ToTensor(), - transforms.Normalize(...) # ← MUST use same mean/std as training -]) +transform = transforms.Compose( + [ + transforms.Resize((64, 64)), # ← MUST match training resolution + transforms.ToTensor(), + transforms.Normalize(...), # ← MUST use same mean/std as training + ] +) # Input tensor shape: (1, C, H, W) = (1, 3, 64, 64) # If resolution doesn't match: wrong AdaptiveAvgPool2d output → wrong Linear layer shape @@ -94,11 +96,13 @@ vi = VisionInference() # Option A — PIL Image: from PIL import Image + img = Image.open("test.jpg") result = vi.predict(img) # Option B — Base64 string: import base64 + with open("test.jpg", "rb") as f: b64 = base64.b64encode(f.read()).decode() result = vi.predict_base64(b64) @@ -132,6 +136,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # If CUDA OOM error: force CPU import os + os.environ["CUDA_VISIBLE_DEVICES"] = "" vi = VisionInference() diff --git a/AI/README.md b/AI/README.md index d8d1be4f1..137423dd1 100644 --- a/AI/README.md +++ b/AI/README.md @@ -200,11 +200,8 @@ Response: Output JSON only: {"result": , "reason": ""} """ -@evals.evaluator(CustomPromptEvaluatorConfig( - name="professionalism", - prompt=custom_prompt, - threshold=3 -)) + +@evals.evaluator(CustomPromptEvaluatorConfig(name="professionalism", prompt=custom_prompt, threshold=3)) def test_professionalism(self, evaluator_results: EvaluatorResults): assert evaluator_results.professionalism.result == "pass" ``` @@ -216,17 +213,15 @@ Implement evaluation logic in Python for deterministic checks: ```python from pytest_agent_evals import CustomCodeEvaluatorConfig + def check_response_length(sample: dict, item: dict) -> float: """Check if response meets minimum length requirements.""" response = sample.get("output_text", "") min_length = 50 return 1.0 if len(response) >= min_length else 0.0 -@evals.evaluator(CustomCodeEvaluatorConfig( - name="response_length", - grader=check_response_length, - threshold=0.5 -)) + +@evals.evaluator(CustomCodeEvaluatorConfig(name="response_length", grader=check_response_length, threshold=0.5)) def test_response_length(self, evaluator_results: EvaluatorResults): assert evaluator_results.response_length.result == "pass" ``` diff --git a/ai-projects/llm-maker/QUICKSTART.md b/ai-projects/llm-maker/QUICKSTART.md index 2a550ac7b..6e3dd8f34 100644 --- a/ai-projects/llm-maker/QUICKSTART.md +++ b/ai-projects/llm-maker/QUICKSTART.md @@ -32,7 +32,7 @@ tool = maker.create_tool( name="calculate_factorial", description="Calculate the factorial of a number", parameters={"n": "int"}, - return_type="int" + return_type="int", ) # Register it @@ -47,13 +47,9 @@ from llm_maker import ToolExecutor executor = ToolExecutor() # Execute the tool -result = executor.execute( - code=tool.code, - function_name="calculate_factorial", - args={"n": 5} -) +result = executor.execute(code=tool.code, function_name="calculate_factorial", args={"n": 5}) -if result['success']: +if result["success"]: print(f"Result: {result['result']}") else: print(f"Error: {result['error']}") @@ -87,11 +83,7 @@ tool = maker.create_tool( description="Calculate the nth Fibonacci number", parameters={"n": "int"}, return_type="int", - examples=[ - {"input": {"n": 0}, "output": 0}, - {"input": {"n": 1}, "output": 1}, - {"input": {"n": 10}, "output": 55} - ] + examples=[{"input": {"n": 0}, "output": 0}, {"input": {"n": 1}, "output": 1}, {"input": {"n": 10}, "output": 55}], ) ``` @@ -99,10 +91,7 @@ tool = maker.create_tool( ```python tool = maker.create_tool( - name="word_count", - description="Count words in a text string", - parameters={"text": "str"}, - return_type="int" + name="word_count", description="Count words in a text string", parameters={"text": "str"}, return_type="int" ) ``` @@ -113,7 +102,7 @@ tool = maker.create_tool( name="filter_dict", description="Filter dictionary by list of keys", parameters={"data": "dict", "keys": "list"}, - return_type="dict" + return_type="dict", ) ``` diff --git a/ai-projects/llm-maker/README.md b/ai-projects/llm-maker/README.md index 4b9dd2237..2f25c1fc0 100644 --- a/ai-projects/llm-maker/README.md +++ b/ai-projects/llm-maker/README.md @@ -60,7 +60,7 @@ tool = maker.create_tool( name="calculate_fibonacci", description="Calculate the nth Fibonacci number", parameters={"n": "int"}, - return_type="int" + return_type="int", ) # Validate the tool diff --git a/ai-projects/llm-maker/UI_ENHANCEMENTS.md b/ai-projects/llm-maker/UI_ENHANCEMENTS.md index c3003c2fd..0f992754b 100644 --- a/ai-projects/llm-maker/UI_ENHANCEMENTS.md +++ b/ai-projects/llm-maker/UI_ENHANCEMENTS.md @@ -76,11 +76,10 @@ GET http://localhost:8090/api/tools ```python import requests -requests.post('http://localhost:8090/api/tools', json={ - 'name': 'my_tool', - 'description': 'What it does', - 'parameters': {'x': 'int'} -}) +requests.post( + "http://localhost:8090/api/tools", + json={"name": "my_tool", "description": "What it does", "parameters": {"x": "int"}}, +) ``` ## User Benefits diff --git a/ai-projects/llm-maker/WEBSITE_MAKER_README.md b/ai-projects/llm-maker/WEBSITE_MAKER_README.md index 626296c1a..cdcd0f92f 100644 --- a/ai-projects/llm-maker/WEBSITE_MAKER_README.md +++ b/ai-projects/llm-maker/WEBSITE_MAKER_README.md @@ -387,14 +387,14 @@ import requests # Create website response = requests.post( - 'http://localhost:8090/api/websites', + "http://localhost:8090/api/websites", json={ - 'name': 'my-site', - 'description': 'A portfolio website...', - 'style': 'modern', - 'pages': ['index', 'about'], - 'features': ['responsive design'] - } + "name": "my-site", + "description": "A portfolio website...", + "style": "modern", + "pages": ["index", "about"], + "features": ["responsive design"], + }, ) result = response.json() @@ -445,14 +445,10 @@ curl -X POST http://localhost:8090/api/websites \ from website_maker import WebsiteMaker # Use specific provider -maker = WebsiteMaker(provider_name='azure') # or 'openai', 'local' +maker = WebsiteMaker(provider_name="azure") # or 'openai', 'local' # Create website -result = maker.create_website( - name='my-site', - description='...', - style='modern' -) +result = maker.create_website(name="my-site", description="...", style="modern") ``` ### Batch Generation @@ -463,9 +459,9 @@ from website_maker import WebsiteMaker maker = WebsiteMaker() websites = [ - {'name': 'site1', 'description': '...', 'style': 'modern'}, - {'name': 'site2', 'description': '...', 'style': 'minimal'}, - {'name': 'site3', 'description': '...', 'style': 'creative'} + {"name": "site1", "description": "...", "style": "modern"}, + {"name": "site2", "description": "...", "style": "minimal"}, + {"name": "site3", "description": "...", "style": "creative"}, ] for site_config in websites: diff --git a/ai-projects/llm-maker/WEB_UI_README.md b/ai-projects/llm-maker/WEB_UI_README.md index 8ebf90279..3be0f161c 100644 --- a/ai-projects/llm-maker/WEB_UI_README.md +++ b/ai-projects/llm-maker/WEB_UI_README.md @@ -214,7 +214,7 @@ By default, server binds to `0.0.0.0` allowing remote access. For local-only, change to `127.0.0.1`: ```python -host = '127.0.0.1' # localhost only +host = "127.0.0.1" # localhost only ``` ### Production Deployment diff --git a/ai-projects/lmstudio-mcp/AGENT_INTEGRATION.md b/ai-projects/lmstudio-mcp/AGENT_INTEGRATION.md index 883646f2f..759a3b21a 100644 --- a/ai-projects/lmstudio-mcp/AGENT_INTEGRATION.md +++ b/ai-projects/lmstudio-mcp/AGENT_INTEGRATION.md @@ -52,9 +52,7 @@ from agi_provider import detect_provider provider, choice = detect_provider(provider_choice="lmstudio") # Use with simple message -response = provider.complete([ - {"role": "user", "content": "Explain neural networks"} -]) +response = provider.complete([{"role": "user", "content": "Explain neural networks"}]) ``` ## Agent Integration @@ -98,6 +96,7 @@ Route to LM Studio with custom logic: ```python from lmstudio_agent_integration import should_use_lmstudio + def select_provider(query: str): if should_use_lmstudio(query): return "lmstudio" @@ -112,6 +111,7 @@ def select_provider(query: str): import asyncio from lmstudio_agent_integration import get_lmstudio_agent_client + async def main(): client = get_lmstudio_agent_client() @@ -125,14 +125,12 @@ async def main(): # Send message response = await client.complete( - messages=[ - {"role": "system", "content": "You are helpful."}, - {"role": "user", "content": "Hello!"} - ], - temperature=0.7 + messages=[{"role": "system", "content": "You are helpful."}, {"role": "user", "content": "Hello!"}], + temperature=0.7, ) print(f"Response: {response}") + asyncio.run(main()) ``` @@ -153,7 +151,7 @@ agi = AGIProvider(base_provider=base_provider) response = agi.reason( "Explain quantum entanglement in simple terms", trace=True, # Show reasoning - decompose=True # Break into subtasks + decompose=True, # Break into subtasks ) print(response) ``` @@ -164,6 +162,7 @@ print(response) from lmstudio_agent_integration import get_lmstudio_agent_client import asyncio + async def multi_agent_workflow(): client = get_lmstudio_agent_client() @@ -171,23 +170,24 @@ async def multi_agent_workflow(): tech_response = await client.complete( messages=[ {"role": "system", "content": "You are a technical expert."}, - {"role": "user", "content": "What is machine learning?"} + {"role": "user", "content": "What is machine learning?"}, ], - temperature=0.3 # Deterministic + temperature=0.3, # Deterministic ) # Agent 2: Simplification simple_response = await client.complete( messages=[ {"role": "system", "content": "Simplify complex technical content."}, - {"role": "user", "content": f"Simplify: {tech_response}"} + {"role": "user", "content": f"Simplify: {tech_response}"}, ], - temperature=0.5 + temperature=0.5, ) print("Technical:", tech_response) print("Simplified:", simple_response) + asyncio.run(multi_agent_workflow()) ``` @@ -197,6 +197,7 @@ asyncio.run(multi_agent_workflow()) from lmstudio_agent_integration import get_lmstudio_agent_client import asyncio + async def compare_models(): client = get_lmstudio_agent_client() @@ -206,12 +207,11 @@ async def compare_models(): # Compare responses from different models for model in models: response = await client.complete( - messages=[{"role": "user", "content": "What is AI?"}], - model=model, - max_tokens=256 + messages=[{"role": "user", "content": "What is AI?"}], model=model, max_tokens=256 ) print(f"\n{model}:\n{response}") + asyncio.run(compare_models()) ``` @@ -240,17 +240,14 @@ from lmstudio_agent_integration import LMStudioAgentClient # Custom configuration client = LMStudioAgentClient( - base_url="http://192.168.1.100:1234/v1", - model="mistral-7b", - temperature=0.5, - max_tokens=4096 + base_url="http://192.168.1.100:1234/v1", model="mistral-7b", temperature=0.5, max_tokens=4096 ) # Use with custom settings response = await client.complete( messages=[...], temperature=0.3, # Override default - max_tokens=512 # Override default + max_tokens=512, # Override default ) ``` @@ -263,6 +260,7 @@ response = await client.complete( from lmstudio_agent_integration import get_lmstudio_agent_client import asyncio + @app.route(route="api/chat", methods=["POST"]) async def chat(req: func.HttpRequest) -> func.HttpResponse: body = req.get_json() @@ -274,10 +272,7 @@ async def chat(req: func.HttpRequest) -> func.HttpResponse: messages = body.get("messages", []) response = await client.complete(messages) - return func.HttpResponse( - json.dumps({"response": response}), - status_code=200 - ) + return func.HttpResponse(json.dumps({"response": response}), status_code=200) ``` ## Troubleshooting diff --git a/ai-projects/lmstudio-mcp/AGI_INTEGRATION_INDEX.md b/ai-projects/lmstudio-mcp/AGI_INTEGRATION_INDEX.md index 817e29adf..4a153ca82 100644 --- a/ai-projects/lmstudio-mcp/AGI_INTEGRATION_INDEX.md +++ b/ai-projects/lmstudio-mcp/AGI_INTEGRATION_INDEX.md @@ -79,9 +79,7 @@ from agi_provider import AGIProvider agi = AGIProvider() # Automatically routes to LM Studio for technical content -response = agi.complete([ - {"role": "user", "content": "Explain neural networks"} -]) +response = agi.complete([{"role": "user", "content": "Explain neural networks"}]) print(response) ``` @@ -124,6 +122,7 @@ print(response) ```python from agi_provider import detect_provider, AGIProvider + provider, _ = detect_provider("lmstudio") agi = AGIProvider(base_provider=provider) ``` @@ -132,6 +131,7 @@ agi = AGIProvider(base_provider=provider) ```python from lmstudio_agi_integration import get_lmstudio_agent_registry_entry + _AGENT_REGISTRY["lmstudio-local"] = get_lmstudio_agent_registry_entry() ``` @@ -139,6 +139,7 @@ _AGENT_REGISTRY["lmstudio-local"] = get_lmstudio_agent_registry_entry() ```python from lmstudio_agi_integration import AGILMStudioRouter + router = AGILMStudioRouter() # Manual routing decisions ``` @@ -147,6 +148,7 @@ router = AGILMStudioRouter() ```python from lmstudio_agi_integration import decompose_task_with_lmstudio + subtasks = await decompose_task_with_lmstudio(complex_task) ``` @@ -208,6 +210,7 @@ python agi_provider_examples.py ```python from lmstudio_agi_integration import AGILMStudioRouter + router = AGILMStudioRouter() # Test different analyses diff --git a/ai-projects/lmstudio-mcp/AGI_PROVIDER_INTEGRATION.md b/ai-projects/lmstudio-mcp/AGI_PROVIDER_INTEGRATION.md index 8a037bfa4..7ed79b21b 100644 --- a/ai-projects/lmstudio-mcp/AGI_PROVIDER_INTEGRATION.md +++ b/ai-projects/lmstudio-mcp/AGI_PROVIDER_INTEGRATION.md @@ -51,9 +51,7 @@ provider, choice = detect_provider("lmstudio") agi = AGIProvider(base_provider=provider) # Use AGI reasoning with LM Studio backend -response = agi.complete([ - {"role": "user", "content": "Explain generators in Python"} -]) +response = agi.complete([{"role": "user", "content": "Explain generators in Python"}]) ``` ### Level 2: Agent Registry Integration @@ -69,9 +67,7 @@ _AGENT_REGISTRY["lmstudio-local"] = get_lmstudio_agent_registry_entry() # AGI provider now routes to LM Studio for suitable queries agi = AGIProvider() -response = agi.complete([ - {"role": "user", "content": "Write a Python decorator"} -]) +response = agi.complete([{"role": "user", "content": "Write a Python decorator"}]) # Routes to lmstudio because: domain=technical, intent=coding ``` @@ -113,10 +109,7 @@ subtasks = await decompose_task_with_lmstudio(task, domain="ai") # Reason through implementation for subtask in subtasks: - reasoning = await reason_with_lmstudio_chain_of_thought( - subtask['task'], - depth=3 - ) + reasoning = await reason_with_lmstudio_chain_of_thought(subtask["task"], depth=3) print(f"Subtask: {subtask['task']}") print(f"Reasoning: {reasoning['conclusion']}") ``` @@ -140,9 +133,7 @@ from agi_provider import AGIProvider agi = AGIProvider() # Automatically routes to LM Studio for suitable queries -response = agi.complete([ - {"role": "user", "content": "Explain neural networks"} -]) +response = agi.complete([{"role": "user", "content": "Explain neural networks"}]) print(response) ``` @@ -168,9 +159,7 @@ The AGI provider chains reasoning steps. LM Studio participates: agi = AGIProvider(enable_chain_of_thought=True) # Generates multi-step reasoning -response = agi.complete([ - {"role": "user", "content": "Why does deep learning work?"} -]) +response = agi.complete([{"role": "user", "content": "Why does deep learning work?"}]) # Reasoning chain is tracked: # 1. Understand question @@ -192,9 +181,7 @@ Complex tasks are broken into subtasks: # - Documentation: Technical writer agi = AGIProvider(enable_task_decomposition=True) -response = agi.complete([ - {"role": "user", "content": "Implement a RAG system"} -]) +response = agi.complete([{"role": "user", "content": "Implement a RAG system"}]) ``` ### Self-Reflection @@ -204,9 +191,7 @@ AGI provider reviews and improves responses: ```python agi = AGIProvider(enable_self_reflection=True) -response = agi.complete([ - {"role": "user", "content": "Explain LSTM networks"} -]) +response = agi.complete([{"role": "user", "content": "Explain LSTM networks"}]) # Process: # 1. LM Studio generates initial response @@ -283,19 +268,15 @@ export LMSTUDIO_MAX_TOKENS=2048 agi = AGIProvider( # Base provider (LM Studio or other) base_provider=None, # Auto-detect - # Reasoning features enable_chain_of_thought=True, enable_self_reflection=True, enable_task_decomposition=True, - # Reasoning depth (1-5) reasoning_depth=3, - # Output control temperature=0.7, max_output_tokens=2048, - # Verbosity verbose=False, ) @@ -325,21 +306,13 @@ response = await router.route_query(query, messages, analysis) entry = get_lmstudio_agent_registry_entry() # Complete with LM Studio routing -response = await complete_with_lmstudio_routing( - agi_provider, - messages, - stream=True, - prefer_lmstudio=False -) +response = await complete_with_lmstudio_routing(agi_provider, messages, stream=True, prefer_lmstudio=False) # Decompose task into subtasks subtasks = await decompose_task_with_lmstudio(task, domain="technical") # Generate reasoning chain -reasoning = await reason_with_lmstudio_chain_of_thought( - query, - depth=3 -) +reasoning = await reason_with_lmstudio_chain_of_thought(query, depth=3) ``` ## Examples @@ -352,9 +325,7 @@ from agi_provider import AGIProvider agi = AGIProvider() # Query perfect for LM Studio (domain=technical) -response = agi.complete([ - {"role": "user", "content": "Explain backpropagation in neural networks"} -]) +response = agi.complete([{"role": "user", "content": "Explain backpropagation in neural networks"}]) # Flow: # 1. Query analyzed: technical domain, explanation intent @@ -369,15 +340,10 @@ print(response) ```python from agi_provider import AGIProvider -agi = AGIProvider( - enable_task_decomposition=True, - enable_self_reflection=True -) +agi = AGIProvider(enable_task_decomposition=True, enable_self_reflection=True) # Perfect for code specialist + LM Studio -response = agi.complete([ - {"role": "user", "content": "Write a Python async decorator with error handling"} -]) +response = agi.complete([{"role": "user", "content": "Write a Python async decorator with error handling"}]) # Flow: # 1. Analyzed: coding intent, technical domain @@ -399,10 +365,7 @@ from lmstudio_agi_integration import ( task = "Build a recommendation system using collaborative filtering" # LM Studio decomposes -subtasks = await decompose_task_with_lmstudio( - task, - domain="ai" -) +subtasks = await decompose_task_with_lmstudio(task, domain="ai") # Returns: # 1. Understand collaborative filtering # 2. Design data structures @@ -415,10 +378,7 @@ for subtask in subtasks: print(f"\nSubtask: {subtask['task']}") # Get reasoning from LM Studio - reasoning = await reason_with_lmstudio_chain_of_thought( - subtask['task'], - depth=2 - ) + reasoning = await reason_with_lmstudio_chain_of_thought(subtask["task"], depth=2) print(f"Analysis: {reasoning['conclusion']}") ``` @@ -433,10 +393,10 @@ agi = AGIProvider() # Different queries, different routing decisions queries = [ - "Explain quantum entanglement", # → quantum-specialist - "Write a sorting algorithm", # → lmstudio-local (coding) - "What is the weather?", # → general agent - "Optimize this function locally", # → lmstudio-local (local mention) + "Explain quantum entanglement", # → quantum-specialist + "Write a sorting algorithm", # → lmstudio-local (coding) + "What is the weather?", # → general agent + "Optimize this function locally", # → lmstudio-local (local mention) ] for query in queries: diff --git a/ai-projects/lmstudio-mcp/AGI_PROVIDER_SUMMARY.md b/ai-projects/lmstudio-mcp/AGI_PROVIDER_SUMMARY.md index 549c7c8b6..f0d8487c8 100644 --- a/ai-projects/lmstudio-mcp/AGI_PROVIDER_SUMMARY.md +++ b/ai-projects/lmstudio-mcp/AGI_PROVIDER_SUMMARY.md @@ -144,10 +144,7 @@ subtasks = await decompose_task_with_lmstudio(task, domain="ai") # Each subtask uses appropriate agent for subtask in subtasks: # LM Studio provides technical reasoning - reasoning = await reason_with_lmstudio_chain_of_thought( - subtask['task'], - depth=3 - ) + reasoning = await reason_with_lmstudio_chain_of_thought(subtask["task"], depth=3) # Other agents handle specialized aspects ``` @@ -260,12 +257,10 @@ agi = AGIProvider( enable_chain_of_thought=True, enable_self_reflection=True, enable_task_decomposition=True, - # Reasoning parameters reasoning_depth=3, temperature=0.7, max_output_tokens=2048, - # Verbosity verbose=True, ) @@ -279,8 +274,8 @@ agi = AGIProvider( router = AGILMStudioRouter(fallback_provider="agi") # Health management -await router.ensure_healthy() # Check LM Studio -router.should_use_lmstudio(query_analysis) # Decide routing +await router.ensure_healthy() # Check LM Studio +router.should_use_lmstudio(query_analysis) # Decide routing # Execution await router.route_query(query, messages, analysis) @@ -296,12 +291,7 @@ subtasks = await decompose_task_with_lmstudio(task, domain="ai") reasoning = await reason_with_lmstudio_chain_of_thought(query, depth=3) # Integrated completion -response = await complete_with_lmstudio_routing( - agi_provider, - messages, - stream=True, - prefer_lmstudio=False -) +response = await complete_with_lmstudio_routing(agi_provider, messages, stream=True, prefer_lmstudio=False) ``` ### Registry Integration @@ -380,11 +370,10 @@ python lmstudio_mcp_server.py ```python from agi_provider import AGIProvider + agi = AGIProvider() -response = agi.complete([ - {"role": "user", "content": "Explain neural networks"} -]) +response = agi.complete([{"role": "user", "content": "Explain neural networks"}]) # Automatically routes to LM Studio for technical content ``` @@ -427,6 +416,7 @@ Test routing: ```python from lmstudio_agi_integration import AGILMStudioRouter + router = AGILMStudioRouter() # Test with different query analyses ``` diff --git a/ai-projects/lmstudio-mcp/INTEGRATION_SUMMARY.md b/ai-projects/lmstudio-mcp/INTEGRATION_SUMMARY.md index 75cb0d632..c8f62214f 100644 --- a/ai-projects/lmstudio-mcp/INTEGRATION_SUMMARY.md +++ b/ai-projects/lmstudio-mcp/INTEGRATION_SUMMARY.md @@ -90,17 +90,16 @@ python -m chat_cli --provider lmstudio "Explain neural networks" import asyncio from lmstudio_agent_integration import get_lmstudio_agent_client + async def main(): client = get_lmstudio_agent_client() response = await client.complete( - messages=[ - {"role": "system", "content": "You are helpful."}, - {"role": "user", "content": "Hello!"} - ] + messages=[{"role": "system", "content": "You are helpful."}, {"role": "user", "content": "Hello!"}] ) print(response) + asyncio.run(main()) ``` @@ -118,11 +117,7 @@ base_provider, _ = detect_provider("lmstudio") # Use with reasoning agi = AGIProvider(base_provider=base_provider) -response = agi.reason( - "Explain quantum computing", - decompose=True, - trace=True -) +response = agi.reason("Explain quantum computing", decompose=True, trace=True) ``` ## Agent System Integration Points @@ -152,7 +147,7 @@ Registered agent with capabilities: "temperature_control": True, "token_budgeting": True, "model_switching": True, - } + }, } ``` @@ -231,9 +226,9 @@ LMSTUDIO_MAX_TOKENS=2048 # Max response tokens # Override defaults in code response = await client.complete( messages=[...], - model="llama-13b", # Use different model - temperature=0.3, # Deterministic mode - max_tokens=512 # Shorter responses + model="llama-13b", # Use different model + temperature=0.3, # Deterministic mode + max_tokens=512, # Shorter responses ) ``` @@ -375,6 +370,7 @@ Add endpoint for LM Studio chat: ```python from lmstudio_agent_integration import get_lmstudio_agent_client + @app.route(route="api/chat", methods=["POST"]) async def chat_endpoint(req): if req.params.get("provider") == "lmstudio": diff --git a/ai-projects/lmstudio-mcp/PRIVACY_FIRST_GUIDE.md b/ai-projects/lmstudio-mcp/PRIVACY_FIRST_GUIDE.md index 181d8e05d..02721176d 100644 --- a/ai-projects/lmstudio-mcp/PRIVACY_FIRST_GUIDE.md +++ b/ai-projects/lmstudio-mcp/PRIVACY_FIRST_GUIDE.md @@ -120,22 +120,22 @@ Local Results Only ### Classification Types ```python -PII # Personally identifiable: names, SSN, email -HEALTH # Medical records, diagnoses -FINANCIAL # Bank accounts, credit cards, salary -PROPRIETARY # Trade secrets, code, designs -LEGAL # Contracts, litigation documents -CREDENTIALS # Passwords, API keys, tokens +PII # Personally identifiable: names, SSN, email +HEALTH # Medical records, diagnoses +FINANCIAL # Bank accounts, credit cards, salary +PROPRIETARY # Trade secrets, code, designs +LEGAL # Contracts, litigation documents +CREDENTIALS # Passwords, API keys, tokens ``` ### Privacy Levels ```python -PUBLIC # No constraints -INTERNAL # Organizational, not confidential -CONFIDENTIAL # Sensitive but not regulated -RESTRICTED # Highly sensitive, regulated -ENCRYPTED # Encrypted at rest +PUBLIC # No constraints +INTERNAL # Organizational, not confidential +CONFIDENTIAL # Sensitive but not regulated +RESTRICTED # Highly sensitive, regulated +ENCRYPTED # Encrypted at rest ``` ## Implementation Guide @@ -181,7 +181,7 @@ print(result) # Check audit trail compliance = await processor.verify_privacy_compliance() -if compliance['compliant']: +if compliance["compliant"]: print("✓ All processing stayed local") else: print("✗ Cloud processing detected!") @@ -492,6 +492,7 @@ with open(KEY_PATH, "rb") as f: ```python import shutil + # Securely delete sensitive files def secure_delete(file_path): """Overwrite file content before deletion.""" @@ -502,6 +503,7 @@ def secure_delete(file_path): os.remove(file_path) + secure_delete("sensitive_data.txt") ``` @@ -517,12 +519,14 @@ BLOCKED_PATTERNS = [ r"https?://(.*\.)?amazonaws\.com", ] + def check_network_requests(log_data): for pattern in BLOCKED_PATTERNS: if re.search(pattern, log_data): alert("✗ Cloud API attempt detected!") raise SecurityError("Privacy violation: cloud API detected") + # Monitor outbound connections # Use tools like tcpdump, netstat, or network policy engine ``` @@ -545,6 +549,7 @@ def analyze_audit_log(): ) notify_security_team() + analyze_audit_log() ``` diff --git a/ai-projects/lmstudio-mcp/PRIVACY_FIRST_SUMMARY.md b/ai-projects/lmstudio-mcp/PRIVACY_FIRST_SUMMARY.md index 71296b300..8b1d2fc06 100644 --- a/ai-projects/lmstudio-mcp/PRIVACY_FIRST_SUMMARY.md +++ b/ai-projects/lmstudio-mcp/PRIVACY_FIRST_SUMMARY.md @@ -115,7 +115,7 @@ result = await processor.process_sensitive_data( ```python compliance = await processor.verify_privacy_compliance() -print(compliance['message']) # "All recent processing stayed local" +print(compliance["message"]) # "All recent processing stayed local" ``` ### 4. Use with Multi-Agent Reasoning diff --git a/ai-projects/lmstudio-mcp/QUICK_REFERENCE.md b/ai-projects/lmstudio-mcp/QUICK_REFERENCE.md index ddaa277bf..ffaddb333 100644 --- a/ai-projects/lmstudio-mcp/QUICK_REFERENCE.md +++ b/ai-projects/lmstudio-mcp/QUICK_REFERENCE.md @@ -35,13 +35,13 @@ python -m chat_cli --provider lmstudio "Tell me about AI" from lmstudio_agent_integration import get_lmstudio_agent_client import asyncio + async def main(): client = get_lmstudio_agent_client() - response = await client.complete([ - {"role": "user", "content": "Hello"} - ]) + response = await client.complete([{"role": "user", "content": "Hello"}]) print(response) + asyncio.run(main()) ``` @@ -169,6 +169,7 @@ python -m chat_cli --provider lmstudio "test" ```python from lmstudio_agent_integration import register_lmstudio_agent + register_lmstudio_agent(_AGENT_REGISTRY) ``` diff --git a/ai-projects/lmstudio-mcp/README.md b/ai-projects/lmstudio-mcp/README.md index d132be3ae..bb698965b 100644 --- a/ai-projects/lmstudio-mcp/README.md +++ b/ai-projects/lmstudio-mcp/README.md @@ -305,11 +305,9 @@ Requests follow OpenAI API format: ```python from lmstudio_mcp_server import LMStudioClient + async def main(): - client = LMStudioClient( - base_url="http://127.0.0.1:1234/v1", - model="mistral-7b" - ) + client = LMStudioClient(base_url="http://127.0.0.1:1234/v1", model="mistral-7b") # List models models = await client.list_models() @@ -317,12 +315,10 @@ async def main(): # Send chat message result = await client.chat_completion( - messages=[ - {"role": "user", "content": "Explain quantum computing"} - ], - temperature=0.7 + messages=[{"role": "user", "content": "Explain quantum computing"}], temperature=0.7 ) - print(result['message']) + print(result["message"]) + asyncio.run(main()) ``` diff --git a/ai-projects/lmstudio-mcp/SUMMARY.md b/ai-projects/lmstudio-mcp/SUMMARY.md index 6d2dd9785..88b7a8e49 100644 --- a/ai-projects/lmstudio-mcp/SUMMARY.md +++ b/ai-projects/lmstudio-mcp/SUMMARY.md @@ -156,10 +156,12 @@ python quickstart.py ```python # Existing provider way from shared.chat_providers import LMStudioProvider + provider = LMStudioProvider(base_url="http://127.0.0.1:1234/v1") # New MCP way from lmstudio_mcp_server import LMStudioClient + client = LMStudioClient(base_url="http://127.0.0.1:1234/v1") await client.chat_completion(messages=[...]) ``` diff --git a/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/ADVANCED_TRAINING_GUIDE.md b/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/ADVANCED_TRAINING_GUIDE.md index c0479c8aa..85b882a85 100644 --- a/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/ADVANCED_TRAINING_GUIDE.md +++ b/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/ADVANCED_TRAINING_GUIDE.md @@ -71,11 +71,7 @@ Results saved to `data_out/evaluation_results/`: from scripts.auto_eval import AutomaticEvaluator evaluator = AutomaticEvaluator() -metrics = evaluator.evaluate_model( - model_path="data_out/lora_training", - test_dataset="test.jsonl", - num_samples=100 -) +metrics = evaluator.evaluate_model(model_path="data_out/lora_training", test_dataset="test.jsonl", num_samples=100) print(f"Perplexity: {metrics.perplexity:.2f}") ``` @@ -199,11 +195,11 @@ python scripts\semantic_pruning.py \ from scripts.semantic_pruning import PruningConfig, SemanticPruner config = PruningConfig( - similarity_threshold=0.95, # Higher = more aggressive - min_length=10, # Minimum words - max_length=2048, # Maximum words - quality_threshold=0.3, # 0.0-1.0 scale - target_reduction=0.3 # Target 30% reduction + similarity_threshold=0.95, # Higher = more aggressive + min_length=10, # Minimum words + max_length=2048, # Maximum words + quality_threshold=0.3, # 0.0-1.0 scale + target_reduction=0.3, # Target 30% reduction ) pruner = SemanticPruner(config) diff --git a/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/CONTINUOUS_IMPROVEMENTS_PHASE2.md b/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/CONTINUOUS_IMPROVEMENTS_PHASE2.md index 7125eb789..c239d1bba 100644 --- a/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/CONTINUOUS_IMPROVEMENTS_PHASE2.md +++ b/ai-projects/lora-training/microsoft_phi-silica-3.6_v1/CONTINUOUS_IMPROVEMENTS_PHASE2.md @@ -104,15 +104,10 @@ from scripts.lr_finder import LearningRateFinder lr_finder = LearningRateFinder(model, optimizer, criterion) # Run range test -results = lr_finder.range_test( - train_loader, - start_lr=1e-7, - end_lr=10.0, - num_iter=100 -) +results = lr_finder.range_test(train_loader, start_lr=1e-7, end_lr=10.0, num_iter=100) # Use suggested LR -suggested_lr = results['suggested_lr'] +suggested_lr = results["suggested_lr"] print(f"Suggested LR: {suggested_lr:.2e}") ``` @@ -191,18 +186,16 @@ API Examples: import requests # Single generation -response = requests.post("http://localhost:8000/generate", json={ - "prompt": "Explain quantum computing", - "max_tokens": 100, - "temperature": 0.7 -}) +response = requests.post( + "http://localhost:8000/generate", + json={"prompt": "Explain quantum computing", "max_tokens": 100, "temperature": 0.7}, +) print(response.json()["text"]) # Batch generation -response = requests.post("http://localhost:8000/batch", json={ - "prompts": ["Question 1", "Question 2", "Question 3"], - "max_tokens": 50 -}) +response = requests.post( + "http://localhost:8000/batch", json={"prompts": ["Question 1", "Question 2", "Question 3"], "max_tokens": 50} +) print(response.json()["results"]) ``` @@ -417,6 +410,7 @@ For existing projects: ```python from scripts.training_monitor import TrainingMonitor + monitor = TrainingMonitor() # Add logging calls in training loop ``` diff --git a/ai-projects/lora-training/quantum-ai/README.md b/ai-projects/lora-training/quantum-ai/README.md index d5f06c088..b55fabe79 100644 --- a/ai-projects/lora-training/quantum-ai/README.md +++ b/ai-projects/lora-training/quantum-ai/README.md @@ -141,13 +141,7 @@ import torch from torch.utils.data import DataLoader, TensorDataset # Create hybrid model -model = HybridQNN( - input_dim=8, - hidden_dim=16, - num_qubits=4, - quantum_layers=2, - output_dim=3 -) +model = HybridQNN(input_dim=8, hidden_dim=16, num_qubits=4, quantum_layers=2, output_dim=3) # Prepare data loaders train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True) @@ -179,7 +173,7 @@ backends = azure_qm.list_backends() print(f"Available backends: {backends}") # Run on quantum hardware/simulator -result = azure_qm.run_circuit(qc, shots=1000, backend_name='ionq.simulator') +result = azure_qm.run_circuit(qc, shots=1000, backend_name="ionq.simulator") print(f"Results: {result['counts']}") ``` diff --git a/ai-projects/lora-training/quantum-ai/azure/DEPLOYMENT.md b/ai-projects/lora-training/quantum-ai/azure/DEPLOYMENT.md index 8ba7ea86a..11689ef53 100644 --- a/ai-projects/lora-training/quantum-ai/azure/DEPLOYMENT.md +++ b/ai-projects/lora-training/quantum-ai/azure/DEPLOYMENT.md @@ -90,7 +90,7 @@ azure_qm = AzureQuantumManager() azure_qm.connect() # Estimate cost -cost = azure_qm.estimate_cost(circuit, backend_name='ionq.qpu') +cost = azure_qm.estimate_cost(circuit, backend_name="ionq.qpu") print(cost) ``` diff --git a/ai-projects/quantum-ml/COMPREHENSIVE_BENCHMARK_SUMMARY.md b/ai-projects/quantum-ml/COMPREHENSIVE_BENCHMARK_SUMMARY.md index 01d6f745b..341a04899 100644 --- a/ai-projects/quantum-ml/COMPREHENSIVE_BENCHMARK_SUMMARY.md +++ b/ai-projects/quantum-ml/COMPREHENSIVE_BENCHMARK_SUMMARY.md @@ -110,10 +110,10 @@ Successfully expanded quantum ML infrastructure from **4 to 15 datasets** (275% ```python # wine_red/white: Semicolon delimiter -df = pd.read_csv(path, sep=';') +df = pd.read_csv(path, sep=";") # wheat_seeds: Tab/whitespace delimiter -df = pd.read_csv(path, sep=r'\s+', header=None) +df = pd.read_csv(path, sep=r"\s+", header=None) # breast_cancer: No header, skip ID column df = pd.read_csv(path, header=None) diff --git a/ai-projects/quantum-ml/CUSTOM_DATASET_GUIDE.md b/ai-projects/quantum-ml/CUSTOM_DATASET_GUIDE.md index d8ee48031..9dc845b4f 100644 --- a/ai-projects/quantum-ml/CUSTOM_DATASET_GUIDE.md +++ b/ai-projects/quantum-ml/CUSTOM_DATASET_GUIDE.md @@ -63,29 +63,35 @@ import torch # 1. LOAD YOUR DATA # ============================================ + # Option A: From CSV def load_from_csv(filepath): df = pd.read_csv(filepath) - X = df.drop('label', axis=1).values # All columns except 'label' - y = df['label'].values + X = df.drop("label", axis=1).values # All columns except 'label' + y = df["label"].values return X, y + # Option B: From NumPy def load_from_numpy(X_file, y_file): X = np.load(X_file) y = np.load(y_file) return X, y + # Option C: Manual arrays def create_manual_data(): - X = np.array([ - [1.0, 2.0, 3.0, 4.0], - [2.0, 3.0, 4.0, 5.0], - # ... your data ... - ]) + X = np.array( + [ + [1.0, 2.0, 3.0, 4.0], + [2.0, 3.0, 4.0, 5.0], + # ... your data ... + ] + ) y = np.array([0, 1, ...]) # Your labels return X, y + # ============================================ # 2. LOAD YOUR ACTUAL DATA (CHANGE THIS!) # ============================================ @@ -106,10 +112,11 @@ print(f"Classes: {np.unique(y)}") # Split into train/validation X_train, X_val, y_train, y_val = train_test_split( - X, y, + X, + y, test_size=0.2, random_state=42, - stratify=y # Keeps class balance + stratify=y, # Keeps class balance ) # Standardize features (IMPORTANT for quantum circuits!) @@ -134,6 +141,7 @@ if n_features < n_qubits: elif n_features > n_qubits: # Use PCA to reduce from sklearn.decomposition import PCA + pca = PCA(n_components=n_qubits) X_train = pca.fit_transform(X_train) X_val = pca.transform(X_val) @@ -153,21 +161,15 @@ print(f"Created model with {n_qubits} qubits") # ============================================ print("\nStarting training...") -history = train_quantum_model( - model=model, - X_train=X_train, - y_train=y_train, - X_val=X_val, - y_val=y_val -) +history = train_quantum_model(model=model, X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val) # ============================================ # 6. EVALUATE RESULTS # ============================================ -print("\n" + "="*50) +print("\n" + "=" * 50) print("TRAINING COMPLETE!") -print("="*50) +print("=" * 50) print(f"Final Training Loss: {history['train_loss'][-1]:.4f}") print(f"Final Validation Loss: {history['val_loss'][-1]:.4f}") print(f"Final Validation Accuracy: {history['val_acc'][-1]:.4f}") @@ -183,6 +185,7 @@ print("\nModel saved to: results/my_quantum_model.pt") # Save scaler for inference import joblib + joblib.dump(scaler, "results/scaler.pkl") print("Scaler saved to: results/scaler.pkl") @@ -190,6 +193,7 @@ print("Scaler saved to: results/scaler.pkl") # 8. MAKE PREDICTIONS ON NEW DATA # ============================================ + def predict_new_data(new_X, model, scaler): """Predict on new unseen data""" # Preprocess @@ -214,6 +218,7 @@ def predict_new_data(new_X, model, scaler): return predictions.numpy() + # Example: Predict on validation set predictions = predict_new_data(X_val, model, scaler) print(f"\nPredictions shape: {predictions.shape}") @@ -259,17 +264,18 @@ from hybrid_qnn import HybridQNN, QuantumClassicalTrainer # Example: Load from CSV df = pd.read_csv("your_data.csv") -X = df.drop('label', axis=1).values -y = df['label'].values +X = df.drop("label", axis=1).values +y = df["label"].values # Or create sample data from sklearn.datasets import make_classification + X, y = make_classification( n_samples=500, n_features=10, n_informative=8, n_classes=3, # Multi-class! - random_state=42 + random_state=42, ) print(f"Data: {X.shape[0]} samples, {X.shape[1]} features, {len(np.unique(y))} classes") @@ -279,9 +285,7 @@ print(f"Data: {X.shape[0]} samples, {X.shape[1]} features, {len(np.unique(y))} c # ============================================ # Split -X_train, X_val, y_train, y_val = train_test_split( - X, y, test_size=0.2, random_state=42, stratify=y -) +X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) # Scale scaler = StandardScaler() @@ -289,14 +293,8 @@ X_train = scaler.fit_transform(X_train) X_val = scaler.transform(X_val) # Create data loaders -train_dataset = TensorDataset( - torch.FloatTensor(X_train), - torch.LongTensor(y_train) -) -val_dataset = TensorDataset( - torch.FloatTensor(X_val), - torch.LongTensor(y_val) -) +train_dataset = TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train)) +val_dataset = TensorDataset(torch.FloatTensor(X_val), torch.LongTensor(y_val)) train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False) @@ -306,12 +304,12 @@ val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False) # ============================================ model = HybridQNN( - input_dim=X.shape[1], # Your number of features - hidden_dim=32, # Classical hidden layer size - num_qubits=4, # Quantum circuit size - quantum_layers=2, # Quantum depth (optimal!) + input_dim=X.shape[1], # Your number of features + hidden_dim=32, # Classical hidden layer size + num_qubits=4, # Quantum circuit size + quantum_layers=2, # Quantum depth (optimal!) output_dim=len(np.unique(y)), # Number of classes - dropout=0.2 + dropout=0.2, ) print(model) @@ -323,13 +321,13 @@ print(model) trainer = QuantumClassicalTrainer( model=model, learning_rate=0.01, - device='cpu' # or 'cuda' if you have GPU + device="cpu", # or 'cuda' if you have GPU ) trainer.train( train_loader=train_loader, val_loader=val_loader, - num_epochs=50 # Adjust based on convergence + num_epochs=50, # Adjust based on convergence ) # ============================================ @@ -385,7 +383,8 @@ print("Model saved!") ```python from sklearn.impute import SimpleImputer - imputer = SimpleImputer(strategy='mean') + + imputer = SimpleImputer(strategy="mean") X = imputer.fit_transform(X) ``` @@ -393,6 +392,7 @@ print("Model saved!") ```python from imblearn.over_sampling import SMOTE + smote = SMOTE(random_state=42) X, y = smote.fit_resample(X, y) ``` @@ -401,6 +401,7 @@ print("Model saved!") ```python from sklearn.preprocessing import RobustScaler + scaler = RobustScaler() X = scaler.fit_transform(X) ``` @@ -416,10 +417,10 @@ print("Model saved!") df = pd.read_csv("patient_data.csv") # Features: age, blood_pressure, cholesterol, glucose, BMI -X = df[['age', 'bp', 'cholesterol', 'glucose', 'bmi']].values +X = df[["age", "bp", "cholesterol", "glucose", "bmi"]].values # Label: 0=healthy, 1=disease -y = df['diagnosis'].values +y = df["diagnosis"].values # Preprocess scaler = StandardScaler() @@ -427,6 +428,7 @@ X_scaled = scaler.fit_transform(X) # Pad to 4 features (we have 5, so PCA) from sklearn.decomposition import PCA + pca = PCA(n_components=4) X_reduced = pca.fit_transform(X_scaled) @@ -441,11 +443,12 @@ X_reduced = pca.fit_transform(X_scaled) df = pd.read_csv("transactions.csv") # Features: amount, time, merchant_category, etc. -X = df.drop(['fraud'], axis=1).values -y = df['fraud'].values # 0=legitimate, 1=fraud +X = df.drop(["fraud"], axis=1).values +y = df["fraud"].values # 0=legitimate, 1=fraud # Handle imbalance (fraud is rare!) from imblearn.over_sampling import SMOTE + smote = SMOTE(random_state=42) X_balanced, y_balanced = smote.fit_resample(X, y) @@ -458,6 +461,7 @@ X_balanced, y_balanced = smote.fit_resample(X, y) ```python # Load small images (e.g., MNIST digits) from sklearn.datasets import load_digits + digits = load_digits() X = digits.data # 8x8 images flattened to 64 features @@ -491,6 +495,7 @@ print(f"Explained variance: {pca.explained_variance_ratio_.sum():.2%}") ```python # Always standardize! from sklearn.preprocessing import StandardScaler + scaler = StandardScaler() X = scaler.fit_transform(X) @@ -499,6 +504,7 @@ print(np.bincount(y)) # If imbalanced, use SMOTE from imblearn.over_sampling import SMOTE + X, y = SMOTE().fit_resample(X, y) ``` @@ -521,6 +527,7 @@ epochs = 50 # instead of 100 # Sample your data from sklearn.utils import resample + X_sample, y_sample = resample(X, y, n_samples=500, random_state=42) ``` @@ -576,10 +583,10 @@ print(f"Class distribution: {np.bincount(y)}") # Plot training curves import matplotlib.pyplot as plt -plt.plot(history['train_loss'], label='Train Loss') -plt.plot(history['val_loss'], label='Val Loss') +plt.plot(history["train_loss"], label="Train Loss") +plt.plot(history["val_loss"], label="Val Loss") plt.legend() -plt.savefig('results/training_curve.png') +plt.savefig("results/training_curve.png") ``` ### 4. Compare to Classical Baseline @@ -612,8 +619,8 @@ print(f"Quantum: {quantum_acc:.2%}") 1. **Save model & scaler** ```python - torch.save(model.state_dict(), 'model.pt') - joblib.dump(scaler, 'scaler.pkl') + torch.save(model.state_dict(), "model.pt") + joblib.dump(scaler, "scaler.pkl") ``` 2. **Create inference script** diff --git a/ai-projects/quantum-ml/DEPLOYMENT_SUCCESS_REPORT.md b/ai-projects/quantum-ml/DEPLOYMENT_SUCCESS_REPORT.md index 241197918..def778202 100644 --- a/ai-projects/quantum-ml/DEPLOYMENT_SUCCESS_REPORT.md +++ b/ai-projects/quantum-ml/DEPLOYMENT_SUCCESS_REPORT.md @@ -62,6 +62,7 @@ az quantum workspace show --resource-group rg-quantum-ai --workspace-name quantu ```python from azure.quantum import Workspace + workspace = Workspace(...) targets = workspace.get_targets() ``` diff --git a/ai-projects/quantum-ml/MCP_SERVER_README.md b/ai-projects/quantum-ml/MCP_SERVER_README.md index 7648ce16d..d091bdb35 100644 --- a/ai-projects/quantum-ml/MCP_SERVER_README.md +++ b/ai-projects/quantum-ml/MCP_SERVER_README.md @@ -83,32 +83,24 @@ import asyncio from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client + async def main(): - server_params = StdioServerParameters( - command="python", - args=["quantum_mcp_server.py"], - env={} - ) + server_params = StdioServerParameters(command="python", args=["quantum_mcp_server.py"], env={}) async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: await session.initialize() # Create a Bell state circuit - result = await session.call_tool( - "create_quantum_circuit", - {"n_qubits": 2, "circuit_type": "bell"} - ) + result = await session.call_tool("create_quantum_circuit", {"n_qubits": 2, "circuit_type": "bell"}) print(result.content[0].text) # Simulate it circuit_id = "..." # Extract from result - sim_result = await session.call_tool( - "simulate_quantum_circuit", - {"circuit_id": circuit_id, "shots": 1024} - ) + sim_result = await session.call_tool("simulate_quantum_circuit", {"circuit_id": circuit_id, "shots": 1024}) print(sim_result.content[0].text) + asyncio.run(main()) ``` @@ -118,23 +110,11 @@ asyncio.run(main()) ```python # Create a GHZ state (3-qubit entanglement) -result = await session.call_tool( - "create_quantum_circuit", - { - "n_qubits": 3, - "circuit_type": "ghz" - } -) +result = await session.call_tool("create_quantum_circuit", {"n_qubits": 3, "circuit_type": "ghz"}) # Extract circuit ID from result # Then simulate with 10,000 shots -sim_result = await session.call_tool( - "simulate_quantum_circuit", - { - "circuit_id": "", - "shots": 10000 - } -) +sim_result = await session.call_tool("simulate_quantum_circuit", {"circuit_id": "", "shots": 10000}) ``` ### 2. Train a Quantum Machine Learning Model @@ -143,13 +123,7 @@ sim_result = await session.call_tool( # Train on the Iris dataset with quantum circuit result = await session.call_tool( "train_quantum_classifier", - { - "dataset": "iris", - "n_qubits": 4, - "n_layers": 2, - "epochs": 100, - "entanglement": "linear" - } + {"dataset": "iris", "n_qubits": 4, "n_layers": 2, "epochs": 100, "entanglement": "linear"}, ) ``` @@ -159,11 +133,7 @@ result = await session.call_tool( # Connect to Azure await session.call_tool( "connect_azure_quantum", - { - "subscription_id": "", - "resource_group": "rg-quantum-ai", - "workspace_name": "quantum-ai-workspace" - } + {"subscription_id": "", "resource_group": "rg-quantum-ai", "workspace_name": "quantum-ai-workspace"}, ) # List available backends @@ -171,36 +141,19 @@ backends = await session.call_tool("list_quantum_backends", {}) # Estimate cost cost = await session.call_tool( - "estimate_quantum_cost", - { - "circuit_id": "", - "backend_name": "ionq.simulator", - "shots": 100 - } + "estimate_quantum_cost", {"circuit_id": "", "backend_name": "ionq.simulator", "shots": 100} ) # Submit job job = await session.call_tool( - "submit_quantum_job", - { - "circuit_id": "", - "backend_name": "ionq.simulator", - "shots": 500 - } + "submit_quantum_job", {"circuit_id": "", "backend_name": "ionq.simulator", "shots": 500} ) ``` ### 4. Grover Search for Marked States ```python -grover = await session.call_tool( - "grover_search", - { - "n_qubits": 3, - "marked_states": [5], - "shots": 2000 - } -) +grover = await session.call_tool("grover_search", {"n_qubits": 3, "marked_states": [5], "shots": 2000}) ``` ### 5. Execute Enhanced Variational Circuit @@ -208,22 +161,14 @@ grover = await session.call_tool( ```python vqc = await session.call_tool( "run_variational_circuit", - { - "input_vector": [0.1, 0.2, -0.3, 0.4], - "encoding": "hybrid", - "entanglement": "pyramid", - "n_layers": 3 - } + {"input_vector": [0.1, 0.2, -0.3, 0.4], "encoding": "hybrid", "entanglement": "pyramid", "n_layers": 3}, ) ``` ### 6. Visualize a Cached Circuit ```python -viz = await session.call_tool( - "visualize_cached_circuit", - {"circuit_id": "", "style": "mpl"} -) +viz = await session.call_tool("visualize_cached_circuit", {"circuit_id": "", "style": "mpl"}) ``` ## Tool Reference diff --git a/ai-projects/quantum-ml/PRODUCTION_DEPLOYMENT_GUIDE.md b/ai-projects/quantum-ml/PRODUCTION_DEPLOYMENT_GUIDE.md index a48b80b9c..37babfa3e 100644 --- a/ai-projects/quantum-ml/PRODUCTION_DEPLOYMENT_GUIDE.md +++ b/ai-projects/quantum-ml/PRODUCTION_DEPLOYMENT_GUIDE.md @@ -259,9 +259,7 @@ quantum: ```python deployer = QuantumAzureMLDeployment() run = deployer.submit_training_job( - script_path='train_azure_ml.py', - experiment_name='quantum-8qubit', - arguments={'n_qubits': 8, 'epochs': 100} + script_path="train_azure_ml.py", experiment_name="quantum-8qubit", arguments={"n_qubits": 8, "epochs": 100} ) ``` @@ -269,19 +267,14 @@ quantum: ```python model = deployer.register_model( - model_path='outputs/quantum_model.pt', - model_name='quantum-classifier-8q', - tags={'qubits': '8', 'accuracy': '97.5%'} + model_path="outputs/quantum_model.pt", model_name="quantum-classifier-8q", tags={"qubits": "8", "accuracy": "97.5%"} ) ``` 3. **REST API Deployment** ```python - service = deployer.deploy_inference_endpoint( - model_name='quantum-classifier-8q', - service_name='quantum-api' - ) + service = deployer.deploy_inference_endpoint(model_name="quantum-classifier-8q", service_name="quantum-api") # Access at: service.scoring_uri ``` diff --git a/ai-projects/quantum-ml/PROVIDER_COMPARISON_RESULTS.md b/ai-projects/quantum-ml/PROVIDER_COMPARISON_RESULTS.md index caad0df68..fce6b7f09 100644 --- a/ai-projects/quantum-ml/PROVIDER_COMPARISON_RESULTS.md +++ b/ai-projects/quantum-ml/PROVIDER_COMPARISON_RESULTS.md @@ -279,14 +279,14 @@ Shots: 1000 ```python # H decomposition -qc.rz(np.pi/2, q) -qc.rx(np.pi/2, q) -qc.rz(np.pi/2, q) +qc.rz(np.pi / 2, q) +qc.rx(np.pi / 2, q) +qc.rz(np.pi / 2, q) # CX decomposition -qc.rx(np.pi/2, control) -qc.rzz(np.pi/2, control, target) -qc.rx(-np.pi/2, control) +qc.rx(np.pi / 2, control) +qc.rzz(np.pi / 2, control, target) +qc.rx(-np.pi / 2, control) ``` - **Works on:** None tested ✅ @@ -304,12 +304,12 @@ qc.rx(-np.pi/2, control) ```python # H decomposition qc.rz(np.pi, q) -qc.rx(np.pi/2, q) +qc.rx(np.pi / 2, q) # CX decomposition -qc.rz(np.pi/2, target) +qc.rz(np.pi / 2, target) qc.cz(control, target) -qc.rz(-np.pi/2, target) +qc.rz(-np.pi / 2, target) ``` - **Works on:** Not directly tested on Rigetti (standard worked) @@ -415,7 +415,7 @@ qc.rz(-np.pi/2, target) ```python def compute_entropy(counts): total = sum(counts.values()) - p = [v/total for v in counts.values() if v > 0] + p = [v / total for v in counts.values() if v > 0] return -sum(p_i * np.log2(p_i) for p_i in p) ``` diff --git a/ai-projects/quantum-ml/QUANTUM_LLM_IMPROVEMENTS_SUMMARY.md b/ai-projects/quantum-ml/QUANTUM_LLM_IMPROVEMENTS_SUMMARY.md index b6fdeba77..194ba9de5 100644 --- a/ai-projects/quantum-ml/QUANTUM_LLM_IMPROVEMENTS_SUMMARY.md +++ b/ai-projects/quantum-ml/QUANTUM_LLM_IMPROVEMENTS_SUMMARY.md @@ -42,8 +42,8 @@ This comprehensive work package includes: # Users can now tune caching behavior per environment config = QuantumLLMConfig( cache_enabled=True, - cache_max_size=512, # Larger cache for high-traffic services - cache_ttl_seconds=300, # Shorter TTL for real-time updates + cache_max_size=512, # Larger cache for high-traffic services + cache_ttl_seconds=300, # Shorter TTL for real-time updates ) ``` diff --git a/ai-projects/quantum-ml/QUANTUM_LLM_README.md b/ai-projects/quantum-ml/QUANTUM_LLM_README.md index 39d1796ff..839d0439a 100644 --- a/ai-projects/quantum-ml/QUANTUM_LLM_README.md +++ b/ai-projects/quantum-ml/QUANTUM_LLM_README.md @@ -50,11 +50,7 @@ python quantum_llm_quickstart.py --mode generate --model data_out/quantum_llm_qu from quantum_llm_advanced import MultiScaleQuantumAttention # Creates 4 attention heads with 2, 3, 4, and 6 qubits -attention = MultiScaleQuantumAttention( - d_model=128, - n_heads=4, - n_qubits_per_head=[2, 3, 4, 6] -) +attention = MultiScaleQuantumAttention(d_model=128, n_heads=4, n_qubits_per_head=[2, 3, 4, 6]) ``` **Benefits:** @@ -106,11 +102,7 @@ cache = QuantumCircuitCache(cache_size=1000) ```python from quantum_llm_advanced import QuantumPromptTuning -prompt_tuner = QuantumPromptTuning( - d_model=128, - n_qubits=4, - n_prompts=10 -) +prompt_tuner = QuantumPromptTuning(d_model=128, n_qubits=4, n_prompts=10) ``` **Benefits:** @@ -451,8 +443,8 @@ config["optimization_level"] = 2 # Adaptive batch sizing across stages stages = [ TrainingStage(..., batch_size=16), # Classical - TrainingStage(..., batch_size=8), # Transition - TrainingStage(..., batch_size=4), # Full quantum + TrainingStage(..., batch_size=8), # Transition + TrainingStage(..., batch_size=4), # Full quantum ] ``` diff --git a/ai-projects/quantum-ml/README.md b/ai-projects/quantum-ml/README.md index 80ecc284c..e56dce644 100644 --- a/ai-projects/quantum-ml/README.md +++ b/ai-projects/quantum-ml/README.md @@ -34,13 +34,7 @@ Hybrid quantum-classical neural networks with configurable depth: from src.hybrid_qnn import HybridQNN import torch -model = HybridQNN( - input_dim=10, - hidden_dim=16, - n_qubits=4, - n_quantum_layers=2, - output_dim=1 -) +model = HybridQNN(input_dim=10, hidden_dim=16, n_qubits=4, n_quantum_layers=2, output_dim=1) output = model(torch.randn(8, 10)) ``` @@ -370,6 +364,7 @@ model = HybridQuantumClassifier(input_dim=4, quantum_classifier=qc) # Train the model from src.quantum_classifier import train_quantum_model + history = train_quantum_model(model, X_train, y_train, X_val, y_val) print(f"Final accuracy: {history['val_acc'][-1]:.4f}") @@ -382,13 +377,7 @@ from src.hybrid_qnn import HybridQNN import torch # Create hybrid model -model = HybridQNN( - input_dim=10, - hidden_dim=16, - n_qubits=4, - n_quantum_layers=2, - output_dim=1 -) +model = HybridQNN(input_dim=10, hidden_dim=16, n_qubits=4, n_quantum_layers=2, output_dim=1) # Forward pass x = torch.randn(8, 10) # Batch of 8 samples diff --git a/ai-projects/quantum-ml/TRAINING_SESSION_SUMMARY.md b/ai-projects/quantum-ml/TRAINING_SESSION_SUMMARY.md index 94ffe41f0..b1d19cb0e 100644 --- a/ai-projects/quantum-ml/TRAINING_SESSION_SUMMARY.md +++ b/ai-projects/quantum-ml/TRAINING_SESSION_SUMMARY.md @@ -350,9 +350,8 @@ python .\benchmark_all_datasets.py import torch from src.hybrid_qnn import HybridQNN -model = HybridQNN(input_dim=4, hidden_dim=16, n_qubits=4, - n_quantum_layers=2, output_dim=2) -model.load_state_dict(torch.load('results/ionosphere_model.pt')) +model = HybridQNN(input_dim=4, hidden_dim=16, n_qubits=4, n_quantum_layers=2, output_dim=2) +model.load_state_dict(torch.load("results/ionosphere_model.pt")) model.eval() ``` diff --git a/ai-projects/quantum-ml/WEB_DASHBOARD_README.md b/ai-projects/quantum-ml/WEB_DASHBOARD_README.md index 645fec692..65482c3be 100644 --- a/ai-projects/quantum-ml/WEB_DASHBOARD_README.md +++ b/ai-projects/quantum-ml/WEB_DASHBOARD_README.md @@ -215,7 +215,7 @@ If port 5000 is busy: ```python # Edit web_app.py, change last line: -app.run(host='0.0.0.0', port=5001, debug=True, threaded=True) +app.run(host="0.0.0.0", port=5001, debug=True, threaded=True) ``` ### Dependencies Not Found @@ -257,9 +257,9 @@ Edit `create_quantum_circuit()` in `web_app.py`: ```python def create_quantum_circuit(n_qubits, n_layers): - dev = qml.device('default.qubit', wires=n_qubits) + dev = qml.device("default.qubit", wires=n_qubits) - @qml.qnode(dev, interface='autograd') + @qml.qnode(dev, interface="autograd") def circuit(inputs, weights): # Your custom circuit here # Example: Add RX rotation layer @@ -286,18 +286,21 @@ Use the REST API programmatically: import requests # Start training -response = requests.post('http://localhost:5000/api/train/start', json={ - 'dataset': 'heart', - 'n_qubits': 4, - 'n_layers': 2, - 'learning_rate': 0.01, - 'duration_minutes': 10, - 'batch_size': 32 -}) -session_id = response.json()['session_id'] +response = requests.post( + "http://localhost:5000/api/train/start", + json={ + "dataset": "heart", + "n_qubits": 4, + "n_layers": 2, + "learning_rate": 0.01, + "duration_minutes": 10, + "batch_size": 32, + }, +) +session_id = response.json()["session_id"] # Poll status -status = requests.get(f'http://localhost:5000/api/train/status/{session_id}').json() +status = requests.get(f"http://localhost:5000/api/train/status/{session_id}").json() print(f"Epoch {status['current_epoch']}, Accuracy: {status['best_val_acc']}") ``` diff --git a/ai-projects/quantum-ml/WEB_DASHBOARD_SUCCESS.md b/ai-projects/quantum-ml/WEB_DASHBOARD_SUCCESS.md index c81d032f5..0c53561c2 100644 --- a/ai-projects/quantum-ml/WEB_DASHBOARD_SUCCESS.md +++ b/ai-projects/quantum-ml/WEB_DASHBOARD_SUCCESS.md @@ -207,7 +207,7 @@ GET /api/results/:file # Get detailed results Edit `web_app.py` last line: ```python -app.run(host='0.0.0.0', port=5001, debug=True, threaded=True) +app.run(host="0.0.0.0", port=5001, debug=True, threaded=True) ``` ### Add Custom Dataset @@ -221,9 +221,9 @@ Edit `create_quantum_circuit()` in `web_app.py`: ```python def create_quantum_circuit(n_qubits, n_layers): - dev = qml.device('default.qubit', wires=n_qubits) + dev = qml.device("default.qubit", wires=n_qubits) - @qml.qnode(dev, interface='autograd') + @qml.qnode(dev, interface="autograd") def circuit(inputs, weights): # Your custom quantum circuit here qml.AmplitudeEmbedding(features=inputs, wires=range(n_qubits)) diff --git a/ai-projects/quantum-ml/azure/DEPLOYMENT.md b/ai-projects/quantum-ml/azure/DEPLOYMENT.md index 47b2598d3..acac8e866 100644 --- a/ai-projects/quantum-ml/azure/DEPLOYMENT.md +++ b/ai-projects/quantum-ml/azure/DEPLOYMENT.md @@ -153,7 +153,7 @@ workspace = Workspace( subscription_id="", resource_group="rg-quantum-ai", name="quantum-ai-workspace", - credential=credential + credential=credential, ) print(f"Connected to workspace: {workspace.name}") diff --git a/ai-projects/quantum-ml/examples/README.md b/ai-projects/quantum-ml/examples/README.md index 27f301f8c..b543e522e 100644 --- a/ai-projects/quantum-ml/examples/README.md +++ b/ai-projects/quantum-ml/examples/README.md @@ -240,6 +240,7 @@ In `train_models.py`, replace data generation: ```python from sklearn.datasets import make_blobs, make_classification + X, y = make_blobs(n_samples=200, centers=2, random_state=42) ``` diff --git a/ai-projects/quantum-ml/experiments/AZURE_QUICKSTART.md b/ai-projects/quantum-ml/experiments/AZURE_QUICKSTART.md index 6bb9a76c8..8825ae452 100644 --- a/ai-projects/quantum-ml/experiments/AZURE_QUICKSTART.md +++ b/ai-projects/quantum-ml/experiments/AZURE_QUICKSTART.md @@ -173,12 +173,7 @@ azure = AzureQuantumIntegration() workspace = azure.connect() # Submit to IonQ simulator (FREE) -job = azure.submit_circuit( - circuit=circuit, - shots=100, - job_name="my_first_quantum_job", - backend="ionq.simulator" -) +job = azure.submit_circuit(circuit=circuit, shots=100, job_name="my_first_quantum_job", backend="ionq.simulator") print(f"Job submitted: {job.id}") diff --git a/ai-projects/quantum-ml/production/README.md b/ai-projects/quantum-ml/production/README.md index 2b79fa3a3..0ff0c210b 100644 --- a/ai-projects/quantum-ml/production/README.md +++ b/ai-projects/quantum-ml/production/README.md @@ -318,7 +318,7 @@ python train_custom_dataset.py --preset banknote --epochs 25 Change port in `banknote_api.py`: ```python -app.run(host='0.0.0.0', port=8081) # Use different port +app.run(host="0.0.0.0", port=8081) # Use different port ``` ### Import Errors @@ -347,7 +347,7 @@ pytest test_api.py -v ### Enable Debug Mode ```python -app.run(host='0.0.0.0', port=8080, debug=True) +app.run(host="0.0.0.0", port=8080, debug=True) ``` ## License diff --git a/ai-projects/quantum-ml/src/quantum_llm/COMPREHENSIVE_README.md b/ai-projects/quantum-ml/src/quantum_llm/COMPREHENSIVE_README.md index 7a0aed617..032b8bbf4 100644 --- a/ai-projects/quantum-ml/src/quantum_llm/COMPREHENSIVE_README.md +++ b/ai-projects/quantum-ml/src/quantum_llm/COMPREHENSIVE_README.md @@ -47,10 +47,12 @@ import asyncio pipeline = QuantumLLMPipeline() # Non-streaming completion -result = asyncio.run(pipeline.generate( - prompt="What is quantum computing?", - provider="auto", # auto-detect or specify azure/openai/lmstudio/local -)) +result = asyncio.run( + pipeline.generate( + prompt="What is quantum computing?", + provider="auto", # auto-detect or specify azure/openai/lmstudio/local + ) +) print(result) # Output: { # "response": "...", @@ -62,11 +64,13 @@ print(result) # "quantum_augmented": True # } + # Streaming completion async def stream_example(): async for chunk in pipeline.stream("Tell me about quantum entanglement"): print(chunk, end="", flush=True) + asyncio.run(stream_example()) ``` @@ -111,12 +115,12 @@ export QUANTUM_LLM_CACHE_TTL_SECONDS=3600 from ai_projects.quantum_ml.src.quantum_llm import QuantumLLMConfig, QuantumLLMPipeline config = QuantumLLMConfig( - backend="classical", # "auto", "qiskit", "pennylane", "classical" + backend="classical", # "auto", "qiskit", "pennylane", "classical" num_qubits=4, shots=512, num_layers=2, top_k=10, - temperature_blend=0.3, # 0 = classical, 1 = quantum + temperature_blend=0.3, # 0 = classical, 1 = quantum provider="auto", model="gpt-4", temperature=0.7, @@ -346,8 +350,8 @@ The module includes LRU caching with TTL expiration to avoid recomputing identic ```python config = QuantumLLMConfig( - cache_enabled=True, # Enable caching - cache_max_size=256, # Max 256 cached circuits + cache_enabled=True, # Enable caching + cache_max_size=256, # Max 256 cached circuits cache_ttl_seconds=3600.0, # Expire after 1 hour ) pipeline = QuantumLLMPipeline(config=config) @@ -464,6 +468,7 @@ export LOG_LEVEL=DEBUG ```python import logging + logging.basicConfig(level=logging.DEBUG) ``` @@ -475,6 +480,7 @@ Track pipeline performance: import time import asyncio + async def benchmark(): pipeline = QuantumLLMPipeline() times = [] @@ -485,10 +491,11 @@ async def benchmark(): t1 = time.monotonic() times.append((t1 - t0) * 1000) - print(f"Mean: {sum(times)/len(times):.1f}ms") + print(f"Mean: {sum(times) / len(times):.1f}ms") print(f"Min: {min(times):.1f}ms") print(f"Max: {max(times):.1f}ms") + asyncio.run(benchmark()) ``` diff --git a/apps/aria/AUTO-EXECUTE.md b/apps/aria/AUTO-EXECUTE.md index 1c29b0767..175316e66 100644 --- a/apps/aria/AUTO-EXECUTE.md +++ b/apps/aria/AUTO-EXECUTE.md @@ -53,43 +53,43 @@ ARIA_ACTIONS = { "move": { "params": ["target", "speed"], "description": "Move Aria to a target position or object", - "example": {"action": "move", "target": {"x": 50, "y": 30}, "speed": "normal"} + "example": {"action": "move", "target": {"x": 50, "y": 30}, "speed": "normal"}, }, "say": { "params": ["text", "emotion"], "description": "Make Aria speak with optional emotion", - "example": {"action": "say", "text": "Hello!", "emotion": "happy"} + "example": {"action": "say", "text": "Hello!", "emotion": "happy"}, }, "pickup": { "params": ["object_id"], "description": "Pick up an object from the stage", - "example": {"action": "pickup", "object_id": "apple"} + "example": {"action": "pickup", "object_id": "apple"}, }, "drop": { "params": ["position"], "description": "Drop currently held object at position", - "example": {"action": "drop", "position": {"x": 50, "y": 30}} + "example": {"action": "drop", "position": {"x": 50, "y": 30}}, }, "throw": { "params": ["target", "force"], "description": "Throw held object toward target", - "example": {"action": "throw", "target": {"x": 70, "y": 40}, "force": "medium"} + "example": {"action": "throw", "target": {"x": 70, "y": 40}, "force": "medium"}, }, "gesture": { "params": ["gesture_type"], "description": "Perform a gesture animation", - "example": {"action": "gesture", "gesture_type": "wave"} + "example": {"action": "gesture", "gesture_type": "wave"}, }, "look": { "params": ["target"], "description": "Look at a target position or object", - "example": {"action": "look", "target": "apple"} + "example": {"action": "look", "target": "apple"}, }, "wait": { "params": ["duration"], "description": "Wait for specified duration in seconds", - "example": {"action": "wait", "duration": 2.0} - } + "example": {"action": "wait", "duration": 2.0}, + }, } ``` @@ -267,22 +267,19 @@ The global `stage_state` dict tracks: ```python stage_state = { - 'aria': { - 'position': {'x': 15, 'y': 20}, # % coordinates (0-100) - 'expression': 'neutral', # facial expression - 'held_object': None, # currently held object ID - 'facing': 'right' # left or right + "aria": { + "position": {"x": 15, "y": 20}, # % coordinates (0-100) + "expression": "neutral", # facial expression + "held_object": None, # currently held object ID + "facing": "right", # left or right }, - 'objects': { - 'apple': { - 'position': {'x': 55, 'y': 35}, - 'state': 'on_table' # on_table, held, dropped, thrown + "objects": { + "apple": { + "position": {"x": 55, "y": 35}, + "state": "on_table", # on_table, held, dropped, thrown } }, - 'environment': { - 'table': {'position': {'x': 60, 'y': 20}}, - 'stage_bounds': {'width': 100, 'height': 100} - } + "environment": {"table": {"position": {"x": 60, "y": 20}}, "stage_bounds": {"width": 100, "height": 100}}, } ``` diff --git a/apps/aria/IMPLEMENTATION_SUMMARY.md b/apps/aria/IMPLEMENTATION_SUMMARY.md index 3b90afc54..e0e0a8cd2 100644 --- a/apps/aria/IMPLEMENTATION_SUMMARY.md +++ b/apps/aria/IMPLEMENTATION_SUMMARY.md @@ -270,7 +270,7 @@ elif action_type == 'new_action': 3. Add parsing in `parse_with_fallback()`: ```python -if 'trigger_word' in command_lower: +if "trigger_word" in command_lower: actions.append({"action": "new_action", "param1": "..."}) ``` @@ -282,6 +282,7 @@ if 'trigger_word' in command_lower: ```python from aria_web.server import action_parser + actions = action_parser.parse("your command", use_llm=False) ``` @@ -289,8 +290,9 @@ actions = action_parser.parse("your command", use_llm=False) ```python from aria_web.server import execute_aria_action, stage_state + result = execute_aria_action({"action": "move", "target": {"x": 50, "y": 50}}) -print(stage_state['aria']['position']) # Should be updated +print(stage_state["aria"]["position"]) # Should be updated ``` **Test API**: diff --git a/database/README.md b/database/README.md index e8724247e..b87b2f38e 100644 --- a/database/README.md +++ b/database/README.md @@ -178,7 +178,8 @@ conn_str = ( conn = pyodbc.connect(conn_str) cursor = conn.cursor() -cursor.execute(""" +cursor.execute( + """ DECLARE @RunId UNIQUEIDENTIFIER; EXEC sp_LogQuantumTrainingRun @JobName = ?, @@ -195,8 +196,20 @@ cursor.execute(""" @Status = ?, @RunId = @RunId OUTPUT; SELECT @RunId; -""", 'heart_quick', 'heart_disease', 'qiskit_aer', 4, 2, 'linear', - 0.01, 10, 16, 0.85, 45.2, 'completed') +""", + "heart_quick", + "heart_disease", + "qiskit_aer", + 4, + 2, + "linear", + 0.01, + 10, + 16, + 0.85, + 45.2, + "completed", +) run_id = cursor.fetchone()[0] conn.commit() @@ -216,7 +229,8 @@ def log_to_database(job_config, results): conn = pyodbc.connect(DB_CONNECTION_STRING) cursor = conn.cursor() - cursor.execute(""" + cursor.execute( + """ DECLARE @RunId UNIQUEIDENTIFIER; EXEC sp_LogQuantumTrainingRun @JobName = ?, @DatasetName = ?, @Backend = ?, @@ -225,11 +239,23 @@ def log_to_database(job_config, results): @TestAccuracy = ?, @TestLoss = ?, @ExecutionTimeSeconds = ?, @StatusJsonPath = ?, @ResultsJsonPath = ?, @Status = ?, @RunId = @RunId OUTPUT; - """, job_config['name'], job_config['dataset'], job_config['backend'], - job_config['n_qubits'], job_config['n_layers'], job_config['entanglement'], - job_config['learning_rate'], job_config['epochs'], job_config['batch_size'], - results['test_accuracy'], results['test_loss'], results['execution_time'], - results['status_json_path'], results['results_json_path'], 'completed') + """, + job_config["name"], + job_config["dataset"], + job_config["backend"], + job_config["n_qubits"], + job_config["n_layers"], + job_config["entanglement"], + job_config["learning_rate"], + job_config["epochs"], + job_config["batch_size"], + results["test_accuracy"], + results["test_loss"], + results["execution_time"], + results["status_json_path"], + results["results_json_path"], + "completed", + ) conn.commit() ``` @@ -265,13 +291,21 @@ async def chat_endpoint(req: func.HttpRequest) -> func.HttpResponse: # Log conversation conn = get_db_connection() cursor = conn.cursor() - cursor.execute(""" + cursor.execute( + """ DECLARE @ConvId UNIQUEIDENTIFIER, @MsgId UNIQUEIDENTIFIER; EXEC sp_LogChatConversation @SessionId = ?, @Provider = ?, @Model = ?, @Role = ?, @Content = ?, @TotalTokens = ?, @ConversationId = @ConvId OUTPUT, @MessageId = @MsgId OUTPUT; - """, session_id, provider, model, 'user', user_message, token_count) + """, + session_id, + provider, + model, + "user", + user_message, + token_count, + ) conn.commit() ``` diff --git a/docs/CODE_INEFFICIENCY_ANALYSIS_2026-02-17.md b/docs/CODE_INEFFICIENCY_ANALYSIS_2026-02-17.md index dc6f2a215..5d5c69eb9 100644 --- a/docs/CODE_INEFFICIENCY_ANALYSIS_2026-02-17.md +++ b/docs/CODE_INEFFICIENCY_ANALYSIS_2026-02-17.md @@ -27,15 +27,15 @@ def compute_gradient(circuit, X, y, weights, use_parameter_shift=True): if use_parameter_shift: shift = np.pi / 2 - for i in range(weights.shape[0]): # Layer loop - for j in range(weights.shape[1]): # Qubit loop - for k in range(weights.shape[2]): # Gate loop - weights_plus = weights.copy() # O(n) copy operation + for i in range(weights.shape[0]): # Layer loop + for j in range(weights.shape[1]): # Qubit loop + for k in range(weights.shape[2]): # Gate loop + weights_plus = weights.copy() # O(n) copy operation weights_minus = weights.copy() # O(n) copy operation weights_plus[i, j, k] += shift weights_minus[i, j, k] -= shift - loss_plus = compute_loss(circuit, X, y, weights_plus) # O(batch_size) - loss_minus = compute_loss(circuit, X, y, weights_minus) # O(batch_size) + loss_plus = compute_loss(circuit, X, y, weights_plus) # O(batch_size) + loss_minus = compute_loss(circuit, X, y, weights_minus) # O(batch_size) grad[i, j, k] = (loss_plus - loss_minus) / 2 # ... similar pattern in finite differences fallback (lines 238-244) ``` @@ -93,19 +93,19 @@ Multiple API endpoints repeatedly read and parse the same JSON files: ```python # get_job_progress() - line 273 -with open(status_file, 'r') as f: +with open(status_file, "r") as f: data = json.load(f) # get_job_metrics() - line 319 -with open(status_file, 'r') as f: +with open(status_file, "r") as f: data = json.load(f) # READS SAME FILE AGAIN # get_job_details() - line 495 -with open(status_file, 'r') as f: +with open(status_file, "r") as f: data = json.load(f) # READS SAME FILE AGAIN # get_job_logs() - line 517 -with open(status_file, 'r') as f: +with open(status_file, "r") as f: data = json.load(f) # READS SAME FILE AGAIN ``` @@ -128,6 +128,7 @@ from pathlib import Path _file_cache = {} _FILE_CACHE_TTL = 5 # seconds + def _load_json_cached(filepath: Path) -> dict: """Load JSON with TTL-based caching""" now = time.time() @@ -140,7 +141,7 @@ def _load_json_cached(filepath: Path) -> dict: # Cache miss or expired - read file try: - with open(filepath, 'r') as f: + with open(filepath, "r") as f: data = json.load(f) _file_cache[cache_key] = (data, now) return data @@ -150,9 +151,10 @@ def _load_json_cached(filepath: Path) -> dict: return _file_cache[cache_key][0] raise + # Usage in all methods: def get_job_progress(self, job_id): - status_file = root_dir / 'data_out' / 'autotrain' / 'status.json' + status_file = root_dir / "data_out" / "autotrain" / "status.json" data = _load_json_cached(status_file) # Cached! # ... rest of logic ``` @@ -229,6 +231,7 @@ _DOMAIN_KEYWORDS = { "technical": frozenset(["code", "program", "api", "function", "database"]), } + def _analyze_query_optimized(query: str) -> dict: """Optimized query analysis with single-pass word extraction""" query_lower = query.lower() @@ -367,10 +370,10 @@ def get_global_stats(): accuracies.append(session.best_val_acc) return { - 'active_sessions': active_count, - 'completed_sessions': completed_count, - 'total_epochs': total_epochs, - 'avg_accuracy': np.mean(accuracies) if accuracies else 0.0, + "active_sessions": active_count, + "completed_sessions": completed_count, + "total_epochs": total_epochs, + "avg_accuracy": np.mean(accuracies) if accuracies else 0.0, } ``` @@ -396,9 +399,9 @@ Creates intermediate lists when only counting: ```python # Line 700-701 -health['checks']['datasets'] = { - 'exists': datasets_dir.exists(), - 'count': len(list(datasets_dir.glob('*/train.json'))) # Creates full list! +health["checks"]["datasets"] = { + "exists": datasets_dir.exists(), + "count": len(list(datasets_dir.glob("*/train.json"))), # Creates full list! } # Line 761 @@ -414,9 +417,9 @@ Use generator expressions with sum(): ```python # Optimized counting without intermediate lists -health['checks']['datasets'] = { - 'exists': datasets_dir.exists(), - 'count': sum(1 for _ in datasets_dir.glob('*/train.json')) # Generator! +health["checks"]["datasets"] = { + "exists": datasets_dir.exists(), + "count": sum(1 for _ in datasets_dir.glob("*/train.json")), # Generator! } dataset_count = sum(1 for d in datasets_dir.iterdir() if d.is_dir()) @@ -460,6 +463,7 @@ _request_timestamps = defaultdict(deque) _MAX_REQUESTS = 100 _WINDOW_SECONDS = 60 + def check_rate_limit(client_ip: str) -> bool: """Check if client is within rate limit""" now = time.time() @@ -588,6 +592,7 @@ def test_gradient_optimization(): np.testing.assert_allclose(grad_manual, grad_optimized, rtol=1e-5) + def test_gradient_performance(): """Benchmark gradient computation speedup""" import time diff --git a/docs/FUTURE_PERFORMANCE_OPPORTUNITIES.md b/docs/FUTURE_PERFORMANCE_OPPORTUNITIES.md index bbb2a657e..b30b772a1 100644 --- a/docs/FUTURE_PERFORMANCE_OPPORTUNITIES.md +++ b/docs/FUTURE_PERFORMANCE_OPPORTUNITIES.md @@ -16,7 +16,7 @@ This document identifies potential performance improvements found during code an for c in classes: folder = self.root / c for f in folder.iterdir(): - if f.suffix.lower() in ('.png', '.jpg', '.jpeg'): + if f.suffix.lower() in (".png", ".jpg", ".jpeg"): self.samples.append((f, self.class_to_idx[c])) ``` @@ -24,8 +24,8 @@ for c in classes: ```python # Use rglob for cleaner recursive traversal -for img_path in self.root.rglob('*'): - if img_path.suffix.lower() in ('.png', '.jpg', '.jpeg'): +for img_path in self.root.rglob("*"): + if img_path.suffix.lower() in (".png", ".jpg", ".jpeg"): class_name = img_path.parent.name if class_name in self.class_to_idx: self.samples.append((img_path, self.class_to_idx[class_name])) @@ -62,14 +62,17 @@ Implement circuit result caching for identical inputs: from functools import lru_cache import hashlib + def make_hashable_key(arr): return hashlib.sha256(arr.tobytes()).hexdigest() + # Use a session-level cache @lru_cache(maxsize=1000) def cached_circuit_eval(x_hash, weights_hash): return circuit(x_array, weights_array) + for xi, yi in zip(X_val, y_val): x_hash = make_hashable_key(xi) w_hash = make_hashable_key(weights) diff --git a/docs/FUTURE_PERFORMANCE_OPTIMIZATIONS.md b/docs/FUTURE_PERFORMANCE_OPTIMIZATIONS.md index ca861489b..fec1779b9 100644 --- a/docs/FUTURE_PERFORMANCE_OPTIMIZATIONS.md +++ b/docs/FUTURE_PERFORMANCE_OPTIMIZATIONS.md @@ -29,6 +29,7 @@ result = vi.predict(img) from functools import lru_cache import hashlib + @lru_cache(maxsize=50) def _fetch_image_cached(url: str, cache_buster: str = None): """Fetch image with LRU cache. cache_buster allows invalidation.""" @@ -36,6 +37,7 @@ def _fetch_image_cached(url: str, cache_buster: str = None): response.raise_for_status() return response.content + # Usage image_bytes = _fetch_image_cached(image_url) img = Image.open(io.BytesIO(image_bytes)) @@ -68,6 +70,7 @@ def _cosine(a: Sequence[float], b: Sequence[float]) -> float: return 0.0 return dot / (na * nb) + # Loop over all embeddings for r in rows: emb = _deserialize_f32(r.EmbeddingVector, dim) @@ -79,10 +82,12 @@ for r in rows: ```python try: import numpy as np + _HAS_NUMPY = True except ImportError: _HAS_NUMPY = False + def _cosine_batch(query: Sequence[float], embeddings: List[Sequence[float]]) -> List[float]: """Vectorized cosine similarity using NumPy if available.""" if _HAS_NUMPY and len(embeddings) > 10: @@ -135,6 +140,7 @@ import time _file_cache = {} _CACHE_TTL = 10 # seconds + def _exists_cached(path: str) -> bool: """Check file existence with 10s TTL cache.""" now = time.time() @@ -180,8 +186,10 @@ To identify the next set of optimizations, consider: import time import functools + def timed(func): """Decorator to measure and log function execution time.""" + @functools.wraps(func) def wrapper(*args, **kwargs): start = time.perf_counter() @@ -190,6 +198,7 @@ def timed(func): if duration > 0.1: # Log slow operations print(f"⚠️ {func.__name__} took {duration:.3f}s") return result + return wrapper ``` @@ -207,6 +216,7 @@ Apply to: import time import logging + def execute_with_timing(cursor, query, *args): start = time.perf_counter() cursor.execute(query, *args) @@ -239,17 +249,17 @@ Look for functions with: Consider adding `/api/performance/metrics` endpoint: ```python -@app.route('/api/performance/metrics') +@app.route("/api/performance/metrics") def performance_metrics(): return { "connection_pool": { "size": len(_connection_pool), "max_size": _MAX_POOL_SIZE, - "utilization": len(_connection_pool) / _MAX_POOL_SIZE + "utilization": len(_connection_pool) / _MAX_POOL_SIZE, }, "keyword_cache_hits": _keyword_cache_hits, # If implemented - "regex_pattern_count": len([k for k in globals() if k.startswith('_RE_')]), - "recent_slow_operations": get_slow_operations_log() + "regex_pattern_count": len([k for k in globals() if k.startswith("_RE_")]), + "recent_slow_operations": get_slow_operations_log(), } ``` diff --git a/docs/OPTIMIZATION_QUICK_GUIDE.md b/docs/OPTIMIZATION_QUICK_GUIDE.md index cfe745550..da926b79a 100644 --- a/docs/OPTIMIZATION_QUICK_GUIDE.md +++ b/docs/OPTIMIZATION_QUICK_GUIDE.md @@ -10,11 +10,11 @@ ```python # BEFORE -count = len(list(directory.glob('*.json'))) +count = len(list(directory.glob("*.json"))) count = len([d for d in dir.iterdir() if d.is_dir()]) # AFTER -count = sum(1 for _ in directory.glob('*.json')) +count = sum(1 for _ in directory.glob("*.json")) count = sum(1 for d in dir.iterdir() if d.is_dir()) ``` @@ -45,10 +45,13 @@ avg_acc = np.mean([s.best_val_acc for s in sessions.values() if s.best_val_acc > active = completed = total_epochs = 0 accuracies = [] for s in sessions.values(): - if s.status == "training": active += 1 - elif s.status == "completed": completed += 1 + if s.status == "training": + active += 1 + elif s.status == "completed": + completed += 1 total_epochs += s.current_epoch - if s.best_val_acc > 0: accuracies.append(s.best_val_acc) + if s.best_val_acc > 0: + accuracies.append(s.best_val_acc) avg_acc = np.mean(accuracies) if accuracies else 0.0 ``` @@ -66,9 +69,11 @@ from typing import Dict, Tuple, Any _file_cache: Dict[str, Tuple[Any, float]] = {} _FILE_CACHE_TTL = 5 # seconds + def _load_json_cached(filepath: Path) -> dict: """Load JSON file with TTL-based caching""" import json + now = time.time() cache_key = str(filepath) @@ -80,7 +85,7 @@ def _load_json_cached(filepath: Path) -> dict: # Read file try: - with open(filepath, 'r') as f: + with open(filepath, "r") as f: data = json.load(f) _file_cache[cache_key] = (data, now) return data @@ -90,6 +95,7 @@ def _load_json_cached(filepath: Path) -> dict: return _file_cache[cache_key][0] raise + # Replace all instances of: # with open(status_file, 'r') as f: # data = json.load(f) @@ -117,6 +123,7 @@ _DOMAIN_KEYWORDS = { "technical": frozenset(["code", "program", "api", "function", "database"]), } + # In _analyze_query function, replace multiple any() calls with: def _analyze_query_optimized(query: str) -> dict: query_lower = query.lower() @@ -192,6 +199,7 @@ _request_timestamps = defaultdict(deque) _MAX_REQUESTS = 100 _WINDOW_SECONDS = 60 + def check_rate_limit(client_ip: str) -> bool: """Check if client is within rate limit using deque""" now = time.time() @@ -207,6 +215,7 @@ def check_rate_limit(client_ip: str) -> bool: timestamps.append(now) return True + # Replace list filtering with deque-based check ``` @@ -236,9 +245,11 @@ def compute_gradient_optimized(circuit, X, y, weights): except Exception as e: # Fallback to manual parameter-shift if autograd fails import warnings + warnings.warn(f"Autograd failed ({e}), using manual gradient") return compute_gradient(circuit, X, y, weights, use_parameter_shift=True) + # In training loop, replace: # grad = compute_gradient(circuit, X_batch, y_batch, weights) # With: @@ -261,6 +272,7 @@ For each optimization: import time import numpy as np + def benchmark_optimization(old_func, new_func, *args, iterations=100): """Compare performance of old vs new implementation""" @@ -287,8 +299,8 @@ def benchmark_optimization(old_func, new_func, *args, iterations=100): assert result_old == result_new, "Results don't match!" speedup = time_old / time_new - print(f"Old: {time_old*1000:.2f}ms") - print(f"New: {time_new*1000:.2f}ms") + print(f"Old: {time_old * 1000:.2f}ms") + print(f"New: {time_new * 1000:.2f}ms") print(f"Speedup: {speedup:.1f}x") return speedup diff --git a/docs/PERFORMANCE_ANALYSIS.md b/docs/PERFORMANCE_ANALYSIS.md index 1b1ef895f..fa442ca08 100644 --- a/docs/PERFORMANCE_ANALYSIS.md +++ b/docs/PERFORMANCE_ANALYSIS.md @@ -33,12 +33,12 @@ This report identifies **15 performance improvement opportunities** across the A ```python # Current implementation - creates new list for EVERY check -if any(k in cmd for k in ['jump', 'leap', 'hop']): - return '[aria:position:50:60]' -elif any(k in cmd for k in ['dance', 'spin', 'twirl']): - return '[aria:position:50:50]' -elif any(k in cmd for k in ['wave', 'greet', 'hello', 'hi']): - return '[aria:position:30:70]' +if any(k in cmd for k in ["jump", "leap", "hop"]): + return "[aria:position:50:60]" +elif any(k in cmd for k in ["dance", "spin", "twirl"]): + return "[aria:position:50:50]" +elif any(k in cmd for k in ["wave", "greet", "hello", "hi"]): + return "[aria:position:30:70]" # ... 15 more similar checks ``` @@ -107,7 +107,10 @@ def store_embedding(message_id: Optional[str], embedding: Sequence[float], model blob = _serialize_f32(embedding) cursor.execute( "INSERT INTO dbo.ChatMessageEmbeddings ...", - message_id, model or "unknown-model", len(embedding), blob, + message_id, + model or "unknown-model", + len(embedding), + blob, ) conn.commit() return True @@ -152,7 +155,7 @@ def store_embeddings_batch(embeddings: List[Tuple[str, Sequence[float], str]]) - # Bulk insert - single transaction cursor.executemany( "INSERT INTO dbo.ChatMessageEmbeddings (MessageId, EmbeddingModel, EmbeddingDim, EmbeddingVector) VALUES (?,?,?,?)", - values + values, ) conn.commit() return len(values) @@ -161,6 +164,7 @@ def store_embeddings_batch(embeddings: List[Tuple[str, Sequence[float], str]]) - finally: conn.close() + # Keep single-insert API for backward compatibility def store_embedding(message_id: Optional[str], embedding: Sequence[float], model: str) -> bool: """Store single embedding (wraps batch API).""" @@ -215,11 +219,7 @@ class BatchEvaluator: def compare_models(self, model_ids: List[str]) -> Dict: """Compare specific models side-by-side (optimized).""" # O(1) lookup per model = O(n) total - comparison = [ - self._results_index[model_id] - for model_id in model_ids - if model_id in self._results_index - ] + comparison = [self._results_index[model_id] for model_id in model_ids if model_id in self._results_index] return { "models": [r.model_id for r in comparison], @@ -232,7 +232,7 @@ class BatchEvaluator: "duration": r.duration, } for r in comparison - ] + ], } ``` @@ -279,10 +279,7 @@ for epochs, accuracies in epoch_performance.items(): ```python # Even better: find max in one pass -best_epochs, best_avg = max( - epoch_performance.items(), - key=lambda item: statistics.mean(item[1]) -) +best_epochs, best_avg = max(epoch_performance.items(), key=lambda item: statistics.mean(item[1])) ``` **Expected improvement:** 20-30% faster, more robust @@ -299,9 +296,9 @@ best_epochs, best_avg = max( ```python report = [] -report.append("\n" + "="*80) +report.append("\n" + "=" * 80) report.append("AUTONOMOUS TRAINING ANALYTICS REPORT") -report.append("="*80 + "\n") +report.append("=" * 80 + "\n") ``` Later in the code (not shown but referenced in analysis): @@ -384,11 +381,7 @@ def aggregate_results(self) -> Dict: failed.append(r) # Rank succeeded models - ranked = sorted( - succeeded, - key=lambda r: r.metrics.get("accuracy", r.metrics.get("perplexity", 0)), - reverse=True - ) + ranked = sorted(succeeded, key=lambda r: r.metrics.get("accuracy", r.metrics.get("perplexity", 0)), reverse=True) # ... use already-built 'failed' list ... @@ -401,7 +394,7 @@ def aggregate_results(self) -> Dict: "best_model": ranked[0].model_id if ranked else None, "results": [r.__dict__ for r in self.results], "ranking": [...], - "failed_models": [r.__dict__ for r in failed] # Use existing list + "failed_models": [r.__dict__ for r in failed], # Use existing list } ``` @@ -462,6 +455,7 @@ def _validate_tts_request(req_body: Dict) -> Tuple[Optional[str], Optional[Dict] } return (None, params) + def _try_azure_tts(text: str, voice: str, rate: str, pitch: str) -> Optional[bytes]: """Attempt Azure TTS synthesis. @@ -471,6 +465,7 @@ def _try_azure_tts(text: str, voice: str, rate: str, pitch: str) -> Optional[byt # Azure TTS logic here pass + def _try_local_tts(text: str) -> Optional[bytes]: """Attempt local TTS fallback. @@ -480,6 +475,7 @@ def _try_local_tts(text: str) -> Optional[bytes]: # Local TTS logic here pass + @app.route(route="tts", methods=["POST"]) def tts(req: func.HttpRequest) -> func.HttpResponse: """Text-to-speech endpoint (refactored).""" @@ -499,7 +495,7 @@ def tts(req: func.HttpRequest) -> func.HttpResponse: if audio: return func.HttpResponse( body=json.dumps({"audio_base64": base64.b64encode(audio).decode(), "format": "mp3"}), - mimetype="application/json" + mimetype="application/json", ) # Fallback to local @@ -507,7 +503,7 @@ def tts(req: func.HttpRequest) -> func.HttpResponse: if audio: return func.HttpResponse( body=json.dumps({"audio_base64": base64.b64encode(audio).decode(), "format": "wav"}), - mimetype="application/json" + mimetype="application/json", ) return func.HttpResponse("TTS failed", status_code=500) @@ -545,6 +541,7 @@ for r in rows: ```python import numpy as np + def retrieve_similar(query_embedding: Sequence[float], top_k: int = 5) -> List[Dict]: """Retrieve similar messages (vectorized).""" # ... fetch rows ... @@ -558,11 +555,13 @@ def retrieve_similar(query_embedding: Sequence[float], top_k: int = 5) -> List[D for r in rows: emb = _deserialize_f32(r.EmbeddingVector, r.EmbeddingDim) embeddings.append(emb) - metadata.append({ - "message_id": r.MessageId, - "content": r.Content, - "embedding_model": r.EmbeddingModel, - }) + metadata.append( + { + "message_id": r.MessageId, + "content": r.Content, + "embedding_model": r.EmbeddingModel, + } + ) # Vectorized cosine similarity (all at once) query_np = np.array(query_embedding) @@ -670,12 +669,7 @@ if result["status"] not in SUCCESSFUL_STATUSES: ```python # Every status request spawns subprocess to check venv -proc = subprocess.run( - [str(venv_python), "-c", code], - capture_output=True, - text=True, - timeout=12 -) +proc = subprocess.run([str(venv_python), "-c", code], capture_output=True, text=True, timeout=12) ``` **Why it's slow:** @@ -690,6 +684,7 @@ proc = subprocess.run( import time from functools import lru_cache + @lru_cache(maxsize=1) def _get_venv_info(venv_path: str, cache_time: float) -> Dict: """Get venv info with caching (5-minute TTL). @@ -703,6 +698,7 @@ def _get_venv_info(venv_path: str, cache_time: float) -> Dict: # ... subprocess logic ... return venv_info + def ai_status(req: func.HttpRequest) -> func.HttpResponse: # ... @@ -912,10 +908,13 @@ Example benchmark structure: ```python import time + def benchmark_keyword_lookup(): """Benchmark command keyword lookups.""" commands = [ - "jump high", "dance around", "wave hello", + "jump high", + "dance around", + "wave hello", # ... 100 test commands ] @@ -930,7 +929,7 @@ def benchmark_keyword_lookup(): elapsed = time.perf_counter() - start print(f"Processed {len(commands)} commands in {elapsed:.3f}s") - print(f"Average: {elapsed/len(commands)*1000:.2f}ms per command") + print(f"Average: {elapsed / len(commands) * 1000:.2f}ms per command") ``` --- diff --git a/docs/PERFORMANCE_CRITICAL_FIXES_2026.md b/docs/PERFORMANCE_CRITICAL_FIXES_2026.md index cbd108af7..ae594507f 100644 --- a/docs/PERFORMANCE_CRITICAL_FIXES_2026.md +++ b/docs/PERFORMANCE_CRITICAL_FIXES_2026.md @@ -132,6 +132,7 @@ _conn_cache = {} _conn_lock = threading.Lock() _MAX_CONN_AGE_SECONDS = 300 # 5 minutes + def _get_conn(): """Get or create a cached DB connection.""" thread_id = threading.current_thread().ident @@ -161,6 +162,7 @@ def _get_conn(): _conn_cache[thread_id] = (new_conn, current_time) return new_conn + # Usage (connection NOT closed - stays in cache) def store_embedding(message_id, embedding, model): conn = _get_conn() # Uses cache after first call diff --git a/docs/PERFORMANCE_IMPLEMENTATION_GUIDE.md b/docs/PERFORMANCE_IMPLEMENTATION_GUIDE.md index 5e86bd89e..56b6cf09b 100644 --- a/docs/PERFORMANCE_IMPLEMENTATION_GUIDE.md +++ b/docs/PERFORMANCE_IMPLEMENTATION_GUIDE.md @@ -43,17 +43,17 @@ Add this near the top of the file, after imports: # Performance optimization: Pre-compile action keywords # Used by _extract_action_position() - converts ~20 list creations per call to 0 _ACTION_KEYWORDS = { - 'jump': (('jump', 'leap', 'hop'), '[aria:position:50:60]'), - 'dance': (('dance', 'spin', 'twirl'), '[aria:position:50:50]'), - 'wave': (('wave', 'greet', 'hello', 'hi'), '[aria:position:30:70]'), - 'look': (('look', 'see', 'watch', 'observe'), None), # Special handling - 'sit': (('sit', 'rest', 'relax'), None), # Special handling - 'run': (('run', 'race', 'sprint'), '[aria:position:85:70]'), - 'hide': (('hide', 'crouch', 'duck'), '[aria:position:10:75]'), - 'present': (('present', 'show', 'display'), '[aria:position:50:50]'), - 'think': (('think', 'wonder', 'ponder'), '[aria:position:25:50]'), - 'walk_left': (('walk left', 'go left', 'left'), '[aria:position:20:70]'), - 'walk_right': (('walk right', 'go right', 'right'), '[aria:position:80:70]'), + "jump": (("jump", "leap", "hop"), "[aria:position:50:60]"), + "dance": (("dance", "spin", "twirl"), "[aria:position:50:50]"), + "wave": (("wave", "greet", "hello", "hi"), "[aria:position:30:70]"), + "look": (("look", "see", "watch", "observe"), None), # Special handling + "sit": (("sit", "rest", "relax"), None), # Special handling + "run": (("run", "race", "sprint"), "[aria:position:85:70]"), + "hide": (("hide", "crouch", "duck"), "[aria:position:10:75]"), + "present": (("present", "show", "display"), "[aria:position:50:50]"), + "think": (("think", "wonder", "ponder"), "[aria:position:25:50]"), + "walk_left": (("walk left", "go left", "left"), "[aria:position:20:70]"), + "walk_right": (("walk right", "go right", "right"), "[aria:position:80:70]"), } ``` @@ -68,46 +68,47 @@ def _extract_action_position(cmd: str, world_state: Dict) -> Optional[str]: Uses pre-compiled keyword tuples to avoid creating lists on every call. """ # Get table position for context-dependent positioning - table_pos = {'x': 60, 'y': 50} # Default - for obj_name, obj_data in world_state.get('objects', {}).items(): - if 'table' in obj_name.lower(): - if isinstance(obj_data, dict) and 'position' in obj_data: - table_pos = obj_data['position'] + table_pos = {"x": 60, "y": 50} # Default + for obj_name, obj_data in world_state.get("objects", {}).items(): + if "table" in obj_name.lower(): + if isinstance(obj_data, dict) and "position" in obj_data: + table_pos = obj_data["position"] break # Check for objects in command (pickup/drop context) - for obj_name, obj_data in world_state.get('objects', {}).items(): + for obj_name, obj_data in world_state.get("objects", {}).items(): if obj_name.lower() in cmd: - if any(word in cmd for word in ('pick', 'get', 'grab', 'take')): - obj_pos = obj_data.get('position', {}) - if isinstance(obj_pos, dict) and 'x' in obj_pos and 'y' in obj_pos: - return f'[aria:position:{max(10, obj_pos["x"] - 10)}:{obj_pos["y"] + 10}]' + if any(word in cmd for word in ("pick", "get", "grab", "take")): + obj_pos = obj_data.get("position", {}) + if isinstance(obj_pos, dict) and "x" in obj_pos and "y" in obj_pos: + return f"[aria:position:{max(10, obj_pos['x'] - 10)}:{obj_pos['y'] + 10}]" # Action-based positioning (optimized with pre-compiled keywords) for action, (keywords, position) in _ACTION_KEYWORDS.items(): if any(k in cmd for k in keywords): # Handle special cases - if action == 'look' and 'table' in cmd: - return '[aria:position:40:60]' - elif action == 'look': - return '[aria:position:20:40]' - elif action == 'sit': - return f'[aria:position:{table_pos["x"] - 5}:{table_pos["y"] + 35}]' + if action == "look" and "table" in cmd: + return "[aria:position:40:60]" + elif action == "look": + return "[aria:position:20:40]" + elif action == "sit": + return f"[aria:position:{table_pos['x'] - 5}:{table_pos['y'] + 35}]" # Return standard position if position: return position # Handle add/create commands - if any(word in cmd for word in ('add', 'create', 'spawn')): - return f'[aria:position:{table_pos["x"] - 15}:{table_pos["y"] + 20}]' + if any(word in cmd for word in ("add", "create", "spawn")): + return f"[aria:position:{table_pos['x'] - 15}:{table_pos['y'] + 20}]" # Default: context-aware positioning import hashlib + pos_hash = int(hashlib.md5(cmd.encode()).hexdigest()[:4], 16) x = 30 + (pos_hash % 40) y = 50 + ((pos_hash // 40) % 30) - return f'[aria:position:{x}:{y}]' + return f"[aria:position:{x}:{y}]" ``` #### Step 3: Test the changes @@ -119,11 +120,17 @@ def test_keyword_performance(): import time test_commands = [ - "jump high", "dance around", "wave hello", "look at table", - "sit down", "run fast", "hide quickly", "think deeply" + "jump high", + "dance around", + "wave hello", + "look at table", + "sit down", + "run fast", + "hide quickly", + "think deeply", ] * 100 # 800 total commands - world_state = {'objects': {'table': {'position': {'x': 60, 'y': 50}}}} + world_state = {"objects": {"table": {"position": {"x": 60, "y": 50}}}} start = time.perf_counter() for cmd in test_commands: @@ -131,7 +138,8 @@ def test_keyword_performance(): elapsed = time.perf_counter() - start print(f"Processed {len(test_commands)} commands in {elapsed:.3f}s") - print(f"Average: {elapsed/len(test_commands)*1000:.2f}ms per command") + print(f"Average: {elapsed / len(test_commands) * 1000:.2f}ms per command") + # Run test test_keyword_performance() @@ -183,11 +191,7 @@ Replace lines 305-312: def compare_models(self, model_ids: List[str]) -> Dict: """Compare specific models side-by-side (optimized O(1) lookup).""" # O(1) lookup per model instead of O(n) linear search - comparison = [ - self._results_index[model_id] - for model_id in model_ids - if model_id in self._results_index - ] + comparison = [self._results_index[model_id] for model_id in model_ids if model_id in self._results_index] return { "models": [r.model_id for r in comparison], @@ -201,7 +205,7 @@ def compare_models(self, model_ids: List[str]) -> Dict: "error": r.error, } for r in comparison - ] + ], } ``` @@ -224,7 +228,7 @@ def test_compare_performance(): status="succeeded", metrics={"accuracy": 0.85}, duration=10.0, - error=None + error=None, ) evaluator.add_result(result) @@ -237,7 +241,7 @@ def test_compare_performance(): elapsed = time.perf_counter() - start print(f"100 comparisons in {elapsed:.3f}s") - print(f"Average: {elapsed/100*1000:.2f}ms per comparison") + print(f"Average: {elapsed / 100 * 1000:.2f}ms per comparison") ``` Expected: <1ms per comparison (vs 10-50ms before for large result sets) @@ -326,12 +330,7 @@ def store_embeddings_batch(embeddings: List[Tuple[str, Sequence[float], str]]) - if not message_id or not embedding: continue blob = _serialize_f32(embedding) - values.append(( - message_id, - model or "unknown-model", - len(embedding), - blob - )) + values.append((message_id, model or "unknown-model", len(embedding), blob)) if not values: return 0 @@ -341,7 +340,7 @@ def store_embeddings_batch(embeddings: List[Tuple[str, Sequence[float], str]]) - """INSERT INTO dbo.ChatMessageEmbeddings (MessageId, EmbeddingModel, EmbeddingDim, EmbeddingVector) VALUES (?,?,?,?)""", - values + values, ) conn.commit() return len(values) @@ -433,12 +432,7 @@ def _get_venv_info_cached(venv_path: str, cache_slot: int) -> Dict: Cache invalidates every 5 minutes via cache_slot parameter. """ venv_python = Path(venv_path) - venv_info = { - "path": str(venv_python), - "exists": venv_python.exists(), - "packages": {}, - "error": None - } + venv_info = {"path": str(venv_python), "exists": venv_python.exists(), "packages": {}, "error": None} if not venv_info["exists"]: return venv_info @@ -453,12 +447,7 @@ def _get_venv_info_cached(venv_path: str, cache_slot: int) -> Dict: "\n\ttry:\n\t\tvers[m]=md.version(m)\n\texcept Exception:\n\t\tvers[m]=None;" "print(json.dumps({'available':avail,'versions':vers}))" ) - proc = subprocess.run( - [str(venv_python), "-c", code], - capture_output=True, - text=True, - timeout=12 - ) + proc = subprocess.run([str(venv_python), "-c", code], capture_output=True, text=True, timeout=12) if proc.returncode == 0: data = json.loads(proc.stdout.strip() or "{}") @@ -492,6 +481,7 @@ venv_info = _get_venv_info_cached(str(venv_python), current_cache_slot) # Test cache performance import time + def test_venv_cache(): """Test venv info caching.""" # First call (cache miss) @@ -506,7 +496,8 @@ def test_venv_cache(): print(f"First call (miss): {elapsed1:.3f}s") print(f"Second call (hit): {elapsed2:.3f}s") - print(f"Speedup: {elapsed1/elapsed2:.0f}x") + print(f"Speedup: {elapsed1 / elapsed2:.0f}x") + test_venv_cache() ``` @@ -528,6 +519,7 @@ from scripts.batch_evaluator import BatchEvaluator, EvaluationResult from aria_web.server import _extract_action_position from shared.chat_memory import store_embeddings_batch + class TestPerformanceOptimizations: """Test performance improvements.""" @@ -538,11 +530,7 @@ class TestPerformanceOptimizations: # Add 100 results for i in range(100): result = EvaluationResult( - model_id=f"model_{i}", - model_type="test", - status="succeeded", - metrics={"acc": 0.9}, - duration=1.0 + model_id=f"model_{i}", model_type="test", status="succeeded", metrics={"acc": 0.9}, duration=1.0 ) evaluator.add_result(result) @@ -571,10 +559,7 @@ class TestPerformanceOptimizations: def test_batch_embedding_storage(self): """Test batch embedding API.""" - embeddings = [ - (f"msg_{i}", [0.1] * 128, "test-model") - for i in range(10) - ] + embeddings = [(f"msg_{i}", [0.1] * 128, "test-model") for i in range(10)] start = time.perf_counter() count = store_embeddings_batch(embeddings) @@ -614,13 +599,8 @@ import time import statistics from typing import Callable, List -def benchmark( - name: str, - func: Callable, - *args, - iterations: int = 100, - warmup: int = 10 -) -> float: + +def benchmark(name: str, func: Callable, *args, iterations: int = 100, warmup: int = 10) -> float: """Benchmark a function.""" # Warm-up for _ in range(warmup): @@ -638,13 +618,14 @@ def benchmark( std_dev = statistics.stdev(times) if len(times) > 1 else 0 print(f"{name}:") - print(f" Mean: {mean_time*1000:.3f}ms") - print(f" StdDev: {std_dev*1000:.3f}ms") - print(f" Min: {min(times)*1000:.3f}ms") - print(f" Max: {max(times)*1000:.3f}ms") + print(f" Mean: {mean_time * 1000:.3f}ms") + print(f" StdDev: {std_dev * 1000:.3f}ms") + print(f" Min: {min(times) * 1000:.3f}ms") + print(f" Max: {max(times) * 1000:.3f}ms") return mean_time + if __name__ == "__main__": print("Performance Optimization Benchmarks") print("=" * 60) diff --git a/docs/PERFORMANCE_IMPLEMENTATION_SUMMARY.md b/docs/PERFORMANCE_IMPLEMENTATION_SUMMARY.md index b1401f809..fba0c3920 100644 --- a/docs/PERFORMANCE_IMPLEMENTATION_SUMMARY.md +++ b/docs/PERFORMANCE_IMPLEMENTATION_SUMMARY.md @@ -184,10 +184,7 @@ python -c "from batch_evaluator import BatchEvaluator" ### Using Performance Utilities ```python -from shared.performance_utils import ( - tail_file, stream_jsonl, find_json_in_output, - FileCache, timeit, memoize_with_ttl -) +from shared.performance_utils import tail_file, stream_jsonl, find_json_in_output, FileCache, timeit, memoize_with_ttl # Memory-efficient log tailing logs = tail_file(Path("training.log"), max_lines=50) @@ -197,8 +194,9 @@ for record in stream_jsonl(Path("data.jsonl")): process(record) # Extract JSON from subprocess output -result = subprocess.run(['./script'], capture_output=True, text=True) -metrics = find_json_in_output(result.stdout, key='metrics') +result = subprocess.run(["./script"], capture_output=True, text=True) +metrics = find_json_in_output(result.stdout, key="metrics") + # Time expensive functions @timeit @@ -206,6 +204,7 @@ def expensive_operation(): # work here pass + # Cache with TTL @memoize_with_ttl(ttl_seconds=300) def fetch_config(): diff --git a/docs/PERFORMANCE_IMPROVEMENTS.md b/docs/PERFORMANCE_IMPROVEMENTS.md index bc8fee2b2..0c6b37157 100644 --- a/docs/PERFORMANCE_IMPROVEMENTS.md +++ b/docs/PERFORMANCE_IMPROVEMENTS.md @@ -271,8 +271,10 @@ def _get_text_encoder(provider: str, model: Optional[str]) -> Callable[[str], in if AutoTokenizer is not None and mdl: try: tok = AutoTokenizer.from_pretrained(model, use_fast=True) # SLOW! + def _count(text: str) -> int: return len(tok.encode(text or "")) + return _count except Exception: pass @@ -283,6 +285,7 @@ def _get_text_encoder(provider: str, model: Optional[str]) -> Callable[[str], in ```python from functools import lru_cache + @lru_cache(maxsize=8) def _get_cached_tokenizer(model: str): """Cache tokenizer instances to avoid repeated loading.""" @@ -328,10 +331,12 @@ def _cosine(a: Sequence[float], b: Sequence[float]) -> float: ```python try: import numpy as np + _HAS_NUMPY = True except ImportError: _HAS_NUMPY = False + def _cosine(a: Sequence[float], b: Sequence[float]) -> float: if not a or not b or len(a) != len(b): return 0.0 @@ -394,6 +399,7 @@ def generate_embedding(text: str) -> List[float]: ```python _embedding_clients: Dict[str, Any] = {} + def _get_embedding_client(provider: str) -> Any: """Get or create a cached embedding client.""" if provider in _embedding_clients: @@ -447,7 +453,7 @@ def validate_jsonl(self, filepath: Path, verbose: bool = False) -> Dict: ```python def validate_jsonl(self, filepath: Path, verbose: bool = False) -> Dict: - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, "r", encoding="utf-8") as f: for i, line in enumerate(f, 1): # STREAMS LINE BY LINE line = line.strip() # ... validate line @@ -478,6 +484,7 @@ try: # Quick health check for LM Studio import urllib.request import urllib.error + req = urllib.request.Request(lms_url.replace("/v1", "") + "/v1/models", headers={"User-Agent": "QAI"}) urllib.request.urlopen(req, timeout=1) # BLOCKS FOR 1 SECOND ON EVERY CALL # ... use LM Studio @@ -491,6 +498,7 @@ except (urllib.error.URLError, Exception): _lmstudio_cache = {"available": None, "checked_at": 0} _LMSTUDIO_CACHE_TTL = 30 # seconds + def _check_lmstudio_available(url: str) -> bool: """Check LM Studio availability with caching.""" now = time.time() diff --git a/docs/PERFORMANCE_IMPROVEMENTS_REPORT.md b/docs/PERFORMANCE_IMPROVEMENTS_REPORT.md index 55678c477..63c3726e2 100644 --- a/docs/PERFORMANCE_IMPROVEMENTS_REPORT.md +++ b/docs/PERFORMANCE_IMPROVEMENTS_REPORT.md @@ -78,9 +78,12 @@ if any(x.get("role") == "user" for x in window) and \ has_user = has_assistant = False for x in window: role = x.get("role") - if role == "user": has_user = True - elif role == "assistant": has_assistant = True - if has_user and has_assistant: break + if role == "user": + has_user = True + elif role == "assistant": + has_assistant = True + if has_user and has_assistant: + break ``` **Impact:** 50% reduction in window traversal @@ -171,6 +174,7 @@ if durations: ```python import statistics + if durations: avg = statistics.mean(durations) ``` @@ -309,11 +313,11 @@ for compiled_pattern, desc in _FILE_OPERATION_PATTERNS: ```python # ❌ DON'T: Compile in loop for item in items: - if re.search(r'pattern', item): + if re.search(r"pattern", item): ... # ✅ DO: Compile once -PATTERN = re.compile(r'pattern') +PATTERN = re.compile(r"pattern") for item in items: if PATTERN.search(item): ... @@ -349,15 +353,15 @@ result = "".join(parts) ```python # ❌ DON'T: Multiple passes -count_a = sum(1 for x in items if x.type == 'a') -count_b = sum(1 for x in items if x.type == 'b') +count_a = sum(1 for x in items if x.type == "a") +count_b = sum(1 for x in items if x.type == "b") total = sum(x.value for x in items) # ✅ DO: Single pass -counts = {'a': 0, 'b': 0, 'total': 0} +counts = {"a": 0, "b": 0, "total": 0} for x in items: counts[x.type] += 1 - counts['total'] += x.value + counts["total"] += x.value ``` --- @@ -384,9 +388,9 @@ python -m py_compile scripts/*.py dashboard/app.py function_app.py ```python import extract_chat_logs_dataset # ✓ -import status_dashboard # ✓ -import job_queue # ✓ -import tool_validator # ✓ +import status_dashboard # ✓ +import job_queue # ✓ +import tool_validator # ✓ # All imports successful ``` diff --git a/docs/PERFORMANCE_INDEX.md b/docs/PERFORMANCE_INDEX.md index 60716d892..8c0799f30 100644 --- a/docs/PERFORMANCE_INDEX.md +++ b/docs/PERFORMANCE_INDEX.md @@ -102,6 +102,8 @@ ```python _cache = {} _TTL = 5 + + def cached_load(key): now = time.time() if key in _cache and (now - _cache[key][1]) < _TTL: diff --git a/docs/PERFORMANCE_OPTIMIZATIONS.md b/docs/PERFORMANCE_OPTIMIZATIONS.md index c3b8c901e..abe3c4f88 100644 --- a/docs/PERFORMANCE_OPTIMIZATIONS.md +++ b/docs/PERFORMANCE_OPTIMIZATIONS.md @@ -86,7 +86,7 @@ def _get_process_list(self) -> List[psutil.Process]: if current_time - self._process_cache_time < self._process_cache_ttl: return self._process_cache - self._process_cache = list(psutil.process_iter(['pid', 'name', 'cmdline'])) + self._process_cache = list(psutil.process_iter(["pid", "name", "cmdline"])) self._process_cache_time = current_time return self._process_cache ``` @@ -147,16 +147,16 @@ class BatchEvaluator: ```python # Before (3 passes): -succeeded = sum(1 for r in results if r.status == 'succeeded') -skipped = sum(1 for r in results if r.status == 'skipped') -failed = sum(1 for r in results if r.status == 'failed') +succeeded = sum(1 for r in results if r.status == "succeeded") +skipped = sum(1 for r in results if r.status == "skipped") +failed = sum(1 for r in results if r.status == "failed") # After (1 pass): succeeded = skipped = failed = 0 for r in results: - if r.status == 'succeeded': + if r.status == "succeeded": succeeded += 1 - elif r.status == 'skipped': + elif r.status == "skipped": skipped += 1 else: failed += 1 @@ -194,6 +194,7 @@ All optimizations have been validated: ```python import time + start = time.time() # expensive operation logger.debug(f"Operation took {time.time() - start:.2f}s") diff --git a/docs/PERFORMANCE_OPTIMIZATIONS_FEB_2026.md b/docs/PERFORMANCE_OPTIMIZATIONS_FEB_2026.md index fc6ead8d2..f045a4294 100644 --- a/docs/PERFORMANCE_OPTIMIZATIONS_FEB_2026.md +++ b/docs/PERFORMANCE_OPTIMIZATIONS_FEB_2026.md @@ -112,10 +112,10 @@ The original code had 12 separate `if` statements, each checking multiple patter lower_text = text.lower() commands = [] -if '[aria:walk:left]' in lower_text or 'walk left' in lower_text: - commands.append({'action': 'walk', 'direction': 'left', 'distance': 200}) -if '[aria:walk:right]' in lower_text or 'walk right' in lower_text: - commands.append({'action': 'walk', 'direction': 'right', 'distance': 200}) +if "[aria:walk:left]" in lower_text or "walk left" in lower_text: + commands.append({"action": "walk", "direction": "left", "distance": 200}) +if "[aria:walk:right]" in lower_text or "walk right" in lower_text: + commands.append({"action": "walk", "direction": "right", "distance": 200}) # ... 10 more similar checks ``` @@ -133,11 +133,12 @@ Pre-define a command pattern lookup table at module level: ```python # Command pattern lookup table for O(1) matching _COMMAND_PATTERNS = ( - (('[aria:walk:left]', 'walk left'), {'action': 'walk', 'direction': 'left', 'distance': 200}), - (('[aria:walk:right]', 'walk right'), {'action': 'walk', 'direction': 'right', 'distance': 200}), + (("[aria:walk:left]", "walk left"), {"action": "walk", "direction": "left", "distance": 200}), + (("[aria:walk:right]", "walk right"), {"action": "walk", "direction": "right", "distance": 200}), # ... all patterns ) + def parse_movement_commands(text: str) -> dict: """Parse movement commands from AI response text using optimized pattern matching""" lower_text = text.lower() @@ -148,7 +149,7 @@ def parse_movement_commands(text: str) -> dict: if any(pattern in lower_text for pattern in patterns): commands.append(command) - return {'commands': commands} if commands else {} + return {"commands": commands} if commands else {} ``` Benefits: @@ -263,9 +264,9 @@ def compute_gradient(circuit, X, y, weights, use_parameter_shift=True): if use_parameter_shift: shift = np.pi / 2 - for i in range(weights.shape[0]): # Layer - for j in range(weights.shape[1]): # Qubit - for k in range(weights.shape[2]): # Rotation parameter + for i in range(weights.shape[0]): # Layer + for j in range(weights.shape[1]): # Qubit + for k in range(weights.shape[2]): # Rotation parameter # Shift parameter and compute loss twice weights_plus = weights.copy() weights_minus = weights.copy() diff --git a/docs/PERFORMANCE_OPTIMIZATIONS_SUMMARY_FEB_2026.md b/docs/PERFORMANCE_OPTIMIZATIONS_SUMMARY_FEB_2026.md index 9f6de364c..0275b854c 100644 --- a/docs/PERFORMANCE_OPTIMIZATIONS_SUMMARY_FEB_2026.md +++ b/docs/PERFORMANCE_OPTIMIZATIONS_SUMMARY_FEB_2026.md @@ -42,16 +42,16 @@ jobs = [j for j in jobs if set(j.tags) & tags_set] ```python # Before (12 separate if statements) -if '[aria:walk:left]' in lower_text or 'walk left' in lower_text: - commands.append({'action': 'walk', 'direction': 'left', 'distance': 200}) -if '[aria:walk:right]' in lower_text or 'walk right' in lower_text: - commands.append({'action': 'walk', 'direction': 'right', 'distance': 200}) +if "[aria:walk:left]" in lower_text or "walk left" in lower_text: + commands.append({"action": "walk", "direction": "left", "distance": 200}) +if "[aria:walk:right]" in lower_text or "walk right" in lower_text: + commands.append({"action": "walk", "direction": "right", "distance": 200}) # ... 10 more # After (data-driven, single loop) _COMMAND_PATTERNS = ( - (('[aria:walk:left]', 'walk left'), {'action': 'walk', 'direction': 'left', 'distance': 200}), - (('[aria:walk:right]', 'walk right'), {'action': 'walk', 'direction': 'right', 'distance': 200}), + (("[aria:walk:left]", "walk left"), {"action": "walk", "direction": "left", "distance": 200}), + (("[aria:walk:right]", "walk right"), {"action": "walk", "direction": "right", "distance": 200}), # ... ) for patterns, command in _COMMAND_PATTERNS: diff --git a/docs/PERFORMANCE_OPTIMIZATION_GUIDE.md b/docs/PERFORMANCE_OPTIMIZATION_GUIDE.md index 2acd86e0b..9f9eceb71 100644 --- a/docs/PERFORMANCE_OPTIMIZATION_GUIDE.md +++ b/docs/PERFORMANCE_OPTIMIZATION_GUIDE.md @@ -12,13 +12,14 @@ This document describes performance optimizations implemented in the Aria codeba ```python # ❌ BAD: Loads entire file into memory -with open(log_file, 'r') as f: +with open(log_file, "r") as f: all_lines = f.readlines() return all_lines[-20:] # Only need last 20 lines # ✅ GOOD: Memory-efficient with deque from collections import deque -with open(log_file, 'r') as f: + +with open(log_file, "r") as f: return list(deque(f, maxlen=20)) # Only keeps last 20 lines ``` @@ -46,6 +47,7 @@ def load_all_records(file_path): records.append(json.loads(line)) return records + # ✅ GOOD: Yields records one at a time def load_records(file_path): with open(file_path) as f: @@ -115,10 +117,7 @@ def _tail_lines(path: Path, max_lines: int) -> List[str]: ```python with ThreadPoolExecutor(max_workers=3) as executor: - futures = { - executor.submit(evaluate_model, task): task - for task in tasks - } + futures = {executor.submit(evaluate_model, task): task for task in tasks} for future in as_completed(futures): result = future.result() @@ -170,12 +169,11 @@ for result in results: ```python # ❌ BAD: Sequential subprocess calls for script in scripts: - subprocess.run(['python', script]) # Blocks until complete + subprocess.run(["python", script]) # Blocks until complete # ✅ GOOD: Parallel execution with ThreadPoolExecutor() as executor: - futures = [executor.submit(subprocess.run, ['python', s]) - for s in scripts] + futures = [executor.submit(subprocess.run, ["python", s]) for s in scripts] results = [f.result() for f in futures] ``` @@ -227,9 +225,7 @@ python scripts/training_analytics.py async def run_training_cycle(): # Concurrent dataset downloads results = await asyncio.gather( - download_dataset('dataset1'), - download_dataset('dataset2'), - download_dataset('dataset3') + download_dataset("dataset1"), download_dataset("dataset2"), download_dataset("dataset3") ) # Sequential training (GPU bound) @@ -268,6 +264,7 @@ if output_path.exists(): ```python from functools import lru_cache + @lru_cache(maxsize=128) def expensive_computation(param): # Computation here diff --git a/docs/PERFORMANCE_OPTIMIZATION_PR_SUMMARY.md b/docs/PERFORMANCE_OPTIMIZATION_PR_SUMMARY.md index fbed1ee50..0627c0fa6 100644 --- a/docs/PERFORMANCE_OPTIMIZATION_PR_SUMMARY.md +++ b/docs/PERFORMANCE_OPTIMIZATION_PR_SUMMARY.md @@ -39,10 +39,12 @@ def validate(): onclick = re.findall(r'onclick=["\']([^"\']+)["\']', content) ids = re.findall(r'id=["\']([^"\']+)["\']', content) + # After (optimized) _RE_ONCLICK = re.compile(r'onclick=["\']([^"\']+)["\']') _RE_IDS = re.compile(r'id=["\']([^"\']+)["\']') + def validate(): onclick = _RE_ONCLICK.findall(content) ids = _RE_IDS.findall(content) diff --git a/docs/PERFORMANCE_OPTIMIZATION_README.md b/docs/PERFORMANCE_OPTIMIZATION_README.md index 1acb0622e..308dca4d4 100644 --- a/docs/PERFORMANCE_OPTIMIZATION_README.md +++ b/docs/PERFORMANCE_OPTIMIZATION_README.md @@ -79,12 +79,14 @@ Performance Benchmark: ```python # Define keyword sets at module level -_ACTION_KEYWORDS = frozenset(['jump', 'run', 'dance']) +_ACTION_KEYWORDS = frozenset(["jump", "run", "dance"]) + def _any_word_in_text(keywords: frozenset, text: str) -> bool: """Fast O(n) keyword matching using set intersection.""" return bool(keywords & set(text.split())) + # Usage if _any_word_in_text(_ACTION_KEYWORDS, user_command): process_action() @@ -95,6 +97,7 @@ if _any_word_in_text(_ACTION_KEYWORDS, user_command): ```python _connection_pool = [] + def _get_conn(): """Get connection from pool or create new.""" while _connection_pool: @@ -110,6 +113,7 @@ def _get_conn(): pass return create_new_connection() + def _return_conn(conn): """Return connection to pool.""" if len(_connection_pool) < 5: @@ -143,7 +147,7 @@ def read_tail(filepath: Path, max_lines: int = 500): return f.readlines()[-max_lines:] # Large file: stream backwards - with open(filepath, 'rb') as f: + with open(filepath, "rb") as f: f.seek(0, 2) # End remaining = f.tell() chunks = [] @@ -155,8 +159,8 @@ def read_tail(filepath: Path, max_lines: int = 500): remaining -= block_size # Check if we have enough lines - decoded = b''.join(chunks).decode('utf-8', errors='ignore') - if decoded.count('\n') >= max_lines: + decoded = b"".join(chunks).decode("utf-8", errors="ignore") + if decoded.count("\n") >= max_lines: break return decoded.splitlines(keepends=True)[-max_lines:] diff --git a/docs/PERFORMANCE_OPTIMIZATION_SUMMARY.md b/docs/PERFORMANCE_OPTIMIZATION_SUMMARY.md index fb480f5cf..6e819cf00 100644 --- a/docs/PERFORMANCE_OPTIMIZATION_SUMMARY.md +++ b/docs/PERFORMANCE_OPTIMIZATION_SUMMARY.md @@ -70,6 +70,7 @@ if _any_word_in_text(_JUMP_KEYWORDS, cmd): ```python _connection_pool = [] + def _get_conn(): """Get connection from pool or create new one.""" # Try to reuse existing connection @@ -91,6 +92,7 @@ def _get_conn(): # Create new connection return pyodbc.connect(conn_str, timeout=4) + def _return_conn(conn): """Return connection to pool for reuse.""" if len(_connection_pool) < 5: # Max 5 connections @@ -142,7 +144,7 @@ for model_id in model_ids: ```python # OLD: Memory = file size (potentially GB) lines = f.readlines() -return {'logs': ''.join(lines[-500:])} +return {"logs": "".join(lines[-500:])} ``` **Solution**: Block-based streaming for large files @@ -152,10 +154,10 @@ return {'logs': ''.join(lines[-500:])} if file_size <= 65536: # Small files: fast in-memory read lines = f.readlines() - return {'logs': ''.join(lines[-500:])} + return {"logs": "".join(lines[-500:])} else: # Large files: stream backwards in 32KB blocks - with open(log_file, 'rb') as f: + with open(log_file, "rb") as f: f.seek(0, 2) # End # Read backwards until we have 500 lines # ... (see implementation for details) @@ -229,11 +231,11 @@ for name in status_map: ```python # ❌ Avoid: Repeated any() with list comprehensions -if any(word in text for word in ['jump', 'leap', 'hop']): +if any(word in text for word in ["jump", "leap", "hop"]): pass # ✅ Use: Pre-compiled frozensets with set operations -_KEYWORDS = frozenset(['jump', 'leap', 'hop']) +_KEYWORDS = frozenset(["jump", "leap", "hop"]) if _any_word_in_text(_KEYWORDS, text): pass ``` @@ -275,7 +277,7 @@ lines = f.readlines() process_last_n(lines[-100:]) # ✅ Use: Stream or block-based reading for large files -if file_size < 64*1024: +if file_size < 64 * 1024: lines = f.readlines() # Fast path for small files else: lines = stream_tail(f, 100) # Efficient for large files diff --git a/docs/PERFORMANCE_OPTIMIZATION_SUMMARY_2026-02-17.md b/docs/PERFORMANCE_OPTIMIZATION_SUMMARY_2026-02-17.md index 57d6de376..bacd4faf7 100644 --- a/docs/PERFORMANCE_OPTIMIZATION_SUMMARY_2026-02-17.md +++ b/docs/PERFORMANCE_OPTIMIZATION_SUMMARY_2026-02-17.md @@ -15,10 +15,10 @@ This document summarizes the performance optimizations implemented to address sl **Before**: ```python -if any(k in cmd for k in ['jump', 'leap', 'hop']): - return '[aria:position:50:60]' -elif any(k in cmd for k in ['dance', 'spin', 'twirl']): - return '[aria:position:50:50]' +if any(k in cmd for k in ["jump", "leap", "hop"]): + return "[aria:position:50:60]" +elif any(k in cmd for k in ["dance", "spin", "twirl"]): + return "[aria:position:50:50]" # ... 18+ more similar checks ``` @@ -26,17 +26,19 @@ elif any(k in cmd for k in ['dance', 'spin', 'twirl']): ```python # Pre-compiled keyword sets at module level -JUMP_KEYWORDS = frozenset(['jump', 'leap', 'hop']) -DANCE_KEYWORDS = frozenset(['dance', 'spin', 'twirl']) +JUMP_KEYWORDS = frozenset(["jump", "leap", "hop"]) +DANCE_KEYWORDS = frozenset(["dance", "spin", "twirl"]) # ... 20 more sets + def _contains_any_keyword(text: str, keywords: frozenset) -> bool: """Check if text contains any keyword from set. O(1) per keyword check.""" return any(kw in text for kw in keywords) + # Usage if _contains_any_keyword(cmd, JUMP_KEYWORDS): - return '[aria:position:50:60]' + return "[aria:position:50:60]" ``` **Impact**: @@ -128,25 +130,19 @@ def store_embedding(...): **Before**: ```python -tags = re.findall(r'\[aria:[^\]]+\]', response) # COMPILED EVERY TIME -say_match = re.search( - r"(?:\b(?:say|announce|shout|speak|tell)\b)...", - command, flags=re.I -) # COMPILED EVERY TIME +tags = re.findall(r"\[aria:[^\]]+\]", response) # COMPILED EVERY TIME +say_match = re.search(r"(?:\b(?:say|announce|shout|speak|tell)\b)...", command, flags=re.I) # COMPILED EVERY TIME ``` **After**: ```python # Pre-compiled at module level -_RE_JSON_BLOCK = re.compile(r'\[.*\]', re.DOTALL) -_RE_ARIA_TAGS = re.compile(r'\[aria:[^\]]+\]') -_RE_SAY_COMMAND = re.compile( - r"(?:\b(?:say|announce|shout|speak|tell)\b)...", - re.IGNORECASE -) -_RE_SANITIZE_BRACKETS = re.compile(r'\]') -_RE_COORDINATES = re.compile(r'(\d{1,3})%?.*?(\d{1,3})%?') +_RE_JSON_BLOCK = re.compile(r"\[.*\]", re.DOTALL) +_RE_ARIA_TAGS = re.compile(r"\[aria:[^\]]+\]") +_RE_SAY_COMMAND = re.compile(r"(?:\b(?:say|announce|shout|speak|tell)\b)...", re.IGNORECASE) +_RE_SANITIZE_BRACKETS = re.compile(r"\]") +_RE_COORDINATES = re.compile(r"(\d{1,3})%?.*?(\d{1,3})%?") # Usage tags = _RE_ARIA_TAGS.findall(response) # USES COMPILED PATTERN @@ -173,16 +169,17 @@ say_match = _RE_SAY_COMMAND.search(command) # USES COMPILED PATTERN ```python words = [w for msg in assistant_messages for w in msg.split()] # FULL LIST IN MEMORY if words: - diversity = len(set(words))/len(words) + diversity = len(set(words)) / len(words) ``` **After**: ```python from itertools import chain + words = list(chain.from_iterable(msg.split() for msg in assistant_messages)) # STREAMING if words: - diversity = len(set(words))/len(words) + diversity = len(set(words)) / len(words) ``` **Impact**: diff --git a/docs/PERFORMANCE_QUICK_FIXES.md b/docs/PERFORMANCE_QUICK_FIXES.md index 6302dc839..05588c749 100644 --- a/docs/PERFORMANCE_QUICK_FIXES.md +++ b/docs/PERFORMANCE_QUICK_FIXES.md @@ -11,7 +11,7 @@ Quick reference for common performance anti-patterns and their fixes in the Aria ❌ **SLOW - Creates list every time:** ```python -if any(k in cmd for k in ['jump', 'leap', 'hop']): +if any(k in cmd for k in ["jump", "leap", "hop"]): do_something() ``` @@ -19,11 +19,11 @@ if any(k in cmd for k in ['jump', 'leap', 'hop']): ```python # For small sets, tuple is fastest -if any(k in cmd for k in ('jump', 'leap', 'hop')): +if any(k in cmd for k in ("jump", "leap", "hop")): do_something() # For repeated checks or large sets, use module-level constant -_JUMP_KEYWORDS = frozenset(['jump', 'leap', 'hop']) +_JUMP_KEYWORDS = frozenset(["jump", "leap", "hop"]) if any(k in cmd for k in _JUMP_KEYWORDS): do_something() ``` @@ -161,7 +161,7 @@ for key, values in data.items(): ❌ **SLOW - Manual tracking:** ```python -best_value = float('-inf') +best_value = float("-inf") best_item = None for item in items: @@ -300,6 +300,7 @@ grep -n "sum(.*).*len(" *.py ```python import time + def benchmark(func, *args, iterations=1000): """Benchmark a function.""" # Warm-up @@ -312,13 +313,14 @@ def benchmark(func, *args, iterations=1000): func(*args) elapsed = time.perf_counter() - start - print(f"{func.__name__}: {elapsed:.3f}s total, {elapsed/iterations*1000:.3f}ms avg") + print(f"{func.__name__}: {elapsed:.3f}s total, {elapsed / iterations * 1000:.3f}ms avg") return elapsed + # Usage old_time = benchmark(old_function, test_data) new_time = benchmark(new_function, test_data) -print(f"Speedup: {old_time/new_time:.2f}x") +print(f"Speedup: {old_time / new_time:.2f}x") ``` --- @@ -368,7 +370,7 @@ result = expensive_function() profiler.disable() stats = pstats.Stats(profiler) -stats.sort_stats('cumulative') +stats.sort_stats("cumulative") stats.print_stats(10) # Top 10 slowest ``` diff --git a/docs/PERFORMANCE_README.md b/docs/PERFORMANCE_README.md index 24c14c8be..418d32c0b 100644 --- a/docs/PERFORMANCE_README.md +++ b/docs/PERFORMANCE_README.md @@ -33,26 +33,28 @@ python scripts/benchmark_performance.py ```python from shared.performance_utils import ( - tail_file, # Memory-efficient log tailing - stream_jsonl, # Generator-based JSONL reading - find_json_in_output, # Fast JSON extraction - FileCache, # In-memory file caching - timeit, # Function timing decorator - memoize_with_ttl # Time-based memoization + tail_file, # Memory-efficient log tailing + stream_jsonl, # Generator-based JSONL reading + find_json_in_output, # Fast JSON extraction + FileCache, # In-memory file caching + timeit, # Function timing decorator + memoize_with_ttl, # Time-based memoization ) # Example: Tail a log file logs = tail_file(Path("training.log"), max_lines=50) # Example: Extract JSON from subprocess -result = subprocess.run(['./script'], capture_output=True, text=True) -metrics = find_json_in_output(result.stdout, key='metrics') +result = subprocess.run(["./script"], capture_output=True, text=True) +metrics = find_json_in_output(result.stdout, key="metrics") + # Example: Time a function @timeit def expensive_operation(): pass + # Example: Cache with TTL @memoize_with_ttl(ttl_seconds=300) def fetch_config(): @@ -66,7 +68,7 @@ def fetch_config(): **Before:** ```python -with open(log_file, 'r') as f: +with open(log_file, "r") as f: all_lines = f.readlines() # Loads entire file! return all_lines[-20:] ``` @@ -75,6 +77,7 @@ with open(log_file, 'r') as f: ```python from shared.performance_utils import tail_file + return tail_file(log_file, max_lines=20) # Only keeps 20 lines in memory ``` @@ -94,7 +97,8 @@ for line in output.splitlines(): # Parse all lines ```python from shared.performance_utils import find_json_in_output -return find_json_in_output(output, key='metrics', search_from_end=True) + +return find_json_in_output(output, key="metrics", search_from_end=True) ``` ### 3. JSONL Streaming (1.1x faster, 1.2 MB saved) @@ -102,7 +106,7 @@ return find_json_in_output(output, key='metrics', search_from_end=True) **Before:** ```python -with open('data.jsonl', 'r') as f: +with open("data.jsonl", "r") as f: all_records = [json.loads(line) for line in f] # Loads all into memory for record in all_records: process(record) @@ -112,7 +116,8 @@ for record in all_records: ```python from shared.performance_utils import stream_jsonl -for record in stream_jsonl(Path('data.jsonl')): # Streams one at a time + +for record in stream_jsonl(Path("data.jsonl")): # Streams one at a time process(record) ``` diff --git a/docs/QUANTUM_LLM_STATUS_TRACKING.md b/docs/QUANTUM_LLM_STATUS_TRACKING.md index 878cb9c41..388bdfbdf 100644 --- a/docs/QUANTUM_LLM_STATUS_TRACKING.md +++ b/docs/QUANTUM_LLM_STATUS_TRACKING.md @@ -55,8 +55,8 @@ status = get_quantum_llm_status() status = get_quantum_llm_status(output_dir="data_out/custom_training") # Check if inference ready -if status['inference_ready']: - checkpoint = status['checkpoint_path'] +if status["inference_ready"]: + checkpoint = status["checkpoint_path"] # Load and use model ``` @@ -73,7 +73,7 @@ data = { "status": "completed", "epochs_completed": 5, "best_loss": 0.089, - "checkpoint_path": "data_out/quantum_llm_training/best_quantum_llm.pt" + "checkpoint_path": "data_out/quantum_llm_training/best_quantum_llm.pt", } write_quantum_llm_status(data, output_dir="data_out/quantum_llm_training") @@ -168,12 +168,12 @@ from quantum_llm_trainer import get_quantum_llm_status status = get_quantum_llm_status() -if status['checkpoint_exists'] and status['inference_ready']: - path = status['checkpoint_path'] +if status["checkpoint_exists"] and status["inference_ready"]: + path = status["checkpoint_path"] print(f"✓ Checkpoint ready at: {path}") else: print("✗ Checkpoint not ready") - if error := status.get('last_error'): + if error := status.get("last_error"): print(f" Error: {error}") ``` @@ -182,7 +182,7 @@ else: When using passive training mode: ```python -trainer = QuantumEnhancedLLMTrainer(config={'passive': True}) +trainer = QuantumEnhancedLLMTrainer(config={"passive": True}) await trainer.run_passive_training_loop() ``` @@ -247,13 +247,12 @@ python scripts/quantum_llm_status_check.py --json | jq '.status_file_exists' # In function_app.py from quantum_llm_trainer import get_quantum_llm_status + @app.route(route="api/quantum-llm-ready", methods=["GET"]) def quantum_ready(req): status = get_quantum_llm_status() return func.HttpResponse( - json.dumps({"ready": status["inference_ready"]}), - status_code=200, - mimetype="application/json" + json.dumps({"ready": status["inference_ready"]}), status_code=200, mimetype="application/json" ) ``` diff --git a/docs/README_PAGES.md b/docs/README_PAGES.md index db4c0471a..0f6857e92 100644 --- a/docs/README_PAGES.md +++ b/docs/README_PAGES.md @@ -221,7 +221,7 @@ import sys REPO_ROOT = Path(__file__).resolve().parent.parent if str(REPO_ROOT) not in sys.path: - sys.path.insert(0, str(REPO_ROOT)) + sys.path.insert(0, str(REPO_ROOT)) # Now import local packages safely: from shared.json_utils import load_status_json diff --git a/docs/REGEX_OPTIMIZATION_2026-02-17.md b/docs/REGEX_OPTIMIZATION_2026-02-17.md index 65bc925f1..5ebe84fb4 100644 --- a/docs/REGEX_OPTIMIZATION_2026-02-17.md +++ b/docs/REGEX_OPTIMIZATION_2026-02-17.md @@ -46,7 +46,7 @@ Based on benchmark tests (see `tests/test_regex_optimizations.py`): ```python _RE_ONCLICK = re.compile(r'onclick=["\']([^"\']+)["\']') -_RE_FUNC_NAMES = re.compile(r'([a-zA-Z_][a-zA-Z0-9_]*)\s*\(') +_RE_FUNC_NAMES = re.compile(r"([a-zA-Z_][a-zA-Z0-9_]*)\s*\(") _RE_ELEMENT_IDS = re.compile(r'id=["\']([^"\']+)["\']') _RE_GET_BY_ID = re.compile(r"getElementById\(['\"]([^'\"]+)['\"]\)") _RE_FETCH_CALLS = re.compile(r"fetch\(['\"]([^'\"]+)['\"]\)") @@ -66,13 +66,13 @@ _RE_FETCH_CALLS = re.compile(r"fetch\(['\"]([^'\"]+)['\"]\)") **Patterns Compiled**: ```python -_RE_CONSOLE_LOG = re.compile(r'console\.log\([^)]+\)') +_RE_CONSOLE_LOG = re.compile(r"console\.log\([^)]+\)") _RE_GET_BY_ID = re.compile(r"getElementById\(['\"]([^'\"]+)['\"]\)") _RE_QUERY_SELECTOR = re.compile(r"querySelector\(['\"]([^'\"]+)['\"]\)") _RE_ELEMENT_IDS = re.compile(r'id=["\']([^"\']+)["\']') -_RE_ASYNC_FUNCTION = re.compile(r'async\s+function') -_RE_AWAIT = re.compile(r'\bawait\s+') -_RE_EVENT_LISTENER = re.compile(r'addEventListener\s*\(') +_RE_ASYNC_FUNCTION = re.compile(r"async\s+function") +_RE_AWAIT = re.compile(r"\bawait\s+") +_RE_EVENT_LISTENER = re.compile(r"addEventListener\s*\(") _RE_FETCH_CALLS = re.compile(r"fetch\(['\"]([^'\"]+)['\"]\)") _RE_LOCALSTORAGE = re.compile(r"localStorage\.(getItem|setItem|removeItem)\(['\"]([^'\"]+)['\"]\)") _RE_ONCLICK = re.compile(r'onclick=["\']([^"\']+)["\']') @@ -114,8 +114,8 @@ _RE_WORD_SPLIT = re.compile(r"\S+") **Patterns Compiled**: ```python -_RE_HTML_TAGS = re.compile(r'<[^<]+?>') -_RE_WHITESPACE = re.compile(r'\s+') +_RE_HTML_TAGS = re.compile(r"<[^<]+?>") +_RE_WHITESPACE = re.compile(r"\s+") ``` **Impact**: @@ -201,8 +201,8 @@ Examples: import re # Pre-compile regex patterns for performance -_RE_PATTERN_1 = re.compile(r'...') -_RE_PATTERN_2 = re.compile(r'...') +_RE_PATTERN_1 = re.compile(r"...") +_RE_PATTERN_2 = re.compile(r"...") # ... rest of module code ``` diff --git a/docs/SPARKLE_IMPROVEMENTS.md b/docs/SPARKLE_IMPROVEMENTS.md index 5bcf9e098..938e2d92c 100644 --- a/docs/SPARKLE_IMPROVEMENTS.md +++ b/docs/SPARKLE_IMPROVEMENTS.md @@ -19,9 +19,9 @@ Enhanced the Aria character system's sparkle visual effects with performance opt - **Benefit**: Faster keyword matching, consistent with existing patterns in codebase ```python -SPARKLE_KEYWORDS = frozenset(['sparkle', 'sparkles', 'glitter', 'shimmer', 'shine']) -GLOW_KEYWORDS = frozenset(['glow', 'glowing', 'radiate', 'illuminate']) -HEARTS_KEYWORDS = frozenset(['hearts', 'heart', 'love']) +SPARKLE_KEYWORDS = frozenset(["sparkle", "sparkles", "glitter", "shimmer", "shine"]) +GLOW_KEYWORDS = frozenset(["glow", "glowing", "radiate", "illuminate"]) +HEARTS_KEYWORDS = frozenset(["hearts", "heart", "love"]) ``` #### requestAnimationFrame for DOM Manipulation diff --git a/docs/architecture/QAI_PHASE_24_IMPROVEMENTS.md b/docs/architecture/QAI_PHASE_24_IMPROVEMENTS.md index 408a097f4..a9aa24186 100644 --- a/docs/architecture/QAI_PHASE_24_IMPROVEMENTS.md +++ b/docs/architecture/QAI_PHASE_24_IMPROVEMENTS.md @@ -505,8 +505,8 @@ notifier = NotificationManager() notifier.send_notification( title="Title", message="Message body", - icon="info", # info/success/warning/error - duration=10 # seconds + icon="info", # info/success/warning/error + duration=10, # seconds ) # Training events @@ -590,6 +590,7 @@ updateNotificationUI() ```python # Add notification support from scripts.notification_system import NotificationManager + notifier = NotificationManager() # Before job diff --git a/docs/architecture/agi-provider.md b/docs/architecture/agi-provider.md index 282c0351b..c558492be 100644 --- a/docs/architecture/agi-provider.md +++ b/docs/architecture/agi-provider.md @@ -88,11 +88,13 @@ can replace `AGIContext` without inheriting from it: ```python from agi_provider import MemoryInterface, AGIProvider + class RedisMemory: def add_message(self, message): ... def add_reasoning_chain(self, chain): ... def get_relevant_context(self, query) -> str: ... + assert isinstance(RedisMemory(), MemoryInterface) # True ``` @@ -156,13 +158,13 @@ event loop: import asyncio from agi_provider import create_agi_provider + async def main(): provider, _ = create_agi_provider() - response = await provider.async_complete( - [{"role": "user", "content": "Explain LoRA fine-tuning"}] - ) + response = await provider.async_complete([{"role": "user", "content": "Explain LoRA fine-tuning"}]) print(response) + asyncio.run(main()) ``` diff --git a/docs/aria/ARIA_MOVEMENT_COMPLETE.md b/docs/aria/ARIA_MOVEMENT_COMPLETE.md index c3907e1f7..19c102acb 100644 --- a/docs/aria/ARIA_MOVEMENT_COMPLETE.md +++ b/docs/aria/ARIA_MOVEMENT_COMPLETE.md @@ -466,6 +466,7 @@ Get-Content data_out\aria_models\aria_direct\training.log | Select-String "loss" ```python # Test parser directly from function_app import parse_movement_commands + test_text = "I'll move Aria left! [aria:move:left]" print(parse_movement_commands(test_text)) ``` diff --git a/docs/database/AZURE_MONITOR_SQL_SETUP.md b/docs/database/AZURE_MONITOR_SQL_SETUP.md index 033323dc4..78bd93d97 100644 --- a/docs/database/AZURE_MONITOR_SQL_SETUP.md +++ b/docs/database/AZURE_MONITOR_SQL_SETUP.md @@ -220,8 +220,8 @@ _ENGINE = create_engine( url, pool_pre_ping=True, pool_recycle=1800, - pool_size=30, # Override default (5) - max_overflow=10, # Override default (10) + pool_size=30, # Override default (5) + max_overflow=10, # Override default (10) future=True, ) ``` diff --git a/docs/database/DATABASE_INTEGRATION_GUIDE.md b/docs/database/DATABASE_INTEGRATION_GUIDE.md index 2a9b00473..e3933477a 100644 --- a/docs/database/DATABASE_INTEGRATION_GUIDE.md +++ b/docs/database/DATABASE_INTEGRATION_GUIDE.md @@ -56,9 +56,11 @@ from uuid import uuid4 # ADD THIS try: # noqa: SIM105 from shared.db_logging import log_chat_message_safe # type: ignore except Exception: # noqa: BLE001 + def log_chat_message_safe(*_a, **_kw): # type: ignore return {"success": False, "skipped": True} + # Add talk-to-ai to path so we can import chat_providers talk_to_ai_path = Path(__file__).resolve().parent / "talk-to-ai" / "src" sys.path.insert(0, str(talk_to_ai_path)) @@ -69,82 +71,82 @@ sys.path.insert(0, str(talk_to_ai_path)) **FIND:** ```python - # Get completion (non-streaming for HTTP simplicity) - result = provider.complete(pruned_messages, stream=False) - - # If result is still a generator, consume it - if hasattr(result, '__iter__') and not isinstance(result, str): - result = ''.join(result) - - response_data = { - "response": result, - "provider": info.name, - "model": info.model, - "pruning": { - "original_tokens": stats.original_tokens, - "pruned_tokens": stats.pruned_tokens, - "removed_count": stats.removed_count, - "budget": stats.budget, - "reserve_output_tokens": stats.reserve_output_tokens, - } - } +# Get completion (non-streaming for HTTP simplicity) +result = provider.complete(pruned_messages, stream=False) + +# If result is still a generator, consume it +if hasattr(result, "__iter__") and not isinstance(result, str): + result = "".join(result) + +response_data = { + "response": result, + "provider": info.name, + "model": info.model, + "pruning": { + "original_tokens": stats.original_tokens, + "pruned_tokens": stats.pruned_tokens, + "removed_count": stats.removed_count, + "budget": stats.budget, + "reserve_output_tokens": stats.reserve_output_tokens, + }, +} ``` **REPLACE WITH:** ```python - # Get completion (non-streaming for HTTP simplicity) - result = provider.complete(pruned_messages, stream=False) - - # If result is still a generator, consume it - if hasattr(result, '__iter__') and not isinstance(result, str): - result = ''.join(result) - - # Session handling (client may pass session_id to maintain conversation linkage) - session_id = req_body.get("session_id") or req.headers.get("X-Session-Id") or str(uuid4()) - - # Log latest user message (last in list) BEFORE assistant reply - try: - if messages: - last_user = messages[-1] - if last_user.get("role") == "user": - log_chat_message_safe( - session_id=session_id, - provider=info.name, - model=info.model, - role="user", - content=str(last_user.get("content", ""))[:4000], # truncate safety - ) - except Exception as e: # noqa: BLE001 - logging.warning(f"User chat log skipped: {e}") - - # Log assistant reply - try: - log_resp = log_chat_message_safe( +# Get completion (non-streaming for HTTP simplicity) +result = provider.complete(pruned_messages, stream=False) + +# If result is still a generator, consume it +if hasattr(result, "__iter__") and not isinstance(result, str): + result = "".join(result) + +# Session handling (client may pass session_id to maintain conversation linkage) +session_id = req_body.get("session_id") or req.headers.get("X-Session-Id") or str(uuid4()) + +# Log latest user message (last in list) BEFORE assistant reply +try: + if messages: + last_user = messages[-1] + if last_user.get("role") == "user": + log_chat_message_safe( session_id=session_id, provider=info.name, model=info.model, - role="assistant", - content=str(result)[:8000], # truncate safety + role="user", + content=str(last_user.get("content", ""))[:4000], # truncate safety ) - except Exception as e: # noqa: BLE001 - logging.warning(f"Assistant chat log skipped: {e}") - log_resp = {"success": False} - - response_data = { - "response": result, - "provider": info.name, - "model": info.model, - "session_id": session_id, - "conversation_id": log_resp.get("conversation_id"), - "pruning": { - "original_tokens": stats.original_tokens, - "pruned_tokens": stats.pruned_tokens, - "removed_count": stats.removed_count, - "budget": stats.budget, - "reserve_output_tokens": stats.reserve_output_tokens, - } - } +except Exception as e: # noqa: BLE001 + logging.warning(f"User chat log skipped: {e}") + +# Log assistant reply +try: + log_resp = log_chat_message_safe( + session_id=session_id, + provider=info.name, + model=info.model, + role="assistant", + content=str(result)[:8000], # truncate safety + ) +except Exception as e: # noqa: BLE001 + logging.warning(f"Assistant chat log skipped: {e}") + log_resp = {"success": False} + +response_data = { + "response": result, + "provider": info.name, + "model": info.model, + "session_id": session_id, + "conversation_id": log_resp.get("conversation_id"), + "pruning": { + "original_tokens": stats.original_tokens, + "pruned_tokens": stats.pruned_tokens, + "removed_count": stats.removed_count, + "budget": stats.budget, + "reserve_output_tokens": stats.reserve_output_tokens, + }, +} ``` **Step 3:** In the `chat_stream()` function (around line 280), add session handling: diff --git a/docs/database/DATABASE_SQL_SETUP.md b/docs/database/DATABASE_SQL_SETUP.md index 312953d0a..152376a05 100644 --- a/docs/database/DATABASE_SQL_SETUP.md +++ b/docs/database/DATABASE_SQL_SETUP.md @@ -108,6 +108,7 @@ delete_value("last_model") ```python from shared.sql_engine import quick_query + rows = quick_query("SELECT TOP 5 * FROM QuantumTrainingRuns ORDER BY CreatedAt DESC") for r in rows: print(r) @@ -119,6 +120,7 @@ for r in rows: ```python from shared.sql_engine import quick_query + rows = quick_query("SELECT 1", simulate_delay=0.6) # sleeps 0.6s before execution ``` @@ -397,8 +399,8 @@ _ENGINE = create_engine( url, pool_pre_ping=True, pool_recycle=1800, - pool_size=30, # Override - max_overflow=10, # Override + pool_size=30, # Override + max_overflow=10, # Override future=True, ) ``` diff --git a/docs/database/TELEMETRY_COSMOS_ENABLEMENT.md b/docs/database/TELEMETRY_COSMOS_ENABLEMENT.md index 5f34a6670..8ac0a8ddf 100644 --- a/docs/database/TELEMETRY_COSMOS_ENABLEMENT.md +++ b/docs/database/TELEMETRY_COSMOS_ENABLEMENT.md @@ -58,6 +58,7 @@ curl http://localhost:7071/api/ai/status | jq '.telemetry' from opentelemetry import trace from azure.monitor.opentelemetry import configure_azure_monitor + def init_telemetry(): conn_str = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") if conn_str: @@ -65,6 +66,7 @@ def init_telemetry(): return True return False + def is_enabled() -> bool: return bool(os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")) ``` @@ -73,6 +75,7 @@ def is_enabled() -> bool: ```python from opentelemetry import trace + _tracer = trace.get_tracer("qai.functions") with _tracer.start_as_current_span("chat_request") as span: @@ -230,6 +233,7 @@ curl -X POST http://localhost:7071/api/chat -H "Content-Type: application/json" ```python from azure.cosmos import CosmosClient + class QAICosmosClient: def __init__(self): endpoint = os.getenv("COSMOS_ENDPOINT") @@ -440,6 +444,7 @@ Set `QAI_ENABLE_COSMOS=false` or remove the environment variable. The system wil ```python from opentelemetry.sdk.trace.sampling import TraceIdRatioBased + sampler = TraceIdRatioBased(0.1) # 10% sampling ``` diff --git a/docs/deployment/AZURE_ML_TRAINING_GUIDE.md b/docs/deployment/AZURE_ML_TRAINING_GUIDE.md index c2cc61a57..e4b65ba2e 100644 --- a/docs/deployment/AZURE_ML_TRAINING_GUIDE.md +++ b/docs/deployment/AZURE_ML_TRAINING_GUIDE.md @@ -116,7 +116,7 @@ ml_client = MLClient( credential=DefaultAzureCredential(), subscription_id="", resource_group_name="", - workspace_name="" + workspace_name="", ) # Configure training job @@ -126,7 +126,7 @@ job = command( environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest", compute="cpu-cluster", display_name="phi-lora-training", - experiment_name="lora-finetuning" + experiment_name="lora-finetuning", ) # Submit job diff --git a/docs/deployment/PRODUCTION_DEPLOYMENT_PLAN.md b/docs/deployment/PRODUCTION_DEPLOYMENT_PLAN.md index 0f59834de..603a76851 100644 --- a/docs/deployment/PRODUCTION_DEPLOYMENT_PLAN.md +++ b/docs/deployment/PRODUCTION_DEPLOYMENT_PLAN.md @@ -308,14 +308,17 @@ def rank_for_production(results): ```python # api/quantum_inference.py from fastapi import FastAPI + app = FastAPI() + @app.post("/predict/{model_id}") async def predict(model_id: str, features: List[float]): model = load_model(model_id) result = model.predict(features) return {"prediction": result, "confidence": model.confidence} + # Deploy with: # uvicorn api.quantum_inference:app --host 0.0.0.0 --port 8000 ``` diff --git a/docs/guides/ADVANCED_AUTOMATION.md b/docs/guides/ADVANCED_AUTOMATION.md index 7c2cb8684..0f7a0ef1e 100644 --- a/docs/guides/ADVANCED_AUTOMATION.md +++ b/docs/guides/ADVANCED_AUTOMATION.md @@ -498,7 +498,7 @@ Auto-scheduler disables jobs after consecutive failures: ```python max_consecutive_failures: 3 # Disable after 3 failures -notify_on_failure: true # Send notification +notify_on_failure: true # Send notification ``` ## Monitoring & Notifications diff --git a/docs/guides/ADVANCED_FEATURES_GUIDE.md b/docs/guides/ADVANCED_FEATURES_GUIDE.md index 555aeb58c..cbd07a607 100644 --- a/docs/guides/ADVANCED_FEATURES_GUIDE.md +++ b/docs/guides/ADVANCED_FEATURES_GUIDE.md @@ -144,11 +144,7 @@ from scripts.job_queue import JobQueue, JobPriority queue = JobQueue() # Add high-priority job -queue.add_job( - name="urgent_training", - config={...}, - priority=JobPriority.HIGH -) +queue.add_job(name="urgent_training", config={...}, priority=JobPriority.HIGH) ``` Priority order: `CRITICAL` > `HIGH` > `NORMAL` > `LOW` @@ -158,11 +154,7 @@ Priority order: `CRITICAL` > `HIGH` > `NORMAL` > `LOW` ```python # Job 2 won't start until Job 1 completes job1 = queue.add_job(name="preprocess", config={...}) -job2 = queue.add_job( - name="train", - config={...}, - dependencies=[job1] -) +job2 = queue.add_job(name="train", config={...}, dependencies=[job1]) ``` #### **Automatic Retry** @@ -221,7 +213,7 @@ prep_job = queue.add_job( name="preprocess_dataset", config={"action": "clean", "dataset": "mixed_chat"}, priority=JobPriority.HIGH, - estimated_duration=300 # 5 minutes + estimated_duration=300, # 5 minutes ) # 2. Train model (depends on preprocessing) @@ -230,7 +222,7 @@ train_job = queue.add_job( config={"epochs": 3, "dataset": "mixed_chat"}, priority=JobPriority.NORMAL, dependencies=[prep_job], - estimated_duration=1800 # 30 minutes + estimated_duration=1800, # 30 minutes ) # 3. Evaluate model (depends on training) @@ -239,7 +231,7 @@ eval_job = queue.add_job( config={"model": "phi35_v2"}, priority=JobPriority.NORMAL, dependencies=[train_job], - estimated_duration=600 # 10 minutes + estimated_duration=600, # 10 minutes ) # Get next job to execute diff --git a/docs/guides/AI_DATASETS_CATALOG.md b/docs/guides/AI_DATASETS_CATALOG.md index 598071088..d8d78dfe8 100644 --- a/docs/guides/AI_DATASETS_CATALOG.md +++ b/docs/guides/AI_DATASETS_CATALOG.md @@ -330,6 +330,7 @@ Computer vision feature-based classification ```python from datasets import load_dataset + ds = load_dataset("anon8231489123/ShareGPT_Vicuna_unfiltered") ``` @@ -345,6 +346,7 @@ ds = load_dataset("anon8231489123/ShareGPT_Vicuna_unfiltered") ```python from datasets import load_dataset + ds = load_dataset("OpenAssistant/oasst1") ``` @@ -368,6 +370,7 @@ ds = load_dataset("OpenAssistant/oasst1") ```python from datasets import load_dataset + ds = load_dataset("databricks/databricks-dolly-15k") ``` @@ -382,6 +385,7 @@ ds = load_dataset("databricks/databricks-dolly-15k") ```python from datasets import load_dataset + ds = load_dataset("WizardLM/WizardLM_evol_instruct_70k") ``` diff --git a/docs/guides/AI_IMPROVEMENTS.md b/docs/guides/AI_IMPROVEMENTS.md index dfb16b03b..b7c95de4c 100644 --- a/docs/guides/AI_IMPROVEMENTS.md +++ b/docs/guides/AI_IMPROVEMENTS.md @@ -82,9 +82,9 @@ Comprehensive improvements applied across all AI components to enhance performan **Advanced Generation Parameters**: ```python -top_p: float = 0.9 # Nucleus sampling (90th percentile) -top_k: int = 50 # Top-K sampling -repetition_penalty: 1.1 # Reduces repetitive text +top_p: float = 0.9 # Nucleus sampling (90th percentile) +top_k: int = 50 # Top-K sampling +repetition_penalty: 1.1 # Reduces repetitive text ``` **Benefits**: @@ -179,11 +179,11 @@ ml: # Enhanced LoRA initialization provider = LoraLocalProvider( adapter_dir="path/to/adapter", - temperature=0.7, # Creativity level - top_p=0.9, # Nucleus sampling - top_k=50, # Top-K sampling - repetition_penalty=1.1, # Reduce repetition - max_new_tokens=256 # Max response length + temperature=0.7, # Creativity level + top_p=0.9, # Nucleus sampling + top_k=50, # Top-K sampling + repetition_penalty=1.1, # Reduce repetition + max_new_tokens=256, # Max response length ) ``` @@ -240,9 +240,9 @@ provider = LoraLocalProvider( adapter_dir, temperature=0.7, max_new_tokens=256, - top_p=0.9, # Add these for better quality + top_p=0.9, # Add these for better quality top_k=50, - repetition_penalty=1.1 + repetition_penalty=1.1, ) ``` diff --git a/docs/guides/AUTONOMOUS_ORCHESTRATOR_MODEL.md b/docs/guides/AUTONOMOUS_ORCHESTRATOR_MODEL.md index 120e31d59..7af69bdfa 100644 --- a/docs/guides/AUTONOMOUS_ORCHESTRATOR_MODEL.md +++ b/docs/guides/AUTONOMOUS_ORCHESTRATOR_MODEL.md @@ -119,10 +119,12 @@ import yaml # Optional: Ray for distributed execution try: import ray + RAY_AVAILABLE = True except ImportError: RAY_AVAILABLE = False + class AutonomousOrchestrator: """ Production-grade autonomous training orchestrator with scaling. @@ -152,7 +154,7 @@ class AutonomousOrchestrator: "datasets_discovered": 0, "datasets_trained": 0, "models_deployed": 0, - "errors": [] + "errors": [], } def _load_config(self) -> Dict: @@ -223,16 +225,17 @@ class AutonomousOrchestrator: cmd = [ sys.executable, str(benchmark_script), - "--datasets-dir", str(datasets_dir), - "--workers", str(max_workers), - "--epochs", str(epochs) + "--datasets-dir", + str(datasets_dir), + "--workers", + str(max_workers), + "--epochs", + str(epochs), ] # Execute training (async subprocess) process = await asyncio.create_subprocess_exec( - *cmd, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE + *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) stdout, stderr = await process.communicate() @@ -248,12 +251,14 @@ class AutonomousOrchestrator: def _save_status(self): """Persist orchestrator status to JSON.""" import json + status_file = Path("data_out/autonomous_training_status.json") status_file.parent.mkdir(parents=True, exist_ok=True) - with open(status_file, 'w') as f: + with open(status_file, "w") as f: json.dump(self.status, f, indent=2) + # CLI Entry Point if __name__ == "__main__": import argparse @@ -280,6 +285,7 @@ from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score + def train_single_dataset(args) -> Dict: """ Train model on single dataset (worker function). @@ -299,9 +305,7 @@ def train_single_dataset(args) -> Dict: y = df.iloc[:, -1].values # Split - X_train, X_test, y_train, y_test = train_test_split( - X, y, test_size=0.2, random_state=42 - ) + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train model = RandomForestClassifier(n_estimators=100, random_state=42) @@ -310,26 +314,13 @@ def train_single_dataset(args) -> Dict: # Evaluate accuracy = accuracy_score(y_test, model.predict(X_test)) - return { - "dataset": dataset_path.name, - "accuracy": accuracy, - "status": "success", - "samples": len(df) - } + return {"dataset": dataset_path.name, "accuracy": accuracy, "status": "success", "samples": len(df)} except Exception as e: - return { - "dataset": dataset_path.name, - "status": "failed", - "error": str(e) - } + return {"dataset": dataset_path.name, "status": "failed", "error": str(e)} -def train_all_datasets( - datasets_dir: Path, - workers: int, - epochs: int, - output_dir: Path -) -> List[Dict]: + +def train_all_datasets(datasets_dir: Path, workers: int, epochs: int, output_dir: Path) -> List[Dict]: """ Train models on all datasets using multiprocessing. @@ -355,6 +346,7 @@ def train_all_datasets( return results + if __name__ == "__main__": import argparse @@ -365,18 +357,14 @@ if __name__ == "__main__": parser.add_argument("--output-dir", default="data_out/benchmark") args = parser.parse_args() - results = train_all_datasets( - Path(args.datasets_dir), - args.workers, - args.epochs, - Path(args.output_dir) - ) + results = train_all_datasets(Path(args.datasets_dir), args.workers, args.epochs, Path(args.output_dir)) # Save results import json + output_file = Path(args.output_dir) / "results.json" output_file.parent.mkdir(parents=True, exist_ok=True) - with open(output_file, 'w') as f: + with open(output_file, "w") as f: json.dump(results, f, indent=2) ``` @@ -388,6 +376,7 @@ from qiskit import QuantumCircuit from qiskit_aer import AerSimulator from azure.quantum import Workspace + class QuantumMLBackend: """ Unified interface for quantum ML training (local + cloud). @@ -402,9 +391,7 @@ class QuantumMLBackend: if backend_type == "azure": self.workspace = Workspace( - subscription_id="YOUR_SUBSCRIPTION_ID", - resource_group="YOUR_RESOURCE_GROUP", - name="YOUR_WORKSPACE_NAME" + subscription_id="YOUR_SUBSCRIPTION_ID", resource_group="YOUR_RESOURCE_GROUP", name="YOUR_WORKSPACE_NAME" ) def create_variational_circuit(self, n_qubits: int, n_layers: int) -> QuantumCircuit: @@ -564,6 +551,7 @@ training: ```python import mlflow + class AutonomousOrchestrator: async def train_models(self): with mlflow.start_run(): @@ -583,6 +571,7 @@ class AutonomousOrchestrator: from rich.live import Live from rich.table import Table + def create_status_table(orchestrator): table = Table(title="Training Status") table.add_column("Metric", style="cyan") @@ -594,6 +583,7 @@ def create_status_table(orchestrator): return table + # Use in orchestrator with Live(create_status_table(self), refresh_per_second=1): await self.train_models() @@ -634,6 +624,7 @@ except Exception as e: ```python import psutil + def check_resources(): cpu_percent = psutil.cpu_percent() memory_percent = psutil.virtual_memory().percent diff --git a/docs/guides/CHAT_WEBSITE_GUIDE.md b/docs/guides/CHAT_WEBSITE_GUIDE.md index 76380310e..0ed43d5d4 100644 --- a/docs/guides/CHAT_WEBSITE_GUIDE.md +++ b/docs/guides/CHAT_WEBSITE_GUIDE.md @@ -230,10 +230,7 @@ background: linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 100%); Edit `function_app.py`, add to the messages array: ```python -messages.insert(0, { - "role": "system", - "content": "You are a friendly cooking assistant." -}) +messages.insert(0, {"role": "system", "content": "You are a friendly cooking assistant."}) ``` ### Add Authentication diff --git a/docs/guides/DATASETS_SETUP_COMPLETE.md b/docs/guides/DATASETS_SETUP_COMPLETE.md index dc086359e..4e5d022ce 100644 --- a/docs/guides/DATASETS_SETUP_COMPLETE.md +++ b/docs/guides/DATASETS_SETUP_COMPLETE.md @@ -67,7 +67,7 @@ The script already uses the Wine dataset for demo. Modify `load_your_data()` to # In train_custom_dataset.py, replace the demo code with: df = pd.read_csv("../datasets/quantum/heart_disease.csv") X = df.iloc[:, :-1].values # All features except last -y = df.iloc[:, -1].values # Last column as label +y = df.iloc[:, -1].values # Last column as label return X, y ``` diff --git a/docs/guides/DEVELOPER_TOOLING_SUMMARY.md b/docs/guides/DEVELOPER_TOOLING_SUMMARY.md index c7ea7d668..a8277fcfa 100644 --- a/docs/guides/DEVELOPER_TOOLING_SUMMARY.md +++ b/docs/guides/DEVELOPER_TOOLING_SUMMARY.md @@ -162,10 +162,7 @@ log_file = latest / "stdout.log" # Path: fast_fail/last_run.json/stdout.log (in ```python # FIXED: Filter to directories only -job_dirs = [ - p for p in (REPO_ROOT / "data_out" / "autotrain" / "fast_fail").glob("*") - if p.is_dir() -] +job_dirs = [p for p in (REPO_ROOT / "data_out" / "autotrain" / "fast_fail").glob("*") if p.is_dir()] latest = max(job_dirs, key=lambda p: p.name) # Now picks timestamp dir log_file = latest / "stdout.log" # Path: fast_fail/20251121T051213Z/stdout.log (valid) ``` diff --git a/docs/guides/MONETIZATION_GUIDE.md b/docs/guides/MONETIZATION_GUIDE.md index 9f13da05b..faad371a7 100644 --- a/docs/guides/MONETIZATION_GUIDE.md +++ b/docs/guides/MONETIZATION_GUIDE.md @@ -100,11 +100,7 @@ Core features: - Persistent storage ```python -from shared.subscription_manager import ( - get_subscription_manager, - SubscriptionTier, - Feature -) +from shared.subscription_manager import get_subscription_manager, SubscriptionTier, Feature manager = get_subscription_manager() subscription = manager.get_subscription(user_id) @@ -115,7 +111,7 @@ if subscription.has_feature(Feature.QUANTUM_COMPUTING): pass # Track usage -if manager.track_usage(user_id, 'quantum_jobs', 1): +if manager.track_usage(user_id, "quantum_jobs", 1): # Usage allowed, proceed pass else: @@ -168,11 +164,11 @@ New endpoints added: ```python # Before executing a quantum job manager = get_subscription_manager() -if not manager.track_usage(user_id, 'quantum_jobs', 1): +if not manager.track_usage(user_id, "quantum_jobs", 1): return { "error": "Quantum job limit exceeded", "upgrade_url": "/pricing.html", - "current_tier": subscription.tier.value + "current_tier": subscription.tier.value, } # Proceed with quantum job @@ -192,7 +188,7 @@ manager.upgrade_subscription( tier=SubscriptionTier.PRO, duration_days=30, payment_method="stripe", - stripe_subscription_id=stripe_sub_id + stripe_subscription_id=stripe_sub_id, ) ``` diff --git a/docs/guides/MONITORING_GUIDE.md b/docs/guides/MONITORING_GUIDE.md index 332766ed1..362d37c4d 100644 --- a/docs/guides/MONITORING_GUIDE.md +++ b/docs/guides/MONITORING_GUIDE.md @@ -535,11 +535,10 @@ Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AutoTraining # Add webhook notification on cycle completion import requests + def send_slack_notification(status): webhook_url = "https://hooks.slack.com/services/YOUR/WEBHOOK/URL" - message = { - "text": f"Training Cycle Complete: {status['best_accuracy']:.2%} accuracy" - } + message = {"text": f"Training Cycle Complete: {status['best_accuracy']:.2%} accuracy"} requests.post(webhook_url, json=message) ``` diff --git a/docs/guides/NOTIFICATION_SYSTEM_GUIDE.md b/docs/guides/NOTIFICATION_SYSTEM_GUIDE.md index 9166f7355..3bf4b6c7a 100644 --- a/docs/guides/NOTIFICATION_SYSTEM_GUIDE.md +++ b/docs/guides/NOTIFICATION_SYSTEM_GUIDE.md @@ -279,8 +279,8 @@ Edit in `scripts/notification_system.py`: class TrainingNotifier: def __init__(self): self.milestones = { - 'loss_threshold': 0.5, # Notify when loss drops below this - 'epoch_interval': 5 # Notify every N epochs + "loss_threshold": 0.5, # Notify when loss drops below this + "epoch_interval": 5, # Notify every N epochs } ``` @@ -289,14 +289,14 @@ class TrainingNotifier: ```python # More frequent updates self.milestones = { - 'loss_threshold': 0.3, # Lower threshold - 'epoch_interval': 1 # Every epoch + "loss_threshold": 0.3, # Lower threshold + "epoch_interval": 1, # Every epoch } # Less frequent (production) self.milestones = { - 'loss_threshold': 0.1, # Very low threshold - 'epoch_interval': 10 # Every 10 epochs + "loss_threshold": 0.1, # Very low threshold + "epoch_interval": 10, # Every 10 epochs } ``` @@ -357,6 +357,7 @@ pip install win10toast ```python from scripts.notification_system import NotificationManager + notifier = NotificationManager() print(f"System: {notifier.system}") print(f"Enabled: {notifier.enabled}") diff --git a/docs/guides/QUICK_START_MONETIZATION.md b/docs/guides/QUICK_START_MONETIZATION.md index 9829a8a51..9a23ee543 100644 --- a/docs/guides/QUICK_START_MONETIZATION.md +++ b/docs/guides/QUICK_START_MONETIZATION.md @@ -132,22 +132,19 @@ else: ```python # Before running expensive operation -if manager.track_usage(user_id, 'quantum_jobs', 1): +if manager.track_usage(user_id, "quantum_jobs", 1): # Usage within limits result = run_quantum_job() else: # Limit exceeded - return { - "error": "Quantum job limit exceeded", - "upgrade_url": "/pricing.html" - } + return {"error": "Quantum job limit exceeded", "upgrade_url": "/pricing.html"} ``` ### Check Remaining Usage ```python subscription = manager.get_subscription(user_id) -percentage = subscription.get_usage_percentage('quantum_jobs') +percentage = subscription.get_usage_percentage("quantum_jobs") if percentage > 80: # Send notification: approaching limit diff --git a/docs/guides/VISION_API_GUIDE.md b/docs/guides/VISION_API_GUIDE.md index f93e21b38..239d3e6fc 100644 --- a/docs/guides/VISION_API_GUIDE.md +++ b/docs/guides/VISION_API_GUIDE.md @@ -176,29 +176,23 @@ import base64 import requests # Load and encode image -with open('test.jpg', 'rb') as f: - image_base64 = base64.b64encode(f.read()).decode('utf-8') +with open("test.jpg", "rb") as f: + image_base64 = base64.b64encode(f.read()).decode("utf-8") # Single inference -response = requests.post( - 'http://localhost:7071/api/vision/infer', - json={'image': image_base64} -) +response = requests.post("http://localhost:7071/api/vision/infer", json={"image": image_base64}) result = response.json() print(f"Predicted: {result['label']} ({result['confidence']:.2%})") # Batch inference -with open('test2.jpg', 'rb') as f: - image2_base64 = base64.b64encode(f.read()).decode('utf-8') +with open("test2.jpg", "rb") as f: + image2_base64 = base64.b64encode(f.read()).decode("utf-8") -response = requests.post( - 'http://localhost:7071/api/vision/batch-infer', - json={'images': [image_base64, image2_base64]} -) +response = requests.post("http://localhost:7071/api/vision/batch-infer", json={"images": [image_base64, image2_base64]}) results = response.json() print(f"Analyzed {results['count']} images") -for i, r in enumerate(results['results']): - print(f" Image {i+1}: {r['label']} ({r['confidence']:.2%})") +for i, r in enumerate(results["results"]): + print(f" Image {i + 1}: {r['label']} ({r['confidence']:.2%})") ``` ## CLI Testing diff --git a/docs/guides/VISION_ENDPOINT_QUICKTEST.md b/docs/guides/VISION_ENDPOINT_QUICKTEST.md index b28ced9c5..1d2be973d 100644 --- a/docs/guides/VISION_ENDPOINT_QUICKTEST.md +++ b/docs/guides/VISION_ENDPOINT_QUICKTEST.md @@ -15,13 +15,10 @@ import base64 import requests # Test with any image -with open('test_image.jpg', 'rb') as f: +with open("test_image.jpg", "rb") as f: img_b64 = base64.b64encode(f.read()).decode() -response = requests.post( - 'http://localhost:7071/api/vision/infer', - json={'image': img_b64} -) +response = requests.post("http://localhost:7071/api/vision/infer", json={"image": img_b64}) print(response.json()) ``` diff --git a/docs/lmstudio_agi_integration.md b/docs/lmstudio_agi_integration.md index 3a97f16b0..bef81071c 100644 --- a/docs/lmstudio_agi_integration.md +++ b/docs/lmstudio_agi_integration.md @@ -22,8 +22,10 @@ ```python score = 0.0 -if domain in agent_config.get("domains", []): score += 0.5 -if intent in agent_config.get("intents", []): score += 0.3 +if domain in agent_config.get("domains", []): + score += 0.5 +if intent in agent_config.get("intents", []): + score += 0.3 if score > 0.0: score += agent_config.get("confidence_boost", 0.0) * confidence # learned pattern bonus (time-decay 24h half-life) diff --git a/docs/quantum/QUANTUM_AZURE_EXAMPLES.md b/docs/quantum/QUANTUM_AZURE_EXAMPLES.md index cb86ed39b..dc491e801 100644 --- a/docs/quantum/QUANTUM_AZURE_EXAMPLES.md +++ b/docs/quantum/QUANTUM_AZURE_EXAMPLES.md @@ -148,6 +148,7 @@ python .\scripts\analyze_quantum_results.py # ai-projects/quantum-ml/create_test_circuit.py from qiskit import QuantumCircuit + def create_bell_state(): qc = QuantumCircuit(2, 2) qc.h(0) @@ -155,6 +156,7 @@ def create_bell_state(): qc.measure([0, 1], [0, 1]) return qc + if __name__ == "__main__": circuit = create_bell_state() circuit.qasm(filename="results/bell_state.qasm") @@ -270,11 +272,7 @@ results = [] for job in jobs: cmd = ["python", "scripts/evaluation/quantum_autorun.py", "--job", job] proc = subprocess.run(cmd, capture_output=True, text=True) - results.append({ - "job": job, - "status": "success" if proc.returncode == 0 else "failed", - "output": proc.stdout - }) + results.append({"job": job, "status": "success" if proc.returncode == 0 else "failed", "output": proc.stdout}) # Save batch results with open("data_out/batch_results.json", "w") as f: diff --git a/docs/quickrefs/QUANTUM_LLM_QUICKREF.md b/docs/quickrefs/QUANTUM_LLM_QUICKREF.md index aee4d08e0..32b612f90 100644 --- a/docs/quickrefs/QUANTUM_LLM_QUICKREF.md +++ b/docs/quickrefs/QUANTUM_LLM_QUICKREF.md @@ -84,7 +84,7 @@ from quantum_llm_advanced import MultiScaleQuantumAttention attention = MultiScaleQuantumAttention( d_model=128, n_heads=4, - n_qubits_per_head=[2, 3, 4, 6] # Different scales + n_qubits_per_head=[2, 3, 4, 6], # Different scales ) ``` @@ -115,11 +115,7 @@ stages = [ ```python from quantum_llm_monitor import TrainingDashboard -dashboard = TrainingDashboard( - output_dir=Path("data_out/dashboard"), - update_interval=10, - enable_alerts=True -) +dashboard = TrainingDashboard(output_dir=Path("data_out/dashboard"), update_interval=10, enable_alerts=True) ``` --- diff --git a/docs/root-guides/AGENT_QUICKSTART.md b/docs/root-guides/AGENT_QUICKSTART.md index c89525fef..929ecdffd 100644 --- a/docs/root-guides/AGENT_QUICKSTART.md +++ b/docs/root-guides/AGENT_QUICKSTART.md @@ -290,10 +290,10 @@ export LMSTUDIO_MODEL="local-model" Edit [autonomous_code_agent.py](./scripts/autonomous_code_agent.py): ```python -MAX_FILE_SIZE = 100_000 # Max bytes to read -MAX_CHANGES_PER_FILE = 5 # File modification limit -MAX_TASK_TOKENS = 2000 # Planning token budget -MIN_TEST_PASSING_RATE = 0.8 # 80% tests must pass +MAX_FILE_SIZE = 100_000 # Max bytes to read +MAX_CHANGES_PER_FILE = 5 # File modification limit +MAX_TASK_TOKENS = 2000 # Planning token budget +MIN_TEST_PASSING_RATE = 0.8 # 80% tests must pass ``` ## Features @@ -488,7 +488,7 @@ import sys REPO_ROOT = Path(__file__).resolve().parent.parent if str(REPO_ROOT) not in sys.path: - sys.path.insert(0, str(REPO_ROOT)) + sys.path.insert(0, str(REPO_ROOT)) # Now safe to import local packages, e.g.: from shared.json_utils import load_status_json diff --git a/docs/root-guides/AUTONOMOUS_AGENT_IMPLEMENTATION.md b/docs/root-guides/AUTONOMOUS_AGENT_IMPLEMENTATION.md index be834d64e..6ce8e0633 100644 --- a/docs/root-guides/AUTONOMOUS_AGENT_IMPLEMENTATION.md +++ b/docs/root-guides/AUTONOMOUS_AGENT_IMPLEMENTATION.md @@ -199,10 +199,10 @@ export LMSTUDIO_MODEL="local-model" Edit [autonomous_code_agent.py](./scripts/autonomous_code_agent.py): ```python -MAX_FILE_SIZE = 100_000 # bytes -MAX_CHANGES_PER_FILE = 5 # per file -MAX_TASK_TOKENS = 2000 # planning budget -MIN_TEST_PASSING_RATE = 0.8 # 80% required +MAX_FILE_SIZE = 100_000 # bytes +MAX_CHANGES_PER_FILE = 5 # per file +MAX_TASK_TOKENS = 2000 # planning budget +MIN_TEST_PASSING_RATE = 0.8 # 80% required ``` ## Usage Examples diff --git a/docs/root-guides/ENVIRONMENT_SETUP.md b/docs/root-guides/ENVIRONMENT_SETUP.md index 56db6c8c7..5ee7a08a9 100644 --- a/docs/root-guides/ENVIRONMENT_SETUP.md +++ b/docs/root-guides/ENVIRONMENT_SETUP.md @@ -207,22 +207,23 @@ The system checks providers in this order: Run this to verify key packages are installed: ```python -python3 << 'EOF' +python3 << "EOF" import sys + critical = { - 'torch': '2.8+', - 'transformers': 'latest', - 'qiskit': '1.3+', - 'peft': 'latest', - 'azure-openai': 'latest', - 'azure-quantum': '1.0+', - 'mcp': '1.0+' + "torch": "2.8+", + "transformers": "latest", + "qiskit": "1.3+", + "peft": "latest", + "azure-openai": "latest", + "azure-quantum": "1.0+", + "mcp": "1.0+", } missing = [] for pkg, version in critical.items(): try: - __import__(pkg.replace('-', '_')) + __import__(pkg.replace("-", "_")) print(f"✓ {pkg:<20} installed") except ImportError: print(f"✗ {pkg:<20} MISSING") diff --git a/docs/setup/OLLAMA_USAGE_GUIDE.md b/docs/setup/OLLAMA_USAGE_GUIDE.md index cf181b6c2..c9a5174a4 100644 --- a/docs/setup/OLLAMA_USAGE_GUIDE.md +++ b/docs/setup/OLLAMA_USAGE_GUIDE.md @@ -207,13 +207,11 @@ from openai import OpenAI client = OpenAI( base_url="http://127.0.0.1:11434/v1", - api_key="ollama" # Dummy key, not validated + api_key="ollama", # Dummy key, not validated ) response = client.chat.completions.create( - model="tinyllama", - messages=[{"role": "user", "content": "Hello!"}], - max_tokens=100 + model="tinyllama", messages=[{"role": "user", "content": "Hello!"}], max_tokens=100 ) print(response.choices[0].message.content) diff --git a/docs/summaries/INCOME_STREAM_SUMMARY.md b/docs/summaries/INCOME_STREAM_SUMMARY.md index 9de31e1f6..07ec42fe3 100644 --- a/docs/summaries/INCOME_STREAM_SUMMARY.md +++ b/docs/summaries/INCOME_STREAM_SUMMARY.md @@ -134,7 +134,7 @@ if subscription.has_feature(Feature.QUANTUM_COMPUTING): pass # Track usage -if manager.track_usage(user_id, 'quantum_jobs', 1): +if manager.track_usage(user_id, "quantum_jobs", 1): # Usage allowed run_quantum_job() else: diff --git a/docs/summaries/PHASE_2_COMPLETE.md b/docs/summaries/PHASE_2_COMPLETE.md index c6bb15aac..73ca68fba 100644 --- a/docs/summaries/PHASE_2_COMPLETE.md +++ b/docs/summaries/PHASE_2_COMPLETE.md @@ -235,10 +235,7 @@ referral_system = get_referral_system() # When user signs up with referral code result = referral_system.record_referral( - referrer_code="USER1234AB", - new_user_id="new_user_456", - tier="pro", - subscription_value=49.00 + referrer_code="USER1234AB", new_user_id="new_user_456", tier="pro", subscription_value=49.00 ) # Returns: {"success": True, "commission": 9.80, ...} @@ -253,11 +250,7 @@ email_system = get_email_system() # When usage reaches 80% email_system.notify_usage_warning( - user_email="user@example.com", - resource="chat_messages", - percentage=85.0, - current=850, - limit=1000 + user_email="user@example.com", resource="chat_messages", percentage=85.0, current=850, limit=1000 ) ``` @@ -271,8 +264,8 @@ handler = get_webhook_handler() # Process webhook event result = handler.handle_webhook( payload=request.body, - signature=request.headers['Stripe-Signature'], - webhook_secret=os.environ['STRIPE_WEBHOOK_SECRET'] + signature=request.headers["Stripe-Signature"], + webhook_secret=os.environ["STRIPE_WEBHOOK_SECRET"], ) # Automatically updates subscriptions and sends notifications diff --git a/docs/training/AUTONOMOUS_TRAINING_README.md b/docs/training/AUTONOMOUS_TRAINING_README.md index 18df21c4a..e2960c732 100644 --- a/docs/training/AUTONOMOUS_TRAINING_README.md +++ b/docs/training/AUTONOMOUS_TRAINING_README.md @@ -261,6 +261,7 @@ print(f"Current best model: {status['best_accuracy']:.2%}") import azure.functions as func from scripts.autonomous_training_orchestrator import AutonomousTrainingOrchestrator + async def main(req: func.HttpRequest): orchestrator = AutonomousTrainingOrchestrator() await orchestrator.run_once() @@ -274,7 +275,7 @@ async def main(req: func.HttpRequest): from scripts.autonomous_training_orchestrator import AutonomousTrainingOrchestrator orchestrator = AutonomousTrainingOrchestrator() -best_accuracy = orchestrator.status['best_accuracy'] +best_accuracy = orchestrator.status["best_accuracy"] # Deploy to quantum hardware if threshold met ``` @@ -361,7 +362,7 @@ import sys REPO_ROOT = Path(__file__).resolve().parent.parent if str(REPO_ROOT) not in sys.path: - sys.path.insert(0, str(REPO_ROOT)) + sys.path.insert(0, str(REPO_ROOT)) from shared.json_utils import load_status_json ``` diff --git a/docs/training/BATCH_EVALUATION_GUIDE.md b/docs/training/BATCH_EVALUATION_GUIDE.md index 51bffdccf..6cb7f4bf8 100644 --- a/docs/training/BATCH_EVALUATION_GUIDE.md +++ b/docs/training/BATCH_EVALUATION_GUIDE.md @@ -277,6 +277,7 @@ def compute_custom_metric(texts: List[str]) -> float: # Your metric logic here return score + # Add to evaluation: if "custom" in metrics: results["custom"] = compute_custom_metric(texts) diff --git a/notebooks/README.md b/notebooks/README.md index d8d1be4f1..137423dd1 100644 --- a/notebooks/README.md +++ b/notebooks/README.md @@ -200,11 +200,8 @@ Response: Output JSON only: {"result": , "reason": ""} """ -@evals.evaluator(CustomPromptEvaluatorConfig( - name="professionalism", - prompt=custom_prompt, - threshold=3 -)) + +@evals.evaluator(CustomPromptEvaluatorConfig(name="professionalism", prompt=custom_prompt, threshold=3)) def test_professionalism(self, evaluator_results: EvaluatorResults): assert evaluator_results.professionalism.result == "pass" ``` @@ -216,17 +213,15 @@ Implement evaluation logic in Python for deterministic checks: ```python from pytest_agent_evals import CustomCodeEvaluatorConfig + def check_response_length(sample: dict, item: dict) -> float: """Check if response meets minimum length requirements.""" response = sample.get("output_text", "") min_length = 50 return 1.0 if len(response) >= min_length else 0.0 -@evals.evaluator(CustomCodeEvaluatorConfig( - name="response_length", - grader=check_response_length, - threshold=0.5 -)) + +@evals.evaluator(CustomCodeEvaluatorConfig(name="response_length", grader=check_response_length, threshold=0.5)) def test_response_length(self, evaluator_results: EvaluatorResults): assert evaluator_results.response_length.result == "pass" ``` diff --git a/shared/README.md b/shared/README.md index 41935afe8..09f7e9ba3 100644 --- a/shared/README.md +++ b/shared/README.md @@ -43,6 +43,7 @@ detect_provider = chat_api.detect_provider # In function_app.py: try: from shared.infrastructure.sql_engine import sql_health + db_available = True except ImportError: db_available = False @@ -97,7 +98,8 @@ except ImportError: # In function_app.py or endpoints: try: from shared.premium.subscription_manager import check_subscription - feature_available = check_subscription(user_id, 'quantum_jobs') + + feature_available = check_subscription(user_id, "quantum_jobs") except ImportError: feature_available = False ``` @@ -173,7 +175,7 @@ Each tier should have a clear `__init__.py` that exports public APIs: # shared/domain/__init__.py from .chat_providers import detect_provider, BaseChatProvider -__all__ = ['detect_provider', 'BaseChatProvider'] +__all__ = ["detect_provider", "BaseChatProvider"] ``` --- @@ -189,6 +191,7 @@ from token_utils import prune_messages # ✓ New: use registry from shared.core.module_registry import AIProjectsRegistry + registry = AIProjectsRegistry() chat_api = registry.chat_cli() detect_provider = chat_api.detect_provider @@ -199,12 +202,13 @@ prune_messages = chat_api.token_utils.prune_messages ```python # ❌ Old: scattered sys.path -sys.path.insert(0, 'ai-projects/chat-cli/src') -sys.path.insert(0, 'ai-projects/quantum-ml/src') +sys.path.insert(0, "ai-projects/chat-cli/src") +sys.path.insert(0, "ai-projects/quantum-ml/src") # ✓ New: centralized registration from shared.core.module_registry import AIProjectsRegistry -AIProjectsRegistry.register_paths(['chat-cli', 'quantum-ml']) + +AIProjectsRegistry.register_paths(["chat-cli", "quantum-ml"]) ``` ---