forked from krinathakkar646/Simple-Agentic-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
100 lines (79 loc) · 3.49 KB
/
agent.py
File metadata and controls
100 lines (79 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import re
#STEP 1: DEFINE THE TOOLS
def calculate(expression):
"Evaluates a mathematical expression safely."
try:
# Debugging: Print exactly what we are trying to solve
print(f" [DEBUG] Raw math input: '{expression}'")
# Remove anything that isn't a number or math symbol
# This fixes the "invalid syntax" error by stripping hidden junk
clean_expr = re.sub(r'[^0-9+\-*/().]', '', expression)
print(f" [DEBUG] Cleaned math input: '{clean_expr}'")
return str(eval(clean_expr))
except Exception as e:
return f"Error: {e}"
def check_ip_reputation(ip):
"""Simulates a cybersecurity tool to check IP safety."""
print(f" [DEBUG] Scanning IP: {ip}")
malicious_ips = ["192.168.1.50", "10.0.0.5"]
if ip in malicious_ips:
return "DANGER: Malicious IP detected!"
return "SAFE: IP is clean."
# --- STEP 2: THE BRAIN (LOGIC) ---
def mock_llm_brain(prompt):
"""Decides which tool to use based on the user prompt."""
prompt = prompt.lower()
# Logic for Math
if "calculate" in prompt or "math" in prompt:
numbers = re.findall(r'\d+', prompt)
if len(numbers) >= 2:
return f"TOOL_USE: calculate({numbers[0]} + {numbers[1]})"
# Logic for Security
# We check for 'check ip', 'scan', or just 'ip' to be safer
elif "check ip" in prompt or "scan" in prompt or "ip " in prompt:
# Regex to find an IP address (digits.digits.digits.digits)
ips = re.findall(r'\d+\.\d+\.\d+\.\d+', prompt)
if ips:
return f"TOOL_USE: check_ip_reputation('{ips[0]}')"
else:
return "RESPONSE: I understood you want to scan an IP, but I couldn't find the IP address numbers."
# Fallback (Must be indented to the LEFT, outside the if/elif blocks)
return "RESPONSE: I don't need tools for this. Hello!"
# --- STEP 3: THE AGENT LOOP ---
def run_agent(user_request):
print(f"🤖 USER SAYS: {user_request}")
# 1. THINK
decision = mock_llm_brain(user_request)
print(f"🧠 THOUGHT: {decision}")
# Safety Check: Did the brain return nothing?
if decision is None:
print("❌ CRITICAL ERROR: The brain returned None. Logic fell through.")
print("-" * 30)
return
# 2. ACT
# Handle "TOOL_USE:" vs "TOOL_USE :" (fix spacing issues)
clean_decision = decision.replace(" :", ":")
if clean_decision.startswith("TOOL_USE:"):
# Remove the prefix to get the function call
tool_call = clean_decision.replace("TOOL_USE: ", "").strip()
if "calculate" in tool_call:
# Remove the word 'calculate' to get just the math part
math_part = tool_call.replace("calculate", "")
result = calculate(math_part)
print(f"🔧 ACTION: Calculated result -> {result}")
elif "check_ip_reputation" in tool_call:
# Extract IP safely between quotes
if "'" in tool_call:
ip = tool_call.split("'")[1]
else:
ip = "0.0.0.0"
result = check_ip_reputation(ip)
print(f"🛡️ SECURITY ACTION: Result -> {result}")
else:
# If no tool is needed, just print the response
print(clean_decision)
print("-" * 30)
# --- STEP 4: EXECUTION ---
if __name__ == "__main__":
run_agent("Please calculate 50 + 100 for me.")
run_agent("Can you check ip 192.168.1.50 for viruses?")