Che237 commited on
Commit
ecec503
Β·
verified Β·
1 Parent(s): 9cd197b

Upload notebooks/04_agent_intelligence.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. notebooks/04_agent_intelligence.ipynb +47 -28
notebooks/04_agent_intelligence.ipynb CHANGED
@@ -40,7 +40,9 @@
40
  "warnings.filterwarnings('ignore')\n",
41
  "\n",
42
  "# Load configuration\n",
43
- "config_path = Path(\"notebook_config.json\")\nif not config_path.exists():\n config_path = Path(\"/home/user/app/notebooks/notebook_config.json\")\n",
 
 
44
  "with open(config_path) as f:\n",
45
  " CONFIG = json.load(f)\n",
46
  "\n",
@@ -48,8 +50,8 @@
48
  "AGENT_DIR = MODELS_DIR.parent / \"agent\"\n",
49
  "AGENT_DIR.mkdir(exist_ok=True)\n",
50
  "\n",
51
- "print(f\"\u2713 Configuration loaded\")\n",
52
- "print(f\"\u2713 Agent output: {AGENT_DIR}\")"
53
  ]
54
  },
55
  {
@@ -103,7 +105,7 @@
103
  " def to_dict(self) -> Dict:\n",
104
  " return asdict(self)\n",
105
  "\n",
106
- "print(\"\u2713 Task definitions loaded\")"
107
  ]
108
  },
109
  {
@@ -207,7 +209,7 @@
207
  " reverse=True)\n",
208
  "\n",
209
  "scoring_engine = DecisionScoringEngine()\n",
210
- "print(\"\u2713 Decision Scoring Engine initialized\")"
211
  ]
212
  },
213
  {
@@ -225,13 +227,14 @@
225
  "metadata": {},
226
  "outputs": [],
227
  "source": [
 
228
  "try:\n",
229
- " import google.generativeai as genai\n",
230
  " GEMINI_AVAILABLE = True\n",
231
  "except ImportError:\n",
232
  " import subprocess\n",
233
- " subprocess.run(['pip', 'install', 'google-generativeai', '-q'])\n",
234
- " import google.generativeai as genai\n",
235
  " GEMINI_AVAILABLE = True\n",
236
  "\n",
237
  "class GeminiReasoningEngine:\n",
@@ -257,21 +260,30 @@
257
  " \n",
258
  " def __init__(self):\n",
259
  " self.api_key = CONFIG.get('gemini_api_key', os.environ.get('GEMINI_API_KEY'))\n",
260
- " self.model = None\n",
 
261
  " \n",
262
  " if self.api_key:\n",
263
  " try:\n",
264
- " genai.configure(api_key=self.api_key)\n",
265
- " self.model = genai.GenerativeModel(CONFIG.get('gemini_model', 'gemini-pro'))\n",
266
- " print(\" \u2713 Gemini API connected\")\n",
 
 
 
 
 
 
 
267
  " except Exception as e:\n",
268
- " print(f\" \u26a0 Gemini API error: {e}\")\n",
 
269
  " else:\n",
270
- " print(\" \u26a0 No Gemini API key found (will use fallback reasoning)\")\n",
271
  " \n",
272
  " def analyze_threat(self, threat_data: Dict) -> AgentDecision:\n",
273
  " \"\"\"Analyze threat and generate decision with reasoning\"\"\"\n",
274
- " if self.model:\n",
275
  " return self._gemini_analyze(threat_data)\n",
276
  " else:\n",
277
  " return self._fallback_analyze(threat_data)\n",
@@ -286,7 +298,14 @@
286
  "Provide your analysis as JSON.\"\"\"\n",
287
  " \n",
288
  " try:\n",
289
- " response = self.model.generate_content(prompt)\n",
 
 
 
 
 
 
 
290
  " \n",
291
  " # Parse response\n",
292
  " text = response.text\n",
@@ -307,7 +326,7 @@
307
  " recommended_follow_up=result.get('recommended_follow_up', [])\n",
308
  " )\n",
309
  " except Exception as e:\n",
310
- " print(f\" \u26a0 Gemini error: {e}\")\n",
311
  " return self._fallback_analyze(threat_data)\n",
312
  " \n",
313
  " def _fallback_analyze(self, threat_data: Dict) -> AgentDecision:\n",
@@ -339,7 +358,7 @@
339
  " )\n",
340
  "\n",
341
  "reasoning_engine = GeminiReasoningEngine()\n",
342
- "print(\"\u2713 Gemini Reasoning Engine initialized\")"
343
  ]
344
  },
345
  {
@@ -435,7 +454,7 @@
435
  " }\n",
436
  "\n",
437
  "task_queue = AgentTaskQueue()\n",
438
- "print(\"\u2713 Task Queue Manager initialized\")"
439
  ]
440
  },
441
  {
@@ -560,7 +579,7 @@
560
  " }\n",
561
  "\n",
562
  "agent = CyberForgeAgent()\n",
563
- "print(\"\u2713 CyberForge Agent initialized\")"
564
  ]
565
  },
566
  {
@@ -634,14 +653,14 @@
634
  " 'severity_weights': DecisionScoringEngine.SEVERITY_WEIGHTS,\n",
635
  " 'evidence_weights': DecisionScoringEngine.EVIDENCE_WEIGHTS,\n",
636
  " 'task_priorities': {p.name: p.value for p in TaskPriority},\n",
637
- " 'gemini_model': 'gemini-pro'\n",
638
  "}\n",
639
  "\n",
640
  "config_path = AGENT_DIR / \"agent_config.json\"\n",
641
  "with open(config_path, 'w') as f:\n",
642
  " json.dump(agent_config, f, indent=2)\n",
643
  "\n",
644
- "print(f\"\u2713 Agent config saved to: {config_path}\")"
645
  ]
646
  },
647
  {
@@ -718,7 +737,7 @@
718
  "with open(module_path, 'w') as f:\n",
719
  " f.write(agent_module)\n",
720
  "\n",
721
- "print(f\"\u2713 Agent module saved to: {module_path}\")"
722
  ]
723
  },
724
  {
@@ -741,23 +760,23 @@
741
  "print(\"=\" * 60)\n",
742
  "\n",
743
  "print(f\"\"\"\n",
744
- "\ud83e\udd16 Agent Capabilities:\n",
745
  " - Decision Scoring: Weighted threat assessment\n",
746
  " - Gemini Integration: AI-powered reasoning\n",
747
  " - Task Queue: Priority-based execution\n",
748
  " - Action History: Full audit trail\n",
749
  "\n",
750
- "\ud83d\udcca Test Results:\n",
751
  " - Action: {decision.action}\n",
752
  " - Confidence: {decision.confidence:.2%}\n",
753
  " - Risk Level: {decision.risk_level}\n",
754
  "\n",
755
- "\ud83d\udcc1 Output Files:\n",
756
  " - Config: {AGENT_DIR}/agent_config.json\n",
757
  " - Module: {AGENT_DIR}/cyberforge_agent.py\n",
758
  "\n",
759
  "Next step:\n",
760
- " \u2192 05_model_validation.ipynb\n",
761
  "\"\"\")\n",
762
  "print(\"=\" * 60)"
763
  ]
@@ -770,4 +789,4 @@
770
  },
771
  "nbformat": 4,
772
  "nbformat_minor": 5
773
- }
 
40
  "warnings.filterwarnings('ignore')\n",
41
  "\n",
42
  "# Load configuration\n",
43
+ "config_path = Path(\"notebook_config.json\")\n",
44
+ "if not config_path.exists():\n",
45
+ " config_path = Path(\"/home/user/app/notebooks/notebook_config.json\")\n",
46
  "with open(config_path) as f:\n",
47
  " CONFIG = json.load(f)\n",
48
  "\n",
 
50
  "AGENT_DIR = MODELS_DIR.parent / \"agent\"\n",
51
  "AGENT_DIR.mkdir(exist_ok=True)\n",
52
  "\n",
53
+ "print(f\"βœ“ Configuration loaded\")\n",
54
+ "print(f\"βœ“ Agent output: {AGENT_DIR}\")"
55
  ]
56
  },
57
  {
 
105
  " def to_dict(self) -> Dict:\n",
106
  " return asdict(self)\n",
107
  "\n",
108
+ "print(\"βœ“ Task definitions loaded\")"
109
  ]
110
  },
111
  {
 
209
  " reverse=True)\n",
210
  "\n",
211
  "scoring_engine = DecisionScoringEngine()\n",
212
+ "print(\"βœ“ Decision Scoring Engine initialized\")"
213
  ]
214
  },
215
  {
 
227
  "metadata": {},
228
  "outputs": [],
229
  "source": [
230
+ "# Gemini Integration - using google-genai (same as ml-services/app/services/gemini_service.py)\n",
231
  "try:\n",
232
+ " from google import genai\n",
233
  " GEMINI_AVAILABLE = True\n",
234
  "except ImportError:\n",
235
  " import subprocess\n",
236
+ " subprocess.run(['pip', 'install', 'google-genai', '-q'])\n",
237
+ " from google import genai\n",
238
  " GEMINI_AVAILABLE = True\n",
239
  "\n",
240
  "class GeminiReasoningEngine:\n",
 
260
  " \n",
261
  " def __init__(self):\n",
262
  " self.api_key = CONFIG.get('gemini_api_key', os.environ.get('GEMINI_API_KEY'))\n",
263
+ " self.client = None\n",
264
+ " self.model_name = CONFIG.get('gemini_model', os.environ.get('GEMINI_MODEL', 'gemini-2.5-flash'))\n",
265
  " \n",
266
  " if self.api_key:\n",
267
  " try:\n",
268
+ " self.client = genai.Client(api_key=self.api_key)\n",
269
+ " # Test connection\n",
270
+ " test = self.client.models.generate_content(\n",
271
+ " model=self.model_name,\n",
272
+ " contents=\"Test. Respond with OK.\"\n",
273
+ " )\n",
274
+ " if test.text:\n",
275
+ " print(f\" βœ“ Gemini API connected (model: {self.model_name})\")\n",
276
+ " else:\n",
277
+ " print(\" ⚠ Gemini API responded but with empty text\")\n",
278
  " except Exception as e:\n",
279
+ " print(f\" ⚠ Gemini API error: {e}\")\n",
280
+ " self.client = None\n",
281
  " else:\n",
282
+ " print(\" ⚠ No Gemini API key found (will use fallback reasoning)\")\n",
283
  " \n",
284
  " def analyze_threat(self, threat_data: Dict) -> AgentDecision:\n",
285
  " \"\"\"Analyze threat and generate decision with reasoning\"\"\"\n",
286
+ " if self.client:\n",
287
  " return self._gemini_analyze(threat_data)\n",
288
  " else:\n",
289
  " return self._fallback_analyze(threat_data)\n",
 
298
  "Provide your analysis as JSON.\"\"\"\n",
299
  " \n",
300
  " try:\n",
301
+ " response = self.client.models.generate_content(\n",
302
+ " model=self.model_name,\n",
303
+ " contents=prompt,\n",
304
+ " config={\n",
305
+ " \"temperature\": 0.3,\n",
306
+ " \"max_output_tokens\": 1024,\n",
307
+ " }\n",
308
+ " )\n",
309
  " \n",
310
  " # Parse response\n",
311
  " text = response.text\n",
 
326
  " recommended_follow_up=result.get('recommended_follow_up', [])\n",
327
  " )\n",
328
  " except Exception as e:\n",
329
+ " print(f\" ⚠ Gemini error: {e}\")\n",
330
  " return self._fallback_analyze(threat_data)\n",
331
  " \n",
332
  " def _fallback_analyze(self, threat_data: Dict) -> AgentDecision:\n",
 
358
  " )\n",
359
  "\n",
360
  "reasoning_engine = GeminiReasoningEngine()\n",
361
+ "print(\"βœ“ Gemini Reasoning Engine initialized\")"
362
  ]
363
  },
364
  {
 
454
  " }\n",
455
  "\n",
456
  "task_queue = AgentTaskQueue()\n",
457
+ "print(\"βœ“ Task Queue Manager initialized\")"
458
  ]
459
  },
460
  {
 
579
  " }\n",
580
  "\n",
581
  "agent = CyberForgeAgent()\n",
582
+ "print(\"βœ“ CyberForge Agent initialized\")"
583
  ]
584
  },
585
  {
 
653
  " 'severity_weights': DecisionScoringEngine.SEVERITY_WEIGHTS,\n",
654
  " 'evidence_weights': DecisionScoringEngine.EVIDENCE_WEIGHTS,\n",
655
  " 'task_priorities': {p.name: p.value for p in TaskPriority},\n",
656
+ " 'gemini_model': CONFIG.get('gemini_model', 'gemini-2.5-flash')\n",
657
  "}\n",
658
  "\n",
659
  "config_path = AGENT_DIR / \"agent_config.json\"\n",
660
  "with open(config_path, 'w') as f:\n",
661
  " json.dump(agent_config, f, indent=2)\n",
662
  "\n",
663
+ "print(f\"βœ“ Agent config saved to: {config_path}\")"
664
  ]
665
  },
666
  {
 
737
  "with open(module_path, 'w') as f:\n",
738
  " f.write(agent_module)\n",
739
  "\n",
740
+ "print(f\"βœ“ Agent module saved to: {module_path}\")"
741
  ]
742
  },
743
  {
 
760
  "print(\"=\" * 60)\n",
761
  "\n",
762
  "print(f\"\"\"\n",
763
+ "πŸ€– Agent Capabilities:\n",
764
  " - Decision Scoring: Weighted threat assessment\n",
765
  " - Gemini Integration: AI-powered reasoning\n",
766
  " - Task Queue: Priority-based execution\n",
767
  " - Action History: Full audit trail\n",
768
  "\n",
769
+ "πŸ“Š Test Results:\n",
770
  " - Action: {decision.action}\n",
771
  " - Confidence: {decision.confidence:.2%}\n",
772
  " - Risk Level: {decision.risk_level}\n",
773
  "\n",
774
+ "πŸ“ Output Files:\n",
775
  " - Config: {AGENT_DIR}/agent_config.json\n",
776
  " - Module: {AGENT_DIR}/cyberforge_agent.py\n",
777
  "\n",
778
  "Next step:\n",
779
+ " β†’ 05_model_validation.ipynb\n",
780
  "\"\"\")\n",
781
  "print(\"=\" * 60)"
782
  ]
 
789
  },
790
  "nbformat": 4,
791
  "nbformat_minor": 5
792
+ }