-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
executable file
·201 lines (169 loc) · 6.3 KB
/
Copy pathstart.py
File metadata and controls
executable file
·201 lines (169 loc) · 6.3 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
Main entry point for the AI system.
Initializes the Agent and starts the primary process.
"""
import os
import sys
import argparse
import time
from pathlib import Path
# Import project modules
from src.agent import Agent
from src.logger import setup_logger
# Setup logger
logger = setup_logger(__name__)
def parse_arguments():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description="Start the AI Agent system")
# Model source options - only one should be used
model_group = parser.add_argument_group('Model Source (use only one)')
model_group.add_argument(
"--api-url",
type=str,
help="URL of the LLM API server",
default=None
)
model_group.add_argument(
"--model-path",
type=str,
help="Path to local model weights",
default="./local_model_weights"
)
model_group.add_argument(
"--ollama",
action="store_true",
help="Use Ollama for LLM inference"
)
# Ollama specific options
ollama_group = parser.add_argument_group('Ollama Options')
ollama_group.add_argument(
"--ollama-url",
type=str,
help="URL of the Ollama server (default: http://localhost:11434)",
default="http://localhost:11434"
)
ollama_group.add_argument(
"--model-name",
type=str,
help="Name of the Ollama model to use (default: llama3)",
default="llama3"
)
# Operation mode
parser.add_argument(
"--interactive",
action="store_true",
help="Start in interactive mode to accept commands directly"
)
parser.add_argument(
"--command",
type=str,
help="Single command to execute (non-interactive mode)",
default=None
)
return parser.parse_args()
def start_interactive_mode(agent):
"""
Start the agent in interactive mode, accepting commands from the console.
Args:
agent (Agent): The initialized Agent instance
"""
logger.info("Starting interactive mode")
print("\nAI Agent Interactive Mode")
print("Type 'exit' or 'quit' to end the session")
print("------------------------------")
agent.start()
while agent.running:
try:
# Get command from user
command = input("\nEnter command: ")
# Check for exit command
if command.lower() in ["exit", "quit"]:
agent.stop()
print("Exiting interactive mode")
break
# Process the command
result = agent.process_command(command)
# Display the result
if result["status"] == "success":
if "result" in result and "output" in result["result"]:
print("\nResult:")
print(result["result"]["output"])
elif "result" in result and "stdout" in result["result"]:
print("\nCommand Output:")
if result["result"]["stdout"]:
print(result["result"]["stdout"])
if result["result"]["stderr"]:
print("Errors:", result["result"]["stderr"])
print(f"Return code: {result['result']['return_code']}")
else:
print("\nCommand executed successfully")
else:
print(f"\nError: {result.get('error', 'Unknown error')}")
except KeyboardInterrupt:
print("\nInterrupted by user")
agent.stop()
break
except Exception as e:
logger.error(f"Error in interactive mode: {str(e)}")
print(f"\nSystem error: {str(e)}")
def main():
"""Main entry point"""
try:
# Parse command line arguments
args = parse_arguments()
# Display startup banner
print("\n===================================")
print(" AI Agent System")
print("===================================\n")
# Determine LLM mode
if args.ollama:
# Ollama mode
logger.info(f"Using Ollama mode with model: {args.model_name}")
agent = Agent(
ollama_url=args.ollama_url,
model_name=args.model_name
)
elif args.api_url:
# API mode
logger.info(f"Using API mode with URL: {args.api_url}")
agent = Agent(api_url=args.api_url)
else:
# Direct mode
logger.info(f"Using direct mode with model path: {args.model_path}")
agent = Agent(model_path=args.model_path)
logger.info("Agent initialized successfully")
# Determine operating mode
if args.command:
# Single command mode
logger.info(f"Executing single command: {args.command}")
result = agent.process_command(args.command)
if result["status"] == "success":
if "result" in result and "output" in result["result"]:
print("\nResult:")
print(result["result"]["output"])
elif "result" in result and "stdout" in result["result"]:
print("\nCommand Output:")
if result["result"]["stdout"]:
print(result["result"]["stdout"])
if result["result"]["stderr"]:
print("Errors:", result["result"]["stderr"])
print(f"Return code: {result['result']['return_code']}")
else:
print("\nCommand executed successfully")
else:
print(f"\nError: {result.get('error', 'Unknown error')}")
elif args.interactive:
# Interactive mode
start_interactive_mode(agent)
else:
# Default to interactive mode if no specific mode is specified
start_interactive_mode(agent)
except Exception as e:
logger.error(f"Error in main: {str(e)}")
print(f"Fatal error: {str(e)}")
return 1
logger.info("System shutdown complete")
return 0
if __name__ == "__main__":
sys.exit(main())