-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubzero.py
More file actions
152 lines (127 loc) · 4.9 KB
/
subzero.py
File metadata and controls
152 lines (127 loc) · 4.9 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
SubZero - Lightweight AI Agent
Uses Ollama locally with a simple skill system
"""
import json
import subprocess
import sys
from pathlib import Path
from datetime import datetime
class SubZero:
def __init__(self):
self.model = "qwen2.5:3b"
self.memory_file = Path.home() / ".subzero" / "memory_cli.json"
self.skills_dir = Path.home() / ".subzero" / "skills"
self.conversation = []
self._init_dirs()
self._load_memory()
def _init_dirs(self):
"""Initialize SubZero directories"""
self.memory_file.parent.mkdir(exist_ok=True)
self.skills_dir.mkdir(exist_ok=True)
def _load_memory(self):
"""Load conversation memory"""
if self.memory_file.exists():
with open(self.memory_file, 'r') as f:
self.conversation = json.load(f)
def _save_memory(self):
"""Save conversation memory"""
with open(self.memory_file, 'w') as f:
json.dump(self.conversation[-50:], f, indent=2) # Keep last 50 messages
def run_ollama(self, prompt):
"""Send prompt to Ollama"""
try:
# Build conversation context
context = "\n".join([
f"{'User' if msg['role'] == 'user' else 'Assistant'}: {msg['content']}"
for msg in self.conversation[-10:] # Last 10 messages for context
])
full_prompt = f"{context}\nUser: {prompt}\nAssistant:"
result = subprocess.run(
['ollama', 'run', self.model, full_prompt],
capture_output=True,
text=True,
timeout=60,
creationflags=subprocess.CREATE_NO_WINDOW,
)
if result.returncode == 0:
return result.stdout.strip()
else:
return f"Error: {result.stderr}"
except Exception as e:
return f"Error calling Ollama: {e}"
def execute_command(self, command):
"""Execute system command (skill)"""
try:
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=30,
creationflags=subprocess.CREATE_NO_WINDOW,
)
return result.stdout if result.returncode == 0 else result.stderr
except Exception as e:
return f"Error: {e}"
def chat(self, user_input):
"""Main chat interface"""
# Add user message to conversation
self.conversation.append({
'role': 'user',
'content': user_input,
'timestamp': datetime.now().isoformat()
})
# Get AI response
response = self.run_ollama(user_input)
# Add AI response to conversation
self.conversation.append({
'role': 'assistant',
'content': response,
'timestamp': datetime.now().isoformat()
})
# Save memory
self._save_memory()
return response
def interactive(self):
"""Start interactive chat"""
print("╔════════════════════════════════════╗")
print("║ SubZero Agent v1.0 ║")
print("║ Lightweight • Local • Free ║")
print("╚════════════════════════════════════╝")
print(f"\nUsing model: {self.model}")
print("Type 'exit' to quit, 'clear' to clear memory\n")
while True:
try:
user_input = input("\n🧊 You: ").strip()
if not user_input:
continue
if user_input.lower() == 'exit':
print("\n❄️ SubZero shutting down...")
break
if user_input.lower() == 'clear':
self.conversation = []
self._save_memory()
print("💭 Memory cleared!")
continue
print("\n🤖 SubZero: ", end='', flush=True)
response = self.chat(user_input)
print(response)
except KeyboardInterrupt:
print("\n\n❄️ SubZero interrupted. Goodbye!")
break
except Exception as e:
print(f"\n❌ Error: {e}")
def main():
agent = SubZero()
if len(sys.argv) > 1:
# Single message mode
message = ' '.join(sys.argv[1:])
response = agent.chat(message)
print(response)
else:
# Interactive mode
agent.interactive()
if __name__ == "__main__":
main()